forms.class.inc.php 27 KB

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