forms.class.inc.php 31 KB

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