forms.class.inc.php 41 KB

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