forms.class.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. public function __construct()
  39. {
  40. $this->aFieldSets = array();
  41. $this->sCurrentFieldSet = '';
  42. $this->sScript = '';
  43. $this->sReadyScript = '';
  44. $this->sFormPrefix = '';
  45. $this->sParamsContainer = '';
  46. $this->sFormId = 'form_'.rand();
  47. $this->oParentForm = null;
  48. $this->bReadOnly = false;
  49. $this->StartFieldSet($this->sCurrentFieldSet);
  50. }
  51. public function AddField(DesignerFormField $oField)
  52. {
  53. if (!is_array($this->aFieldSets[$this->sCurrentFieldSet]))
  54. {
  55. $this->aFieldSets[$this->sCurrentFieldSet] = array();
  56. }
  57. $this->aFieldSets[$this->sCurrentFieldSet][] = $oField;
  58. $oField->SetForm($this);
  59. }
  60. public function StartFieldSet($sLabel)
  61. {
  62. $this->sCurrentFieldSet = $sLabel;
  63. if (!array_key_exists($this->sCurrentFieldSet, $this->aFieldSets))
  64. {
  65. $this->aFieldSets[$this->sCurrentFieldSet] = array();
  66. }
  67. }
  68. public function Render($oP, $bReturnHTML = false)
  69. {
  70. $sReturn = '';
  71. if ($this->oParentForm == null)
  72. {
  73. $sFormId = $this->sFormId;
  74. $sReturn = '<form id="'.$sFormId.'">';
  75. }
  76. else
  77. {
  78. $sFormId = $this->oParentForm->sFormId;
  79. }
  80. $sHiddenFields = '';
  81. foreach($this->aFieldSets as $sLabel => $aFields)
  82. {
  83. $aDetails = array();
  84. if ($sLabel != '')
  85. {
  86. $sReturn .= '<fieldset>';
  87. $sReturn .= '<legend>'.$sLabel.'</legend>';
  88. }
  89. foreach($aFields as $oField)
  90. {
  91. $aRow = $oField->Render($oP, $sFormId);
  92. if ($oField->IsVisible())
  93. {
  94. $sValidation = '&nbsp;<span class="prop_apply">'.$this->GetValidationArea($oField->GetCode()).'</span>';
  95. $sField = $aRow['value'].$sValidation;
  96. $aDetails[] = array('label' => $aRow['label'], 'value' => $sField);
  97. }
  98. else
  99. {
  100. $sHiddenFields .= $aRow['value'];
  101. }
  102. }
  103. $sReturn .= $oP->GetDetails($aDetails);
  104. if ($sLabel != '')
  105. {
  106. $sReturn .= '</fieldset>';
  107. }
  108. }
  109. $sReturn .= $sHiddenFields;
  110. if ($this->oParentForm == null)
  111. {
  112. $sReturn .= '</form>';
  113. }
  114. if($this->sScript != '')
  115. {
  116. $oP->add_script($this->sScript);
  117. }
  118. if($this->sReadyScript != '')
  119. {
  120. $oP->add_ready_script($this->sReadyScript);
  121. }
  122. if ($bReturnHTML)
  123. {
  124. return $sReturn;
  125. }
  126. else
  127. {
  128. $oP->add($sReturn);
  129. }
  130. }
  131. public function SetSubmitParams($sSubmitToUrl, $aSubmitParams)
  132. {
  133. $this->sSubmitTo = $sSubmitToUrl;
  134. $this->aSubmitParams = $aSubmitParams;
  135. }
  136. public function CopySubmitParams($oParentForm)
  137. {
  138. $this->sSubmitTo = $oParentForm->sSubmitTo;
  139. $this->aSubmitParams = $oParentForm->aSubmitParams;
  140. }
  141. public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
  142. {
  143. $sReturn = '';
  144. $sActionUrl = addslashes($this->sSubmitTo);
  145. $sJSSubmitParams = json_encode($this->aSubmitParams);
  146. if ($this->oParentForm == null)
  147. {
  148. $sFormId = $this->sFormId;
  149. $sReturn = '<form id="'.$sFormId.'" onsubmit="return false;">';
  150. $sReturn .= '<table class="prop_table">';
  151. $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>';
  152. }
  153. else
  154. {
  155. $sFormId = $this->oParentForm->sFormId;
  156. }
  157. $sHiddenFields = '';
  158. foreach($this->aFieldSets as $sLabel => $aFields)
  159. {
  160. $aDetails = array();
  161. if ($sLabel != '')
  162. {
  163. $sReturn .= '<tr><th colspan="4">'.$sLabel.'</th></tr>';
  164. }
  165. foreach($aFields as $oField)
  166. {
  167. $aRow = $oField->Render($oP, $sFormId, 'property');
  168. if ($oField->IsVisible())
  169. {
  170. $sFieldId = $this->GetFieldId($oField->GetCode());
  171. $sValidation = $this->GetValidationArea($oField->GetCode(), '<span title="Apply" class="ui-icon ui-icon-circle-check"/>');
  172. $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>';
  173. $sReturn .= '<tr id="row_'.$sFieldId.'"><td class="prop_label">'.$aRow['label'].'</td><td class="prop_value">'.$aRow['value'];
  174. if (!($oField instanceof DesignerFormSelectorField))
  175. {
  176. $sReturn .= $sValidationFields;
  177. }
  178. $sNotifyParentSelectorJS = is_null($sNotifyParentSelector) ? 'null' : "'".addslashes($sNotifyParentSelector)."'";
  179. $sAutoApply = $oField->IsAutoApply() ? 'true' : 'false';
  180. $this->AddReadyScript(
  181. <<<EOF
  182. $('#row_$sFieldId').property_field({parent_selector: $sNotifyParentSelectorJS, field_id: '$sFieldId', auto_apply: $sAutoApply, value: '', submit_to: '$sActionUrl', submit_parameters: $sJSSubmitParams });
  183. EOF
  184. );
  185. }
  186. else
  187. {
  188. $sHiddenFields .= $aRow['value'];
  189. }
  190. }
  191. }
  192. if ($this->oParentForm == null)
  193. {
  194. $sFormId = $this->sFormId;
  195. $sReturn .= '</tbody>';
  196. $sReturn .= '</table>';
  197. $sReturn .= $sHiddenFields;
  198. $sReturn .= '</form>';
  199. $sReturn .= '<div id="prop_submit_result"/>'; // for the return of the submit operation
  200. }
  201. else
  202. {
  203. $sReturn .= $sHiddenFields;
  204. }
  205. $this->AddReadyScript(
  206. <<<EOF
  207. $('.prop_table').tableHover();
  208. var idx = 0;
  209. $('.prop_table tbody tr').each(function() {
  210. if ((idx % 2) == 0)
  211. {
  212. $(this).addClass('even');
  213. }
  214. else
  215. {
  216. $(this).addClass('odd');
  217. }
  218. idx++;
  219. });
  220. EOF
  221. );
  222. if($this->sScript != '')
  223. {
  224. $oP->add_script($this->sScript);
  225. }
  226. if($this->sReadyScript != '')
  227. {
  228. $oP->add_ready_script($this->sReadyScript);
  229. }
  230. if ($bReturnHTML)
  231. {
  232. return $sReturn;
  233. }
  234. else
  235. {
  236. $oP->add($sReturn);
  237. }
  238. }
  239. public function RenderAsDialog($oPage, $sDialogId, $sDialogTitle, $iDialogWidth, $sOkButtonLabel)
  240. {
  241. $sDialogTitle = addslashes($sDialogTitle);
  242. $sOkButtonLabel = addslashes($sOkButtonLabel);
  243. $sCancelButtonLabel = 'Cancel'; //TODO: localize
  244. $oPage->add("<div id=\"$sDialogId\">");
  245. $this->Render($oPage);
  246. $oPage->add('</div>');
  247. $oPage->add_ready_script(
  248. <<<EOF
  249. $('#$sDialogId').dialog({
  250. height: 'auto',
  251. width: 500,
  252. modal: true,
  253. title: '$sDialogTitle',
  254. buttons: [
  255. { text: "$sOkButtonLabel", click: function() {
  256. var oForm = $(this).parents('.ui-dialog :first').find('form');
  257. oForm.submit();
  258. } },
  259. { text: "$sCancelButtonLabel", click: function() { KillAllMenus(); $(this).dialog( "close" ); $(this).remove(); } },
  260. ],
  261. close: function() { KillAllMenus(); $(this).remove(); }
  262. });
  263. var oForm = $('#$sDialogId form');
  264. var sFormId = oForm.attr('id');
  265. ValidateForm(sFormId, true);
  266. EOF
  267. );
  268. }
  269. public function ReadParams(&$aValues = array())
  270. {
  271. foreach($this->aFieldSets as $sLabel => $aFields)
  272. {
  273. foreach($aFields as $oField)
  274. {
  275. $oField->ReadParam($aValues);
  276. }
  277. }
  278. return $aValues;
  279. }
  280. public function SetPrefix($sPrefix)
  281. {
  282. $this->sFormPrefix = $sPrefix;
  283. }
  284. public function GetPrefix()
  285. {
  286. return $this->sFormPrefix;
  287. }
  288. public function SetReadOnly($bReadOnly = true)
  289. {
  290. $this->bReadOnly = $bReadOnly;
  291. }
  292. public function IsReadOnly()
  293. {
  294. if ($this->oParentForm == null)
  295. {
  296. return $this->bReadOnly;
  297. }
  298. else
  299. {
  300. return $this->oParentForm->IsReadOnly();
  301. }
  302. }
  303. public function SetParamsContainer($sParamsContainer)
  304. {
  305. $this->sParamsContainer = $sParamsContainer;
  306. }
  307. public function GetParamsContainer()
  308. {
  309. if ($this->oParentForm == null)
  310. {
  311. return $this->sParamsContainer;
  312. }
  313. else
  314. {
  315. return $this->oParentForm->GetParamsContainer();
  316. }
  317. }
  318. public function SetParentForm($oParentForm)
  319. {
  320. $this->oParentForm = $oParentForm;
  321. }
  322. public function AddScript($sScript)
  323. {
  324. $this->sScript .= $sScript;
  325. }
  326. public function AddReadyScript($sScript)
  327. {
  328. $this->sReadyScript .= $sScript;
  329. }
  330. public function GetFieldId($sCode)
  331. {
  332. return $this->sFormPrefix.'attr_'.$sCode;
  333. }
  334. public function GetFieldName($sCode)
  335. {
  336. return 'attr_'.$sCode;
  337. }
  338. public function GetParamName($sCode)
  339. {
  340. return 'attr_'.$sCode;
  341. }
  342. public function GetValidationArea($sCode, $sContent = '')
  343. {
  344. return "<span style=\"display:inline-block;width:20px;\" id=\"v_{$this->sFormPrefix}attr_$sCode\"><span class=\"ui-icon ui-icon-alert\"></span>$sContent</span>";
  345. }
  346. public function GetAsyncActionClass()
  347. {
  348. return $this->sAsyncActionClass;
  349. }
  350. }
  351. class DesignerTabularForm extends DesignerForm
  352. {
  353. protected $aTable;
  354. public function __construct()
  355. {
  356. parent::__construct();
  357. $this->aTable = array();
  358. }
  359. public function AddRow($aRow)
  360. {
  361. $this->aTable[] = $aRow;
  362. }
  363. public function Render($oP, $bReturnHTML = false)
  364. {
  365. $sReturn = '';
  366. if ($this->oParentForm == null)
  367. {
  368. $sFormId = $this->sFormId;
  369. $sReturn = '<form id="'.$sFormId.'">';
  370. }
  371. else
  372. {
  373. $sFormId = $this->oParentForm->sFormId;
  374. }
  375. $sHiddenFields = '';
  376. $sReturn .= '<table style="width:100%">';
  377. foreach($this->aTable as $aRow)
  378. {
  379. $sReturn .= '<tr>';
  380. foreach($aRow as $field)
  381. {
  382. if (!is_object($field))
  383. {
  384. // Shortcut: pass a string for a cell containing just a label
  385. $sReturn .= '<td>'.$field.'</td>';
  386. }
  387. else
  388. {
  389. $field->SetForm($this);
  390. $aFieldData = $field->Render($oP, $sFormId);
  391. if ($field->IsVisible())
  392. {
  393. // put the label and value separated by a non-breaking space if needed
  394. $aData = array();
  395. foreach(array('label', 'value') as $sCode )
  396. {
  397. if ($aFieldData[$sCode] != '')
  398. {
  399. $aData[] = $aFieldData[$sCode];
  400. }
  401. }
  402. $sReturn .= '<td>'.implode('&nbsp;', $aData).'</td>';
  403. }
  404. else
  405. {
  406. $sHiddenFields .= $aRow['value'];
  407. }
  408. }
  409. }
  410. $sReturn .= '</tr>';
  411. }
  412. $sReturn .= '</table>';
  413. $sReturn .= $sHiddenFields;
  414. if($this->sScript != '')
  415. {
  416. $oP->add_script($this->sScript);
  417. }
  418. if($this->sReadyScript != '')
  419. {
  420. $oP->add_ready_script($this->sReadyScript);
  421. }
  422. if ($bReturnHTML)
  423. {
  424. return $sReturn;
  425. }
  426. else
  427. {
  428. $oP->add($sReturn);
  429. }
  430. }
  431. public function ReadParams(&$aValues = array())
  432. {
  433. foreach($this->aTable as $aRow)
  434. {
  435. foreach($aRow as $field)
  436. {
  437. if (is_object($field))
  438. {
  439. $field->SetForm($this);
  440. $field->ReadParam($aValues);
  441. }
  442. }
  443. }
  444. return $aValues;
  445. }
  446. }
  447. class DesignerFormField
  448. {
  449. protected $sLabel;
  450. protected $sCode;
  451. protected $defaultValue;
  452. protected $oForm;
  453. protected $bMandatory;
  454. protected $bReadOnly;
  455. protected $bAutoApply;
  456. public function __construct($sCode, $sLabel, $defaultValue)
  457. {
  458. $this->sLabel = $sLabel;
  459. $this->sCode = $sCode;
  460. $this->defaultValue = $defaultValue;
  461. $this->bMandatory = false;
  462. $this->bReadOnly = false;
  463. $this->bAutoApply = false;
  464. }
  465. public function GetCode()
  466. {
  467. return $this->sCode;
  468. }
  469. public function SetForm($oForm)
  470. {
  471. $this->oForm = $oForm;
  472. }
  473. public function SetMandatory($bMandatory = true)
  474. {
  475. $this->bMandatory = $bMandatory;
  476. }
  477. public function SetReadOnly($bReadOnly = true)
  478. {
  479. $this->bReadOnly = $bReadOnly;
  480. }
  481. public function IsReadOnly()
  482. {
  483. return ($this->oForm->IsReadOnly() || $this->bReadOnly);
  484. }
  485. public function SetAutoApply($bAutoApply)
  486. {
  487. $this->bAutoApply = $bAutoApply;
  488. }
  489. public function IsAutoApply()
  490. {
  491. return $this->bAutoApply;
  492. }
  493. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  494. {
  495. $sId = $this->oForm->GetFieldId($this->sCode);
  496. $sName = $this->oForm->GetFieldName($this->sCode);
  497. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  498. }
  499. public function ReadParam(&$aValues)
  500. {
  501. if ($this->IsReadOnly())
  502. {
  503. $aValues[$this->sCode] = $this->defaultValue;
  504. }
  505. else
  506. {
  507. if ($this->oForm->GetParamsContainer() != '')
  508. {
  509. $aParams = utils::ReadParam($this->oForm->GetParamsContainer(), array(), false, 'raw_data');
  510. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  511. {
  512. $aValues[$this->sCode] = $aParams[$this->oForm->GetParamName($this->sCode)];
  513. }
  514. else
  515. {
  516. $aValues[$this->sCode] = $this->defaultValue;
  517. }
  518. }
  519. else
  520. {
  521. $aValues[$this->sCode] = utils::ReadParam($this->oForm->GetParamName($this->sCode), $this->defaultValue, false, 'raw_data');
  522. }
  523. }
  524. }
  525. public function IsVisible()
  526. {
  527. return true;
  528. }
  529. }
  530. class DesignerLabelField extends DesignerFormField
  531. {
  532. protected $sDescription;
  533. public function __construct($sLabel, $sDescription)
  534. {
  535. parent::__construct('', $sLabel, '');
  536. $this->sDescription = $sDescription;
  537. }
  538. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  539. {
  540. $sId = $this->oForm->GetFieldId($this->sCode);
  541. $sName = $this->oForm->GetFieldName($this->sCode);
  542. return array('label' => $this->sLabel, 'value' => $sDescription);
  543. }
  544. public function ReadParam(&$aValues)
  545. {
  546. }
  547. public function IsVisible()
  548. {
  549. return true;
  550. }
  551. }
  552. class DesignerTextField extends DesignerFormField
  553. {
  554. protected $sValidationPattern;
  555. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  556. {
  557. parent::__construct($sCode, $sLabel, $defaultValue);
  558. $this->sValidationPattern = '';
  559. }
  560. public function SetValidationPattern($sValidationPattern)
  561. {
  562. $this->sValidationPattern = $sValidationPattern;
  563. }
  564. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  565. {
  566. $sId = $this->oForm->GetFieldId($this->sCode);
  567. $sName = $this->oForm->GetFieldName($this->sCode);
  568. $sPattern = addslashes($this->sValidationPattern);
  569. $sMandatory = $this->bMandatory ? 'true' : 'false';
  570. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  571. $oP->add_ready_script(
  572. <<<EOF
  573. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId'); } );
  574. {
  575. var myTimer = null;
  576. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  577. }
  578. EOF
  579. );
  580. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" $sReadOnly name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  581. }
  582. public function ReadParam(&$aValues)
  583. {
  584. parent::ReadParam($aValues);
  585. if (($this->sValidationPattern != '') &&(!preg_match('/'.$this->sValidationPattern.'/', $aValues[$this->sCode])) )
  586. {
  587. $aValues[$this->sCode] = $this->defaultValue;
  588. }
  589. }
  590. }
  591. class DesignerLongTextField extends DesignerTextField
  592. {
  593. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  594. {
  595. $sId = $this->oForm->GetFieldId($this->sCode);
  596. $sName = $this->oForm->GetFieldName($this->sCode);
  597. $sPattern = addslashes($this->sValidationPattern);
  598. $sMandatory = $this->bMandatory ? 'true' : 'false';
  599. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  600. $oP->add_ready_script(
  601. <<<EOF
  602. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId'); } );
  603. {
  604. var myTimer = null;
  605. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  606. }
  607. EOF
  608. );
  609. return array('label' => $this->sLabel, 'value' => "<textarea id=\"$sId\" $sReadOnly name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>");
  610. }
  611. }
  612. class DesignerComboField extends DesignerFormField
  613. {
  614. protected $aAllowedValues;
  615. protected $bMultipleSelection;
  616. protected $bOtherChoices;
  617. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  618. {
  619. parent::__construct($sCode, $sLabel, $defaultValue);
  620. $this->aAllowedValues = array();
  621. $this->bMultipleSelection = false;
  622. $this->bOtherChoices = false;
  623. $this->bAutoApply = true;
  624. }
  625. public function SetAllowedValues($aAllowedValues)
  626. {
  627. $this->aAllowedValues = $aAllowedValues;
  628. }
  629. public function MultipleSelection($bMultipleSelection = true)
  630. {
  631. $this->bMultipleSelection = $bMultipleSelection;
  632. }
  633. public function OtherChoices($bOtherChoices = true)
  634. {
  635. $this->bOtherChoices = $bOtherChoices;
  636. }
  637. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  638. {
  639. $sId = $this->oForm->GetFieldId($this->sCode);
  640. $sName = $this->oForm->GetFieldName($this->sCode);
  641. $sChecked = $this->defaultValue ? 'checked' : '';
  642. $sMandatory = $this->bMandatory ? 'true' : 'false';
  643. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  644. if ($this->bMultipleSelection)
  645. {
  646. $sHtml = "<select multiple size=\"8\"id=\"$sId\" name=\"$sName\" $sReadOnly>";
  647. }
  648. else
  649. {
  650. $sHtml = "<select id=\"$sId\" name=\"$sName\" $sReadOnly>";
  651. $sHtml .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>";
  652. }
  653. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  654. {
  655. if ($this->bMultipleSelection)
  656. {
  657. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  658. }
  659. else
  660. {
  661. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  662. }
  663. // Quick and dirty: display the menu parents as a tree
  664. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  665. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  666. }
  667. $sHtml .= "</select>";
  668. if ($this->bOtherChoices)
  669. {
  670. $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"/>';
  671. }
  672. $oP->add_ready_script(
  673. <<<EOF
  674. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId'); } );
  675. EOF
  676. );
  677. return array('label' => $this->sLabel, 'value' => $sHtml);
  678. }
  679. public function ReadParam(&$aValues)
  680. {
  681. parent::ReadParam($aValues);
  682. if ($aValues[$this->sCode] == 'null')
  683. {
  684. $aValues[$this->sCode] = array();
  685. }
  686. }
  687. }
  688. class DesignerBooleanField extends DesignerFormField
  689. {
  690. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  691. {
  692. parent::__construct($sCode, $sLabel, $defaultValue);
  693. $this->bAutoApply = true;
  694. }
  695. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  696. {
  697. $sId = $this->oForm->GetFieldId($this->sCode);
  698. $sName = $this->oForm->GetFieldName($this->sCode);
  699. $sChecked = $this->defaultValue ? 'checked' : '';
  700. $sReadOnly = $this->IsReadOnly() ? 'disabled' : ''; // readonly does not work as expected on checkboxes:
  701. // readonly prevents the user from changing the input's value not its state (checked/unchecked)
  702. return array('label' => $this->sLabel, 'value' => "<input type=\"checkbox\" $sChecked $sReadOnly id=\"$sId\" name=\"$sName\" value=\"true\">");
  703. }
  704. public function ReadParam(&$aValues)
  705. {
  706. if ($this->IsReadOnly())
  707. {
  708. $aValues[$this->sCode] = $this->defaultValue;
  709. }
  710. else
  711. {
  712. $sParamsContainer = $this->oForm->GetParamsContainer();
  713. if ($sParamsContainer != '')
  714. {
  715. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  716. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  717. {
  718. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  719. }
  720. else
  721. {
  722. $sValue = 'false';
  723. }
  724. }
  725. else
  726. {
  727. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  728. }
  729. }
  730. $aValues[$this->sCode] = ($sValue == 'true');
  731. }
  732. }
  733. class DesignerHiddenField extends DesignerFormField
  734. {
  735. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  736. {
  737. parent::__construct($sCode, $sLabel, $defaultValue);
  738. }
  739. public function IsVisible()
  740. {
  741. return false;
  742. }
  743. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  744. {
  745. $sId = $this->oForm->GetFieldId($this->sCode);
  746. $sName = $this->oForm->GetFieldName($this->sCode);
  747. $sChecked = $this->defaultValue ? 'checked' : '';
  748. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  749. }
  750. }
  751. class DesignerIconSelectionField extends DesignerFormField
  752. {
  753. protected $aAllowedValues;
  754. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  755. {
  756. parent::__construct($sCode, $sLabel, $defaultValue);
  757. $this->bAutoApply = true;
  758. }
  759. public function SetAllowedValues($aAllowedValues)
  760. {
  761. $this->aAllowedValues = $aAllowedValues;
  762. }
  763. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  764. {
  765. $sId = $this->oForm->GetFieldId($this->sCode);
  766. $sName = $this->oForm->GetFieldName($this->sCode);
  767. $idx = 0;
  768. foreach($this->aAllowedValues as $index => $aValue)
  769. {
  770. if ($aValue['value'] == $this->defaultValue)
  771. {
  772. $idx = $index;
  773. break;
  774. }
  775. }
  776. $sJSItems = json_encode($this->aAllowedValues);
  777. if (!$this->IsReadOnly())
  778. {
  779. $oP->add_ready_script(
  780. <<<EOF
  781. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems});
  782. EOF
  783. );
  784. }
  785. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  786. return array('label' =>$this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>");
  787. }
  788. }
  789. class DesignerSortableField extends DesignerFormField
  790. {
  791. protected $aAllowedValues;
  792. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  793. {
  794. parent::__construct($sCode, $sLabel, $defaultValue);
  795. $this->aAllowedValues = array();
  796. }
  797. public function SetAllowedValues($aAllowedValues)
  798. {
  799. $this->aAllowedValues = $aAllowedValues;
  800. }
  801. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  802. {
  803. $bOpen = false;
  804. $sId = $this->oForm->GetFieldId($this->sCode);
  805. $sName = $this->oForm->GetFieldName($this->sCode);
  806. $sHtml = "<span class=\"sort_$sId fieldslist\" id=\"sortable_$sId\">";
  807. foreach($this->defaultValue as $sValue)
  808. {
  809. $sHtml .= "<span class=\"movable_attr\">$sValue</span>";
  810. }
  811. $sHtml .="</span>";
  812. $sIconClass = $bOpen ? 'ui-icon-circle-triangle-s' : 'ui-icon-circle-triangle-e';
  813. $sStyle = $bOpen ? '' : 'style="display:none"';
  814. $sHtml .= "<div class=\"fieldspicker\"><table><tr><td><span id=\"collapse_$sId\" class=\"ui-icon $sIconClass\" style=\"opacity: 0.5\"></span>Fields</td></tr><tr><td><div $sStyle id=\"fieldsbasket_$sId\" class=\"sort_$sId fieldsbasket\">";
  815. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  816. {
  817. $sHtml .= "<span class=\"movable_attr\">$sDisplayValue</span>";
  818. }
  819. $sHtml .="</div></td></tr>";
  820. $sHtml .="<tr id=\"trash_icon_$sId\" $sStyle><td><span class=\"ui-icon ui-icon-trash\" style=\"opacity: 0.5\"></span>Trash</td></tr><tr id=\"trash_$sId\" $sStyle><td><div id=\"recycle_$sId\" class=\"sort_$sId fieldstrash\"></div></div></td></tr></table></div>";
  821. $oP->add_ready_script(
  822. <<<EOF
  823. $('#collapse_$sId').click(function() { $(this).toggleClass('ui-icon-circle-triangle-s').toggleClass('ui-icon-circle-triangle-e'); $('#fieldsbasket_$sId').toggle(); $('#trash_icon_$sId').toggle(); $('#trash_$sId').toggle(); } );
  824. $('#fieldsbasket_$sId .movable_attr').draggable({connectToSortable: '#sortable_$sId', helper: 'clone', revert: false });
  825. $('#recycle_$sId').sortable({ receive: function(event, ui) { ui.item.animate({opacity: 0.25}, { complete: function() { $(this).remove(); } });} });
  826. $('#sortable_$sId').sortable({connectWith: '#recycle_$sId', forcePlaceholderSize: true});
  827. $('#sortable_$sId').disableSelection();
  828. EOF
  829. );
  830. return array('label' => $this->sLabel, 'value' => $sHtml);
  831. }
  832. }
  833. class DesignerFormSelectorField extends DesignerFormField
  834. {
  835. protected $aSubForms;
  836. protected $defaultRealValue; // What's stored as default value is actually the index
  837. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  838. {
  839. parent::__construct($sCode, $sLabel, 0);
  840. $this->defaultRealValue = $defaultValue;
  841. $this->aSubForms = array();
  842. }
  843. public function AddSubForm($oSubForm, $sLabel, $sValue)
  844. {
  845. $idx = count($this->aSubForms);
  846. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  847. if ($sValue == $this->defaultRealValue)
  848. {
  849. // Store the index of the selected/default form
  850. $this->defaultValue = count($this->aSubForms) - 1;
  851. }
  852. }
  853. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  854. {
  855. $sId = $this->oForm->GetFieldId($this->sCode);
  856. $sName = $this->oForm->GetFieldName($this->sCode);
  857. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  858. $sHtml = "<select id=\"$sId\" name=\"$sName\" $sReadOnly>";
  859. foreach($this->aSubForms as $sKey => $aFormData)
  860. {
  861. $sDisplayValue = $aFormData['label'];
  862. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  863. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>".htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8')."</option>";
  864. }
  865. $sHtml .= "</select>";
  866. if ($sRenderMode == 'property')
  867. {
  868. $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>';
  869. }
  870. foreach($this->aSubForms as $sKey => $aFormData)
  871. {
  872. $sId = $this->oForm->GetFieldId($this->sCode);
  873. $sStyle = ($sKey == $this->defaultValue) ? '' : 'style="display:none"';
  874. $oSubForm = $aFormData['form'];
  875. $oSubForm->SetParentForm($this->oForm);
  876. $oSubForm->CopySubmitParams($this->oForm);
  877. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  878. if ($sRenderMode == 'property')
  879. {
  880. $sHtml .= "</tbody><tbody class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  881. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  882. }
  883. else
  884. {
  885. $sHtml .= "<div class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  886. $sHtml .= $oSubForm->Render($oP, true);
  887. $sHtml .= "</div>";
  888. }
  889. }
  890. $oP->add_ready_script(
  891. <<<EOF
  892. $('#$sId').bind('change reverted', function() { $('.subform').hide(); $('#{$sId}_'+this.value).show(); } );
  893. EOF
  894. );
  895. return array('label' => $this->sLabel, 'value' => $sHtml);
  896. }
  897. public function ReadParam(&$aValues)
  898. {
  899. parent::ReadParam($aValues);
  900. $sKey = $aValues[$this->sCode];
  901. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  902. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  903. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  904. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  905. }
  906. }
  907. class DesignerSubFormField extends DesignerFormField
  908. {
  909. protected $oSubForm;
  910. public function __construct($sLabel, $oSubForm)
  911. {
  912. parent::__construct('', $sLabel, '');
  913. $this->oSubForm = $oSubForm;
  914. }
  915. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  916. {
  917. $this->oSubForm->SetParentForm($this->oForm);
  918. $this->oSubForm->CopySubmitParams($this->oForm);
  919. if ($sRenderMode == 'property')
  920. {
  921. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  922. }
  923. else
  924. {
  925. $sHtml = $this->oSubForm->Render($oP, true);
  926. }
  927. return array('label' => $this->sLabel, 'value' => $sHtml);
  928. }
  929. public function ReadParam(&$aValues)
  930. {
  931. $this->oSubForm->SetParentForm($this->oForm);
  932. $this->oSubForm->ReadParams($aValues);
  933. }
  934. }
  935. ?>