forms.class.inc.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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)
  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. $oPage->add_ready_script(
  329. <<<EOF
  330. $('#$sDialogId').dialog({
  331. height: 'auto',
  332. width: $iDialogWidth,
  333. modal: true,
  334. title: '$sDialogTitle',
  335. buttons: [
  336. { text: "$sOkButtonLabel", click: function() {
  337. var oForm = $(this).closest('.ui-dialog').find('form');
  338. oForm.submit();
  339. if (AnimateDlgButtons)
  340. {
  341. sFormId = oForm.attr('id');
  342. if (oFormValidation[sFormId].length == 0)
  343. {
  344. AnimateDlgButtons(this);
  345. }
  346. }
  347. } },
  348. { text: "$sCancelButtonLabel", click: function() { KillAllMenus(); $(this).dialog( "close" ); $(this).remove(); } },
  349. ],
  350. close: function() { KillAllMenus(); $(this).remove(); }
  351. });
  352. var oForm = $('#$sDialogId form');
  353. var sFormId = oForm.attr('id');
  354. ValidateForm(sFormId, true);
  355. EOF
  356. );
  357. }
  358. public function ReadParams(&$aValues = array())
  359. {
  360. foreach($this->aFieldSets as $sLabel => $aFields)
  361. {
  362. foreach($aFields as $oField)
  363. {
  364. $oField->ReadParam($aValues);
  365. }
  366. }
  367. return $aValues;
  368. }
  369. public function SetPrefix($sPrefix)
  370. {
  371. $this->sFormPrefix = $sPrefix;
  372. }
  373. public function GetPrefix()
  374. {
  375. $sPrefix = '';
  376. if ($this->oParentForm != null)
  377. {
  378. $sPrefix = $this->oParentForm->GetPrefix();
  379. }
  380. return $sPrefix.$this->sFormPrefix;
  381. }
  382. public function SetSuffix($sSuffix)
  383. {
  384. $this->sFieldsSuffix = $sSuffix;
  385. }
  386. public function GetSuffix()
  387. {
  388. $sSuffix = '';
  389. if ($this->oParentForm != null)
  390. {
  391. $sSuffix = $this->oParentForm->GetSuffix();
  392. }
  393. return $sSuffix.$this->sFieldsSuffix;
  394. }
  395. public function SetReadOnly($bReadOnly = true)
  396. {
  397. $this->bReadOnly = $bReadOnly;
  398. }
  399. public function IsReadOnly()
  400. {
  401. if ($this->oParentForm == null)
  402. {
  403. return $this->bReadOnly;
  404. }
  405. else
  406. {
  407. return $this->oParentForm->IsReadOnly();
  408. }
  409. }
  410. public function SetParamsContainer($sParamsContainer)
  411. {
  412. $this->sParamsContainer = $sParamsContainer;
  413. }
  414. public function GetParamsContainer()
  415. {
  416. if ($this->oParentForm == null)
  417. {
  418. return $this->sParamsContainer;
  419. }
  420. else
  421. {
  422. return $this->oParentForm->GetParamsContainer();
  423. }
  424. }
  425. public function SetParentForm($oParentForm)
  426. {
  427. $this->oParentForm = $oParentForm;
  428. }
  429. public function SetDefaultValues($aDefaultValues)
  430. {
  431. if (!is_array($aDefaultValues)) return;
  432. foreach($this->aFieldSets as $sLabel => $aFields)
  433. {
  434. foreach($aFields as $oField)
  435. {
  436. $oField->SetDefaultValueFrom($aDefaultValues);
  437. }
  438. }
  439. }
  440. public function GetDefaultValues()
  441. {
  442. return $this->aDefaultValues;
  443. }
  444. public function GetParentForm()
  445. {
  446. return $this->oParentForm;
  447. }
  448. public function GetFormId()
  449. {
  450. if ($this->oParentForm)
  451. {
  452. $this->oParentForm->GetFormId();
  453. }
  454. return $this->sFormId;
  455. }
  456. public function SetDisplayed($bDisplayed)
  457. {
  458. $this->bDisplayed = $bDisplayed;
  459. }
  460. public function IsDisplayed()
  461. {
  462. if ($this->oParentForm == null)
  463. {
  464. return $this->bDisplayed;
  465. }
  466. else
  467. {
  468. return ($this->bDisplayed && $this->oParentForm->IsDisplayed());
  469. }
  470. }
  471. public function AddScript($sScript)
  472. {
  473. $this->sScript .= $sScript;
  474. }
  475. public function AddReadyScript($sScript)
  476. {
  477. $this->sReadyScript .= $sScript;
  478. }
  479. public function GetFieldId($sCode)
  480. {
  481. return $this->GetPrefix().'attr_'.$sCode;
  482. }
  483. public function GetFieldName($sCode)
  484. {
  485. return 'attr_'.$sCode.$this->GetSuffix();
  486. }
  487. public function GetParamName($sCode)
  488. {
  489. return 'attr_'.$sCode.$this->GetSuffix();
  490. }
  491. public function GetValidationArea($sId, $sContent = '')
  492. {
  493. return "<span style=\"display:inline-block;width:20px;\" id=\"v_{$sId}\"><span class=\"ui-icon ui-icon-alert\"></span>$sContent</span>";
  494. }
  495. public function GetAsyncActionClass()
  496. {
  497. return $this->sAsyncActionClass;
  498. }
  499. public function FindField($sFieldCode)
  500. {
  501. $oFoundField = false;
  502. foreach($this->aFieldSets as $sLabel => $aFields)
  503. {
  504. foreach($aFields as $oField)
  505. {
  506. $oFoundField = $oField->FindField($sFieldCode);
  507. if ($oFoundField !== false) break;
  508. }
  509. if ($oFoundField !== false) break;
  510. }
  511. return $oFoundField;
  512. }
  513. }
  514. class DesignerTabularForm extends DesignerForm
  515. {
  516. protected $aTable;
  517. public function __construct()
  518. {
  519. parent::__construct();
  520. $this->aTable = array();
  521. }
  522. public function AddRow($aRow)
  523. {
  524. $this->aTable[] = $aRow;
  525. }
  526. public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
  527. {
  528. return $this->Render($oP, $bReturnHTML);
  529. }
  530. public function Render($oP, $bReturnHTML = false)
  531. {
  532. $sReturn = '';
  533. if ($this->oParentForm == null)
  534. {
  535. $sFormId = $this->sFormId;
  536. $sReturn = '<form id="'.$sFormId.'">';
  537. }
  538. else
  539. {
  540. $sFormId = $this->oParentForm->sFormId;
  541. }
  542. $sHiddenFields = '';
  543. $sReturn .= '<table style="width:100%">';
  544. foreach($this->aTable as $aRow)
  545. {
  546. $sReturn .= '<tr>';
  547. foreach($aRow as $field)
  548. {
  549. if (!is_object($field))
  550. {
  551. // Shortcut: pass a string for a cell containing just a label
  552. $sReturn .= '<td>'.$field.'</td>';
  553. }
  554. else
  555. {
  556. $field->SetForm($this);
  557. $aFieldData = $field->Render($oP, $sFormId);
  558. if ($field->IsVisible())
  559. {
  560. // put the label and value separated by a non-breaking space if needed
  561. $aData = array();
  562. foreach(array('label', 'value') as $sCode )
  563. {
  564. if ($aFieldData[$sCode] != '')
  565. {
  566. $aData[] = $aFieldData[$sCode];
  567. }
  568. }
  569. $sReturn .= '<td>'.implode('&nbsp;', $aData).'</td>';
  570. }
  571. else
  572. {
  573. $sHiddenFields .= $aRow['value'];
  574. }
  575. }
  576. }
  577. $sReturn .= '</tr>';
  578. }
  579. $sReturn .= '</table>';
  580. $sReturn .= $sHiddenFields;
  581. if($this->sScript != '')
  582. {
  583. $oP->add_script($this->sScript);
  584. }
  585. if($this->sReadyScript != '')
  586. {
  587. $oP->add_ready_script($this->sReadyScript);
  588. }
  589. if ($bReturnHTML)
  590. {
  591. return $sReturn;
  592. }
  593. else
  594. {
  595. $oP->add($sReturn);
  596. }
  597. }
  598. public function ReadParams(&$aValues = array())
  599. {
  600. foreach($this->aTable as $aRow)
  601. {
  602. foreach($aRow as $field)
  603. {
  604. if (is_object($field))
  605. {
  606. $field->SetForm($this);
  607. $field->ReadParam($aValues);
  608. }
  609. }
  610. }
  611. return $aValues;
  612. }
  613. }
  614. class DesignerFormField
  615. {
  616. protected $sLabel;
  617. protected $sCode;
  618. protected $defaultValue;
  619. protected $oForm;
  620. protected $bMandatory;
  621. protected $bReadOnly;
  622. protected $bAutoApply;
  623. protected $aCSSClasses;
  624. protected $bDisplayed;
  625. protected $aWidgetExtraParams;
  626. public function __construct($sCode, $sLabel, $defaultValue)
  627. {
  628. $this->sLabel = $sLabel;
  629. $this->sCode = $sCode;
  630. $this->defaultValue = $defaultValue;
  631. $this->bMandatory = false;
  632. $this->bReadOnly = false;
  633. $this->bAutoApply = false;
  634. $this->aCSSClasses = array();
  635. $this->bDisplayed = true;
  636. $this->aWidgetExtraParams = array();
  637. }
  638. public function GetCode()
  639. {
  640. return $this->sCode;
  641. }
  642. public function SetForm($oForm)
  643. {
  644. $this->oForm = $oForm;
  645. }
  646. public function SetMandatory($bMandatory = true)
  647. {
  648. $this->bMandatory = $bMandatory;
  649. }
  650. public function SetReadOnly($bReadOnly = true)
  651. {
  652. $this->bReadOnly = $bReadOnly;
  653. }
  654. public function IsReadOnly()
  655. {
  656. return ($this->oForm->IsReadOnly() || $this->bReadOnly);
  657. }
  658. public function SetAutoApply($bAutoApply)
  659. {
  660. $this->bAutoApply = $bAutoApply;
  661. }
  662. public function IsAutoApply()
  663. {
  664. return $this->bAutoApply;
  665. }
  666. public function SetDisplayed($bDisplayed)
  667. {
  668. $this->bDisplayed = $bDisplayed;
  669. }
  670. public function IsDisplayed()
  671. {
  672. return $this->bDisplayed;
  673. }
  674. public function GetFieldId()
  675. {
  676. return $this->oForm->GetFieldId($this->sCode);
  677. }
  678. public function GetWidgetClass()
  679. {
  680. return 'property_field';
  681. }
  682. public function GetWidgetExtraParams()
  683. {
  684. return $this->aWidgetExtraParams;
  685. }
  686. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  687. {
  688. $sId = $this->oForm->GetFieldId($this->sCode);
  689. $sName = $this->oForm->GetFieldName($this->sCode);
  690. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  691. }
  692. public function ReadParam(&$aValues)
  693. {
  694. if ($this->IsReadOnly())
  695. {
  696. $aValues[$this->sCode] = $this->defaultValue;
  697. }
  698. else
  699. {
  700. if ($this->oForm->GetParamsContainer() != '')
  701. {
  702. $aParams = utils::ReadParam($this->oForm->GetParamsContainer(), array(), false, 'raw_data');
  703. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  704. {
  705. $aValues[$this->sCode] = $aParams[$this->oForm->GetParamName($this->sCode)];
  706. }
  707. else
  708. {
  709. $aValues[$this->sCode] = $this->defaultValue;
  710. }
  711. }
  712. else
  713. {
  714. $aValues[$this->sCode] = utils::ReadParam($this->oForm->GetParamName($this->sCode), $this->defaultValue, false, 'raw_data');
  715. }
  716. }
  717. }
  718. public function IsVisible()
  719. {
  720. return true;
  721. }
  722. public function AddCSSClass($sCSSClass)
  723. {
  724. $this->aCSSClasses[] = $sCSSClass;
  725. }
  726. /**
  727. * A way to set/change the default value after constructing the field
  728. */
  729. public function SetDefaultValueFrom($aAllDefaultValue)
  730. {
  731. if (array_key_exists($this->GetCode(), $aAllDefaultValue))
  732. {
  733. $this->defaultValue = $aAllDefaultValue[$this->GetCode()];
  734. }
  735. }
  736. public function FindField($sFieldCode)
  737. {
  738. if ($this->sCode == $sFieldCode)
  739. {
  740. return $this;
  741. }
  742. return false;
  743. }
  744. public function GetHandlerEquals()
  745. {
  746. return 'null';
  747. }
  748. public function GetHandlerGetValue()
  749. {
  750. return 'null';
  751. }
  752. }
  753. class DesignerLabelField extends DesignerFormField
  754. {
  755. protected $sDescription;
  756. public function __construct($sLabel, $sDescription)
  757. {
  758. parent::__construct('', $sLabel, '');
  759. $this->sDescription = $sDescription;
  760. }
  761. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  762. {
  763. $sId = $this->oForm->GetFieldId($this->sCode);
  764. $sName = $this->oForm->GetFieldName($this->sCode);
  765. return array('label' => $this->sLabel, 'value' => $this->sDescription);
  766. }
  767. public function ReadParam(&$aValues)
  768. {
  769. }
  770. public function IsVisible()
  771. {
  772. return true;
  773. }
  774. }
  775. class DesignerTextField extends DesignerFormField
  776. {
  777. protected $sValidationPattern;
  778. protected $aForbiddenValues;
  779. protected $sExplainForbiddenValues;
  780. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  781. {
  782. parent::__construct($sCode, $sLabel, $defaultValue);
  783. $this->sValidationPattern = '';
  784. $this->aForbiddenValues = null;
  785. $this->sExplainForbiddenValues = null;
  786. }
  787. public function SetValidationPattern($sValidationPattern)
  788. {
  789. $this->sValidationPattern = $sValidationPattern;
  790. }
  791. public function SetForbiddenValues($aValues, $sExplain)
  792. {
  793. $this->aForbiddenValues = $aValues;
  794. $iDefaultKey = array_search($this->defaultValue, $this->aForbiddenValues);
  795. if ($iDefaultKey !== false)
  796. {
  797. // The default (current) value is always allowed...
  798. unset($this->aForbiddenValues[$iDefaultKey]);
  799. }
  800. $this->sExplainForbiddenValues = $sExplain;
  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. $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues);
  817. }
  818. else
  819. {
  820. $sForbiddenValues = 'null';
  821. $sExplainForbiddenValues = 'null';
  822. }
  823. $sMandatory = $this->bMandatory ? 'true' : 'false';
  824. $oP->add_ready_script(
  825. <<<EOF
  826. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', $(this).closest('form').attr('id'), $sForbiddenValues, '$sExplainForbiddenValues'); } );
  827. {
  828. var myTimer = null;
  829. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  830. }
  831. EOF
  832. );
  833. $sCSSClasses = '';
  834. if (count($this->aCSSClasses) > 0)
  835. {
  836. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  837. }
  838. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  839. }
  840. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  841. }
  842. public function ReadParam(&$aValues)
  843. {
  844. parent::ReadParam($aValues);
  845. if (($this->sValidationPattern != '') &&(!preg_match('/'.$this->sValidationPattern.'/', $aValues[$this->sCode])) )
  846. {
  847. $aValues[$this->sCode] = $this->defaultValue;
  848. }
  849. else if(($this->aForbiddenValues != null) && in_array($aValues[$this->sCode], $this->aForbiddenValues))
  850. {
  851. // Reject the value...
  852. $aValues[$this->sCode] = $this->defaultValue;
  853. }
  854. }
  855. }
  856. class DesignerLongTextField extends DesignerTextField
  857. {
  858. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  859. {
  860. $sId = $this->oForm->GetFieldId($this->sCode);
  861. $sName = $this->oForm->GetFieldName($this->sCode);
  862. $sPattern = addslashes($this->sValidationPattern);
  863. if (is_array($this->aForbiddenValues))
  864. {
  865. $sForbiddenValues = json_encode($this->aForbiddenValues);
  866. $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues);
  867. }
  868. else
  869. {
  870. $sForbiddenValues = 'null';
  871. $sExplainForbiddenValues = 'null';
  872. }
  873. $sMandatory = $this->bMandatory ? 'true' : 'false';
  874. $sCSSClasses = '';
  875. if (count($this->aCSSClasses) > 0)
  876. {
  877. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  878. }
  879. if (!$this->IsReadOnly())
  880. {
  881. $oP->add_ready_script(
  882. <<<EOF
  883. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', $(this).closest('form').attr('id'), $sForbiddenValues, '$sExplainForbiddenValues'); } );
  884. {
  885. var myTimer = null;
  886. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  887. }
  888. EOF
  889. );
  890. $sValue = "<textarea $sCSSClasses id=\"$sId\" name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>";
  891. }
  892. else
  893. {
  894. $sValue = "<div $sCSSClasses id=\"$sId\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</div>";
  895. }
  896. return array('label' => $this->sLabel, 'value' => $sValue);
  897. }
  898. }
  899. class DesignerIntegerField extends DesignerFormField
  900. {
  901. protected $iMin; // Lower boundary, inclusive
  902. protected $iMax; // Higher boundary, inclusive
  903. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  904. {
  905. parent::__construct($sCode, $sLabel, $defaultValue);
  906. $this->iMin = 0; // Positive integer is the default
  907. $this->iMax = null;
  908. }
  909. public function SetBoundaries($iMin = null, $iMax = null)
  910. {
  911. $this->iMin = $iMin;
  912. $this->iMax = $iMax;
  913. }
  914. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  915. {
  916. $sId = $this->oForm->GetFieldId($this->sCode);
  917. $sName = $this->oForm->GetFieldName($this->sCode);
  918. if ($this->IsReadOnly())
  919. {
  920. $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>";
  921. }
  922. else
  923. {
  924. $sMin = json_encode($this->iMin);
  925. $sMax = json_encode($this->iMax);
  926. $sMandatory = $this->bMandatory ? 'true' : 'false';
  927. $oP->add_ready_script(
  928. <<<EOF
  929. $('#$sId').bind('change keyup validate', function() { ValidateInteger('$sId', $sMandatory, $(this).closest('form').attr('id'), $sMin, $sMax); } );
  930. {
  931. var myTimer = null;
  932. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  933. }
  934. EOF
  935. );
  936. $sCSSClasses = '';
  937. if (count($this->aCSSClasses) > 0)
  938. {
  939. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  940. }
  941. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  942. }
  943. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  944. }
  945. public function ReadParam(&$aValues)
  946. {
  947. parent::ReadParam($aValues);
  948. if (!is_null($this->iMin) && ($aValues[$this->sCode] < $this->iMin))
  949. {
  950. // Reject the value...
  951. $aValues[$this->sCode] = $this->defaultValue;
  952. }
  953. if (!is_null($this->iMax) && ($aValues[$this->sCode] > $this->iMax))
  954. {
  955. // Reject the value...
  956. $aValues[$this->sCode] = $this->defaultValue;
  957. }
  958. }
  959. }
  960. class DesignerComboField extends DesignerFormField
  961. {
  962. protected $aAllowedValues;
  963. protected $bMultipleSelection;
  964. protected $bOtherChoices;
  965. protected $sNullLabel;
  966. protected $bSorted;
  967. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  968. {
  969. parent::__construct($sCode, $sLabel, $defaultValue);
  970. $this->aAllowedValues = array();
  971. $this->bMultipleSelection = false;
  972. $this->bOtherChoices = false;
  973. $this->sNullLabel = Dict::S('UI:SelectOne');
  974. $this->bAutoApply = true;
  975. $this->bSorted = true; // Sorted by default
  976. }
  977. public function SetAllowedValues($aAllowedValues)
  978. {
  979. $this->aAllowedValues = $aAllowedValues;
  980. }
  981. public function MultipleSelection($bMultipleSelection = true)
  982. {
  983. $this->bMultipleSelection = $bMultipleSelection;
  984. }
  985. public function OtherChoices($bOtherChoices = true)
  986. {
  987. $this->bOtherChoices = $bOtherChoices;
  988. }
  989. /**
  990. * An empty label will disable the default empty value
  991. */
  992. public function SetNullLabel($sLabel)
  993. {
  994. $this->sNullLabel = $sLabel;
  995. }
  996. public function IsSorted()
  997. {
  998. return $this->bSorted;
  999. }
  1000. public function SetSorted($bSorted)
  1001. {
  1002. $this->bSorted = $bSorted;
  1003. }
  1004. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1005. {
  1006. $sId = $this->oForm->GetFieldId($this->sCode);
  1007. $sName = $this->oForm->GetFieldName($this->sCode);
  1008. $sChecked = $this->defaultValue ? 'checked' : '';
  1009. $sMandatory = $this->bMandatory ? 'true' : 'false';
  1010. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  1011. if ($this->IsSorted())
  1012. {
  1013. asort($this->aAllowedValues);
  1014. }
  1015. $sCSSClasses = '';
  1016. if (count($this->aCSSClasses) > 0)
  1017. {
  1018. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1019. }
  1020. if ($this->IsReadOnly())
  1021. {
  1022. $aSelected = array();
  1023. $aHiddenValues = array();
  1024. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1025. {
  1026. if ($this->bMultipleSelection)
  1027. {
  1028. if(in_array($sKey, $this->defaultValue))
  1029. {
  1030. $aSelected[] = $sDisplayValue;
  1031. $aHiddenValues[] = "<input type=\"hidden\" name=\"{$sName}[]\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1032. }
  1033. }
  1034. else
  1035. {
  1036. if ($sKey == $this->defaultValue)
  1037. {
  1038. $aSelected[] = $sDisplayValue;
  1039. $aHiddenValues[] = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1040. }
  1041. }
  1042. }
  1043. $sHtml = "<span $sCSSClasses>".htmlentities(implode(', ', $aSelected), ENT_QUOTES, 'UTF-8').implode($aHiddenValues)."</span>";
  1044. }
  1045. else
  1046. {
  1047. if ($this->bMultipleSelection)
  1048. {
  1049. $sHtml = "<select $sCSSClasses multiple size=\"8\"id=\"$sId\" name=\"$sName\">";
  1050. }
  1051. else
  1052. {
  1053. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\">";
  1054. if ($this->sNullLabel != '')
  1055. {
  1056. $sHtml .= "<option value=\"\">".$this->sNullLabel."</option>";
  1057. }
  1058. }
  1059. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1060. {
  1061. if ($this->bMultipleSelection)
  1062. {
  1063. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  1064. }
  1065. else
  1066. {
  1067. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  1068. }
  1069. // Quick and dirty: display the menu parents as a tree
  1070. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  1071. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  1072. }
  1073. $sHtml .= "</select>";
  1074. if ($this->bOtherChoices)
  1075. {
  1076. $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"/>';
  1077. }
  1078. $oP->add_ready_script(
  1079. <<<EOF
  1080. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', $(this).closest('form').attr('id'), null, null); } );
  1081. EOF
  1082. );
  1083. }
  1084. return array('label' => $this->sLabel, 'value' => $sHtml);
  1085. }
  1086. public function ReadParam(&$aValues)
  1087. {
  1088. parent::ReadParam($aValues);
  1089. if ($aValues[$this->sCode] == 'null')
  1090. {
  1091. $aValues[$this->sCode] = array();
  1092. }
  1093. }
  1094. }
  1095. class DesignerBooleanField extends DesignerFormField
  1096. {
  1097. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1098. {
  1099. parent::__construct($sCode, $sLabel, $defaultValue);
  1100. $this->bAutoApply = true;
  1101. }
  1102. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1103. {
  1104. $sId = $this->oForm->GetFieldId($this->sCode);
  1105. $sName = $this->oForm->GetFieldName($this->sCode);
  1106. $sChecked = $this->defaultValue ? 'checked' : '';
  1107. if ($this->IsReadOnly())
  1108. {
  1109. $sLabel = $this->defaultValue ? Dict::S('UI:UserManagement:ActionAllowed:Yes') : Dict::S('UI:UserManagement:ActionAllowed:No'); //TODO use our own yes/no translations
  1110. $sHtmlValue = "<span>".htmlentities($sLabel)."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  1111. }
  1112. else
  1113. {
  1114. $sCSSClasses = '';
  1115. if (count($this->aCSSClasses) > 0)
  1116. {
  1117. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1118. }
  1119. $sHtmlValue = "<input $sCSSClasses type=\"checkbox\" $sChecked id=\"$sId\" name=\"$sName\" value=\"true\">";
  1120. }
  1121. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  1122. }
  1123. public function ReadParam(&$aValues)
  1124. {
  1125. if ($this->IsReadOnly())
  1126. {
  1127. $aValues[$this->sCode] = $this->defaultValue;
  1128. }
  1129. else
  1130. {
  1131. $sParamsContainer = $this->oForm->GetParamsContainer();
  1132. if ($sParamsContainer != '')
  1133. {
  1134. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  1135. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  1136. {
  1137. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  1138. }
  1139. else
  1140. {
  1141. $sValue = 'false';
  1142. }
  1143. }
  1144. else
  1145. {
  1146. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  1147. }
  1148. $aValues[$this->sCode] = ($sValue == 'true');
  1149. }
  1150. }
  1151. }
  1152. class DesignerHiddenField extends DesignerFormField
  1153. {
  1154. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1155. {
  1156. parent::__construct($sCode, $sLabel, $defaultValue);
  1157. }
  1158. public function IsVisible()
  1159. {
  1160. return false;
  1161. }
  1162. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1163. {
  1164. $sId = $this->oForm->GetFieldId($this->sCode);
  1165. $sName = $this->oForm->GetFieldName($this->sCode);
  1166. $sChecked = $this->defaultValue ? 'checked' : '';
  1167. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1168. }
  1169. }
  1170. class DesignerIconSelectionField extends DesignerFormField
  1171. {
  1172. protected $sUploadUrl;
  1173. protected $aAllowedValues;
  1174. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1175. {
  1176. parent::__construct($sCode, $sLabel, $defaultValue);
  1177. $this->bAutoApply = true;
  1178. $this->sUploadUrl = null;
  1179. }
  1180. public function SetAllowedValues($aAllowedValues)
  1181. {
  1182. $this->aAllowedValues = $aAllowedValues;
  1183. }
  1184. public function EnableUpload($sIconUploadUrl)
  1185. {
  1186. $this->sUploadUrl = $sIconUploadUrl;
  1187. }
  1188. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1189. {
  1190. $sId = $this->oForm->GetFieldId($this->sCode);
  1191. $sName = $this->oForm->GetFieldName($this->sCode);
  1192. $idx = 0;
  1193. foreach($this->aAllowedValues as $index => $aValue)
  1194. {
  1195. if ($aValue['value'] == $this->defaultValue)
  1196. {
  1197. $idx = $index;
  1198. break;
  1199. }
  1200. }
  1201. $sJSItems = json_encode($this->aAllowedValues);
  1202. $sPostUploadTo = ($this->sUploadUrl == null) ? 'null' : "'{$this->sUploadUrl}'";
  1203. if (!$this->IsReadOnly())
  1204. {
  1205. $sValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>";
  1206. $oP->add_ready_script(
  1207. <<<EOF
  1208. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems, post_upload_to: $sPostUploadTo});
  1209. EOF
  1210. );
  1211. }
  1212. else
  1213. {
  1214. $sValue = '<img src="'.$this->MakeFileUrl($this->defaultValue).'" />';
  1215. }
  1216. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  1217. return array('label' => $this->sLabel, 'value' => $sValue);
  1218. }
  1219. }
  1220. class RunTimeIconSelectionField extends DesignerIconSelectionField
  1221. {
  1222. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1223. {
  1224. parent::__construct($sCode, $sLabel, $defaultValue);
  1225. $aAllIcons = self::FindIconsOnDisk(APPROOT.'env-'.utils::GetCurrentEnvironment());
  1226. ksort($aAllIcons);
  1227. $aValues = array();
  1228. foreach($aAllIcons as $sFilePath)
  1229. {
  1230. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  1231. }
  1232. $this->SetAllowedValues($aValues);
  1233. }
  1234. static protected function FindIconsOnDisk($sBaseDir, $sDir = '')
  1235. {
  1236. $aResult = array();
  1237. // Populate automatically the list of icon files
  1238. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  1239. {
  1240. while (($sFile = readdir($hDir)) !== false)
  1241. {
  1242. $aMatches = array();
  1243. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  1244. {
  1245. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  1246. $aResult = array_merge($aResult, self::FindIconsOnDisk($sBaseDir, $sDirSubPath));
  1247. }
  1248. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  1249. {
  1250. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  1251. }
  1252. }
  1253. closedir($hDir);
  1254. }
  1255. return $aResult;
  1256. }
  1257. public function ValueFromDOMNode($oDOMNode)
  1258. {
  1259. return $oDOMNode->textContent;
  1260. }
  1261. public function ValueToDOMNode($oDOMNode, $value)
  1262. {
  1263. $oTextNode = $oDOMNode->ownerDocument->createTextNode($value);
  1264. $oDOMNode->appendChild($oTextNode);
  1265. }
  1266. public function MakeFileUrl($value)
  1267. {
  1268. return utils::GetAbsoluteUrlModulesRoot().$value;
  1269. }
  1270. public function GetDefaultValue($sClass = 'Contact')
  1271. {
  1272. $sIconPath = MetaModel::GetClassIcon($sClass, false);
  1273. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIconPath);
  1274. return $sIcon;
  1275. }
  1276. }
  1277. class DesignerSortableField extends DesignerFormField
  1278. {
  1279. protected $aAllowedValues;
  1280. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1281. {
  1282. parent::__construct($sCode, $sLabel, $defaultValue);
  1283. $this->aAllowedValues = array();
  1284. }
  1285. public function SetAllowedValues($aAllowedValues)
  1286. {
  1287. $this->aAllowedValues = $aAllowedValues;
  1288. }
  1289. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1290. {
  1291. $bOpen = false;
  1292. $sId = $this->oForm->GetFieldId($this->sCode);
  1293. $sName = $this->oForm->GetFieldName($this->sCode);
  1294. $sReadOnly = $this->IsReadOnly() ? 'readonly="readonly"' : '';
  1295. $aResult = array('label' => $this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" $sReadOnly value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1296. $sJSFields = json_encode(array_keys($this->aAllowedValues));
  1297. $oP->add_ready_script(
  1298. "$('#$sId').sortable_field({aAvailableFields: $sJSFields});"
  1299. );
  1300. return $aResult;
  1301. }
  1302. }
  1303. class DesignerFormSelectorField extends DesignerFormField
  1304. {
  1305. protected $aSubForms;
  1306. protected $defaultRealValue; // What's stored as default value is actually the index
  1307. protected $bSorted;
  1308. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1309. {
  1310. parent::__construct($sCode, $sLabel, 0);
  1311. $this->defaultRealValue = $defaultValue;
  1312. $this->aSubForms = array();
  1313. $this->bSorted = true;
  1314. }
  1315. public function IsSorted()
  1316. {
  1317. return $this->bSorted;
  1318. }
  1319. public function SetSorted($bSorted)
  1320. {
  1321. $this->bSorted = $bSorted;
  1322. }
  1323. /**
  1324. * Callback for sorting an array of $aFormData based ont he labels of the subforms
  1325. * @param unknown $aItem1
  1326. * @param unknown $aItem2
  1327. * @return number
  1328. */
  1329. static function SortOnFormLabel($aItem1, $aItem2)
  1330. {
  1331. return strcasecmp($aItem1['label'], $aItem2['label']);
  1332. }
  1333. public function GetWidgetClass()
  1334. {
  1335. return 'selector_property_field';
  1336. }
  1337. public function AddSubForm($oSubForm, $sLabel, $sValue)
  1338. {
  1339. $idx = count($this->aSubForms);
  1340. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  1341. if ($sValue == $this->defaultRealValue)
  1342. {
  1343. // Store the index of the selected/default form
  1344. $this->defaultValue = count($this->aSubForms) - 1;
  1345. }
  1346. }
  1347. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1348. {
  1349. $sId = $this->oForm->GetFieldId($this->sCode);
  1350. $sName = $this->oForm->GetFieldName($this->sCode);
  1351. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  1352. $this->aCSSClasses[] = 'formSelector';
  1353. $sCSSClasses = '';
  1354. if (count($this->aCSSClasses) > 0)
  1355. {
  1356. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1357. }
  1358. if ($this->IsSorted())
  1359. {
  1360. uasort($this->aSubForms, array(get_class($this), 'SortOnFormLabel'));
  1361. }
  1362. if ($this->IsReadOnly())
  1363. {
  1364. $aSelected = array();
  1365. $aHiddenValues = array();
  1366. $sDisplayValue = '';
  1367. $sHiddenValue = '';
  1368. foreach($this->aSubForms as $iKey => $aFormData)
  1369. {
  1370. if ($iKey == $this->defaultValue) // Default value is actually the index
  1371. {
  1372. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');
  1373. $sHiddenValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($iKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1374. break;
  1375. }
  1376. }
  1377. $sHtml = "<span $sCSSClasses>".$sDisplayValue.$sHiddenValue."</span>";
  1378. }
  1379. else
  1380. {
  1381. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  1382. foreach($this->aSubForms as $iKey => $aFormData)
  1383. {
  1384. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');;
  1385. $sSelected = ($iKey == $this->defaultValue) ? 'selected' : '';
  1386. $sHtml .= "<option value=\"$iKey\" $sSelected>".$sDisplayValue."</option>";
  1387. }
  1388. $sHtml .= "</select>";
  1389. }
  1390. if ($sRenderMode == 'property')
  1391. {
  1392. $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>';
  1393. }
  1394. foreach($this->aSubForms as $sKey => $aFormData)
  1395. {
  1396. $sId = $this->oForm->GetFieldId($this->sCode);
  1397. $sStyle = (($sKey == $this->defaultValue) && $this->oForm->IsDisplayed()) ? '' : 'style="display:none"';
  1398. $oSubForm = $aFormData['form'];
  1399. $oSubForm->SetParentForm($this->oForm);
  1400. $oSubForm->CopySubmitParams($this->oForm);
  1401. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1402. if ($sRenderMode == 'property')
  1403. {
  1404. // Note: Managing the visibility of nested subforms had several implications
  1405. // 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
  1406. // 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).
  1407. // The div hierarchy has been emulated by adding attributes to the tbody tags:
  1408. // - data-selector : uniquely identifies the DesignerFormSelectorField that has an impact on the visibility of the node
  1409. // - data-path : uniquely identifies the combination of users choices that must be made to show the node
  1410. // - 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)
  1411. // Then a series of actions are performed to hide and show the relevant nodes, depending on the user choice
  1412. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1413. $oSubForm->SetHierarchyParent($sSelector);
  1414. $sPath = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix().'-'.$sKey;
  1415. $oSubForm->SetHierarchyPath($sPath);
  1416. $oSubForm->SetDisplayed($sKey == $this->defaultValue);
  1417. $sState = ($sKey == $this->defaultValue) ? 'visible' : 'hidden';
  1418. //$sHtml .= "</tbody><tbody data-selector=\"$sSelector\" data-path=\"$sPath\" data-state=\"$sState\" $sStyle>";
  1419. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  1420. $sState = $this->oForm->IsDisplayed() ? 'visible' : 'hidden';
  1421. $sParentStyle = '';
  1422. if ($oParent = $this->oForm->GetParentForm())
  1423. {
  1424. $sParentStyle = ($oParent->IsDisplayed()) ? '' : 'style="display:none"';
  1425. $sParentSelector = $oParent->GetHierarchyParent();
  1426. $sParentPath = $oParent->GetHierarchyPath();
  1427. }
  1428. else
  1429. {
  1430. $sParentSelector = '';
  1431. $sParentPath = '';
  1432. }
  1433. //$sHtml .= "</tbody><tbody data-selector=\"$sParentSelector\" data-path=\"$sParentPath\" data-state=\"$sState\" $sParentStyle>";
  1434. }
  1435. else
  1436. {
  1437. $sHtml .= "<div class=\"subform_{$sId} {$sId}_{$sKey}\" $sStyle>";
  1438. $sHtml .= $oSubForm->Render($oP, true);
  1439. $sHtml .= "</div>";
  1440. }
  1441. }
  1442. if ($sRenderMode == 'property')
  1443. {
  1444. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1445. $this->aWidgetExtraParams['data_selector'] = $sSelector;
  1446. }
  1447. else
  1448. {
  1449. $oP->add_ready_script(
  1450. <<<EOF
  1451. $('#$sId').bind('change reverted', function() { $('.subform_{$sId}').hide(); $('.{$sId}_'+this.value).show(); } );
  1452. EOF
  1453. );
  1454. }
  1455. return array('label' => $this->sLabel, 'value' => $sHtml);
  1456. }
  1457. public function ReadParam(&$aValues)
  1458. {
  1459. parent::ReadParam($aValues);
  1460. $sKey = $aValues[$this->sCode];
  1461. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  1462. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1463. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  1464. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  1465. }
  1466. public function SetDefaultValueFrom($aAllDefaultValues)
  1467. {
  1468. if (array_key_exists($this->GetCode(), $aAllDefaultValues))
  1469. {
  1470. $selectedValue = $aAllDefaultValues[$this->GetCode()];
  1471. foreach($this->aSubForms as $iKey => $aFormData)
  1472. {
  1473. $sId = $this->oForm->GetFieldId($this->sCode);
  1474. if ($selectedValue == $aFormData['value'])
  1475. {
  1476. $this->defaultValue =$iKey;
  1477. $aDefaultValues = $this->oForm->GetDefaultValues();
  1478. $oSubForm = $aFormData['form'];
  1479. $oSubForm->SetDefaultValues($aAllDefaultValues);
  1480. }
  1481. }
  1482. }
  1483. }
  1484. public function FindField($sFieldCode)
  1485. {
  1486. $oField = parent::FindField($sFieldCode);
  1487. if ($oField === false)
  1488. {
  1489. // Look in the subforms
  1490. foreach($this->aSubForms as $sKey => $aFormData)
  1491. {
  1492. $oSubForm = $aFormData['form'];
  1493. $oField = $oSubForm->FindField($sFieldCode);
  1494. if ($oField !== false)
  1495. {
  1496. break;
  1497. }
  1498. }
  1499. }
  1500. return $oField;
  1501. }
  1502. }
  1503. class DesignerSubFormField extends DesignerFormField
  1504. {
  1505. protected $oSubForm;
  1506. public function __construct($sLabel, $oSubForm)
  1507. {
  1508. parent::__construct('', $sLabel, '');
  1509. $this->oSubForm = $oSubForm;
  1510. }
  1511. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1512. {
  1513. $this->oSubForm->SetParentForm($this->oForm);
  1514. $this->oSubForm->CopySubmitParams($this->oForm);
  1515. if ($sRenderMode == 'property')
  1516. {
  1517. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  1518. }
  1519. else
  1520. {
  1521. $sHtml = $this->oSubForm->Render($oP, true);
  1522. }
  1523. return array('label' => $this->sLabel, 'value' => $sHtml);
  1524. }
  1525. public function ReadParam(&$aValues)
  1526. {
  1527. $this->oSubForm->SetParentForm($this->oForm);
  1528. $this->oSubForm->ReadParams($aValues);
  1529. }
  1530. public function FindField($sFieldCode)
  1531. {
  1532. $oField = parent::FindField($sFieldCode);
  1533. if ($oField === false)
  1534. {
  1535. // Look in the subform
  1536. $oField = $this->oSubForm->FindField($sFieldCode);
  1537. }
  1538. return $oField;
  1539. }
  1540. }
  1541. class DesignerStaticTextField extends DesignerFormField
  1542. {
  1543. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1544. {
  1545. parent::__construct($sCode, $sLabel, $defaultValue);
  1546. }
  1547. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1548. {
  1549. return array('label' => $this->sLabel, 'value' => $this->defaultValue);
  1550. }
  1551. }