forms.class.inc.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  875. $oP->add_ready_script(
  876. <<<EOF
  877. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', $(this).closest('form').attr('id'), $sForbiddenValues, '$sExplainForbiddenValues'); } );
  878. {
  879. var myTimer = null;
  880. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  881. }
  882. EOF
  883. );
  884. $sCSSClasses = '';
  885. if (count($this->aCSSClasses) > 0)
  886. {
  887. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  888. }
  889. return array('label' => $this->sLabel, 'value' => "<textarea $sCSSClasses id=\"$sId\" $sReadOnly name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>");
  890. }
  891. }
  892. class DesignerIntegerField extends DesignerFormField
  893. {
  894. protected $iMin; // Lower boundary, inclusive
  895. protected $iMax; // Higher boundary, inclusive
  896. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  897. {
  898. parent::__construct($sCode, $sLabel, $defaultValue);
  899. $this->iMin = 0; // Positive integer is the default
  900. $this->iMax = null;
  901. }
  902. public function SetBoundaries($iMin = null, $iMax = null)
  903. {
  904. $this->iMin = $iMin;
  905. $this->iMax = $iMax;
  906. }
  907. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  908. {
  909. $sId = $this->oForm->GetFieldId($this->sCode);
  910. $sName = $this->oForm->GetFieldName($this->sCode);
  911. if ($this->IsReadOnly())
  912. {
  913. $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>";
  914. }
  915. else
  916. {
  917. $sMin = json_encode($this->iMin);
  918. $sMax = json_encode($this->iMax);
  919. $sMandatory = $this->bMandatory ? 'true' : 'false';
  920. $oP->add_ready_script(
  921. <<<EOF
  922. $('#$sId').bind('change keyup validate', function() { ValidateInteger('$sId', $sMandatory, $(this).closest('form').attr('id'), $sMin, $sMax); } );
  923. {
  924. var myTimer = null;
  925. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  926. }
  927. EOF
  928. );
  929. $sCSSClasses = '';
  930. if (count($this->aCSSClasses) > 0)
  931. {
  932. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  933. }
  934. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  935. }
  936. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  937. }
  938. public function ReadParam(&$aValues)
  939. {
  940. parent::ReadParam($aValues);
  941. if (!is_null($this->iMin) && ($aValues[$this->sCode] < $this->iMin))
  942. {
  943. // Reject the value...
  944. $aValues[$this->sCode] = $this->defaultValue;
  945. }
  946. if (!is_null($this->iMax) && ($aValues[$this->sCode] > $this->iMax))
  947. {
  948. // Reject the value...
  949. $aValues[$this->sCode] = $this->defaultValue;
  950. }
  951. }
  952. }
  953. class DesignerComboField extends DesignerFormField
  954. {
  955. protected $aAllowedValues;
  956. protected $bMultipleSelection;
  957. protected $bOtherChoices;
  958. protected $sNullLabel;
  959. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  960. {
  961. parent::__construct($sCode, $sLabel, $defaultValue);
  962. $this->aAllowedValues = array();
  963. $this->bMultipleSelection = false;
  964. $this->bOtherChoices = false;
  965. $this->sNullLabel = Dict::S('UI:SelectOne');
  966. $this->bAutoApply = true;
  967. }
  968. public function SetAllowedValues($aAllowedValues)
  969. {
  970. $this->aAllowedValues = $aAllowedValues;
  971. }
  972. public function MultipleSelection($bMultipleSelection = true)
  973. {
  974. $this->bMultipleSelection = $bMultipleSelection;
  975. }
  976. public function OtherChoices($bOtherChoices = true)
  977. {
  978. $this->bOtherChoices = $bOtherChoices;
  979. }
  980. /**
  981. * An empty label will disable the default empty value
  982. */
  983. public function SetNullLabel($sLabel)
  984. {
  985. $this->sNullLabel = $sLabel;
  986. }
  987. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  988. {
  989. $sId = $this->oForm->GetFieldId($this->sCode);
  990. $sName = $this->oForm->GetFieldName($this->sCode);
  991. $sChecked = $this->defaultValue ? 'checked' : '';
  992. $sMandatory = $this->bMandatory ? 'true' : 'false';
  993. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  994. $sCSSClasses = '';
  995. if (count($this->aCSSClasses) > 0)
  996. {
  997. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  998. }
  999. if ($this->IsReadOnly())
  1000. {
  1001. $aSelected = array();
  1002. $aHiddenValues = array();
  1003. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1004. {
  1005. if ($this->bMultipleSelection)
  1006. {
  1007. if(in_array($sKey, $this->defaultValue))
  1008. {
  1009. $aSelected[] = $sDisplayValue;
  1010. $aHiddenValues[] = "<input type=\"hidden\" name=\"{$sName}[]\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1011. }
  1012. }
  1013. else
  1014. {
  1015. if ($sKey == $this->defaultValue)
  1016. {
  1017. $aSelected[] = $sDisplayValue;
  1018. $aHiddenValues[] = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1019. }
  1020. }
  1021. }
  1022. $sHtml = "<span $sCSSClasses>".htmlentities(implode(', ', $aSelected), ENT_QUOTES, 'UTF-8').implode($aHiddenValues)."</span>";
  1023. }
  1024. else
  1025. {
  1026. if ($this->bMultipleSelection)
  1027. {
  1028. $sHtml = "<select $sCSSClasses multiple size=\"8\"id=\"$sId\" name=\"$sName\">";
  1029. }
  1030. else
  1031. {
  1032. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\">";
  1033. if ($this->sNullLabel != '')
  1034. {
  1035. $sHtml .= "<option value=\"\">".$this->sNullLabel."</option>";
  1036. }
  1037. }
  1038. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1039. {
  1040. if ($this->bMultipleSelection)
  1041. {
  1042. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  1043. }
  1044. else
  1045. {
  1046. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  1047. }
  1048. // Quick and dirty: display the menu parents as a tree
  1049. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  1050. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  1051. }
  1052. $sHtml .= "</select>";
  1053. if ($this->bOtherChoices)
  1054. {
  1055. $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"/>';
  1056. }
  1057. $oP->add_ready_script(
  1058. <<<EOF
  1059. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', $(this).closest('form').attr('id'), null, null); } );
  1060. EOF
  1061. );
  1062. }
  1063. return array('label' => $this->sLabel, 'value' => $sHtml);
  1064. }
  1065. public function ReadParam(&$aValues)
  1066. {
  1067. parent::ReadParam($aValues);
  1068. if ($aValues[$this->sCode] == 'null')
  1069. {
  1070. $aValues[$this->sCode] = array();
  1071. }
  1072. }
  1073. }
  1074. class DesignerBooleanField extends DesignerFormField
  1075. {
  1076. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1077. {
  1078. parent::__construct($sCode, $sLabel, $defaultValue);
  1079. $this->bAutoApply = true;
  1080. }
  1081. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1082. {
  1083. $sId = $this->oForm->GetFieldId($this->sCode);
  1084. $sName = $this->oForm->GetFieldName($this->sCode);
  1085. $sChecked = $this->defaultValue ? 'checked' : '';
  1086. if ($this->IsReadOnly())
  1087. {
  1088. $sLabel = $this->defaultValue ? Dict::S('UI:UserManagement:ActionAllowed:Yes') : Dict::S('UI:UserManagement:ActionAllowed:No'); //TODO use our own yes/no translations
  1089. $sHtmlValue = "<span>".htmlentities($sLabel)."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  1090. }
  1091. else
  1092. {
  1093. $sCSSClasses = '';
  1094. if (count($this->aCSSClasses) > 0)
  1095. {
  1096. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1097. }
  1098. $sHtmlValue = "<input $sCSSClasses type=\"checkbox\" $sChecked id=\"$sId\" name=\"$sName\" value=\"true\">";
  1099. }
  1100. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  1101. }
  1102. public function ReadParam(&$aValues)
  1103. {
  1104. if ($this->IsReadOnly())
  1105. {
  1106. $aValues[$this->sCode] = $this->defaultValue;
  1107. }
  1108. else
  1109. {
  1110. $sParamsContainer = $this->oForm->GetParamsContainer();
  1111. if ($sParamsContainer != '')
  1112. {
  1113. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  1114. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  1115. {
  1116. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  1117. }
  1118. else
  1119. {
  1120. $sValue = 'false';
  1121. }
  1122. }
  1123. else
  1124. {
  1125. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  1126. }
  1127. $aValues[$this->sCode] = ($sValue == 'true');
  1128. }
  1129. }
  1130. }
  1131. class DesignerHiddenField extends DesignerFormField
  1132. {
  1133. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1134. {
  1135. parent::__construct($sCode, $sLabel, $defaultValue);
  1136. }
  1137. public function IsVisible()
  1138. {
  1139. return false;
  1140. }
  1141. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1142. {
  1143. $sId = $this->oForm->GetFieldId($this->sCode);
  1144. $sName = $this->oForm->GetFieldName($this->sCode);
  1145. $sChecked = $this->defaultValue ? 'checked' : '';
  1146. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1147. }
  1148. }
  1149. class DesignerIconSelectionField extends DesignerFormField
  1150. {
  1151. protected $sUploadUrl;
  1152. protected $aAllowedValues;
  1153. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1154. {
  1155. parent::__construct($sCode, $sLabel, $defaultValue);
  1156. $this->bAutoApply = true;
  1157. $this->sUploadUrl = null;
  1158. }
  1159. public function SetAllowedValues($aAllowedValues)
  1160. {
  1161. $this->aAllowedValues = $aAllowedValues;
  1162. }
  1163. public function EnableUpload($sIconUploadUrl)
  1164. {
  1165. $this->sUploadUrl = $sIconUploadUrl;
  1166. }
  1167. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1168. {
  1169. $sId = $this->oForm->GetFieldId($this->sCode);
  1170. $sName = $this->oForm->GetFieldName($this->sCode);
  1171. $idx = 0;
  1172. foreach($this->aAllowedValues as $index => $aValue)
  1173. {
  1174. if ($aValue['value'] == $this->defaultValue)
  1175. {
  1176. $idx = $index;
  1177. break;
  1178. }
  1179. }
  1180. $sJSItems = json_encode($this->aAllowedValues);
  1181. $sPostUploadTo = ($this->sUploadUrl == null) ? 'null' : "'{$this->sUploadUrl}'";
  1182. if (!$this->IsReadOnly())
  1183. {
  1184. $sValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>";
  1185. $oP->add_ready_script(
  1186. <<<EOF
  1187. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems, post_upload_to: $sPostUploadTo});
  1188. EOF
  1189. );
  1190. }
  1191. else
  1192. {
  1193. $sValue = '<img src="'.$this->MakeFileUrl($this->defaultValue).'" />';
  1194. }
  1195. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  1196. return array('label' => $this->sLabel, 'value' => $sValue);
  1197. }
  1198. }
  1199. class RunTimeIconSelectionField extends DesignerIconSelectionField
  1200. {
  1201. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1202. {
  1203. parent::__construct($sCode, $sLabel, $defaultValue);
  1204. $aAllIcons = self::FindIconsOnDisk(APPROOT.'env-'.utils::GetCurrentEnvironment());
  1205. ksort($aAllIcons);
  1206. $aValues = array();
  1207. foreach($aAllIcons as $sFilePath)
  1208. {
  1209. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  1210. }
  1211. $this->SetAllowedValues($aValues);
  1212. }
  1213. static protected function FindIconsOnDisk($sBaseDir, $sDir = '')
  1214. {
  1215. $aResult = array();
  1216. // Populate automatically the list of icon files
  1217. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  1218. {
  1219. while (($sFile = readdir($hDir)) !== false)
  1220. {
  1221. $aMatches = array();
  1222. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  1223. {
  1224. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  1225. $aResult = array_merge($aResult, self::FindIconsOnDisk($sBaseDir, $sDirSubPath));
  1226. }
  1227. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  1228. {
  1229. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  1230. }
  1231. }
  1232. closedir($hDir);
  1233. }
  1234. return $aResult;
  1235. }
  1236. public function ValueFromDOMNode($oDOMNode)
  1237. {
  1238. return $oDOMNode->textContent;
  1239. }
  1240. public function ValueToDOMNode($oDOMNode, $value)
  1241. {
  1242. $oTextNode = $oDOMNode->ownerDocument->createTextNode($value);
  1243. $oDOMNode->appendChild($oTextNode);
  1244. }
  1245. public function MakeFileUrl($value)
  1246. {
  1247. return utils::GetAbsoluteUrlModulesRoot().$value;
  1248. }
  1249. public function GetDefaultValue($sClass = 'Contact')
  1250. {
  1251. $sIconPath = MetaModel::GetClassIcon($sClass, false);
  1252. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIconPath);
  1253. return $sIcon;
  1254. }
  1255. }
  1256. class DesignerSortableField extends DesignerFormField
  1257. {
  1258. protected $aAllowedValues;
  1259. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1260. {
  1261. parent::__construct($sCode, $sLabel, $defaultValue);
  1262. $this->aAllowedValues = array();
  1263. }
  1264. public function SetAllowedValues($aAllowedValues)
  1265. {
  1266. $this->aAllowedValues = $aAllowedValues;
  1267. }
  1268. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1269. {
  1270. $bOpen = false;
  1271. $sId = $this->oForm->GetFieldId($this->sCode);
  1272. $sName = $this->oForm->GetFieldName($this->sCode);
  1273. $sReadOnly = $this->IsReadOnly() ? 'readonly="readonly"' : '';
  1274. $aResult = array('label' => $this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" $sReadOnly value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1275. $sJSFields = json_encode(array_keys($this->aAllowedValues));
  1276. $oP->add_ready_script(
  1277. "$('#$sId').sortable_field({aAvailableFields: $sJSFields});"
  1278. );
  1279. return $aResult;
  1280. }
  1281. }
  1282. class DesignerFormSelectorField extends DesignerFormField
  1283. {
  1284. protected $aSubForms;
  1285. protected $defaultRealValue; // What's stored as default value is actually the index
  1286. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1287. {
  1288. parent::__construct($sCode, $sLabel, 0);
  1289. $this->defaultRealValue = $defaultValue;
  1290. $this->aSubForms = array();
  1291. }
  1292. public function GetWidgetClass()
  1293. {
  1294. return 'selector_property_field';
  1295. }
  1296. public function AddSubForm($oSubForm, $sLabel, $sValue)
  1297. {
  1298. $idx = count($this->aSubForms);
  1299. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  1300. if ($sValue == $this->defaultRealValue)
  1301. {
  1302. // Store the index of the selected/default form
  1303. $this->defaultValue = count($this->aSubForms) - 1;
  1304. }
  1305. }
  1306. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1307. {
  1308. $sId = $this->oForm->GetFieldId($this->sCode);
  1309. $sName = $this->oForm->GetFieldName($this->sCode);
  1310. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  1311. $this->aCSSClasses[] = 'formSelector';
  1312. $sCSSClasses = '';
  1313. if (count($this->aCSSClasses) > 0)
  1314. {
  1315. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1316. }
  1317. if ($this->IsReadOnly())
  1318. {
  1319. $aSelected = array();
  1320. $aHiddenValues = array();
  1321. $sDisplayValue = '';
  1322. $sHiddenValue = '';
  1323. foreach($this->aSubForms as $iKey => $aFormData)
  1324. {
  1325. if ($iKey == $this->defaultValue) // Default value is actually the index
  1326. {
  1327. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');
  1328. $sHiddenValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($iKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1329. break;
  1330. }
  1331. }
  1332. $sHtml = "<span $sCSSClasses>".$sDisplayValue.$sHiddenValue."</span>";
  1333. }
  1334. else
  1335. {
  1336. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  1337. foreach($this->aSubForms as $iKey => $aFormData)
  1338. {
  1339. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');;
  1340. $sSelected = ($iKey == $this->defaultValue) ? 'selected' : '';
  1341. $sHtml .= "<option value=\"$iKey\" $sSelected>".$sDisplayValue."</option>";
  1342. }
  1343. $sHtml .= "</select>";
  1344. }
  1345. if ($sRenderMode == 'property')
  1346. {
  1347. $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>';
  1348. }
  1349. foreach($this->aSubForms as $sKey => $aFormData)
  1350. {
  1351. $sId = $this->oForm->GetFieldId($this->sCode);
  1352. $sStyle = (($sKey == $this->defaultValue) && $this->oForm->IsDisplayed()) ? '' : 'style="display:none"';
  1353. $oSubForm = $aFormData['form'];
  1354. $oSubForm->SetParentForm($this->oForm);
  1355. $oSubForm->CopySubmitParams($this->oForm);
  1356. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1357. if ($sRenderMode == 'property')
  1358. {
  1359. // Note: Managing the visibility of nested subforms had several implications
  1360. // 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
  1361. // 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).
  1362. // The div hierarchy has been emulated by adding attributes to the tbody tags:
  1363. // - data-selector : uniquely identifies the DesignerFormSelectorField that has an impact on the visibility of the node
  1364. // - data-path : uniquely identifies the combination of users choices that must be made to show the node
  1365. // - 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)
  1366. // Then a series of actions are performed to hide and show the relevant nodes, depending on the user choice
  1367. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1368. $oSubForm->SetHierarchyParent($sSelector);
  1369. $sPath = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix().'-'.$sKey;
  1370. $oSubForm->SetHierarchyPath($sPath);
  1371. $oSubForm->SetDisplayed($sKey == $this->defaultValue);
  1372. $sState = ($sKey == $this->defaultValue) ? 'visible' : 'hidden';
  1373. //$sHtml .= "</tbody><tbody data-selector=\"$sSelector\" data-path=\"$sPath\" data-state=\"$sState\" $sStyle>";
  1374. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  1375. $sState = $this->oForm->IsDisplayed() ? 'visible' : 'hidden';
  1376. $sParentStyle = '';
  1377. if ($oParent = $this->oForm->GetParentForm())
  1378. {
  1379. $sParentStyle = ($oParent->IsDisplayed()) ? '' : 'style="display:none"';
  1380. $sParentSelector = $oParent->GetHierarchyParent();
  1381. $sParentPath = $oParent->GetHierarchyPath();
  1382. }
  1383. else
  1384. {
  1385. $sParentSelector = '';
  1386. $sParentPath = '';
  1387. }
  1388. //$sHtml .= "</tbody><tbody data-selector=\"$sParentSelector\" data-path=\"$sParentPath\" data-state=\"$sState\" $sParentStyle>";
  1389. }
  1390. else
  1391. {
  1392. $sHtml .= "<div class=\"subform_{$sId} {$sId}_{$sKey}\" $sStyle>";
  1393. $sHtml .= $oSubForm->Render($oP, true);
  1394. $sHtml .= "</div>";
  1395. }
  1396. }
  1397. if ($sRenderMode == 'property')
  1398. {
  1399. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1400. $this->aWidgetExtraParams['data_selector'] = $sSelector;
  1401. }
  1402. else
  1403. {
  1404. $oP->add_ready_script(
  1405. <<<EOF
  1406. $('#$sId').bind('change reverted', function() { $('.subform_{$sId}').hide(); $('.{$sId}_'+this.value).show(); } );
  1407. EOF
  1408. );
  1409. }
  1410. return array('label' => $this->sLabel, 'value' => $sHtml);
  1411. }
  1412. public function ReadParam(&$aValues)
  1413. {
  1414. parent::ReadParam($aValues);
  1415. $sKey = $aValues[$this->sCode];
  1416. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  1417. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1418. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  1419. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  1420. }
  1421. public function SetDefaultValueFrom($aAllDefaultValues)
  1422. {
  1423. if (array_key_exists($this->GetCode(), $aAllDefaultValues))
  1424. {
  1425. $selectedValue = $aAllDefaultValues[$this->GetCode()];
  1426. foreach($this->aSubForms as $iKey => $aFormData)
  1427. {
  1428. $sId = $this->oForm->GetFieldId($this->sCode);
  1429. if ($selectedValue == $aFormData['value'])
  1430. {
  1431. $this->defaultValue =$iKey;
  1432. $aDefaultValues = $this->oForm->GetDefaultValues();
  1433. $oSubForm = $aFormData['form'];
  1434. $oSubForm->SetDefaultValues($aAllDefaultValues);
  1435. }
  1436. }
  1437. }
  1438. }
  1439. public function FindField($sFieldCode)
  1440. {
  1441. $oField = parent::FindField($sFieldCode);
  1442. if ($oField === false)
  1443. {
  1444. // Look in the subforms
  1445. foreach($this->aSubForms as $sKey => $aFormData)
  1446. {
  1447. $oSubForm = $aFormData['form'];
  1448. $oField = $oSubForm->FindField($sFieldCode);
  1449. if ($oField !== false)
  1450. {
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. return $oField;
  1456. }
  1457. }
  1458. class DesignerSubFormField extends DesignerFormField
  1459. {
  1460. protected $oSubForm;
  1461. public function __construct($sLabel, $oSubForm)
  1462. {
  1463. parent::__construct('', $sLabel, '');
  1464. $this->oSubForm = $oSubForm;
  1465. }
  1466. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1467. {
  1468. $this->oSubForm->SetParentForm($this->oForm);
  1469. $this->oSubForm->CopySubmitParams($this->oForm);
  1470. if ($sRenderMode == 'property')
  1471. {
  1472. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  1473. }
  1474. else
  1475. {
  1476. $sHtml = $this->oSubForm->Render($oP, true);
  1477. }
  1478. return array('label' => $this->sLabel, 'value' => $sHtml);
  1479. }
  1480. public function ReadParam(&$aValues)
  1481. {
  1482. $this->oSubForm->SetParentForm($this->oForm);
  1483. $this->oSubForm->ReadParams($aValues);
  1484. }
  1485. public function FindField($sFieldCode)
  1486. {
  1487. $oField = parent::FindField($sFieldCode);
  1488. if ($oField === false)
  1489. {
  1490. // Look in the subform
  1491. $oField = $this->oSubForm->FindField($sFieldCode);
  1492. }
  1493. return $oField;
  1494. }
  1495. }
  1496. class DesignerStaticTextField extends DesignerFormField
  1497. {
  1498. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1499. {
  1500. parent::__construct($sCode, $sLabel, $defaultValue);
  1501. }
  1502. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1503. {
  1504. return array('label' => $this->sLabel, 'value' => $this->defaultValue);
  1505. }
  1506. }