forms.class.inc.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 = Dict::S('UI:Button:Cancel');
  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).closest('.ui-dialog').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. protected $aCSSClasses;
  457. public function __construct($sCode, $sLabel, $defaultValue)
  458. {
  459. $this->sLabel = $sLabel;
  460. $this->sCode = $sCode;
  461. $this->defaultValue = $defaultValue;
  462. $this->bMandatory = false;
  463. $this->bReadOnly = false;
  464. $this->bAutoApply = false;
  465. $this->aCSSClasses = array();
  466. }
  467. public function GetCode()
  468. {
  469. return $this->sCode;
  470. }
  471. public function SetForm($oForm)
  472. {
  473. $this->oForm = $oForm;
  474. }
  475. public function SetMandatory($bMandatory = true)
  476. {
  477. $this->bMandatory = $bMandatory;
  478. }
  479. public function SetReadOnly($bReadOnly = true)
  480. {
  481. $this->bReadOnly = $bReadOnly;
  482. }
  483. public function IsReadOnly()
  484. {
  485. return ($this->oForm->IsReadOnly() || $this->bReadOnly);
  486. }
  487. public function SetAutoApply($bAutoApply)
  488. {
  489. $this->bAutoApply = $bAutoApply;
  490. }
  491. public function IsAutoApply()
  492. {
  493. return $this->bAutoApply;
  494. }
  495. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  496. {
  497. $sId = $this->oForm->GetFieldId($this->sCode);
  498. $sName = $this->oForm->GetFieldName($this->sCode);
  499. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  500. }
  501. public function ReadParam(&$aValues)
  502. {
  503. if ($this->IsReadOnly())
  504. {
  505. $aValues[$this->sCode] = $this->defaultValue;
  506. }
  507. else
  508. {
  509. if ($this->oForm->GetParamsContainer() != '')
  510. {
  511. $aParams = utils::ReadParam($this->oForm->GetParamsContainer(), array(), false, 'raw_data');
  512. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  513. {
  514. $aValues[$this->sCode] = $aParams[$this->oForm->GetParamName($this->sCode)];
  515. }
  516. else
  517. {
  518. $aValues[$this->sCode] = $this->defaultValue;
  519. }
  520. }
  521. else
  522. {
  523. $aValues[$this->sCode] = utils::ReadParam($this->oForm->GetParamName($this->sCode), $this->defaultValue, false, 'raw_data');
  524. }
  525. }
  526. }
  527. public function IsVisible()
  528. {
  529. return true;
  530. }
  531. public function AddCSSClass($sCSSClass)
  532. {
  533. $this->aCSSClasses[] = $sCSSClass;
  534. }
  535. }
  536. class DesignerLabelField extends DesignerFormField
  537. {
  538. protected $sDescription;
  539. public function __construct($sLabel, $sDescription)
  540. {
  541. parent::__construct('', $sLabel, '');
  542. $this->sDescription = $sDescription;
  543. }
  544. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  545. {
  546. $sId = $this->oForm->GetFieldId($this->sCode);
  547. $sName = $this->oForm->GetFieldName($this->sCode);
  548. return array('label' => $this->sLabel, 'value' => $sDescription);
  549. }
  550. public function ReadParam(&$aValues)
  551. {
  552. }
  553. public function IsVisible()
  554. {
  555. return true;
  556. }
  557. }
  558. class DesignerTextField extends DesignerFormField
  559. {
  560. protected $sValidationPattern;
  561. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  562. {
  563. parent::__construct($sCode, $sLabel, $defaultValue);
  564. $this->sValidationPattern = '';
  565. }
  566. public function SetValidationPattern($sValidationPattern)
  567. {
  568. $this->sValidationPattern = $sValidationPattern;
  569. }
  570. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  571. {
  572. $sId = $this->oForm->GetFieldId($this->sCode);
  573. $sName = $this->oForm->GetFieldName($this->sCode);
  574. $sPattern = addslashes($this->sValidationPattern);
  575. $sMandatory = $this->bMandatory ? 'true' : 'false';
  576. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  577. $oP->add_ready_script(
  578. <<<EOF
  579. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId'); } );
  580. {
  581. var myTimer = null;
  582. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  583. }
  584. EOF
  585. );
  586. $sCSSClasses = '';
  587. if (count($this->aCSSClasses) > 0)
  588. {
  589. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  590. }
  591. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" $sCSSClasses id=\"$sId\" $sReadOnly name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  592. }
  593. public function ReadParam(&$aValues)
  594. {
  595. parent::ReadParam($aValues);
  596. if (($this->sValidationPattern != '') &&(!preg_match('/'.$this->sValidationPattern.'/', $aValues[$this->sCode])) )
  597. {
  598. $aValues[$this->sCode] = $this->defaultValue;
  599. }
  600. }
  601. }
  602. class DesignerLongTextField extends DesignerTextField
  603. {
  604. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  605. {
  606. $sId = $this->oForm->GetFieldId($this->sCode);
  607. $sName = $this->oForm->GetFieldName($this->sCode);
  608. $sPattern = addslashes($this->sValidationPattern);
  609. $sMandatory = $this->bMandatory ? 'true' : 'false';
  610. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  611. $oP->add_ready_script(
  612. <<<EOF
  613. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId'); } );
  614. {
  615. var myTimer = null;
  616. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  617. }
  618. EOF
  619. );
  620. $sCSSClasses = '';
  621. if (count($this->aCSSClasses) > 0)
  622. {
  623. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  624. }
  625. return array('label' => $this->sLabel, 'value' => "<textarea $sCSSClasses id=\"$sId\" $sReadOnly name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>");
  626. }
  627. }
  628. class DesignerComboField extends DesignerFormField
  629. {
  630. protected $aAllowedValues;
  631. protected $bMultipleSelection;
  632. protected $bOtherChoices;
  633. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  634. {
  635. parent::__construct($sCode, $sLabel, $defaultValue);
  636. $this->aAllowedValues = array();
  637. $this->bMultipleSelection = false;
  638. $this->bOtherChoices = false;
  639. $this->bAutoApply = true;
  640. }
  641. public function SetAllowedValues($aAllowedValues)
  642. {
  643. $this->aAllowedValues = $aAllowedValues;
  644. }
  645. public function MultipleSelection($bMultipleSelection = true)
  646. {
  647. $this->bMultipleSelection = $bMultipleSelection;
  648. }
  649. public function OtherChoices($bOtherChoices = true)
  650. {
  651. $this->bOtherChoices = $bOtherChoices;
  652. }
  653. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  654. {
  655. $sId = $this->oForm->GetFieldId($this->sCode);
  656. $sName = $this->oForm->GetFieldName($this->sCode);
  657. $sChecked = $this->defaultValue ? 'checked' : '';
  658. $sMandatory = $this->bMandatory ? 'true' : 'false';
  659. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  660. $sCSSClasses = '';
  661. if (count($this->aCSSClasses) > 0)
  662. {
  663. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  664. }
  665. if ($this->bMultipleSelection)
  666. {
  667. $sHtml = "<select $sCSSClasses multiple size=\"8\"id=\"$sId\" name=\"$sName\" $sReadOnly>";
  668. }
  669. else
  670. {
  671. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  672. $sHtml .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>";
  673. }
  674. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  675. {
  676. if ($this->bMultipleSelection)
  677. {
  678. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  679. }
  680. else
  681. {
  682. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  683. }
  684. // Quick and dirty: display the menu parents as a tree
  685. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  686. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  687. }
  688. $sHtml .= "</select>";
  689. if ($this->bOtherChoices)
  690. {
  691. $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"/>';
  692. }
  693. $oP->add_ready_script(
  694. <<<EOF
  695. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId'); } );
  696. EOF
  697. );
  698. return array('label' => $this->sLabel, 'value' => $sHtml);
  699. }
  700. public function ReadParam(&$aValues)
  701. {
  702. parent::ReadParam($aValues);
  703. if ($aValues[$this->sCode] == 'null')
  704. {
  705. $aValues[$this->sCode] = array();
  706. }
  707. }
  708. }
  709. class DesignerBooleanField extends DesignerFormField
  710. {
  711. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  712. {
  713. parent::__construct($sCode, $sLabel, $defaultValue);
  714. $this->bAutoApply = true;
  715. }
  716. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  717. {
  718. $sId = $this->oForm->GetFieldId($this->sCode);
  719. $sName = $this->oForm->GetFieldName($this->sCode);
  720. $sChecked = $this->defaultValue ? 'checked' : '';
  721. $sReadOnly = $this->IsReadOnly() ? 'disabled' : ''; // readonly does not work as expected on checkboxes:
  722. // readonly prevents the user from changing the input's value not its state (checked/unchecked)
  723. $sCSSClasses = '';
  724. if (count($this->aCSSClasses) > 0)
  725. {
  726. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  727. }
  728. return array('label' => $this->sLabel, 'value' => "<input $sCSSClasses type=\"checkbox\" $sChecked $sReadOnly id=\"$sId\" name=\"$sName\" value=\"true\">");
  729. }
  730. public function ReadParam(&$aValues)
  731. {
  732. if ($this->IsReadOnly())
  733. {
  734. $aValues[$this->sCode] = $this->defaultValue;
  735. }
  736. else
  737. {
  738. $sParamsContainer = $this->oForm->GetParamsContainer();
  739. if ($sParamsContainer != '')
  740. {
  741. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  742. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  743. {
  744. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  745. }
  746. else
  747. {
  748. $sValue = 'false';
  749. }
  750. }
  751. else
  752. {
  753. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  754. }
  755. }
  756. $aValues[$this->sCode] = ($sValue == 'true');
  757. }
  758. }
  759. class DesignerHiddenField extends DesignerFormField
  760. {
  761. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  762. {
  763. parent::__construct($sCode, $sLabel, $defaultValue);
  764. }
  765. public function IsVisible()
  766. {
  767. return false;
  768. }
  769. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  770. {
  771. $sId = $this->oForm->GetFieldId($this->sCode);
  772. $sName = $this->oForm->GetFieldName($this->sCode);
  773. $sChecked = $this->defaultValue ? 'checked' : '';
  774. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  775. }
  776. }
  777. class DesignerIconSelectionField extends DesignerFormField
  778. {
  779. protected $sUploadUrl;
  780. protected $aAllowedValues;
  781. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  782. {
  783. parent::__construct($sCode, $sLabel, $defaultValue);
  784. $this->bAutoApply = true;
  785. $this->sUploadUrl = null;
  786. }
  787. public function SetAllowedValues($aAllowedValues)
  788. {
  789. $this->aAllowedValues = $aAllowedValues;
  790. }
  791. public function EnableUpload($sIconUploadUrl)
  792. {
  793. $this->sUploadUrl = $sIconUploadUrl;
  794. }
  795. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  796. {
  797. $sId = $this->oForm->GetFieldId($this->sCode);
  798. $sName = $this->oForm->GetFieldName($this->sCode);
  799. $idx = 0;
  800. foreach($this->aAllowedValues as $index => $aValue)
  801. {
  802. if ($aValue['value'] == $this->defaultValue)
  803. {
  804. $idx = $index;
  805. break;
  806. }
  807. }
  808. $sJSItems = json_encode($this->aAllowedValues);
  809. $sPostUploadTo = ($this->sUploadUrl == null) ? 'null' : "'{$this->sUploadUrl}'";
  810. if (!$this->IsReadOnly())
  811. {
  812. $oP->add_ready_script(
  813. <<<EOF
  814. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems, post_upload_to: $sPostUploadTo});
  815. EOF
  816. );
  817. }
  818. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  819. return array('label' =>$this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>");
  820. }
  821. }
  822. class RunTimeIconSelectionField extends DesignerIconSelectionField
  823. {
  824. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  825. {
  826. parent::__construct($sCode, $sLabel, $defaultValue);
  827. $aAllIcons = self::FindIconsOnDisk(APPROOT.'env-'.utils::GetCurrentEnvironment());
  828. ksort($aAllIcons);
  829. $aValues = array();
  830. foreach($aAllIcons as $sFilePath)
  831. {
  832. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  833. }
  834. $this->SetAllowedValues($aValues);
  835. }
  836. static protected function FindIconsOnDisk($sBaseDir, $sDir = '')
  837. {
  838. $aResult = array();
  839. // Populate automatically the list of icon files
  840. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  841. {
  842. while (($sFile = readdir($hDir)) !== false)
  843. {
  844. $aMatches = array();
  845. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  846. {
  847. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  848. $aResult = array_merge($aResult, self::FindIconsOnDisk($sBaseDir, $sDirSubPath));
  849. }
  850. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  851. {
  852. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  853. }
  854. }
  855. closedir($hDir);
  856. }
  857. return $aResult;
  858. }
  859. public function ValueFromDOMNode($oDOMNode)
  860. {
  861. return $oDOMNode->textContent;
  862. }
  863. public function ValueToDOMNode($oDOMNode, $value)
  864. {
  865. $oTextNode = $oDOMNode->ownerDocument->createTextNode($value);
  866. $oDOMNode->appendChild($oTextNode);
  867. }
  868. public function MakeFileUrl($value)
  869. {
  870. return utils::GetAbsoluteUrlModulesRoot().$value;
  871. }
  872. public function GetDefaultValue($sClass = 'Contact')
  873. {
  874. $sIconPath = MetaModel::GetClassIcon($sClass, false);
  875. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIconPath);
  876. return $sIcon;
  877. }
  878. }
  879. class DesignerSortableField extends DesignerFormField
  880. {
  881. protected $aAllowedValues;
  882. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  883. {
  884. parent::__construct($sCode, $sLabel, $defaultValue);
  885. $this->aAllowedValues = array();
  886. }
  887. public function SetAllowedValues($aAllowedValues)
  888. {
  889. $this->aAllowedValues = $aAllowedValues;
  890. }
  891. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  892. {
  893. $bOpen = false;
  894. $sId = $this->oForm->GetFieldId($this->sCode);
  895. $sName = $this->oForm->GetFieldName($this->sCode);
  896. $sCSSClasses = '';
  897. $this->aCSSClasses[] = "sort_$sId fieldslist";
  898. if (count($this->aCSSClasses) > 0)
  899. {
  900. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  901. }
  902. $sHtml = "<span $sCSSClasses id=\"sortable_$sId\">";
  903. foreach($this->defaultValue as $sValue)
  904. {
  905. $sHtml .= "<span class=\"movable_attr\">$sValue</span>";
  906. }
  907. $sHtml .="</span>";
  908. $sIconClass = $bOpen ? 'ui-icon-circle-triangle-s' : 'ui-icon-circle-triangle-e';
  909. $sStyle = $bOpen ? '' : 'style="display:none"';
  910. $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\">";
  911. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  912. {
  913. $sHtml .= "<span class=\"movable_attr\">$sDisplayValue</span>";
  914. }
  915. $sHtml .="</div></td></tr>";
  916. $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>";
  917. $oP->add_ready_script(
  918. <<<EOF
  919. $('#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(); } );
  920. $('#fieldsbasket_$sId .movable_attr').draggable({connectToSortable: '#sortable_$sId', helper: 'clone', revert: false });
  921. $('#recycle_$sId').sortable({ receive: function(event, ui) { ui.item.animate({opacity: 0.25}, { complete: function() { $(this).remove(); } });} });
  922. $('#sortable_$sId').sortable({connectWith: '#recycle_$sId', forcePlaceholderSize: true});
  923. $('#sortable_$sId').disableSelection();
  924. EOF
  925. );
  926. return array('label' => $this->sLabel, 'value' => $sHtml);
  927. }
  928. }
  929. class DesignerFormSelectorField extends DesignerFormField
  930. {
  931. protected $aSubForms;
  932. protected $defaultRealValue; // What's stored as default value is actually the index
  933. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  934. {
  935. parent::__construct($sCode, $sLabel, 0);
  936. $this->defaultRealValue = $defaultValue;
  937. $this->aSubForms = array();
  938. }
  939. public function AddSubForm($oSubForm, $sLabel, $sValue)
  940. {
  941. $idx = count($this->aSubForms);
  942. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  943. if ($sValue == $this->defaultRealValue)
  944. {
  945. // Store the index of the selected/default form
  946. $this->defaultValue = count($this->aSubForms) - 1;
  947. }
  948. }
  949. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  950. {
  951. $sId = $this->oForm->GetFieldId($this->sCode);
  952. $sName = $this->oForm->GetFieldName($this->sCode);
  953. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  954. $sCSSClasses = '';
  955. if (count($this->aCSSClasses) > 0)
  956. {
  957. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  958. }
  959. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  960. foreach($this->aSubForms as $sKey => $aFormData)
  961. {
  962. $sDisplayValue = $aFormData['label'];
  963. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  964. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>".htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8')."</option>";
  965. }
  966. $sHtml .= "</select>";
  967. if ($sRenderMode == 'property')
  968. {
  969. $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>';
  970. }
  971. foreach($this->aSubForms as $sKey => $aFormData)
  972. {
  973. $sId = $this->oForm->GetFieldId($this->sCode);
  974. $sStyle = ($sKey == $this->defaultValue) ? '' : 'style="display:none"';
  975. $oSubForm = $aFormData['form'];
  976. $oSubForm->SetParentForm($this->oForm);
  977. $oSubForm->CopySubmitParams($this->oForm);
  978. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  979. if ($sRenderMode == 'property')
  980. {
  981. $sHtml .= "</tbody><tbody class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  982. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  983. }
  984. else
  985. {
  986. $sHtml .= "<div class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  987. $sHtml .= $oSubForm->Render($oP, true);
  988. $sHtml .= "</div>";
  989. }
  990. }
  991. $oP->add_ready_script(
  992. <<<EOF
  993. $('#$sId').bind('change reverted', function() { $('.subform').hide(); $('#{$sId}_'+this.value).show(); } );
  994. EOF
  995. );
  996. return array('label' => $this->sLabel, 'value' => $sHtml);
  997. }
  998. public function ReadParam(&$aValues)
  999. {
  1000. parent::ReadParam($aValues);
  1001. $sKey = $aValues[$this->sCode];
  1002. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  1003. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1004. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  1005. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  1006. }
  1007. }
  1008. class DesignerSubFormField extends DesignerFormField
  1009. {
  1010. protected $oSubForm;
  1011. public function __construct($sLabel, $oSubForm)
  1012. {
  1013. parent::__construct('', $sLabel, '');
  1014. $this->oSubForm = $oSubForm;
  1015. }
  1016. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1017. {
  1018. $this->oSubForm->SetParentForm($this->oForm);
  1019. $this->oSubForm->CopySubmitParams($this->oForm);
  1020. if ($sRenderMode == 'property')
  1021. {
  1022. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  1023. }
  1024. else
  1025. {
  1026. $sHtml = $this->oSubForm->Render($oP, true);
  1027. }
  1028. return array('label' => $this->sLabel, 'value' => $sHtml);
  1029. }
  1030. public function ReadParam(&$aValues)
  1031. {
  1032. $this->oSubForm->SetParentForm($this->oForm);
  1033. $this->oSubForm->ReadParams($aValues);
  1034. }
  1035. }
  1036. ?>