forms.class.inc.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. if ($this->oParentForm == null)
  71. {
  72. $sFormId = $this->sFormId;
  73. $sReturn = '<form id="'.$sFormId.'">';
  74. }
  75. else
  76. {
  77. $sReturn = '';
  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, $sIntroduction = null)
  240. {
  241. $sDialogTitle = addslashes($sDialogTitle);
  242. $sOkButtonLabel = addslashes($sOkButtonLabel);
  243. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  244. $oPage->add("<div id=\"$sDialogId\">");
  245. if ($sIntroduction != null)
  246. {
  247. $oPage->add('<div class="ui-dialog-header">'.$sIntroduction.'</div>');
  248. }
  249. $this->Render($oPage);
  250. $oPage->add('</div>');
  251. $oPage->add_ready_script(
  252. <<<EOF
  253. $('#$sDialogId').dialog({
  254. height: 'auto',
  255. width: 500,
  256. modal: true,
  257. title: '$sDialogTitle',
  258. buttons: [
  259. { text: "$sOkButtonLabel", click: function() {
  260. var oForm = $(this).closest('.ui-dialog').find('form');
  261. oForm.submit();
  262. } },
  263. { text: "$sCancelButtonLabel", click: function() { KillAllMenus(); $(this).dialog( "close" ); $(this).remove(); } },
  264. ],
  265. close: function() { KillAllMenus(); $(this).remove(); }
  266. });
  267. var oForm = $('#$sDialogId form');
  268. var sFormId = oForm.attr('id');
  269. ValidateForm(sFormId, true);
  270. EOF
  271. );
  272. }
  273. public function ReadParams(&$aValues = array())
  274. {
  275. foreach($this->aFieldSets as $sLabel => $aFields)
  276. {
  277. foreach($aFields as $oField)
  278. {
  279. $oField->ReadParam($aValues);
  280. }
  281. }
  282. return $aValues;
  283. }
  284. public function SetPrefix($sPrefix)
  285. {
  286. $this->sFormPrefix = $sPrefix;
  287. }
  288. public function GetPrefix()
  289. {
  290. return $this->sFormPrefix;
  291. }
  292. public function SetReadOnly($bReadOnly = true)
  293. {
  294. $this->bReadOnly = $bReadOnly;
  295. }
  296. public function IsReadOnly()
  297. {
  298. if ($this->oParentForm == null)
  299. {
  300. return $this->bReadOnly;
  301. }
  302. else
  303. {
  304. return $this->oParentForm->IsReadOnly();
  305. }
  306. }
  307. public function SetParamsContainer($sParamsContainer)
  308. {
  309. $this->sParamsContainer = $sParamsContainer;
  310. }
  311. public function GetParamsContainer()
  312. {
  313. if ($this->oParentForm == null)
  314. {
  315. return $this->sParamsContainer;
  316. }
  317. else
  318. {
  319. return $this->oParentForm->GetParamsContainer();
  320. }
  321. }
  322. public function SetParentForm($oParentForm)
  323. {
  324. $this->oParentForm = $oParentForm;
  325. }
  326. public function AddScript($sScript)
  327. {
  328. $this->sScript .= $sScript;
  329. }
  330. public function AddReadyScript($sScript)
  331. {
  332. $this->sReadyScript .= $sScript;
  333. }
  334. public function GetFieldId($sCode)
  335. {
  336. return $this->sFormPrefix.'attr_'.$sCode;
  337. }
  338. public function GetFieldName($sCode)
  339. {
  340. return 'attr_'.$sCode;
  341. }
  342. public function GetParamName($sCode)
  343. {
  344. return 'attr_'.$sCode;
  345. }
  346. public function GetValidationArea($sCode, $sContent = '')
  347. {
  348. return "<span style=\"display:inline-block;width:20px;\" id=\"v_{$this->sFormPrefix}attr_$sCode\"><span class=\"ui-icon ui-icon-alert\"></span>$sContent</span>";
  349. }
  350. public function GetAsyncActionClass()
  351. {
  352. return $this->sAsyncActionClass;
  353. }
  354. }
  355. class DesignerTabularForm extends DesignerForm
  356. {
  357. protected $aTable;
  358. public function __construct()
  359. {
  360. parent::__construct();
  361. $this->aTable = array();
  362. }
  363. public function AddRow($aRow)
  364. {
  365. $this->aTable[] = $aRow;
  366. }
  367. public function Render($oP, $bReturnHTML = false)
  368. {
  369. $sReturn = '';
  370. if ($this->oParentForm == null)
  371. {
  372. $sFormId = $this->sFormId;
  373. $sReturn = '<form id="'.$sFormId.'">';
  374. }
  375. else
  376. {
  377. $sFormId = $this->oParentForm->sFormId;
  378. }
  379. $sHiddenFields = '';
  380. $sReturn .= '<table style="width:100%">';
  381. foreach($this->aTable as $aRow)
  382. {
  383. $sReturn .= '<tr>';
  384. foreach($aRow as $field)
  385. {
  386. if (!is_object($field))
  387. {
  388. // Shortcut: pass a string for a cell containing just a label
  389. $sReturn .= '<td>'.$field.'</td>';
  390. }
  391. else
  392. {
  393. $field->SetForm($this);
  394. $aFieldData = $field->Render($oP, $sFormId);
  395. if ($field->IsVisible())
  396. {
  397. // put the label and value separated by a non-breaking space if needed
  398. $aData = array();
  399. foreach(array('label', 'value') as $sCode )
  400. {
  401. if ($aFieldData[$sCode] != '')
  402. {
  403. $aData[] = $aFieldData[$sCode];
  404. }
  405. }
  406. $sReturn .= '<td>'.implode('&nbsp;', $aData).'</td>';
  407. }
  408. else
  409. {
  410. $sHiddenFields .= $aRow['value'];
  411. }
  412. }
  413. }
  414. $sReturn .= '</tr>';
  415. }
  416. $sReturn .= '</table>';
  417. $sReturn .= $sHiddenFields;
  418. if($this->sScript != '')
  419. {
  420. $oP->add_script($this->sScript);
  421. }
  422. if($this->sReadyScript != '')
  423. {
  424. $oP->add_ready_script($this->sReadyScript);
  425. }
  426. if ($bReturnHTML)
  427. {
  428. return $sReturn;
  429. }
  430. else
  431. {
  432. $oP->add($sReturn);
  433. }
  434. }
  435. public function ReadParams(&$aValues = array())
  436. {
  437. foreach($this->aTable as $aRow)
  438. {
  439. foreach($aRow as $field)
  440. {
  441. if (is_object($field))
  442. {
  443. $field->SetForm($this);
  444. $field->ReadParam($aValues);
  445. }
  446. }
  447. }
  448. return $aValues;
  449. }
  450. }
  451. class DesignerFormField
  452. {
  453. protected $sLabel;
  454. protected $sCode;
  455. protected $defaultValue;
  456. protected $oForm;
  457. protected $bMandatory;
  458. protected $bReadOnly;
  459. protected $bAutoApply;
  460. protected $aCSSClasses;
  461. public function __construct($sCode, $sLabel, $defaultValue)
  462. {
  463. $this->sLabel = $sLabel;
  464. $this->sCode = $sCode;
  465. $this->defaultValue = $defaultValue;
  466. $this->bMandatory = false;
  467. $this->bReadOnly = false;
  468. $this->bAutoApply = false;
  469. $this->aCSSClasses = array();
  470. }
  471. public function GetCode()
  472. {
  473. return $this->sCode;
  474. }
  475. public function SetForm($oForm)
  476. {
  477. $this->oForm = $oForm;
  478. }
  479. public function SetMandatory($bMandatory = true)
  480. {
  481. $this->bMandatory = $bMandatory;
  482. }
  483. public function SetReadOnly($bReadOnly = true)
  484. {
  485. $this->bReadOnly = $bReadOnly;
  486. }
  487. public function IsReadOnly()
  488. {
  489. return ($this->oForm->IsReadOnly() || $this->bReadOnly);
  490. }
  491. public function SetAutoApply($bAutoApply)
  492. {
  493. $this->bAutoApply = $bAutoApply;
  494. }
  495. public function IsAutoApply()
  496. {
  497. return $this->bAutoApply;
  498. }
  499. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  500. {
  501. $sId = $this->oForm->GetFieldId($this->sCode);
  502. $sName = $this->oForm->GetFieldName($this->sCode);
  503. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  504. }
  505. public function ReadParam(&$aValues)
  506. {
  507. if ($this->IsReadOnly())
  508. {
  509. $aValues[$this->sCode] = $this->defaultValue;
  510. }
  511. else
  512. {
  513. if ($this->oForm->GetParamsContainer() != '')
  514. {
  515. $aParams = utils::ReadParam($this->oForm->GetParamsContainer(), array(), false, 'raw_data');
  516. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  517. {
  518. $aValues[$this->sCode] = $aParams[$this->oForm->GetParamName($this->sCode)];
  519. }
  520. else
  521. {
  522. $aValues[$this->sCode] = $this->defaultValue;
  523. }
  524. }
  525. else
  526. {
  527. $aValues[$this->sCode] = utils::ReadParam($this->oForm->GetParamName($this->sCode), $this->defaultValue, false, 'raw_data');
  528. }
  529. }
  530. }
  531. public function IsVisible()
  532. {
  533. return true;
  534. }
  535. public function AddCSSClass($sCSSClass)
  536. {
  537. $this->aCSSClasses[] = $sCSSClass;
  538. }
  539. }
  540. class DesignerLabelField extends DesignerFormField
  541. {
  542. protected $sDescription;
  543. public function __construct($sLabel, $sDescription)
  544. {
  545. parent::__construct('', $sLabel, '');
  546. $this->sDescription = $sDescription;
  547. }
  548. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  549. {
  550. $sId = $this->oForm->GetFieldId($this->sCode);
  551. $sName = $this->oForm->GetFieldName($this->sCode);
  552. return array('label' => $this->sLabel, 'value' => $sDescription);
  553. }
  554. public function ReadParam(&$aValues)
  555. {
  556. }
  557. public function IsVisible()
  558. {
  559. return true;
  560. }
  561. }
  562. class DesignerTextField extends DesignerFormField
  563. {
  564. protected $sValidationPattern;
  565. protected $aForbiddenValues;
  566. protected $sExplainForbiddenValues;
  567. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  568. {
  569. parent::__construct($sCode, $sLabel, $defaultValue);
  570. $this->sValidationPattern = '';
  571. $this->aForbiddenValues = null;
  572. $this->sExplainForbiddenValues = null;
  573. }
  574. public function SetValidationPattern($sValidationPattern)
  575. {
  576. $this->sValidationPattern = $sValidationPattern;
  577. }
  578. public function SetForbiddenValues($aValues, $sExplain)
  579. {
  580. $this->aForbiddenValues = $aValues;
  581. $this->sExplainForbiddenValues = $sExplain;
  582. }
  583. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  584. {
  585. $sId = $this->oForm->GetFieldId($this->sCode);
  586. $sName = $this->oForm->GetFieldName($this->sCode);
  587. $sPattern = addslashes($this->sValidationPattern);
  588. if (is_array($this->aForbiddenValues))
  589. {
  590. $sForbiddenValues = json_encode($this->aForbiddenValues);
  591. $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues);
  592. }
  593. else
  594. {
  595. $sForbiddenValues = 'null';
  596. $sExplainForbiddenValues = 'null';
  597. }
  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', $sForbiddenValues, '$sExplainForbiddenValues'); } );
  603. {
  604. var myTimer = null;
  605. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  606. }
  607. EOF
  608. );
  609. $sCSSClasses = '';
  610. if (count($this->aCSSClasses) > 0)
  611. {
  612. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  613. }
  614. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" $sCSSClasses id=\"$sId\" $sReadOnly name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  615. }
  616. public function ReadParam(&$aValues)
  617. {
  618. parent::ReadParam($aValues);
  619. if (($this->sValidationPattern != '') &&(!preg_match('/'.$this->sValidationPattern.'/', $aValues[$this->sCode])) )
  620. {
  621. $aValues[$this->sCode] = $this->defaultValue;
  622. }
  623. }
  624. }
  625. class DesignerLongTextField extends DesignerTextField
  626. {
  627. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  628. {
  629. $sId = $this->oForm->GetFieldId($this->sCode);
  630. $sName = $this->oForm->GetFieldName($this->sCode);
  631. $sPattern = addslashes($this->sValidationPattern);
  632. if (is_array($this->aForbiddenValues))
  633. {
  634. $sForbiddenValues = json_encode($this->aForbiddenValues);
  635. $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues);
  636. }
  637. else
  638. {
  639. $sForbiddenValues = 'null';
  640. $sExplainForbiddenValues = 'null';
  641. }
  642. $sMandatory = $this->bMandatory ? 'true' : 'false';
  643. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  644. $oP->add_ready_script(
  645. <<<EOF
  646. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId', $sForbiddenValues, '$sExplainForbiddenValues'); } );
  647. {
  648. var myTimer = null;
  649. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  650. }
  651. EOF
  652. );
  653. $sCSSClasses = '';
  654. if (count($this->aCSSClasses) > 0)
  655. {
  656. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  657. }
  658. return array('label' => $this->sLabel, 'value' => "<textarea $sCSSClasses id=\"$sId\" $sReadOnly name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>");
  659. }
  660. }
  661. class DesignerComboField extends DesignerFormField
  662. {
  663. protected $aAllowedValues;
  664. protected $bMultipleSelection;
  665. protected $bOtherChoices;
  666. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  667. {
  668. parent::__construct($sCode, $sLabel, $defaultValue);
  669. $this->aAllowedValues = array();
  670. $this->bMultipleSelection = false;
  671. $this->bOtherChoices = false;
  672. $this->bAutoApply = true;
  673. }
  674. public function SetAllowedValues($aAllowedValues)
  675. {
  676. $this->aAllowedValues = $aAllowedValues;
  677. }
  678. public function MultipleSelection($bMultipleSelection = true)
  679. {
  680. $this->bMultipleSelection = $bMultipleSelection;
  681. }
  682. public function OtherChoices($bOtherChoices = true)
  683. {
  684. $this->bOtherChoices = $bOtherChoices;
  685. }
  686. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  687. {
  688. $sId = $this->oForm->GetFieldId($this->sCode);
  689. $sName = $this->oForm->GetFieldName($this->sCode);
  690. $sChecked = $this->defaultValue ? 'checked' : '';
  691. $sMandatory = $this->bMandatory ? 'true' : 'false';
  692. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  693. $sCSSClasses = '';
  694. if (count($this->aCSSClasses) > 0)
  695. {
  696. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  697. }
  698. if ($this->bMultipleSelection)
  699. {
  700. $sHtml = "<select $sCSSClasses multiple size=\"8\"id=\"$sId\" name=\"$sName\" $sReadOnly>";
  701. }
  702. else
  703. {
  704. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  705. $sHtml .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>";
  706. }
  707. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  708. {
  709. if ($this->bMultipleSelection)
  710. {
  711. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  712. }
  713. else
  714. {
  715. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  716. }
  717. // Quick and dirty: display the menu parents as a tree
  718. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  719. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  720. }
  721. $sHtml .= "</select>";
  722. if ($this->bOtherChoices)
  723. {
  724. $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"/>';
  725. }
  726. $oP->add_ready_script(
  727. <<<EOF
  728. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId', null, null); } );
  729. EOF
  730. );
  731. return array('label' => $this->sLabel, 'value' => $sHtml);
  732. }
  733. public function ReadParam(&$aValues)
  734. {
  735. parent::ReadParam($aValues);
  736. if ($aValues[$this->sCode] == 'null')
  737. {
  738. $aValues[$this->sCode] = array();
  739. }
  740. }
  741. }
  742. class DesignerBooleanField extends DesignerFormField
  743. {
  744. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  745. {
  746. parent::__construct($sCode, $sLabel, $defaultValue);
  747. $this->bAutoApply = true;
  748. }
  749. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  750. {
  751. $sId = $this->oForm->GetFieldId($this->sCode);
  752. $sName = $this->oForm->GetFieldName($this->sCode);
  753. $sChecked = $this->defaultValue ? 'checked' : '';
  754. $sReadOnly = $this->IsReadOnly() ? 'disabled' : ''; // readonly does not work as expected on checkboxes:
  755. // readonly prevents the user from changing the input's value not its state (checked/unchecked)
  756. $sCSSClasses = '';
  757. if (count($this->aCSSClasses) > 0)
  758. {
  759. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  760. }
  761. return array('label' => $this->sLabel, 'value' => "<input $sCSSClasses type=\"checkbox\" $sChecked $sReadOnly id=\"$sId\" name=\"$sName\" value=\"true\">");
  762. }
  763. public function ReadParam(&$aValues)
  764. {
  765. if ($this->IsReadOnly())
  766. {
  767. $aValues[$this->sCode] = $this->defaultValue;
  768. }
  769. else
  770. {
  771. $sParamsContainer = $this->oForm->GetParamsContainer();
  772. if ($sParamsContainer != '')
  773. {
  774. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  775. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  776. {
  777. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  778. }
  779. else
  780. {
  781. $sValue = 'false';
  782. }
  783. }
  784. else
  785. {
  786. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  787. }
  788. }
  789. $aValues[$this->sCode] = ($sValue == 'true');
  790. }
  791. }
  792. class DesignerHiddenField extends DesignerFormField
  793. {
  794. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  795. {
  796. parent::__construct($sCode, $sLabel, $defaultValue);
  797. }
  798. public function IsVisible()
  799. {
  800. return false;
  801. }
  802. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  803. {
  804. $sId = $this->oForm->GetFieldId($this->sCode);
  805. $sName = $this->oForm->GetFieldName($this->sCode);
  806. $sChecked = $this->defaultValue ? 'checked' : '';
  807. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  808. }
  809. }
  810. class DesignerIconSelectionField extends DesignerFormField
  811. {
  812. protected $sUploadUrl;
  813. protected $aAllowedValues;
  814. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  815. {
  816. parent::__construct($sCode, $sLabel, $defaultValue);
  817. $this->bAutoApply = true;
  818. $this->sUploadUrl = null;
  819. }
  820. public function SetAllowedValues($aAllowedValues)
  821. {
  822. $this->aAllowedValues = $aAllowedValues;
  823. }
  824. public function EnableUpload($sIconUploadUrl)
  825. {
  826. $this->sUploadUrl = $sIconUploadUrl;
  827. }
  828. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  829. {
  830. $sId = $this->oForm->GetFieldId($this->sCode);
  831. $sName = $this->oForm->GetFieldName($this->sCode);
  832. $idx = 0;
  833. foreach($this->aAllowedValues as $index => $aValue)
  834. {
  835. if ($aValue['value'] == $this->defaultValue)
  836. {
  837. $idx = $index;
  838. break;
  839. }
  840. }
  841. $sJSItems = json_encode($this->aAllowedValues);
  842. $sPostUploadTo = ($this->sUploadUrl == null) ? 'null' : "'{$this->sUploadUrl}'";
  843. if (!$this->IsReadOnly())
  844. {
  845. $oP->add_ready_script(
  846. <<<EOF
  847. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems, post_upload_to: $sPostUploadTo});
  848. EOF
  849. );
  850. }
  851. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  852. return array('label' =>$this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>");
  853. }
  854. }
  855. class RunTimeIconSelectionField extends DesignerIconSelectionField
  856. {
  857. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  858. {
  859. parent::__construct($sCode, $sLabel, $defaultValue);
  860. $aAllIcons = self::FindIconsOnDisk(APPROOT.'env-'.utils::GetCurrentEnvironment());
  861. ksort($aAllIcons);
  862. $aValues = array();
  863. foreach($aAllIcons as $sFilePath)
  864. {
  865. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  866. }
  867. $this->SetAllowedValues($aValues);
  868. }
  869. static protected function FindIconsOnDisk($sBaseDir, $sDir = '')
  870. {
  871. $aResult = array();
  872. // Populate automatically the list of icon files
  873. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  874. {
  875. while (($sFile = readdir($hDir)) !== false)
  876. {
  877. $aMatches = array();
  878. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  879. {
  880. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  881. $aResult = array_merge($aResult, self::FindIconsOnDisk($sBaseDir, $sDirSubPath));
  882. }
  883. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  884. {
  885. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  886. }
  887. }
  888. closedir($hDir);
  889. }
  890. return $aResult;
  891. }
  892. public function ValueFromDOMNode($oDOMNode)
  893. {
  894. return $oDOMNode->textContent;
  895. }
  896. public function ValueToDOMNode($oDOMNode, $value)
  897. {
  898. $oTextNode = $oDOMNode->ownerDocument->createTextNode($value);
  899. $oDOMNode->appendChild($oTextNode);
  900. }
  901. public function MakeFileUrl($value)
  902. {
  903. return utils::GetAbsoluteUrlModulesRoot().$value;
  904. }
  905. public function GetDefaultValue($sClass = 'Contact')
  906. {
  907. $sIconPath = MetaModel::GetClassIcon($sClass, false);
  908. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIconPath);
  909. return $sIcon;
  910. }
  911. }
  912. class DesignerSortableField extends DesignerFormField
  913. {
  914. protected $aAllowedValues;
  915. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  916. {
  917. parent::__construct($sCode, $sLabel, $defaultValue);
  918. $this->aAllowedValues = array();
  919. }
  920. public function SetAllowedValues($aAllowedValues)
  921. {
  922. $this->aAllowedValues = $aAllowedValues;
  923. }
  924. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  925. {
  926. $bOpen = false;
  927. $sId = $this->oForm->GetFieldId($this->sCode);
  928. $sName = $this->oForm->GetFieldName($this->sCode);
  929. $sCSSClasses = '';
  930. $this->aCSSClasses[] = "sort_$sId fieldslist";
  931. if (count($this->aCSSClasses) > 0)
  932. {
  933. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  934. }
  935. $sHtml = "<span $sCSSClasses id=\"sortable_$sId\">";
  936. foreach($this->defaultValue as $sValue)
  937. {
  938. $sHtml .= "<span class=\"movable_attr\">$sValue</span>";
  939. }
  940. $sHtml .="</span>";
  941. $sIconClass = $bOpen ? 'ui-icon-circle-triangle-s' : 'ui-icon-circle-triangle-e';
  942. $sStyle = $bOpen ? '' : 'style="display:none"';
  943. $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\">";
  944. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  945. {
  946. $sHtml .= "<span class=\"movable_attr\">$sDisplayValue</span>";
  947. }
  948. $sHtml .="</div></td></tr>";
  949. $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>";
  950. $oP->add_ready_script(
  951. <<<EOF
  952. $('#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(); } );
  953. $('#fieldsbasket_$sId .movable_attr').draggable({connectToSortable: '#sortable_$sId', helper: 'clone', revert: false });
  954. $('#recycle_$sId').sortable({ receive: function(event, ui) { ui.item.animate({opacity: 0.25}, { complete: function() { $(this).remove(); } });} });
  955. $('#sortable_$sId').sortable({connectWith: '#recycle_$sId', forcePlaceholderSize: true});
  956. $('#sortable_$sId').disableSelection();
  957. EOF
  958. );
  959. return array('label' => $this->sLabel, 'value' => $sHtml);
  960. }
  961. }
  962. class DesignerFormSelectorField extends DesignerFormField
  963. {
  964. protected $aSubForms;
  965. protected $defaultRealValue; // What's stored as default value is actually the index
  966. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  967. {
  968. parent::__construct($sCode, $sLabel, 0);
  969. $this->defaultRealValue = $defaultValue;
  970. $this->aSubForms = array();
  971. }
  972. public function AddSubForm($oSubForm, $sLabel, $sValue)
  973. {
  974. $idx = count($this->aSubForms);
  975. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  976. if ($sValue == $this->defaultRealValue)
  977. {
  978. // Store the index of the selected/default form
  979. $this->defaultValue = count($this->aSubForms) - 1;
  980. }
  981. }
  982. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  983. {
  984. $sId = $this->oForm->GetFieldId($this->sCode);
  985. $sName = $this->oForm->GetFieldName($this->sCode);
  986. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  987. $sCSSClasses = '';
  988. if (count($this->aCSSClasses) > 0)
  989. {
  990. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  991. }
  992. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  993. foreach($this->aSubForms as $sKey => $aFormData)
  994. {
  995. $sDisplayValue = $aFormData['label'];
  996. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  997. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>".htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8')."</option>";
  998. }
  999. $sHtml .= "</select>";
  1000. if ($sRenderMode == 'property')
  1001. {
  1002. $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>';
  1003. }
  1004. foreach($this->aSubForms as $sKey => $aFormData)
  1005. {
  1006. $sId = $this->oForm->GetFieldId($this->sCode);
  1007. $sStyle = ($sKey == $this->defaultValue) ? '' : 'style="display:none"';
  1008. $oSubForm = $aFormData['form'];
  1009. $oSubForm->SetParentForm($this->oForm);
  1010. $oSubForm->CopySubmitParams($this->oForm);
  1011. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1012. if ($sRenderMode == 'property')
  1013. {
  1014. $sHtml .= "</tbody><tbody class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  1015. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  1016. }
  1017. else
  1018. {
  1019. $sHtml .= "<div class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  1020. $sHtml .= $oSubForm->Render($oP, true);
  1021. $sHtml .= "</div>";
  1022. }
  1023. }
  1024. $oP->add_ready_script(
  1025. <<<EOF
  1026. $('#$sId').bind('change reverted', function() { $('.subform').hide(); $('#{$sId}_'+this.value).show(); } );
  1027. EOF
  1028. );
  1029. return array('label' => $this->sLabel, 'value' => $sHtml);
  1030. }
  1031. public function ReadParam(&$aValues)
  1032. {
  1033. parent::ReadParam($aValues);
  1034. $sKey = $aValues[$this->sCode];
  1035. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  1036. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1037. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  1038. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  1039. }
  1040. }
  1041. class DesignerSubFormField extends DesignerFormField
  1042. {
  1043. protected $oSubForm;
  1044. public function __construct($sLabel, $oSubForm)
  1045. {
  1046. parent::__construct('', $sLabel, '');
  1047. $this->oSubForm = $oSubForm;
  1048. }
  1049. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1050. {
  1051. $this->oSubForm->SetParentForm($this->oForm);
  1052. $this->oSubForm->CopySubmitParams($this->oForm);
  1053. if ($sRenderMode == 'property')
  1054. {
  1055. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  1056. }
  1057. else
  1058. {
  1059. $sHtml = $this->oSubForm->Render($oP, true);
  1060. }
  1061. return array('label' => $this->sLabel, 'value' => $sHtml);
  1062. }
  1063. public function ReadParam(&$aValues)
  1064. {
  1065. $this->oSubForm->SetParentForm($this->oForm);
  1066. $this->oSubForm->ReadParams($aValues);
  1067. }
  1068. }
  1069. ?>