forms.class.inc.php 37 KB

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