forms.class.inc.php 27 KB

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