forms.class.inc.php 27 KB

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