forms.class.inc.php 43 KB

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