forms.class.inc.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Helper class to build interactive forms to be used either in stand-alone
  20. * modal dialog or in "property-sheet" panes.
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. class DesignerForm
  26. {
  27. protected $aFieldSets;
  28. protected $sCurrentFieldSet;
  29. protected $sScript;
  30. protected $sReadyScript;
  31. protected $sFormId;
  32. protected $sFormPrefix;
  33. protected $sParamsContainer;
  34. protected $oParentForm;
  35. protected $aSubmitParams;
  36. protected $sSubmitTo;
  37. protected $bReadOnly;
  38. protected $sHierarchyPath; // Needed to manage the visibility of nested subform
  39. protected $sHierarchyParent; // Needed to manage the visibility of nested subform
  40. protected $sHierarchySelector; // Needed to manage the visibility of nested subform
  41. protected $bDisplayed;
  42. protected $aDefaultValues;
  43. protected $sFieldsSuffix;
  44. public function __construct()
  45. {
  46. $this->aFieldSets = array();
  47. $this->sCurrentFieldSet = '';
  48. $this->sScript = '';
  49. $this->sReadyScript = '';
  50. $this->sFormPrefix = '';
  51. $this->sFieldsSuffix = '';
  52. $this->sParamsContainer = '';
  53. $this->sFormId = 'form_'.rand();
  54. $this->oParentForm = null;
  55. $this->bReadOnly = false;
  56. $this->sHierarchyPath = '';
  57. $this->sHierarchyParent = '';
  58. $this->sHierarchySelector = '';
  59. $this->StartFieldSet($this->sCurrentFieldSet);
  60. $this->bDisplayed = true;
  61. $this->aDefaultvalues = array();
  62. }
  63. public function AddField(DesignerFormField $oField)
  64. {
  65. if (!is_array($this->aFieldSets[$this->sCurrentFieldSet]))
  66. {
  67. $this->aFieldSets[$this->sCurrentFieldSet] = array();
  68. }
  69. $this->aFieldSets[$this->sCurrentFieldSet][] = $oField;
  70. $oField->SetForm($this);
  71. }
  72. public function StartFieldSet($sLabel)
  73. {
  74. $this->sCurrentFieldSet = $sLabel;
  75. if (!array_key_exists($this->sCurrentFieldSet, $this->aFieldSets))
  76. {
  77. $this->aFieldSets[$this->sCurrentFieldSet] = array();
  78. }
  79. }
  80. public function Render($oP, $bReturnHTML = false)
  81. {
  82. $sFormId = $this->GetFormId();
  83. if ($this->oParentForm == null)
  84. {
  85. $sReturn = '<form id="'.$sFormId.'">';
  86. }
  87. else
  88. {
  89. $sReturn = '';
  90. }
  91. $sHiddenFields = '';
  92. foreach($this->aFieldSets as $sLabel => $aFields)
  93. {
  94. $aDetails = array();
  95. if ($sLabel != '')
  96. {
  97. $sReturn .= '<fieldset>';
  98. $sReturn .= '<legend>'.$sLabel.'</legend>';
  99. }
  100. foreach($aFields as $oField)
  101. {
  102. $aRow = $oField->Render($oP, $sFormId);
  103. if ($oField->IsVisible())
  104. {
  105. $sValidation = '&nbsp;<span class="prop_apply">'.$this->GetValidationArea($oField->GetFieldId()).'</span>';
  106. $sField = $aRow['value'].$sValidation;
  107. $aDetails[] = array('label' => $aRow['label'], 'value' => $sField);
  108. }
  109. else
  110. {
  111. $sHiddenFields .= $aRow['value'];
  112. }
  113. }
  114. $sReturn .= $oP->GetDetails($aDetails);
  115. if ($sLabel != '')
  116. {
  117. $sReturn .= '</fieldset>';
  118. }
  119. }
  120. $sReturn .= $sHiddenFields;
  121. if ($this->oParentForm == null)
  122. {
  123. $sReturn .= '</form>';
  124. }
  125. if($this->sScript != '')
  126. {
  127. $oP->add_script($this->sScript);
  128. }
  129. if($this->sReadyScript != '')
  130. {
  131. $oP->add_ready_script($this->sReadyScript);
  132. }
  133. if ($bReturnHTML)
  134. {
  135. return $sReturn;
  136. }
  137. else
  138. {
  139. $oP->add($sReturn);
  140. }
  141. }
  142. public function GetFieldSets()
  143. {
  144. return $this->aFieldSets;
  145. }
  146. public function SetSubmitParams($sSubmitToUrl, $aSubmitParams)
  147. {
  148. $this->sSubmitTo = $sSubmitToUrl;
  149. $this->aSubmitParams = $aSubmitParams;
  150. }
  151. public function CopySubmitParams($oParentForm)
  152. {
  153. $this->sSubmitTo = $oParentForm->sSubmitTo;
  154. $this->aSubmitParams = $oParentForm->aSubmitParams;
  155. }
  156. public function GetSubmitParams()
  157. {
  158. return array( 'url' => $this->sSubmitTo, 'params' => $this->aSubmitParams);
  159. }
  160. /**
  161. * Helper to handle subforms hide/show
  162. */
  163. public function SetHierarchyPath($sHierarchy)
  164. {
  165. $this->sHierarchyPath = $sHierarchy;
  166. }
  167. /**
  168. * Helper to handle subforms hide/show
  169. */
  170. public function GetHierarchyPath()
  171. {
  172. return $this->sHierarchyPath;
  173. }
  174. /**
  175. * Helper to handle subforms hide/show
  176. */
  177. public function SetHierarchyParent($sHierarchy)
  178. {
  179. $this->sHierarchyParent = $sHierarchy;
  180. }
  181. /**
  182. * Helper to handle subforms hide/show
  183. */
  184. public function GetHierarchyParent()
  185. {
  186. return $this->sHierarchyParent;
  187. }
  188. public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
  189. {
  190. $sReturn = '';
  191. $sActionUrl = addslashes($this->sSubmitTo);
  192. $sJSSubmitParams = json_encode($this->aSubmitParams);
  193. $sFormId = $this->GetFormId();
  194. if ($this->oParentForm == null)
  195. {
  196. $sReturn = '<form id="'.$sFormId.'" onsubmit="return false;">';
  197. $sReturn .= '<table class="prop_table">';
  198. $sReturn .= '<thead><tr><th class="prop_header">'.Dict::S('UI:Form:Property').'</th><th class="prop_header">'.Dict::S('UI:Form:Value').'</th><th colspan="2" class="prop_header">&nbsp;</th></tr></thead><tbody>';
  199. }
  200. $sHiddenFields = '';
  201. foreach($this->aFieldSets as $sLabel => $aFields)
  202. {
  203. $aDetails = array();
  204. if ($sLabel != '')
  205. {
  206. $sReturn .= $this->StartRow().'<th colspan="4">'.$sLabel.'</th>'.$this->EndRow();
  207. }
  208. foreach($aFields as $oField)
  209. {
  210. $aRow = $oField->Render($oP, $sFormId, 'property');
  211. if ($oField->IsVisible())
  212. {
  213. $sFieldId = $this->GetFieldId($oField->GetCode());
  214. $sValidation = $this->GetValidationArea($sFieldId, '<span title="Apply" class="ui-icon ui-icon-circle-check"/>');
  215. $sValidationFields = '</td><td class="prop_icon prop_apply">'.$sValidation.'</td><td class="prop_icon prop_cancel"><span title="Revert" class="ui-icon ui-icon-circle-close"/></td>'.$this->EndRow();
  216. $sPath = $this->GetHierarchyPath().'/'.$oField->GetCode();
  217. if (is_null($aRow['label']))
  218. {
  219. $sReturn .= $this->StartRow($sFieldId).'<td class="prop_value" colspan="2">'.$aRow['value'];
  220. }
  221. else
  222. {
  223. $sReturn .= $this->StartRow($sFieldId).'<td class="prop_label">'.$aRow['label'].'</td><td class="prop_value">'.$aRow['value'];
  224. }
  225. if (!($oField instanceof DesignerFormSelectorField) && !($oField instanceof DesignerMultipleSubFormField))
  226. {
  227. $sReturn .= $sValidationFields;
  228. }
  229. $sNotifyParentSelectorJS = is_null($sNotifyParentSelector) ? 'null' : "'".addslashes($sNotifyParentSelector)."'";
  230. $sAutoApply = $oField->IsAutoApply() ? 'true' : 'false';
  231. $sHandlerEquals = $oField->GetHandlerEquals();
  232. $sHandlerGetValue = $oField->GetHandlerGetValue();
  233. $sWidgetClass = $oField->GetWidgetClass();
  234. $sJSExtraParams = '';
  235. if (count($oField->GetWidgetExtraParams()) > 0)
  236. {
  237. $aExtraParams = array();
  238. foreach($oField->GetWidgetExtraParams() as $key=> $value)
  239. {
  240. $aExtraParams[] = "'$key': ".json_encode($value);
  241. }
  242. $sJSExtraParams = ', '.implode(', ', $aExtraParams);
  243. }
  244. $this->AddReadyScript(
  245. <<<EOF
  246. $('#row_$sFieldId').$sWidgetClass({parent_selector: $sNotifyParentSelectorJS, field_id: '$sFieldId', equals: $sHandlerEquals, get_field_value: $sHandlerGetValue, auto_apply: $sAutoApply, value: '', submit_to: '$sActionUrl', submit_parameters: $sJSSubmitParams $sJSExtraParams });
  247. EOF
  248. );
  249. }
  250. else
  251. {
  252. $sHiddenFields .= $aRow['value'];
  253. }
  254. }
  255. }
  256. if ($this->oParentForm == null)
  257. {
  258. $sFormId = $this->sFormId;
  259. $sReturn .= '</tbody>';
  260. $sReturn .= '</table>';
  261. $sReturn .= $sHiddenFields;
  262. $sReturn .= '</form>';
  263. $sReturn .= '<div id="prop_submit_result"></div>'; // for the return of the submit operation
  264. }
  265. else
  266. {
  267. $sReturn .= $sHiddenFields;
  268. }
  269. $this->AddReadyScript(
  270. <<<EOF
  271. $('.prop_table').tableHover();
  272. var idx = 0;
  273. $('.prop_table tbody tr').each(function() {
  274. if ((idx % 2) == 0)
  275. {
  276. $(this).addClass('even');
  277. }
  278. else
  279. {
  280. $(this).addClass('odd');
  281. }
  282. idx++;
  283. });
  284. EOF
  285. );
  286. if($this->sScript != '')
  287. {
  288. $oP->add_script($this->sScript);
  289. }
  290. if($this->sReadyScript != '')
  291. {
  292. $oP->add_ready_script($this->sReadyScript);
  293. }
  294. if ($bReturnHTML)
  295. {
  296. return $sReturn;
  297. }
  298. else
  299. {
  300. $oP->add($sReturn);
  301. }
  302. }
  303. public function StartRow($sFieldId = null)
  304. {
  305. if ($sFieldId != null)
  306. {
  307. return '<tr id="row_'.$sFieldId.'" data-path="'.$this->GetHierarchyPath().'" data-selector="'.$this->GetHierarchyParent().'">';
  308. }
  309. return '<tr data-path="'.$this->GetHierarchyPath().'" data-selector="'.$this->GetHierarchyParent().'">';
  310. }
  311. public function EndRow()
  312. {
  313. return '</tr>';
  314. }
  315. public function RenderAsDialog($oPage, $sDialogId, $sDialogTitle, $iDialogWidth, $sOkButtonLabel, $sIntroduction = null, $bAutoOpen = true)
  316. {
  317. $this->SetPrefix('dlg_'); // To make sure that the controls have different IDs that the property sheet which may be displayed at the same time
  318. $sDialogTitle = addslashes($sDialogTitle);
  319. $sOkButtonLabel = addslashes($sOkButtonLabel);
  320. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  321. $oPage->add("<div id=\"$sDialogId\">");
  322. if ($sIntroduction != null)
  323. {
  324. $oPage->add('<div class="ui-dialog-header">'.$sIntroduction.'</div>');
  325. }
  326. $this->Render($oPage);
  327. $oPage->add('</div>');
  328. $sAutoOpen = $bAutoOpen ? 'true' : 'false';
  329. $oPage->add_ready_script(
  330. <<<EOF
  331. $('#$sDialogId').dialog({
  332. height: 'auto',
  333. width: $iDialogWidth,
  334. modal: true,
  335. autoOpen: $sAutoOpen,
  336. title: '$sDialogTitle',
  337. buttons: [
  338. { text: "$sOkButtonLabel", click: function() {
  339. var oForm = $(this).closest('.ui-dialog').find('form');
  340. oForm.submit();
  341. if (AnimateDlgButtons)
  342. {
  343. sFormId = oForm.attr('id');
  344. if (oFormValidation[sFormId].length == 0)
  345. {
  346. AnimateDlgButtons(this);
  347. }
  348. }
  349. } },
  350. { text: "$sCancelButtonLabel", click: function() { KillAllMenus(); $(this).dialog( "close" ); $(this).remove(); } },
  351. ],
  352. close: function() { KillAllMenus(); $(this).remove(); }
  353. });
  354. var oForm = $('#$sDialogId form');
  355. var sFormId = oForm.attr('id');
  356. ValidateForm(sFormId, true);
  357. EOF
  358. );
  359. }
  360. public function ReadParams(&$aValues = array())
  361. {
  362. foreach($this->aFieldSets as $sLabel => $aFields)
  363. {
  364. foreach($aFields as $oField)
  365. {
  366. $oField->ReadParam($aValues);
  367. }
  368. }
  369. return $aValues;
  370. }
  371. public function SetPrefix($sPrefix)
  372. {
  373. $this->sFormPrefix = $sPrefix;
  374. }
  375. public function GetPrefix()
  376. {
  377. $sPrefix = '';
  378. if ($this->oParentForm != null)
  379. {
  380. $sPrefix = $this->oParentForm->GetPrefix();
  381. }
  382. return $sPrefix.$this->sFormPrefix;
  383. }
  384. public function SetSuffix($sSuffix)
  385. {
  386. $this->sFieldsSuffix = $sSuffix;
  387. }
  388. public function GetSuffix()
  389. {
  390. $sSuffix = '';
  391. if ($this->oParentForm != null)
  392. {
  393. $sSuffix = $this->oParentForm->GetSuffix();
  394. }
  395. return $sSuffix.$this->sFieldsSuffix;
  396. }
  397. public function SetReadOnly($bReadOnly = true)
  398. {
  399. $this->bReadOnly = $bReadOnly;
  400. }
  401. public function IsReadOnly()
  402. {
  403. if ($this->oParentForm == null)
  404. {
  405. return $this->bReadOnly;
  406. }
  407. else
  408. {
  409. return $this->oParentForm->IsReadOnly();
  410. }
  411. }
  412. public function SetParamsContainer($sParamsContainer)
  413. {
  414. $this->sParamsContainer = $sParamsContainer;
  415. }
  416. public function GetParamsContainer()
  417. {
  418. if ($this->oParentForm == null)
  419. {
  420. return $this->sParamsContainer;
  421. }
  422. else
  423. {
  424. return $this->oParentForm->GetParamsContainer();
  425. }
  426. }
  427. public function SetParentForm($oParentForm)
  428. {
  429. $this->oParentForm = $oParentForm;
  430. }
  431. public function SetDefaultValues($aDefaultValues)
  432. {
  433. if (!is_array($aDefaultValues)) return;
  434. foreach($this->aFieldSets as $sLabel => $aFields)
  435. {
  436. foreach($aFields as $oField)
  437. {
  438. $oField->SetDefaultValueFrom($aDefaultValues);
  439. }
  440. }
  441. }
  442. public function GetDefaultValues()
  443. {
  444. return $this->aDefaultValues;
  445. }
  446. public function GetParentForm()
  447. {
  448. return $this->oParentForm;
  449. }
  450. public function GetFormId()
  451. {
  452. if ($this->oParentForm)
  453. {
  454. $this->oParentForm->GetFormId();
  455. }
  456. return $this->sFormId;
  457. }
  458. public function SetDisplayed($bDisplayed)
  459. {
  460. $this->bDisplayed = $bDisplayed;
  461. }
  462. public function IsDisplayed()
  463. {
  464. if ($this->oParentForm == null)
  465. {
  466. return $this->bDisplayed;
  467. }
  468. else
  469. {
  470. return ($this->bDisplayed && $this->oParentForm->IsDisplayed());
  471. }
  472. }
  473. public function AddScript($sScript)
  474. {
  475. $this->sScript .= $sScript;
  476. }
  477. public function AddReadyScript($sScript)
  478. {
  479. $this->sReadyScript .= $sScript;
  480. }
  481. public function GetFieldId($sCode)
  482. {
  483. return $this->GetPrefix().'attr_'.$sCode;
  484. }
  485. public function GetFieldName($sCode)
  486. {
  487. return 'attr_'.$sCode.$this->GetSuffix();
  488. }
  489. public function GetParamName($sCode)
  490. {
  491. return 'attr_'.$sCode.$this->GetSuffix();
  492. }
  493. public function GetValidationArea($sId, $sContent = '')
  494. {
  495. return "<span style=\"display:inline-block;width:20px;\" id=\"v_{$sId}\"><span class=\"ui-icon ui-icon-alert\"></span>$sContent</span>";
  496. }
  497. public function GetAsyncActionClass()
  498. {
  499. return $this->sAsyncActionClass;
  500. }
  501. public function FindField($sFieldCode)
  502. {
  503. $oFoundField = false;
  504. foreach($this->aFieldSets as $sLabel => $aFields)
  505. {
  506. foreach($aFields as $oField)
  507. {
  508. $oFoundField = $oField->FindField($sFieldCode);
  509. if ($oFoundField !== false) break;
  510. }
  511. if ($oFoundField !== false) break;
  512. }
  513. return $oFoundField;
  514. }
  515. }
  516. class DesignerTabularForm extends DesignerForm
  517. {
  518. protected $aTable;
  519. public function __construct()
  520. {
  521. parent::__construct();
  522. $this->aTable = array();
  523. }
  524. public function AddRow($aRow)
  525. {
  526. $this->aTable[] = $aRow;
  527. }
  528. public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
  529. {
  530. return $this->Render($oP, $bReturnHTML);
  531. }
  532. public function Render($oP, $bReturnHTML = false)
  533. {
  534. $sReturn = '';
  535. if ($this->oParentForm == null)
  536. {
  537. $sFormId = $this->sFormId;
  538. $sReturn = '<form id="'.$sFormId.'">';
  539. }
  540. else
  541. {
  542. $sFormId = $this->oParentForm->sFormId;
  543. }
  544. $sHiddenFields = '';
  545. $sReturn .= '<table style="width:100%">';
  546. foreach($this->aTable as $aRow)
  547. {
  548. $sReturn .= '<tr>';
  549. foreach($aRow as $field)
  550. {
  551. if (!is_object($field))
  552. {
  553. // Shortcut: pass a string for a cell containing just a label
  554. $sReturn .= '<td>'.$field.'</td>';
  555. }
  556. else
  557. {
  558. $field->SetForm($this);
  559. $aFieldData = $field->Render($oP, $sFormId);
  560. if ($field->IsVisible())
  561. {
  562. // put the label and value separated by a non-breaking space if needed
  563. $aData = array();
  564. foreach(array('label', 'value') as $sCode )
  565. {
  566. if ($aFieldData[$sCode] != '')
  567. {
  568. $aData[] = $aFieldData[$sCode];
  569. }
  570. }
  571. $sReturn .= '<td>'.implode('&nbsp;', $aData).'</td>';
  572. }
  573. else
  574. {
  575. $sHiddenFields .= $aRow['value'];
  576. }
  577. }
  578. }
  579. $sReturn .= '</tr>';
  580. }
  581. $sReturn .= '</table>';
  582. $sReturn .= $sHiddenFields;
  583. if($this->sScript != '')
  584. {
  585. $oP->add_script($this->sScript);
  586. }
  587. if($this->sReadyScript != '')
  588. {
  589. $oP->add_ready_script($this->sReadyScript);
  590. }
  591. if ($bReturnHTML)
  592. {
  593. return $sReturn;
  594. }
  595. else
  596. {
  597. $oP->add($sReturn);
  598. }
  599. }
  600. public function ReadParams(&$aValues = array())
  601. {
  602. foreach($this->aTable as $aRow)
  603. {
  604. foreach($aRow as $field)
  605. {
  606. if (is_object($field))
  607. {
  608. $field->SetForm($this);
  609. $field->ReadParam($aValues);
  610. }
  611. }
  612. }
  613. return $aValues;
  614. }
  615. }
  616. class DesignerFormField
  617. {
  618. protected $sLabel;
  619. protected $sCode;
  620. protected $defaultValue;
  621. protected $oForm;
  622. protected $bMandatory;
  623. protected $bReadOnly;
  624. protected $bAutoApply;
  625. protected $aCSSClasses;
  626. protected $bDisplayed;
  627. protected $aWidgetExtraParams;
  628. public function __construct($sCode, $sLabel, $defaultValue)
  629. {
  630. $this->sLabel = $sLabel;
  631. $this->sCode = $sCode;
  632. $this->defaultValue = $defaultValue;
  633. $this->bMandatory = false;
  634. $this->bReadOnly = false;
  635. $this->bAutoApply = false;
  636. $this->aCSSClasses = array();
  637. $this->bDisplayed = true;
  638. $this->aWidgetExtraParams = array();
  639. }
  640. public function GetCode()
  641. {
  642. return $this->sCode;
  643. }
  644. public function SetForm($oForm)
  645. {
  646. $this->oForm = $oForm;
  647. }
  648. public function SetMandatory($bMandatory = true)
  649. {
  650. $this->bMandatory = $bMandatory;
  651. }
  652. public function SetReadOnly($bReadOnly = true)
  653. {
  654. $this->bReadOnly = $bReadOnly;
  655. }
  656. public function IsReadOnly()
  657. {
  658. return ($this->oForm->IsReadOnly() || $this->bReadOnly);
  659. }
  660. public function SetAutoApply($bAutoApply)
  661. {
  662. $this->bAutoApply = $bAutoApply;
  663. }
  664. public function IsAutoApply()
  665. {
  666. return $this->bAutoApply;
  667. }
  668. public function SetDisplayed($bDisplayed)
  669. {
  670. $this->bDisplayed = $bDisplayed;
  671. }
  672. public function IsDisplayed()
  673. {
  674. return $this->bDisplayed;
  675. }
  676. public function GetFieldId()
  677. {
  678. return $this->oForm->GetFieldId($this->sCode);
  679. }
  680. public function GetWidgetClass()
  681. {
  682. return 'property_field';
  683. }
  684. public function GetWidgetExtraParams()
  685. {
  686. return $this->aWidgetExtraParams;
  687. }
  688. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  689. {
  690. $sId = $this->oForm->GetFieldId($this->sCode);
  691. $sName = $this->oForm->GetFieldName($this->sCode);
  692. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  693. }
  694. public function ReadParam(&$aValues)
  695. {
  696. if ($this->IsReadOnly())
  697. {
  698. $aValues[$this->sCode] = $this->defaultValue;
  699. }
  700. else
  701. {
  702. if ($this->oForm->GetParamsContainer() != '')
  703. {
  704. $aParams = utils::ReadParam($this->oForm->GetParamsContainer(), array(), false, 'raw_data');
  705. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  706. {
  707. $aValues[$this->sCode] = $aParams[$this->oForm->GetParamName($this->sCode)];
  708. }
  709. else
  710. {
  711. $aValues[$this->sCode] = $this->defaultValue;
  712. }
  713. }
  714. else
  715. {
  716. $aValues[$this->sCode] = utils::ReadParam($this->oForm->GetParamName($this->sCode), $this->defaultValue, false, 'raw_data');
  717. }
  718. }
  719. }
  720. public function IsVisible()
  721. {
  722. return true;
  723. }
  724. public function AddCSSClass($sCSSClass)
  725. {
  726. $this->aCSSClasses[] = $sCSSClass;
  727. }
  728. /**
  729. * A way to set/change the default value after constructing the field
  730. */
  731. public function SetDefaultValueFrom($aAllDefaultValue)
  732. {
  733. if (array_key_exists($this->GetCode(), $aAllDefaultValue))
  734. {
  735. $this->defaultValue = $aAllDefaultValue[$this->GetCode()];
  736. }
  737. }
  738. public function FindField($sFieldCode)
  739. {
  740. if ($this->sCode == $sFieldCode)
  741. {
  742. return $this;
  743. }
  744. return false;
  745. }
  746. public function GetHandlerEquals()
  747. {
  748. return 'null';
  749. }
  750. public function GetHandlerGetValue()
  751. {
  752. return 'null';
  753. }
  754. }
  755. class DesignerLabelField extends DesignerFormField
  756. {
  757. protected $sDescription;
  758. public function __construct($sLabel, $sDescription)
  759. {
  760. parent::__construct('', $sLabel, '');
  761. $this->sDescription = $sDescription;
  762. }
  763. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  764. {
  765. $sId = $this->oForm->GetFieldId($this->sCode);
  766. $sName = $this->oForm->GetFieldName($this->sCode);
  767. return array('label' => $this->sLabel, 'value' => $this->sDescription);
  768. }
  769. public function ReadParam(&$aValues)
  770. {
  771. }
  772. public function IsVisible()
  773. {
  774. return true;
  775. }
  776. }
  777. class DesignerTextField extends DesignerFormField
  778. {
  779. protected $sValidationPattern;
  780. protected $aForbiddenValues;
  781. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  782. {
  783. parent::__construct($sCode, $sLabel, $defaultValue);
  784. $this->sValidationPattern = '';
  785. $this->aForbiddenValues = array();
  786. }
  787. public function SetValidationPattern($sValidationPattern)
  788. {
  789. $this->sValidationPattern = $sValidationPattern;
  790. }
  791. public function SetForbiddenValues($aValues, $sExplain, $bCaseSensitive = true)
  792. {
  793. $aForbiddenValues = $aValues;
  794. $iDefaultKey = array_search($this->defaultValue, $aForbiddenValues);
  795. if ($iDefaultKey !== false)
  796. {
  797. // The default (current) value is always allowed...
  798. unset($aForbiddenValues[$iDefaultKey]);
  799. }
  800. $this->aForbiddenValues[] = array('values' => $aForbiddenValues, 'message' => $sExplain, 'case_sensitive' => $bCaseSensitive);
  801. }
  802. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  803. {
  804. $sId = $this->oForm->GetFieldId($this->sCode);
  805. $sName = $this->oForm->GetFieldName($this->sCode);
  806. if ($this->IsReadOnly())
  807. {
  808. $sHtmlValue = "<span>".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  809. }
  810. else
  811. {
  812. $sPattern = addslashes($this->sValidationPattern);
  813. if (is_array($this->aForbiddenValues))
  814. {
  815. $sForbiddenValues = json_encode($this->aForbiddenValues);
  816. }
  817. else
  818. {
  819. $sForbiddenValues = '[]'; //Empty JS array
  820. }
  821. $sMandatory = $this->bMandatory ? 'true' : 'false';
  822. $oP->add_ready_script(
  823. <<<EOF
  824. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', $(this).closest('form').attr('id'), $sForbiddenValues); } );
  825. {
  826. var myTimer = null;
  827. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  828. }
  829. EOF
  830. );
  831. $sCSSClasses = '';
  832. if (count($this->aCSSClasses) > 0)
  833. {
  834. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  835. }
  836. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  837. }
  838. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  839. }
  840. public function ReadParam(&$aValues)
  841. {
  842. parent::ReadParam($aValues);
  843. $sPattern = '/'.str_replace('/', '\/', $this->sValidationPattern).'/'; // Escape the forward slashes since they are used as delimiters for preg_match
  844. if (($this->sValidationPattern != '') && (!preg_match($sPattern, $aValues[$this->sCode])) )
  845. {
  846. $aValues[$this->sCode] = $this->defaultValue;
  847. }
  848. else if(($this->aForbiddenValues != null) && in_array($aValues[$this->sCode], $this->aForbiddenValues))
  849. {
  850. // Reject the value...
  851. $aValues[$this->sCode] = $this->defaultValue;
  852. }
  853. }
  854. }
  855. class DesignerLongTextField extends DesignerTextField
  856. {
  857. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  858. {
  859. $sId = $this->oForm->GetFieldId($this->sCode);
  860. $sName = $this->oForm->GetFieldName($this->sCode);
  861. $sPattern = addslashes($this->sValidationPattern);
  862. if (is_array($this->aForbiddenValues))
  863. {
  864. $sForbiddenValues = json_encode($this->aForbiddenValues);
  865. }
  866. else
  867. {
  868. $sForbiddenValues = '[]'; //Empty JS array
  869. }
  870. $sMandatory = $this->bMandatory ? 'true' : 'false';
  871. $sCSSClasses = '';
  872. if (count($this->aCSSClasses) > 0)
  873. {
  874. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  875. }
  876. if (!$this->IsReadOnly())
  877. {
  878. $oP->add_ready_script(
  879. <<<EOF
  880. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', $(this).closest('form').attr('id'), $sForbiddenValues); } );
  881. {
  882. var myTimer = null;
  883. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  884. }
  885. EOF
  886. );
  887. $sValue = "<textarea $sCSSClasses id=\"$sId\" name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>";
  888. }
  889. else
  890. {
  891. $sValue = "<div $sCSSClasses id=\"$sId\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</div>";
  892. }
  893. return array('label' => $this->sLabel, 'value' => $sValue);
  894. }
  895. }
  896. class DesignerIntegerField extends DesignerFormField
  897. {
  898. protected $iMin; // Lower boundary, inclusive
  899. protected $iMax; // Higher boundary, inclusive
  900. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  901. {
  902. parent::__construct($sCode, $sLabel, $defaultValue);
  903. $this->iMin = 0; // Positive integer is the default
  904. $this->iMax = null;
  905. }
  906. public function SetBoundaries($iMin = null, $iMax = null)
  907. {
  908. $this->iMin = $iMin;
  909. $this->iMax = $iMax;
  910. }
  911. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  912. {
  913. $sId = $this->oForm->GetFieldId($this->sCode);
  914. $sName = $this->oForm->GetFieldName($this->sCode);
  915. if ($this->IsReadOnly())
  916. {
  917. $sHtmlValue = "<span>".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  918. }
  919. else
  920. {
  921. $sMin = json_encode($this->iMin);
  922. $sMax = json_encode($this->iMax);
  923. $sMandatory = $this->bMandatory ? 'true' : 'false';
  924. $oP->add_ready_script(
  925. <<<EOF
  926. $('#$sId').bind('change keyup validate', function() { ValidateInteger('$sId', $sMandatory, $(this).closest('form').attr('id'), $sMin, $sMax); } );
  927. {
  928. var myTimer = null;
  929. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  930. }
  931. EOF
  932. );
  933. $sCSSClasses = '';
  934. if (count($this->aCSSClasses) > 0)
  935. {
  936. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  937. }
  938. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  939. }
  940. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  941. }
  942. public function ReadParam(&$aValues)
  943. {
  944. parent::ReadParam($aValues);
  945. if (!is_null($this->iMin) && ($aValues[$this->sCode] < $this->iMin))
  946. {
  947. // Reject the value...
  948. $aValues[$this->sCode] = $this->defaultValue;
  949. }
  950. if (!is_null($this->iMax) && ($aValues[$this->sCode] > $this->iMax))
  951. {
  952. // Reject the value...
  953. $aValues[$this->sCode] = $this->defaultValue;
  954. }
  955. }
  956. }
  957. class DesignerComboField extends DesignerFormField
  958. {
  959. protected $aAllowedValues;
  960. protected $bMultipleSelection;
  961. protected $bOtherChoices;
  962. protected $sNullLabel;
  963. protected $bSorted;
  964. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  965. {
  966. parent::__construct($sCode, $sLabel, $defaultValue);
  967. $this->aAllowedValues = array();
  968. $this->bMultipleSelection = false;
  969. $this->bOtherChoices = false;
  970. $this->sNullLabel = Dict::S('UI:SelectOne');
  971. $this->bAutoApply = true;
  972. $this->bSorted = true; // Sorted by default
  973. }
  974. public function SetAllowedValues($aAllowedValues)
  975. {
  976. $this->aAllowedValues = $aAllowedValues;
  977. }
  978. public function MultipleSelection($bMultipleSelection = true)
  979. {
  980. $this->bMultipleSelection = $bMultipleSelection;
  981. }
  982. public function OtherChoices($bOtherChoices = true)
  983. {
  984. $this->bOtherChoices = $bOtherChoices;
  985. }
  986. /**
  987. * An empty label will disable the default empty value
  988. */
  989. public function SetNullLabel($sLabel)
  990. {
  991. $this->sNullLabel = $sLabel;
  992. }
  993. public function IsSorted()
  994. {
  995. return $this->bSorted;
  996. }
  997. public function SetSorted($bSorted)
  998. {
  999. $this->bSorted = $bSorted;
  1000. }
  1001. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1002. {
  1003. $sId = $this->oForm->GetFieldId($this->sCode);
  1004. $sName = $this->oForm->GetFieldName($this->sCode);
  1005. $sChecked = $this->defaultValue ? 'checked' : '';
  1006. $sMandatory = $this->bMandatory ? 'true' : 'false';
  1007. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  1008. if ($this->IsSorted())
  1009. {
  1010. asort($this->aAllowedValues);
  1011. }
  1012. $sCSSClasses = '';
  1013. if (count($this->aCSSClasses) > 0)
  1014. {
  1015. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1016. }
  1017. if ($this->IsReadOnly())
  1018. {
  1019. $aSelected = array();
  1020. $aHiddenValues = array();
  1021. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1022. {
  1023. if ($this->bMultipleSelection)
  1024. {
  1025. if(in_array($sKey, $this->defaultValue))
  1026. {
  1027. $aSelected[] = $sDisplayValue;
  1028. $aHiddenValues[] = "<input type=\"hidden\" name=\"{$sName}[]\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1029. }
  1030. }
  1031. else
  1032. {
  1033. if ($sKey == $this->defaultValue)
  1034. {
  1035. $aSelected[] = $sDisplayValue;
  1036. $aHiddenValues[] = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1037. }
  1038. }
  1039. }
  1040. $sHtml = "<span $sCSSClasses>".htmlentities(implode(', ', $aSelected), ENT_QUOTES, 'UTF-8').implode($aHiddenValues)."</span>";
  1041. }
  1042. else
  1043. {
  1044. if ($this->bMultipleSelection)
  1045. {
  1046. $sHtml = "<select $sCSSClasses multiple size=\"8\"id=\"$sId\" name=\"$sName\">";
  1047. }
  1048. else
  1049. {
  1050. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\">";
  1051. if ($this->sNullLabel != '')
  1052. {
  1053. $sHtml .= "<option value=\"\">".$this->sNullLabel."</option>";
  1054. }
  1055. }
  1056. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1057. {
  1058. if ($this->bMultipleSelection)
  1059. {
  1060. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  1061. }
  1062. else
  1063. {
  1064. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  1065. }
  1066. // Quick and dirty: display the menu parents as a tree
  1067. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  1068. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  1069. }
  1070. $sHtml .= "</select>";
  1071. if ($this->bOtherChoices)
  1072. {
  1073. $sHtml .= '<br/><input type="checkbox" id="other_chk_'.$sId.'"><label for="other_chk_'.$sId.'">&nbsp;Other:</label>&nbsp;<input type="text" id="other_'.$sId.'" name="other_'.$sName.'" size="30"/>';
  1074. }
  1075. $oP->add_ready_script(
  1076. <<<EOF
  1077. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', $(this).closest('form').attr('id'), null, null); } );
  1078. EOF
  1079. );
  1080. }
  1081. return array('label' => $this->sLabel, 'value' => $sHtml);
  1082. }
  1083. public function ReadParam(&$aValues)
  1084. {
  1085. parent::ReadParam($aValues);
  1086. if ($aValues[$this->sCode] == 'null')
  1087. {
  1088. $aValues[$this->sCode] = array();
  1089. }
  1090. }
  1091. }
  1092. class DesignerBooleanField extends DesignerFormField
  1093. {
  1094. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1095. {
  1096. parent::__construct($sCode, $sLabel, $defaultValue);
  1097. $this->bAutoApply = true;
  1098. }
  1099. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1100. {
  1101. $sId = $this->oForm->GetFieldId($this->sCode);
  1102. $sName = $this->oForm->GetFieldName($this->sCode);
  1103. $sChecked = $this->defaultValue ? 'checked' : '';
  1104. if ($this->IsReadOnly())
  1105. {
  1106. $sLabel = $this->defaultValue ? Dict::S('UI:UserManagement:ActionAllowed:Yes') : Dict::S('UI:UserManagement:ActionAllowed:No'); //TODO use our own yes/no translations
  1107. $sHtmlValue = "<span>".htmlentities($sLabel)."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  1108. }
  1109. else
  1110. {
  1111. $sCSSClasses = '';
  1112. if (count($this->aCSSClasses) > 0)
  1113. {
  1114. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1115. }
  1116. $sHtmlValue = "<input $sCSSClasses type=\"checkbox\" $sChecked id=\"$sId\" name=\"$sName\" value=\"true\">";
  1117. }
  1118. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  1119. }
  1120. public function ReadParam(&$aValues)
  1121. {
  1122. if ($this->IsReadOnly())
  1123. {
  1124. $aValues[$this->sCode] = $this->defaultValue;
  1125. }
  1126. else
  1127. {
  1128. $sParamsContainer = $this->oForm->GetParamsContainer();
  1129. if ($sParamsContainer != '')
  1130. {
  1131. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  1132. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  1133. {
  1134. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  1135. }
  1136. else
  1137. {
  1138. $sValue = 'false';
  1139. }
  1140. }
  1141. else
  1142. {
  1143. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  1144. }
  1145. $aValues[$this->sCode] = ($sValue == 'true');
  1146. }
  1147. }
  1148. }
  1149. class DesignerHiddenField extends DesignerFormField
  1150. {
  1151. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1152. {
  1153. parent::__construct($sCode, $sLabel, $defaultValue);
  1154. }
  1155. public function IsVisible()
  1156. {
  1157. return false;
  1158. }
  1159. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1160. {
  1161. $sId = $this->oForm->GetFieldId($this->sCode);
  1162. $sName = $this->oForm->GetFieldName($this->sCode);
  1163. $sChecked = $this->defaultValue ? 'checked' : '';
  1164. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1165. }
  1166. }
  1167. class DesignerIconSelectionField extends DesignerFormField
  1168. {
  1169. protected $sUploadUrl;
  1170. protected $aAllowedValues;
  1171. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1172. {
  1173. parent::__construct($sCode, $sLabel, $defaultValue);
  1174. $this->bAutoApply = true;
  1175. $this->sUploadUrl = null;
  1176. }
  1177. public function SetAllowedValues($aAllowedValues)
  1178. {
  1179. $this->aAllowedValues = $aAllowedValues;
  1180. }
  1181. public function EnableUpload($sIconUploadUrl)
  1182. {
  1183. $this->sUploadUrl = $sIconUploadUrl;
  1184. }
  1185. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1186. {
  1187. $sId = $this->oForm->GetFieldId($this->sCode);
  1188. $sName = $this->oForm->GetFieldName($this->sCode);
  1189. $idx = 0;
  1190. foreach($this->aAllowedValues as $index => $aValue)
  1191. {
  1192. if ($aValue['value'] == $this->defaultValue)
  1193. {
  1194. $idx = $index;
  1195. break;
  1196. }
  1197. }
  1198. $sJSItems = json_encode($this->aAllowedValues);
  1199. $sPostUploadTo = ($this->sUploadUrl == null) ? 'null' : "'{$this->sUploadUrl}'";
  1200. if (!$this->IsReadOnly())
  1201. {
  1202. $sValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>";
  1203. $oP->add_ready_script(
  1204. <<<EOF
  1205. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems, post_upload_to: $sPostUploadTo});
  1206. EOF
  1207. );
  1208. }
  1209. else
  1210. {
  1211. $sValue = '<span style="display:inline-block;line-height:48px;height:48px;"><span><img style="vertical-align:middle" src="'.$this->aAllowedValues[$idx]['icon'].'" />&nbsp;'.htmlentities($this->aAllowedValues[$idx]['label'], ENT_QUOTES, 'UTF-8').'</span></span>';
  1212. }
  1213. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  1214. return array('label' => $this->sLabel, 'value' => $sValue);
  1215. }
  1216. }
  1217. class RunTimeIconSelectionField extends DesignerIconSelectionField
  1218. {
  1219. static $aAllIcons = array();
  1220. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1221. {
  1222. parent::__construct($sCode, $sLabel, $defaultValue);
  1223. if (count(self::$aAllIcons) == 0)
  1224. {
  1225. self::$aAllIcons = self::FindIconsOnDisk(APPROOT.'env-'.utils::GetCurrentEnvironment());
  1226. ksort(self::$aAllIcons);
  1227. }
  1228. $aValues = array();
  1229. foreach(self::$aAllIcons as $sFilePath)
  1230. {
  1231. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  1232. }
  1233. $this->SetAllowedValues($aValues);
  1234. }
  1235. static protected function FindIconsOnDisk($sBaseDir, $sDir = '')
  1236. {
  1237. $aFiles = null;
  1238. $sKey = $sBaseDir.'/'.$sDir;
  1239. $sShortKey = abs(crc32($sKey));
  1240. $sCacheFile = utils::GetCachePath().'available-icons-'.$sShortKey.'.php';
  1241. $sCacheClass = 'AvailableIcons_'.$sShortKey;
  1242. if (file_exists($sCacheFile))
  1243. {
  1244. require_once($sCacheFile);
  1245. if ($sCacheClass::$sKey === $sKey) // crc32 collision detection
  1246. {
  1247. $aFiles = $sCacheClass::$aIconFiles;
  1248. }
  1249. }
  1250. if ($aFiles === null)
  1251. {
  1252. $aFiles = self::_FindIconsOnDisk($sBaseDir, $sDir);
  1253. $sAvailableIcons = '<?php'.PHP_EOL;
  1254. $sAvailableIcons .= '// Generated and used by '.__METHOD__.PHP_EOL;
  1255. $sAvailableIcons .= 'class '.$sCacheClass.PHP_EOL;
  1256. $sAvailableIcons .= '{'.PHP_EOL;
  1257. $sAvailableIcons .= ' static $sKey = '.var_export($sKey, true).';'.PHP_EOL;
  1258. $sAvailableIcons .= ' static $aIconFiles = '.var_export($aFiles, true).';'.PHP_EOL;
  1259. $sAvailableIcons .= '}'.PHP_EOL;
  1260. file_put_contents($sCacheFile, $sAvailableIcons, LOCK_EX);
  1261. }
  1262. return $aFiles;
  1263. }
  1264. static protected function _FindIconsOnDisk($sBaseDir, $sDir = '')
  1265. {
  1266. $aResult = array();
  1267. // Populate automatically the list of icon files
  1268. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  1269. {
  1270. while (($sFile = readdir($hDir)) !== false)
  1271. {
  1272. $aMatches = array();
  1273. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  1274. {
  1275. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  1276. $aResult = array_merge($aResult, self::_FindIconsOnDisk($sBaseDir, $sDirSubPath));
  1277. }
  1278. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  1279. {
  1280. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  1281. }
  1282. }
  1283. closedir($hDir);
  1284. }
  1285. return $aResult;
  1286. }
  1287. public function ValueFromDOMNode($oDOMNode)
  1288. {
  1289. return $oDOMNode->textContent;
  1290. }
  1291. public function ValueToDOMNode($oDOMNode, $value)
  1292. {
  1293. $oTextNode = $oDOMNode->ownerDocument->createTextNode($value);
  1294. $oDOMNode->appendChild($oTextNode);
  1295. }
  1296. public function MakeFileUrl($value)
  1297. {
  1298. return utils::GetAbsoluteUrlModulesRoot().$value;
  1299. }
  1300. public function GetDefaultValue($sClass = 'Contact')
  1301. {
  1302. $sIconPath = MetaModel::GetClassIcon($sClass, false);
  1303. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIconPath);
  1304. return $sIcon;
  1305. }
  1306. }
  1307. class DesignerSortableField extends DesignerFormField
  1308. {
  1309. protected $aAllowedValues;
  1310. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1311. {
  1312. parent::__construct($sCode, $sLabel, $defaultValue);
  1313. $this->aAllowedValues = array();
  1314. }
  1315. public function SetAllowedValues($aAllowedValues)
  1316. {
  1317. $this->aAllowedValues = $aAllowedValues;
  1318. }
  1319. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1320. {
  1321. $bOpen = false;
  1322. $sId = $this->oForm->GetFieldId($this->sCode);
  1323. $sName = $this->oForm->GetFieldName($this->sCode);
  1324. $sReadOnly = $this->IsReadOnly() ? 'readonly="readonly"' : '';
  1325. $aResult = array('label' => $this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" $sReadOnly value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1326. $sJSFields = json_encode(array_keys($this->aAllowedValues));
  1327. $oP->add_ready_script(
  1328. "$('#$sId').sortable_field({aAvailableFields: $sJSFields});"
  1329. );
  1330. return $aResult;
  1331. }
  1332. }
  1333. class DesignerFormSelectorField extends DesignerFormField
  1334. {
  1335. protected $aSubForms;
  1336. protected $defaultRealValue; // What's stored as default value is actually the index
  1337. protected $bSorted;
  1338. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1339. {
  1340. parent::__construct($sCode, $sLabel, 0);
  1341. $this->defaultRealValue = $defaultValue;
  1342. $this->aSubForms = array();
  1343. $this->bSorted = true;
  1344. }
  1345. public function IsSorted()
  1346. {
  1347. return $this->bSorted;
  1348. }
  1349. public function SetSorted($bSorted)
  1350. {
  1351. $this->bSorted = $bSorted;
  1352. }
  1353. /**
  1354. * Callback for sorting an array of $aFormData based ont he labels of the subforms
  1355. * @param unknown $aItem1
  1356. * @param unknown $aItem2
  1357. * @return number
  1358. */
  1359. static function SortOnFormLabel($aItem1, $aItem2)
  1360. {
  1361. return strcasecmp($aItem1['label'], $aItem2['label']);
  1362. }
  1363. public function GetWidgetClass()
  1364. {
  1365. return 'selector_property_field';
  1366. }
  1367. public function AddSubForm($oSubForm, $sLabel, $sValue)
  1368. {
  1369. $idx = count($this->aSubForms);
  1370. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  1371. if ($sValue == $this->defaultRealValue)
  1372. {
  1373. // Store the index of the selected/default form
  1374. $this->defaultValue = count($this->aSubForms) - 1;
  1375. }
  1376. }
  1377. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1378. {
  1379. $sId = $this->oForm->GetFieldId($this->sCode);
  1380. $sName = $this->oForm->GetFieldName($this->sCode);
  1381. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  1382. $this->aCSSClasses[] = 'formSelector';
  1383. $sCSSClasses = '';
  1384. if (count($this->aCSSClasses) > 0)
  1385. {
  1386. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1387. }
  1388. if ($this->IsSorted())
  1389. {
  1390. uasort($this->aSubForms, array(get_class($this), 'SortOnFormLabel'));
  1391. }
  1392. if ($this->IsReadOnly())
  1393. {
  1394. $aSelected = array();
  1395. $aHiddenValues = array();
  1396. $sDisplayValue = '';
  1397. $sHiddenValue = '';
  1398. foreach($this->aSubForms as $iKey => $aFormData)
  1399. {
  1400. if ($iKey == $this->defaultValue) // Default value is actually the index
  1401. {
  1402. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');
  1403. $sHiddenValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($iKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1404. break;
  1405. }
  1406. }
  1407. $sHtml = "<span $sCSSClasses>".$sDisplayValue.$sHiddenValue."</span>";
  1408. }
  1409. else
  1410. {
  1411. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  1412. foreach($this->aSubForms as $iKey => $aFormData)
  1413. {
  1414. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');
  1415. $sValue = htmlentities($aFormData['value'], ENT_QUOTES, 'UTF-8');
  1416. $sSelected = ($iKey == $this->defaultValue) ? 'selected' : '';
  1417. $sHtml .= "<option data-value=\"$sValue\" value=\"$iKey\" $sSelected>".$sDisplayValue."</option>";
  1418. }
  1419. $sHtml .= "</select>";
  1420. }
  1421. if ($sRenderMode == 'property')
  1422. {
  1423. $sHtml .= '</td><td class="prop_icon prop_apply"><span title="Apply" class="ui-icon ui-icon-circle-check"/></td><td class="prop_icon prop_cancel"><span title="Revert" class="ui-icon ui-icon-circle-close"/></td></tr>';
  1424. }
  1425. foreach($this->aSubForms as $sKey => $aFormData)
  1426. {
  1427. $sId = $this->oForm->GetFieldId($this->sCode);
  1428. $sStyle = (($sKey == $this->defaultValue) && $this->oForm->IsDisplayed()) ? '' : 'style="display:none"';
  1429. $oSubForm = $aFormData['form'];
  1430. $oSubForm->SetParentForm($this->oForm);
  1431. $oSubForm->CopySubmitParams($this->oForm);
  1432. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1433. if ($sRenderMode == 'property')
  1434. {
  1435. // Note: Managing the visibility of nested subforms had several implications
  1436. // 1) Attributes are displayed in a table and we have to group them in as many tbodys as necessary to hide/show the various options depending on the current selection
  1437. // 2) It is not possible to nest tbody tags. Therefore, it is not possible to manage the visibility the same way as it is done for the dialog mode (using nested divs).
  1438. // The div hierarchy has been emulated by adding attributes to the tbody tags:
  1439. // - data-selector : uniquely identifies the DesignerFormSelectorField that has an impact on the visibility of the node
  1440. // - data-path : uniquely identifies the combination of users choices that must be made to show the node
  1441. // - data-state : records the state, depending on the user choice on the FormSelectorField just above the node, but indepentantly from the visibility in the page (can be visible in the form itself being in a hidden form)
  1442. // Then a series of actions are performed to hide and show the relevant nodes, depending on the user choice
  1443. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1444. $oSubForm->SetHierarchyParent($sSelector);
  1445. $sPath = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix().'-'.$sKey;
  1446. $oSubForm->SetHierarchyPath($sPath);
  1447. $oSubForm->SetDisplayed($sKey == $this->defaultValue);
  1448. $sState = ($sKey == $this->defaultValue) ? 'visible' : 'hidden';
  1449. //$sHtml .= "</tbody><tbody data-selector=\"$sSelector\" data-path=\"$sPath\" data-state=\"$sState\" $sStyle>";
  1450. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  1451. $sState = $this->oForm->IsDisplayed() ? 'visible' : 'hidden';
  1452. $sParentStyle = '';
  1453. if ($oParent = $this->oForm->GetParentForm())
  1454. {
  1455. $sParentStyle = ($oParent->IsDisplayed()) ? '' : 'style="display:none"';
  1456. $sParentSelector = $oParent->GetHierarchyParent();
  1457. $sParentPath = $oParent->GetHierarchyPath();
  1458. }
  1459. else
  1460. {
  1461. $sParentSelector = '';
  1462. $sParentPath = '';
  1463. }
  1464. //$sHtml .= "</tbody><tbody data-selector=\"$sParentSelector\" data-path=\"$sParentPath\" data-state=\"$sState\" $sParentStyle>";
  1465. }
  1466. else
  1467. {
  1468. $sHtml .= "<div class=\"subform_{$sId} {$sId}_{$sKey}\" $sStyle>";
  1469. $sHtml .= $oSubForm->Render($oP, true);
  1470. $sHtml .= "</div>";
  1471. }
  1472. }
  1473. if ($sRenderMode == 'property')
  1474. {
  1475. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1476. $this->aWidgetExtraParams['data_selector'] = $sSelector;
  1477. }
  1478. else
  1479. {
  1480. $oP->add_ready_script(
  1481. <<<EOF
  1482. $('#$sId').bind('change reverted', function() { $('.subform_{$sId}').hide(); $('.{$sId}_'+this.value).show(); } );
  1483. EOF
  1484. );
  1485. }
  1486. return array('label' => $this->sLabel, 'value' => $sHtml);
  1487. }
  1488. public function ReadParam(&$aValues)
  1489. {
  1490. parent::ReadParam($aValues);
  1491. $sKey = $aValues[$this->sCode];
  1492. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  1493. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1494. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  1495. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  1496. }
  1497. public function SetDefaultValueFrom($aAllDefaultValues)
  1498. {
  1499. if (array_key_exists($this->GetCode(), $aAllDefaultValues))
  1500. {
  1501. $selectedValue = $aAllDefaultValues[$this->GetCode()];
  1502. foreach($this->aSubForms as $iKey => $aFormData)
  1503. {
  1504. $sId = $this->oForm->GetFieldId($this->sCode);
  1505. if ($selectedValue == $aFormData['value'])
  1506. {
  1507. $this->defaultValue =$iKey;
  1508. $aDefaultValues = $this->oForm->GetDefaultValues();
  1509. $oSubForm = $aFormData['form'];
  1510. $oSubForm->SetDefaultValues($aAllDefaultValues);
  1511. }
  1512. }
  1513. }
  1514. }
  1515. public function FindField($sFieldCode)
  1516. {
  1517. $oField = parent::FindField($sFieldCode);
  1518. if ($oField === false)
  1519. {
  1520. // Look in the subforms
  1521. foreach($this->aSubForms as $sKey => $aFormData)
  1522. {
  1523. $oSubForm = $aFormData['form'];
  1524. $oField = $oSubForm->FindField($sFieldCode);
  1525. if ($oField !== false)
  1526. {
  1527. break;
  1528. }
  1529. }
  1530. }
  1531. return $oField;
  1532. }
  1533. }
  1534. class DesignerSubFormField extends DesignerFormField
  1535. {
  1536. protected $oSubForm;
  1537. public function __construct($sLabel, $oSubForm)
  1538. {
  1539. parent::__construct('', $sLabel, '');
  1540. $this->oSubForm = $oSubForm;
  1541. }
  1542. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1543. {
  1544. $this->oSubForm->SetParentForm($this->oForm);
  1545. $this->oSubForm->CopySubmitParams($this->oForm);
  1546. if ($sRenderMode == 'property')
  1547. {
  1548. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  1549. }
  1550. else
  1551. {
  1552. $sHtml = $this->oSubForm->Render($oP, true);
  1553. }
  1554. return array('label' => $this->sLabel, 'value' => $sHtml);
  1555. }
  1556. public function ReadParam(&$aValues)
  1557. {
  1558. $this->oSubForm->SetParentForm($this->oForm);
  1559. $this->oSubForm->ReadParams($aValues);
  1560. }
  1561. public function FindField($sFieldCode)
  1562. {
  1563. $oField = parent::FindField($sFieldCode);
  1564. if ($oField === false)
  1565. {
  1566. // Look in the subform
  1567. $oField = $this->oSubForm->FindField($sFieldCode);
  1568. }
  1569. return $oField;
  1570. }
  1571. }
  1572. class DesignerStaticTextField extends DesignerFormField
  1573. {
  1574. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1575. {
  1576. parent::__construct($sCode, $sLabel, $defaultValue);
  1577. }
  1578. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1579. {
  1580. return array('label' => $this->sLabel, 'value' => $this->defaultValue);
  1581. }
  1582. }