forms.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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.'">';
  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 DesignerComboField extends DesignerFormField
  574. {
  575. protected $aAllowedValues;
  576. protected $bMultipleSelection;
  577. protected $bOtherChoices;
  578. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  579. {
  580. parent::__construct($sCode, $sLabel, $defaultValue);
  581. $this->aAllowedValues = array();
  582. $this->bMultipleSelection = false;
  583. $this->bOtherChoices = false;
  584. }
  585. public function SetAllowedValues($aAllowedValues)
  586. {
  587. $this->aAllowedValues = $aAllowedValues;
  588. }
  589. public function MultipleSelection($bMultipleSelection = true)
  590. {
  591. $this->bMultipleSelection = $bMultipleSelection;
  592. }
  593. public function OtherChoices($bOtherChoices = true)
  594. {
  595. $this->bOtherChoices = $bOtherChoices;
  596. }
  597. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  598. {
  599. $sId = $this->oForm->GetFieldId($this->sCode);
  600. $sName = $this->oForm->GetFieldName($this->sCode);
  601. $sChecked = $this->defaultValue ? 'checked' : '';
  602. $sMandatory = $this->bMandatory ? 'true' : 'false';
  603. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  604. $sValidation = $this->oForm->GetValidationArea($this->sCode);
  605. if ($this->bMultipleSelection)
  606. {
  607. $sHtml = "<select multiple size=\"8\"id=\"$sId\" name=\"$sName\" $sReadOnly>";
  608. }
  609. else
  610. {
  611. $sHtml = "<select id=\"$sId\" name=\"$sName\" $sReadOnly>";
  612. $sHtml .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>";
  613. }
  614. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  615. {
  616. if ($this->bMultipleSelection)
  617. {
  618. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  619. }
  620. else
  621. {
  622. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  623. }
  624. // Quick and dirty: display the menu parents as a tree
  625. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  626. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  627. }
  628. $sHtml .= "</select>";
  629. if ($this->bOtherChoices)
  630. {
  631. $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"/>';
  632. }
  633. $oP->add_ready_script(
  634. <<<EOF
  635. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId'); } );
  636. EOF
  637. );
  638. return array('label' => $this->sLabel, 'value' => $sHtml.$sValidation);
  639. }
  640. }
  641. class DesignerBooleanField extends DesignerFormField
  642. {
  643. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  644. {
  645. parent::__construct($sCode, $sLabel, $defaultValue);
  646. }
  647. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  648. {
  649. $sId = $this->oForm->GetFieldId($this->sCode);
  650. $sName = $this->oForm->GetFieldName($this->sCode);
  651. $sValidation = $this->oForm->GetValidationArea($this->sCode);
  652. $sChecked = $this->defaultValue ? 'checked' : '';
  653. $sReadOnly = $this->IsReadOnly() ? 'disabled' : ''; // readonly does not work as expected on checkboxes:
  654. // readonly prevents the user from changing the input's value not its state (checked/unchecked)
  655. return array('label' => $this->sLabel, 'value' => "<input type=\"checkbox\" $sChecked $sReadOnly id=\"$sId\" name=\"$sName\" value=\"true\">".$sValidation);
  656. }
  657. public function ReadParam(&$aValues)
  658. {
  659. if ($this->IsReadOnly())
  660. {
  661. $aValues[$this->sCode] = $this->defaultValue;
  662. }
  663. else
  664. {
  665. $sParamsContainer = $this->oForm->GetParamsContainer();
  666. if ($sParamsContainer != '')
  667. {
  668. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  669. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  670. {
  671. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  672. }
  673. else
  674. {
  675. $sValue = 'false';
  676. }
  677. }
  678. else
  679. {
  680. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  681. }
  682. }
  683. $aValues[$this->sCode] = ($sValue == 'true');
  684. }
  685. }
  686. class DesignerHiddenField extends DesignerFormField
  687. {
  688. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  689. {
  690. parent::__construct($sCode, $sLabel, $defaultValue);
  691. }
  692. public function IsVisible()
  693. {
  694. return false;
  695. }
  696. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  697. {
  698. $sId = $this->oForm->GetFieldId($this->sCode);
  699. $sName = $this->oForm->GetFieldName($this->sCode);
  700. $sValidation = $this->oForm->GetValidationArea($this->sCode);
  701. $sChecked = $this->defaultValue ? 'checked' : '';
  702. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  703. }
  704. }
  705. class DesignerIconSelectionField extends DesignerFormField
  706. {
  707. protected $aAllowedValues;
  708. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  709. {
  710. parent::__construct($sCode, $sLabel, $defaultValue);
  711. }
  712. public function SetAllowedValues($aAllowedValues)
  713. {
  714. $this->aAllowedValues = $aAllowedValues;
  715. }
  716. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  717. {
  718. $sId = $this->oForm->GetFieldId($this->sCode);
  719. $sName = $this->oForm->GetFieldName($this->sCode);
  720. $idx = 0;
  721. foreach($this->aAllowedValues as $index => $aValue)
  722. {
  723. if ($aValue['value'] == $this->defaultValue)
  724. {
  725. $idx = $index;
  726. break;
  727. }
  728. }
  729. $sJSItems = json_encode($this->aAllowedValues);
  730. if (!$this->IsReadOnly())
  731. {
  732. $oP->add_ready_script(
  733. <<<EOF
  734. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems});
  735. EOF
  736. );
  737. }
  738. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  739. return array('label' =>$this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>");
  740. }
  741. }
  742. class DesignerSortableField extends DesignerFormField
  743. {
  744. protected $aAllowedValues;
  745. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  746. {
  747. parent::__construct($sCode, $sLabel, $defaultValue);
  748. $this->aAllowedValues = array();
  749. }
  750. public function SetAllowedValues($aAllowedValues)
  751. {
  752. $this->aAllowedValues = $aAllowedValues;
  753. }
  754. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  755. {
  756. $bOpen = false;
  757. $sId = $this->oForm->GetFieldId($this->sCode);
  758. $sName = $this->oForm->GetFieldName($this->sCode);
  759. $sValidation = $this->oForm->GetValidationArea($this->sCode);
  760. $sHtml = "<span class=\"sort_$sId fieldslist\" id=\"sortable_$sId\">";
  761. foreach($this->defaultValue as $sValue)
  762. {
  763. $sHtml .= "<span class=\"movable_attr\">$sValue</span>";
  764. }
  765. $sHtml .="</span>";
  766. $sIconClass = $bOpen ? 'ui-icon-circle-triangle-s' : 'ui-icon-circle-triangle-e';
  767. $sStyle = $bOpen ? '' : 'style="display:none"';
  768. $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\">";
  769. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  770. {
  771. $sHtml .= "<span class=\"movable_attr\">$sDisplayValue</span>";
  772. }
  773. $sHtml .="</div></td></tr>";
  774. $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>";
  775. $oP->add_ready_script(
  776. <<<EOF
  777. $('#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(); } );
  778. $('#fieldsbasket_$sId .movable_attr').draggable({connectToSortable: '#sortable_$sId', helper: 'clone', revert: false });
  779. $('#recycle_$sId').sortable({ receive: function(event, ui) { ui.item.animate({opacity: 0.25}, { complete: function() { $(this).remove(); } });} });
  780. $('#sortable_$sId').sortable({connectWith: '#recycle_$sId', forcePlaceholderSize: true});
  781. $('#sortable_$sId').disableSelection();
  782. EOF
  783. );
  784. return array('label' => $this->sLabel, 'value' => $sHtml.$sValidation);
  785. }
  786. }
  787. class DesignerFormSelectorField extends DesignerFormField
  788. {
  789. protected $aSubForms;
  790. protected $defaultRealValue; // What's stored as default value is actually the index
  791. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  792. {
  793. parent::__construct($sCode, $sLabel, 0);
  794. $this->defaultRealValue = $defaultValue;
  795. $this->aSubForms = array();
  796. }
  797. public function AddSubForm($oSubForm, $sLabel, $sValue)
  798. {
  799. $idx = count($this->aSubForms);
  800. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  801. if ($sValue == $this->defaultRealValue)
  802. {
  803. // Store the index of the selected/default form
  804. $this->defaultValue = count($this->aSubForms) - 1;
  805. }
  806. }
  807. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  808. {
  809. $sId = $this->oForm->GetFieldId($this->sCode);
  810. $sName = $this->oForm->GetFieldName($this->sCode);
  811. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  812. $sHtml = "<select id=\"$sId\" name=\"$sName\" $sReadOnly>";
  813. foreach($this->aSubForms as $sKey => $aFormData)
  814. {
  815. $sDisplayValue = $aFormData['label'];
  816. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  817. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>".htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8')."</option>";
  818. }
  819. $sHtml .= "</select>";
  820. if ($sRenderMode == 'property')
  821. {
  822. $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>';
  823. }
  824. foreach($this->aSubForms as $sKey => $aFormData)
  825. {
  826. $sId = $this->oForm->GetFieldId($this->sCode);
  827. $sStyle = ($sKey == $this->defaultValue) ? '' : 'style="display:none"';
  828. $oSubForm = $aFormData['form'];
  829. $oSubForm->SetParentForm($this->oForm);
  830. $oSubForm->CopySubmitParams($this->oForm);
  831. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  832. if ($sRenderMode == 'property')
  833. {
  834. $sHtml .= "</tbody><tbody class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  835. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  836. }
  837. else
  838. {
  839. $sHtml .= "<div class=\"subform\" id=\"{$sId}_{$sKey}\" $sStyle>";
  840. $sHtml .= $oSubForm->Render($oP, true);
  841. $sHtml .= "</div>";
  842. }
  843. }
  844. $oP->add_ready_script(
  845. <<<EOF
  846. $('#$sId').bind('change reverted', function() { $('.subform').hide(); $('#{$sId}_'+this.value).show(); } );
  847. EOF
  848. );
  849. return array('label' => $this->sLabel, 'value' => $sHtml);
  850. }
  851. public function ReadParam(&$aValues)
  852. {
  853. parent::ReadParam($aValues);
  854. $sKey = $aValues[$this->sCode];
  855. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  856. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  857. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  858. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  859. }
  860. }
  861. class DesignerSubFormField extends DesignerFormField
  862. {
  863. protected $oSubForm;
  864. public function __construct($sLabel, $oSubForm)
  865. {
  866. parent::__construct('', $sLabel, '');
  867. $this->oSubForm = $oSubForm;
  868. }
  869. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  870. {
  871. $this->oSubForm->SetParentForm($this->oForm);
  872. $oSubForm->CopySubmitParams($this->oForm);
  873. if ($sRenderMode == 'property')
  874. {
  875. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  876. }
  877. else
  878. {
  879. $sHtml = $this->oSubForm->Render($oP, true);
  880. }
  881. return array('label' => $this->sLabel, 'value' => $sHtml);
  882. }
  883. public function ReadParam(&$aValues)
  884. {
  885. $this->oSubForm->SetParentForm($this->oForm);
  886. $this->oSubForm->ReadParams($aValues);
  887. }
  888. }
  889. ?>