forms.class.inc.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Helper class to build interactive forms to be used either in stand-alone
  20. * modal dialog or in "property-sheet" panes.
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. class DesignerForm
  26. {
  27. protected $aFieldSets;
  28. protected $sCurrentFieldSet;
  29. protected $sScript;
  30. protected $sReadyScript;
  31. protected $sFormId;
  32. protected $sFormPrefix;
  33. protected $sParamsContainer;
  34. protected $oParentForm;
  35. protected $aSubmitParams;
  36. protected $sSubmitTo;
  37. protected $bReadOnly;
  38. protected $sHierarchyPath; // Needed to manage the visibility of nested subform
  39. protected $sHierarchyParent; // Needed to manage the visibility of nested subform
  40. protected $sHierarchySelector; // Needed to manage the visibility of nested subform
  41. protected $bDisplayed;
  42. protected $aDefaultValues;
  43. protected $sFieldsSuffix;
  44. public function __construct()
  45. {
  46. $this->aFieldSets = array();
  47. $this->sCurrentFieldSet = '';
  48. $this->sScript = '';
  49. $this->sReadyScript = '';
  50. $this->sFormPrefix = '';
  51. $this->sFieldsSuffix = '';
  52. $this->sParamsContainer = '';
  53. $this->sFormId = 'form_'.rand();
  54. $this->oParentForm = null;
  55. $this->bReadOnly = false;
  56. $this->sHierarchyPath = '';
  57. $this->sHierarchyParent = '';
  58. $this->sHierarchySelector = '';
  59. $this->StartFieldSet($this->sCurrentFieldSet);
  60. $this->bDisplayed = true;
  61. $this->aDefaultvalues = array();
  62. }
  63. public function AddField(DesignerFormField $oField)
  64. {
  65. if (!is_array($this->aFieldSets[$this->sCurrentFieldSet]))
  66. {
  67. $this->aFieldSets[$this->sCurrentFieldSet] = array();
  68. }
  69. $this->aFieldSets[$this->sCurrentFieldSet][] = $oField;
  70. $oField->SetForm($this);
  71. }
  72. public function StartFieldSet($sLabel)
  73. {
  74. $this->sCurrentFieldSet = $sLabel;
  75. if (!array_key_exists($this->sCurrentFieldSet, $this->aFieldSets))
  76. {
  77. $this->aFieldSets[$this->sCurrentFieldSet] = array();
  78. }
  79. }
  80. public function Render($oP, $bReturnHTML = false)
  81. {
  82. if ($this->oParentForm == null)
  83. {
  84. $sFormId = $this->sFormId;
  85. $sReturn = '<form id="'.$sFormId.'">';
  86. }
  87. else
  88. {
  89. $sReturn = '';
  90. $sFormId = $this->oParentForm->sFormId;
  91. }
  92. $sHiddenFields = '';
  93. foreach($this->aFieldSets as $sLabel => $aFields)
  94. {
  95. $aDetails = array();
  96. if ($sLabel != '')
  97. {
  98. $sReturn .= '<fieldset>';
  99. $sReturn .= '<legend>'.$sLabel.'</legend>';
  100. }
  101. foreach($aFields as $oField)
  102. {
  103. $aRow = $oField->Render($oP, $sFormId);
  104. if ($oField->IsVisible())
  105. {
  106. $sValidation = '&nbsp;<span class="prop_apply">'.$this->GetValidationArea($oField->GetFieldId()).'</span>';
  107. $sField = $aRow['value'].$sValidation;
  108. $aDetails[] = array('label' => $aRow['label'], 'value' => $sField);
  109. }
  110. else
  111. {
  112. $sHiddenFields .= $aRow['value'];
  113. }
  114. }
  115. $sReturn .= $oP->GetDetails($aDetails);
  116. if ($sLabel != '')
  117. {
  118. $sReturn .= '</fieldset>';
  119. }
  120. }
  121. $sReturn .= $sHiddenFields;
  122. if ($this->oParentForm == null)
  123. {
  124. $sReturn .= '</form>';
  125. }
  126. if($this->sScript != '')
  127. {
  128. $oP->add_script($this->sScript);
  129. }
  130. if($this->sReadyScript != '')
  131. {
  132. $oP->add_ready_script($this->sReadyScript);
  133. }
  134. if ($bReturnHTML)
  135. {
  136. return $sReturn;
  137. }
  138. else
  139. {
  140. $oP->add($sReturn);
  141. }
  142. }
  143. public function GetFieldSets()
  144. {
  145. return $this->aFieldSets;
  146. }
  147. public function SetSubmitParams($sSubmitToUrl, $aSubmitParams)
  148. {
  149. $this->sSubmitTo = $sSubmitToUrl;
  150. $this->aSubmitParams = $aSubmitParams;
  151. }
  152. public function CopySubmitParams($oParentForm)
  153. {
  154. $this->sSubmitTo = $oParentForm->sSubmitTo;
  155. $this->aSubmitParams = $oParentForm->aSubmitParams;
  156. }
  157. public function GetSubmitParams()
  158. {
  159. return array( 'url' => $this->sSubmitTo, 'params' => $this->aSubmitParams);
  160. }
  161. /**
  162. * Helper to handle subforms hide/show
  163. */
  164. public function SetHierarchyPath($sHierarchy)
  165. {
  166. $this->sHierarchyPath = $sHierarchy;
  167. }
  168. /**
  169. * Helper to handle subforms hide/show
  170. */
  171. public function GetHierarchyPath()
  172. {
  173. return $this->sHierarchyPath;
  174. }
  175. /**
  176. * Helper to handle subforms hide/show
  177. */
  178. public function SetHierarchyParent($sHierarchy)
  179. {
  180. $this->sHierarchyParent = $sHierarchy;
  181. }
  182. /**
  183. * Helper to handle subforms hide/show
  184. */
  185. public function GetHierarchyParent()
  186. {
  187. return $this->sHierarchyParent;
  188. }
  189. public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
  190. {
  191. $sReturn = '';
  192. $sActionUrl = addslashes($this->sSubmitTo);
  193. $sJSSubmitParams = json_encode($this->aSubmitParams);
  194. if ($this->oParentForm == null)
  195. {
  196. $sFormId = $this->sFormId;
  197. $sReturn = '<form id="'.$sFormId.'" onsubmit="return false;">';
  198. $sReturn .= '<table class="prop_table">';
  199. $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>';
  200. }
  201. else
  202. {
  203. $sFormId = $this->oParentForm->sFormId;
  204. }
  205. $sHiddenFields = '';
  206. foreach($this->aFieldSets as $sLabel => $aFields)
  207. {
  208. $aDetails = array();
  209. if ($sLabel != '')
  210. {
  211. $sReturn .= $this->StartRow().'<th colspan="4">'.$sLabel.'</th>'.$this->EndRow();
  212. }
  213. foreach($aFields as $oField)
  214. {
  215. $aRow = $oField->Render($oP, $sFormId, 'property');
  216. if ($oField->IsVisible())
  217. {
  218. $sFieldId = $this->GetFieldId($oField->GetCode());
  219. $sValidation = $this->GetValidationArea($sFieldId, '<span title="Apply" class="ui-icon ui-icon-circle-check"/>');
  220. $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>'.$this->EndRow();
  221. $sPath = $this->GetHierarchyPath().'/'.$oField->GetCode();
  222. if (is_null($aRow['label']))
  223. {
  224. $sReturn .= $this->StartRow($sFieldId).'<td class="prop_value" colspan="2">'.$aRow['value'];
  225. }
  226. else
  227. {
  228. $sReturn .= $this->StartRow($sFieldId).'<td class="prop_label">'.$aRow['label'].'</td><td class="prop_value">'.$aRow['value'];
  229. }
  230. if (!($oField instanceof DesignerFormSelectorField) && !($oField instanceof DesignerMultipleSubFormField))
  231. {
  232. $sReturn .= $sValidationFields;
  233. }
  234. $sNotifyParentSelectorJS = is_null($sNotifyParentSelector) ? 'null' : "'".addslashes($sNotifyParentSelector)."'";
  235. $sAutoApply = $oField->IsAutoApply() ? 'true' : 'false';
  236. $sHandlerEquals = $oField->GetHandlerEquals();
  237. $sHandlerGetValue = $oField->GetHandlerGetValue();
  238. $this->AddReadyScript(
  239. <<<EOF
  240. $('#row_$sFieldId').property_field({parent_selector: $sNotifyParentSelectorJS, field_id: '$sFieldId', equals: $sHandlerEquals, get_field_value: $sHandlerGetValue, auto_apply: $sAutoApply, value: '', submit_to: '$sActionUrl', submit_parameters: $sJSSubmitParams });
  241. EOF
  242. );
  243. }
  244. else
  245. {
  246. $sHiddenFields .= $aRow['value'];
  247. }
  248. }
  249. }
  250. if ($this->oParentForm == null)
  251. {
  252. $sFormId = $this->sFormId;
  253. $sReturn .= '</tbody>';
  254. $sReturn .= '</table>';
  255. $sReturn .= $sHiddenFields;
  256. $sReturn .= '</form>';
  257. $sReturn .= '<div id="prop_submit_result"></div>'; // for the return of the submit operation
  258. }
  259. else
  260. {
  261. $sReturn .= $sHiddenFields;
  262. }
  263. $this->AddReadyScript(
  264. <<<EOF
  265. $('.prop_table').tableHover();
  266. var idx = 0;
  267. $('.prop_table tbody tr').each(function() {
  268. if ((idx % 2) == 0)
  269. {
  270. $(this).addClass('even');
  271. }
  272. else
  273. {
  274. $(this).addClass('odd');
  275. }
  276. idx++;
  277. });
  278. EOF
  279. );
  280. if($this->sScript != '')
  281. {
  282. $oP->add_script($this->sScript);
  283. }
  284. if($this->sReadyScript != '')
  285. {
  286. $oP->add_ready_script($this->sReadyScript);
  287. }
  288. if ($bReturnHTML)
  289. {
  290. return $sReturn;
  291. }
  292. else
  293. {
  294. $oP->add($sReturn);
  295. }
  296. }
  297. public function StartRow($sFieldId = null)
  298. {
  299. if ($sFieldId != null)
  300. {
  301. return '<tr id="row_'.$sFieldId.'" data-path="'.$this->GetHierarchyPath().'" data-selector="'.$this->GetHierarchyParent().'">';
  302. }
  303. return '<tr data-path="'.$this->GetHierarchyPath().'" data-selector="'.$this->GetHierarchyParent().'">';
  304. }
  305. public function EndRow()
  306. {
  307. return '</tr>';
  308. }
  309. public function RenderAsDialog($oPage, $sDialogId, $sDialogTitle, $iDialogWidth, $sOkButtonLabel, $sIntroduction = null)
  310. {
  311. $this->SetPrefix('dlg_'); // To make sure that the controls have different IDs that the property sheet which may be displayed at the same time
  312. $sDialogTitle = addslashes($sDialogTitle);
  313. $sOkButtonLabel = addslashes($sOkButtonLabel);
  314. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  315. $oPage->add("<div id=\"$sDialogId\">");
  316. if ($sIntroduction != null)
  317. {
  318. $oPage->add('<div class="ui-dialog-header">'.$sIntroduction.'</div>');
  319. }
  320. $this->Render($oPage);
  321. $oPage->add('</div>');
  322. $oPage->add_ready_script(
  323. <<<EOF
  324. $('#$sDialogId').dialog({
  325. height: 'auto',
  326. width: $iDialogWidth,
  327. modal: true,
  328. title: '$sDialogTitle',
  329. buttons: [
  330. { text: "$sOkButtonLabel", click: function() {
  331. var oForm = $(this).closest('.ui-dialog').find('form');
  332. oForm.submit();
  333. if (AnimateDlgButtons)
  334. {
  335. AnimateDlgButtons(this);
  336. }
  337. } },
  338. { text: "$sCancelButtonLabel", click: function() { KillAllMenus(); $(this).dialog( "close" ); $(this).remove(); } },
  339. ],
  340. close: function() { KillAllMenus(); $(this).remove(); }
  341. });
  342. var oForm = $('#$sDialogId form');
  343. var sFormId = oForm.attr('id');
  344. ValidateForm(sFormId, true);
  345. EOF
  346. );
  347. }
  348. public function ReadParams(&$aValues = array())
  349. {
  350. foreach($this->aFieldSets as $sLabel => $aFields)
  351. {
  352. foreach($aFields as $oField)
  353. {
  354. $oField->ReadParam($aValues);
  355. }
  356. }
  357. return $aValues;
  358. }
  359. public function SetPrefix($sPrefix)
  360. {
  361. $this->sFormPrefix = $sPrefix;
  362. }
  363. public function GetPrefix()
  364. {
  365. $sPrefix = '';
  366. if ($this->oParentForm != null)
  367. {
  368. $sPrefix = $this->oParentForm->GetPrefix();
  369. }
  370. return $sPrefix.$this->sFormPrefix;
  371. }
  372. public function SetSuffix($sSuffix)
  373. {
  374. $this->sFieldsSuffix = $sSuffix;
  375. }
  376. public function GetSuffix()
  377. {
  378. $sSuffix = '';
  379. if ($this->oParentForm != null)
  380. {
  381. $sSuffix = $this->oParentForm->GetSuffix();
  382. }
  383. return $sSuffix.$this->sFieldsSuffix;
  384. }
  385. public function SetReadOnly($bReadOnly = true)
  386. {
  387. $this->bReadOnly = $bReadOnly;
  388. }
  389. public function IsReadOnly()
  390. {
  391. if ($this->oParentForm == null)
  392. {
  393. return $this->bReadOnly;
  394. }
  395. else
  396. {
  397. return $this->oParentForm->IsReadOnly();
  398. }
  399. }
  400. public function SetParamsContainer($sParamsContainer)
  401. {
  402. $this->sParamsContainer = $sParamsContainer;
  403. }
  404. public function GetParamsContainer()
  405. {
  406. if ($this->oParentForm == null)
  407. {
  408. return $this->sParamsContainer;
  409. }
  410. else
  411. {
  412. return $this->oParentForm->GetParamsContainer();
  413. }
  414. }
  415. public function SetParentForm($oParentForm)
  416. {
  417. $this->oParentForm = $oParentForm;
  418. }
  419. public function SetDefaultValues($aDefaultValues)
  420. {
  421. if (!is_array($aDefaultValues)) return;
  422. foreach($this->aFieldSets as $sLabel => $aFields)
  423. {
  424. foreach($aFields as $oField)
  425. {
  426. $oField->SetDefaultValueFrom($aDefaultValues);
  427. }
  428. }
  429. }
  430. public function GetDefaultValues()
  431. {
  432. return $this->aDefaultValues;
  433. }
  434. public function GetParentForm()
  435. {
  436. return $this->oParentForm;
  437. }
  438. public function SetDisplayed($bDisplayed)
  439. {
  440. $this->bDisplayed = $bDisplayed;
  441. }
  442. public function IsDisplayed()
  443. {
  444. if ($this->oParentForm == null)
  445. {
  446. return $this->bDisplayed;
  447. }
  448. else
  449. {
  450. return ($this->bDisplayed && $this->oParentForm->IsDisplayed());
  451. }
  452. }
  453. public function AddScript($sScript)
  454. {
  455. $this->sScript .= $sScript;
  456. }
  457. public function AddReadyScript($sScript)
  458. {
  459. $this->sReadyScript .= $sScript;
  460. }
  461. public function GetFieldId($sCode)
  462. {
  463. return $this->GetPrefix().'attr_'.$sCode;
  464. }
  465. public function GetFieldName($sCode)
  466. {
  467. return 'attr_'.$sCode.$this->GetSuffix();
  468. }
  469. public function GetParamName($sCode)
  470. {
  471. return 'attr_'.$sCode.$this->GetSuffix();
  472. }
  473. public function GetValidationArea($sId, $sContent = '')
  474. {
  475. return "<span style=\"display:inline-block;width:20px;\" id=\"v_{$sId}\"><span class=\"ui-icon ui-icon-alert\"></span>$sContent</span>";
  476. }
  477. public function GetAsyncActionClass()
  478. {
  479. return $this->sAsyncActionClass;
  480. }
  481. public function FindField($sFieldCode)
  482. {
  483. $oFoundField = false;
  484. foreach($this->aFieldSets as $sLabel => $aFields)
  485. {
  486. foreach($aFields as $oField)
  487. {
  488. $oFoundField = $oField->FindField($sFieldCode);
  489. if ($oFoundField !== false) break;
  490. }
  491. if ($oFoundField !== false) break;
  492. }
  493. return $oFoundField;
  494. }
  495. }
  496. class DesignerTabularForm extends DesignerForm
  497. {
  498. protected $aTable;
  499. public function __construct()
  500. {
  501. parent::__construct();
  502. $this->aTable = array();
  503. }
  504. public function AddRow($aRow)
  505. {
  506. $this->aTable[] = $aRow;
  507. }
  508. public function RenderAsPropertySheet($oP, $bReturnHTML = false, $sNotifyParentSelector = null)
  509. {
  510. return $this->Render($oP, $bReturnHTML);
  511. }
  512. public function Render($oP, $bReturnHTML = false)
  513. {
  514. $sReturn = '';
  515. if ($this->oParentForm == null)
  516. {
  517. $sFormId = $this->sFormId;
  518. $sReturn = '<form id="'.$sFormId.'">';
  519. }
  520. else
  521. {
  522. $sFormId = $this->oParentForm->sFormId;
  523. }
  524. $sHiddenFields = '';
  525. $sReturn .= '<table style="width:100%">';
  526. foreach($this->aTable as $aRow)
  527. {
  528. $sReturn .= '<tr>';
  529. foreach($aRow as $field)
  530. {
  531. if (!is_object($field))
  532. {
  533. // Shortcut: pass a string for a cell containing just a label
  534. $sReturn .= '<td>'.$field.'</td>';
  535. }
  536. else
  537. {
  538. $field->SetForm($this);
  539. $aFieldData = $field->Render($oP, $sFormId);
  540. if ($field->IsVisible())
  541. {
  542. // put the label and value separated by a non-breaking space if needed
  543. $aData = array();
  544. foreach(array('label', 'value') as $sCode )
  545. {
  546. if ($aFieldData[$sCode] != '')
  547. {
  548. $aData[] = $aFieldData[$sCode];
  549. }
  550. }
  551. $sReturn .= '<td>'.implode('&nbsp;', $aData).'</td>';
  552. }
  553. else
  554. {
  555. $sHiddenFields .= $aRow['value'];
  556. }
  557. }
  558. }
  559. $sReturn .= '</tr>';
  560. }
  561. $sReturn .= '</table>';
  562. $sReturn .= $sHiddenFields;
  563. if($this->sScript != '')
  564. {
  565. $oP->add_script($this->sScript);
  566. }
  567. if($this->sReadyScript != '')
  568. {
  569. $oP->add_ready_script($this->sReadyScript);
  570. }
  571. if ($bReturnHTML)
  572. {
  573. return $sReturn;
  574. }
  575. else
  576. {
  577. $oP->add($sReturn);
  578. }
  579. }
  580. public function ReadParams(&$aValues = array())
  581. {
  582. foreach($this->aTable as $aRow)
  583. {
  584. foreach($aRow as $field)
  585. {
  586. if (is_object($field))
  587. {
  588. $field->SetForm($this);
  589. $field->ReadParam($aValues);
  590. }
  591. }
  592. }
  593. return $aValues;
  594. }
  595. }
  596. class DesignerFormField
  597. {
  598. protected $sLabel;
  599. protected $sCode;
  600. protected $defaultValue;
  601. protected $oForm;
  602. protected $bMandatory;
  603. protected $bReadOnly;
  604. protected $bAutoApply;
  605. protected $aCSSClasses;
  606. protected $bDisplayed;
  607. public function __construct($sCode, $sLabel, $defaultValue)
  608. {
  609. $this->sLabel = $sLabel;
  610. $this->sCode = $sCode;
  611. $this->defaultValue = $defaultValue;
  612. $this->bMandatory = false;
  613. $this->bReadOnly = false;
  614. $this->bAutoApply = false;
  615. $this->aCSSClasses = array();
  616. $this->bDisplayed = true;
  617. }
  618. public function GetCode()
  619. {
  620. return $this->sCode;
  621. }
  622. public function SetForm($oForm)
  623. {
  624. $this->oForm = $oForm;
  625. }
  626. public function SetMandatory($bMandatory = true)
  627. {
  628. $this->bMandatory = $bMandatory;
  629. }
  630. public function SetReadOnly($bReadOnly = true)
  631. {
  632. $this->bReadOnly = $bReadOnly;
  633. }
  634. public function IsReadOnly()
  635. {
  636. return ($this->oForm->IsReadOnly() || $this->bReadOnly);
  637. }
  638. public function SetAutoApply($bAutoApply)
  639. {
  640. $this->bAutoApply = $bAutoApply;
  641. }
  642. public function IsAutoApply()
  643. {
  644. return $this->bAutoApply;
  645. }
  646. public function SetDisplayed($bDisplayed)
  647. {
  648. $this->bDisplayed = $bDisplayed;
  649. }
  650. public function IsDisplayed()
  651. {
  652. return $this->bDisplayed;
  653. }
  654. public function GetFieldId()
  655. {
  656. return $this->oForm->GetFieldId($this->sCode);
  657. }
  658. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  659. {
  660. $sId = $this->oForm->GetFieldId($this->sCode);
  661. $sName = $this->oForm->GetFieldName($this->sCode);
  662. return array('label' => $this->sLabel, 'value' => "<input type=\"text\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  663. }
  664. public function ReadParam(&$aValues)
  665. {
  666. if ($this->IsReadOnly())
  667. {
  668. $aValues[$this->sCode] = $this->defaultValue;
  669. }
  670. else
  671. {
  672. if ($this->oForm->GetParamsContainer() != '')
  673. {
  674. $aParams = utils::ReadParam($this->oForm->GetParamsContainer(), array(), false, 'raw_data');
  675. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  676. {
  677. $aValues[$this->sCode] = $aParams[$this->oForm->GetParamName($this->sCode)];
  678. }
  679. else
  680. {
  681. $aValues[$this->sCode] = $this->defaultValue;
  682. }
  683. }
  684. else
  685. {
  686. $aValues[$this->sCode] = utils::ReadParam($this->oForm->GetParamName($this->sCode), $this->defaultValue, false, 'raw_data');
  687. }
  688. }
  689. }
  690. public function IsVisible()
  691. {
  692. return true;
  693. }
  694. public function AddCSSClass($sCSSClass)
  695. {
  696. $this->aCSSClasses[] = $sCSSClass;
  697. }
  698. /**
  699. * A way to set/change the default value after constructing the field
  700. */
  701. public function SetDefaultValueFrom($aAllDefaultValue)
  702. {
  703. if (array_key_exists($this->GetCode(), $aAllDefaultValue))
  704. {
  705. $this->defaultValue = $aAllDefaultValue[$this->GetCode()];
  706. }
  707. }
  708. public function FindField($sFieldCode)
  709. {
  710. if ($this->sCode == $sFieldCode)
  711. {
  712. return $this;
  713. }
  714. return false;
  715. }
  716. public function GetHandlerEquals()
  717. {
  718. return 'null';
  719. }
  720. public function GetHandlerGetValue()
  721. {
  722. return 'null';
  723. }
  724. }
  725. class DesignerLabelField extends DesignerFormField
  726. {
  727. protected $sDescription;
  728. public function __construct($sLabel, $sDescription)
  729. {
  730. parent::__construct('', $sLabel, '');
  731. $this->sDescription = $sDescription;
  732. }
  733. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  734. {
  735. $sId = $this->oForm->GetFieldId($this->sCode);
  736. $sName = $this->oForm->GetFieldName($this->sCode);
  737. return array('label' => $this->sLabel, 'value' => $this->sDescription);
  738. }
  739. public function ReadParam(&$aValues)
  740. {
  741. }
  742. public function IsVisible()
  743. {
  744. return true;
  745. }
  746. }
  747. class DesignerTextField extends DesignerFormField
  748. {
  749. protected $sValidationPattern;
  750. protected $aForbiddenValues;
  751. protected $sExplainForbiddenValues;
  752. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  753. {
  754. parent::__construct($sCode, $sLabel, $defaultValue);
  755. $this->sValidationPattern = '';
  756. $this->aForbiddenValues = null;
  757. $this->sExplainForbiddenValues = null;
  758. }
  759. public function SetValidationPattern($sValidationPattern)
  760. {
  761. $this->sValidationPattern = $sValidationPattern;
  762. }
  763. public function SetForbiddenValues($aValues, $sExplain)
  764. {
  765. $this->aForbiddenValues = $aValues;
  766. $iDefaultKey = array_search($this->defaultValue, $this->aForbiddenValues);
  767. if ($iDefaultKey !== false)
  768. {
  769. // The default (current) value is always allowed...
  770. unset($this->aForbiddenValues[$iDefaultKey]);
  771. }
  772. $this->sExplainForbiddenValues = $sExplain;
  773. }
  774. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  775. {
  776. $sId = $this->oForm->GetFieldId($this->sCode);
  777. $sName = $this->oForm->GetFieldName($this->sCode);
  778. if ($this->IsReadOnly())
  779. {
  780. $sHtmlValue = "<span>".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  781. }
  782. else
  783. {
  784. $sPattern = addslashes($this->sValidationPattern);
  785. if (is_array($this->aForbiddenValues))
  786. {
  787. $sForbiddenValues = json_encode($this->aForbiddenValues);
  788. $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues);
  789. }
  790. else
  791. {
  792. $sForbiddenValues = 'null';
  793. $sExplainForbiddenValues = 'null';
  794. }
  795. $sMandatory = $this->bMandatory ? 'true' : 'false';
  796. $oP->add_ready_script(
  797. <<<EOF
  798. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId', $sForbiddenValues, '$sExplainForbiddenValues'); } );
  799. {
  800. var myTimer = null;
  801. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  802. }
  803. EOF
  804. );
  805. $sCSSClasses = '';
  806. if (count($this->aCSSClasses) > 0)
  807. {
  808. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  809. }
  810. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  811. }
  812. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  813. }
  814. public function ReadParam(&$aValues)
  815. {
  816. parent::ReadParam($aValues);
  817. if (($this->sValidationPattern != '') &&(!preg_match('/'.$this->sValidationPattern.'/', $aValues[$this->sCode])) )
  818. {
  819. $aValues[$this->sCode] = $this->defaultValue;
  820. }
  821. else if(($this->aForbiddenValues != null) && in_array($aValues[$this->sCode], $this->aForbiddenValues))
  822. {
  823. // Reject the value...
  824. $aValues[$this->sCode] = $this->defaultValue;
  825. }
  826. }
  827. }
  828. class DesignerLongTextField extends DesignerTextField
  829. {
  830. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  831. {
  832. $sId = $this->oForm->GetFieldId($this->sCode);
  833. $sName = $this->oForm->GetFieldName($this->sCode);
  834. $sPattern = addslashes($this->sValidationPattern);
  835. if (is_array($this->aForbiddenValues))
  836. {
  837. $sForbiddenValues = json_encode($this->aForbiddenValues);
  838. $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues);
  839. }
  840. else
  841. {
  842. $sForbiddenValues = 'null';
  843. $sExplainForbiddenValues = 'null';
  844. }
  845. $sMandatory = $this->bMandatory ? 'true' : 'false';
  846. $sReadOnly = $this->IsReadOnly() ? 'readonly' : '';
  847. $oP->add_ready_script(
  848. <<<EOF
  849. $('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId', $sForbiddenValues, '$sExplainForbiddenValues'); } );
  850. {
  851. var myTimer = null;
  852. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  853. }
  854. EOF
  855. );
  856. $sCSSClasses = '';
  857. if (count($this->aCSSClasses) > 0)
  858. {
  859. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  860. }
  861. return array('label' => $this->sLabel, 'value' => "<textarea $sCSSClasses id=\"$sId\" $sReadOnly name=\"$sName\">".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."</textarea>");
  862. }
  863. }
  864. class DesignerIntegerField extends DesignerFormField
  865. {
  866. protected $iMin; // Lower boundary, inclusive
  867. protected $iMax; // Higher boundary, inclusive
  868. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  869. {
  870. parent::__construct($sCode, $sLabel, $defaultValue);
  871. $this->iMin = 0; // Positive integer is the default
  872. $this->iMax = null;
  873. }
  874. public function SetBoundaries($iMin = null, $iMax = null)
  875. {
  876. $this->iMin = $iMin;
  877. $this->iMax = $iMax;
  878. }
  879. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  880. {
  881. $sId = $this->oForm->GetFieldId($this->sCode);
  882. $sName = $this->oForm->GetFieldName($this->sCode);
  883. if ($this->IsReadOnly())
  884. {
  885. $sHtmlValue = "<span>".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  886. }
  887. else
  888. {
  889. $sMin = json_encode($this->iMin);
  890. $sMax = json_encode($this->iMax);
  891. $sMandatory = $this->bMandatory ? 'true' : 'false';
  892. $oP->add_ready_script(
  893. <<<EOF
  894. $('#$sId').bind('change keyup validate', function() { ValidateInteger('$sId', $sMandatory, '$sFormId', $sMin, $sMax); } );
  895. {
  896. var myTimer = null;
  897. $('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
  898. }
  899. EOF
  900. );
  901. $sCSSClasses = '';
  902. if (count($this->aCSSClasses) > 0)
  903. {
  904. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  905. }
  906. $sHtmlValue = "<input type=\"text\" $sCSSClasses id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">";
  907. }
  908. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  909. }
  910. public function ReadParam(&$aValues)
  911. {
  912. parent::ReadParam($aValues);
  913. if (!is_null($this->iMin) && ($aValues[$this->sCode] < $this->iMin))
  914. {
  915. // Reject the value...
  916. $aValues[$this->sCode] = $this->defaultValue;
  917. }
  918. if (!is_null($this->iMax) && ($aValues[$this->sCode] > $this->iMax))
  919. {
  920. // Reject the value...
  921. $aValues[$this->sCode] = $this->defaultValue;
  922. }
  923. }
  924. }
  925. class DesignerComboField extends DesignerFormField
  926. {
  927. protected $aAllowedValues;
  928. protected $bMultipleSelection;
  929. protected $bOtherChoices;
  930. protected $sNullLabel;
  931. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  932. {
  933. parent::__construct($sCode, $sLabel, $defaultValue);
  934. $this->aAllowedValues = array();
  935. $this->bMultipleSelection = false;
  936. $this->bOtherChoices = false;
  937. $this->sNullLabel = Dict::S('UI:SelectOne');
  938. $this->bAutoApply = true;
  939. }
  940. public function SetAllowedValues($aAllowedValues)
  941. {
  942. $this->aAllowedValues = $aAllowedValues;
  943. }
  944. public function MultipleSelection($bMultipleSelection = true)
  945. {
  946. $this->bMultipleSelection = $bMultipleSelection;
  947. }
  948. public function OtherChoices($bOtherChoices = true)
  949. {
  950. $this->bOtherChoices = $bOtherChoices;
  951. }
  952. /**
  953. * An empty label will disable the default empty value
  954. */
  955. public function SetNullLabel($sLabel)
  956. {
  957. $this->sNullLabel = $sLabel;
  958. }
  959. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  960. {
  961. $sId = $this->oForm->GetFieldId($this->sCode);
  962. $sName = $this->oForm->GetFieldName($this->sCode);
  963. $sChecked = $this->defaultValue ? 'checked' : '';
  964. $sMandatory = $this->bMandatory ? 'true' : 'false';
  965. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  966. $sCSSClasses = '';
  967. if (count($this->aCSSClasses) > 0)
  968. {
  969. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  970. }
  971. if ($this->IsReadOnly())
  972. {
  973. $aSelected = array();
  974. $aHiddenValues = array();
  975. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  976. {
  977. if ($this->bMultipleSelection)
  978. {
  979. if(in_array($sKey, $this->defaultValue))
  980. {
  981. $aSelected[] = $sDisplayValue;
  982. $aHiddenValues[] = "<input type=\"hidden\" name=\"{$sName}[]\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  983. }
  984. }
  985. else
  986. {
  987. if ($sKey == $this->defaultValue)
  988. {
  989. $aSelected[] = $sDisplayValue;
  990. $aHiddenValues[] = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\"/>";
  991. }
  992. }
  993. }
  994. $sHtml = "<span $sCSSClasses>".htmlentities(implode(', ', $aSelected), ENT_QUOTES, 'UTF-8').implode($aHiddenValues)."</span>";
  995. }
  996. else
  997. {
  998. if ($this->bMultipleSelection)
  999. {
  1000. $sHtml = "<select $sCSSClasses multiple size=\"8\"id=\"$sId\" name=\"$sName\">";
  1001. }
  1002. else
  1003. {
  1004. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\">";
  1005. if ($this->sNullLabel != '')
  1006. {
  1007. $sHtml .= "<option value=\"\">".$this->sNullLabel."</option>";
  1008. }
  1009. }
  1010. foreach($this->aAllowedValues as $sKey => $sDisplayValue)
  1011. {
  1012. if ($this->bMultipleSelection)
  1013. {
  1014. $sSelected = in_array($sKey, $this->defaultValue) ? 'selected' : '';
  1015. }
  1016. else
  1017. {
  1018. $sSelected = ($sKey == $this->defaultValue) ? 'selected' : '';
  1019. }
  1020. // Quick and dirty: display the menu parents as a tree
  1021. $sHtmlValue = str_replace(' ', '&nbsp;', htmlentities($sDisplayValue, ENT_QUOTES, 'UTF-8'));
  1022. $sHtml .= "<option value=\"".htmlentities($sKey, ENT_QUOTES, 'UTF-8')."\" $sSelected>$sHtmlValue</option>";
  1023. }
  1024. $sHtml .= "</select>";
  1025. if ($this->bOtherChoices)
  1026. {
  1027. $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"/>';
  1028. }
  1029. $oP->add_ready_script(
  1030. <<<EOF
  1031. $('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId', null, null); } );
  1032. EOF
  1033. );
  1034. }
  1035. return array('label' => $this->sLabel, 'value' => $sHtml);
  1036. }
  1037. public function ReadParam(&$aValues)
  1038. {
  1039. parent::ReadParam($aValues);
  1040. if ($aValues[$this->sCode] == 'null')
  1041. {
  1042. $aValues[$this->sCode] = array();
  1043. }
  1044. }
  1045. }
  1046. class DesignerBooleanField extends DesignerFormField
  1047. {
  1048. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1049. {
  1050. parent::__construct($sCode, $sLabel, $defaultValue);
  1051. $this->bAutoApply = true;
  1052. }
  1053. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1054. {
  1055. $sId = $this->oForm->GetFieldId($this->sCode);
  1056. $sName = $this->oForm->GetFieldName($this->sCode);
  1057. $sChecked = $this->defaultValue ? 'checked' : '';
  1058. if ($this->IsReadOnly())
  1059. {
  1060. $sLabel = $this->defaultValue ? Dict::S('UI:UserManagement:ActionAllowed:Yes') : Dict::S('UI:UserManagement:ActionAllowed:No'); //TODO use our own yes/no translations
  1061. $sHtmlValue = "<span>".htmlentities($sLabel)."<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\"/></span>";
  1062. }
  1063. else
  1064. {
  1065. $sCSSClasses = '';
  1066. if (count($this->aCSSClasses) > 0)
  1067. {
  1068. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1069. }
  1070. $sHtmlValue = "<input $sCSSClasses type=\"checkbox\" $sChecked id=\"$sId\" name=\"$sName\" value=\"true\">";
  1071. }
  1072. return array('label' => $this->sLabel, 'value' => $sHtmlValue);
  1073. }
  1074. public function ReadParam(&$aValues)
  1075. {
  1076. if ($this->IsReadOnly())
  1077. {
  1078. $aValues[$this->sCode] = $this->defaultValue;
  1079. }
  1080. else
  1081. {
  1082. $sParamsContainer = $this->oForm->GetParamsContainer();
  1083. if ($sParamsContainer != '')
  1084. {
  1085. $aParams = utils::ReadParam($sParamsContainer, array(), false, 'raw_data');
  1086. if (array_key_exists($this->oForm->GetParamName($this->sCode), $aParams))
  1087. {
  1088. $sValue = $aParams[$this->oForm->GetParamName($this->sCode)];
  1089. }
  1090. else
  1091. {
  1092. $sValue = 'false';
  1093. }
  1094. }
  1095. else
  1096. {
  1097. $sValue = utils::ReadParam($this->oForm->GetParamName($this->sCode), 'false', false, 'raw_data');
  1098. }
  1099. $aValues[$this->sCode] = ($sValue == 'true');
  1100. }
  1101. }
  1102. }
  1103. class DesignerHiddenField extends DesignerFormField
  1104. {
  1105. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1106. {
  1107. parent::__construct($sCode, $sLabel, $defaultValue);
  1108. }
  1109. public function IsVisible()
  1110. {
  1111. return false;
  1112. }
  1113. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1114. {
  1115. $sId = $this->oForm->GetFieldId($this->sCode);
  1116. $sName = $this->oForm->GetFieldName($this->sCode);
  1117. $sChecked = $this->defaultValue ? 'checked' : '';
  1118. return array('label' =>'', 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1119. }
  1120. }
  1121. class DesignerIconSelectionField extends DesignerFormField
  1122. {
  1123. protected $sUploadUrl;
  1124. protected $aAllowedValues;
  1125. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1126. {
  1127. parent::__construct($sCode, $sLabel, $defaultValue);
  1128. $this->bAutoApply = true;
  1129. $this->sUploadUrl = null;
  1130. }
  1131. public function SetAllowedValues($aAllowedValues)
  1132. {
  1133. $this->aAllowedValues = $aAllowedValues;
  1134. }
  1135. public function EnableUpload($sIconUploadUrl)
  1136. {
  1137. $this->sUploadUrl = $sIconUploadUrl;
  1138. }
  1139. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1140. {
  1141. $sId = $this->oForm->GetFieldId($this->sCode);
  1142. $sName = $this->oForm->GetFieldName($this->sCode);
  1143. $idx = 0;
  1144. foreach($this->aAllowedValues as $index => $aValue)
  1145. {
  1146. if ($aValue['value'] == $this->defaultValue)
  1147. {
  1148. $idx = $index;
  1149. break;
  1150. }
  1151. }
  1152. $sJSItems = json_encode($this->aAllowedValues);
  1153. $sPostUploadTo = ($this->sUploadUrl == null) ? 'null' : "'{$this->sUploadUrl}'";
  1154. if (!$this->IsReadOnly())
  1155. {
  1156. $sValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"{$this->defaultValue}\"/>";
  1157. $oP->add_ready_script(
  1158. <<<EOF
  1159. $('#$sId').icon_select({current_idx: $idx, items: $sJSItems, post_upload_to: $sPostUploadTo});
  1160. EOF
  1161. );
  1162. }
  1163. else
  1164. {
  1165. $sValue = '<img src="'.$this->MakeFileUrl($this->defaultValue).'" />';
  1166. }
  1167. $sReadOnly = $this->IsReadOnly() ? 'disabled' : '';
  1168. return array('label' => $this->sLabel, 'value' => $sValue);
  1169. }
  1170. }
  1171. class RunTimeIconSelectionField extends DesignerIconSelectionField
  1172. {
  1173. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1174. {
  1175. parent::__construct($sCode, $sLabel, $defaultValue);
  1176. $aAllIcons = self::FindIconsOnDisk(APPROOT.'env-'.utils::GetCurrentEnvironment());
  1177. ksort($aAllIcons);
  1178. $aValues = array();
  1179. foreach($aAllIcons as $sFilePath)
  1180. {
  1181. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  1182. }
  1183. $this->SetAllowedValues($aValues);
  1184. }
  1185. static protected function FindIconsOnDisk($sBaseDir, $sDir = '')
  1186. {
  1187. $aResult = array();
  1188. // Populate automatically the list of icon files
  1189. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  1190. {
  1191. while (($sFile = readdir($hDir)) !== false)
  1192. {
  1193. $aMatches = array();
  1194. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  1195. {
  1196. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  1197. $aResult = array_merge($aResult, self::FindIconsOnDisk($sBaseDir, $sDirSubPath));
  1198. }
  1199. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  1200. {
  1201. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  1202. }
  1203. }
  1204. closedir($hDir);
  1205. }
  1206. return $aResult;
  1207. }
  1208. public function ValueFromDOMNode($oDOMNode)
  1209. {
  1210. return $oDOMNode->textContent;
  1211. }
  1212. public function ValueToDOMNode($oDOMNode, $value)
  1213. {
  1214. $oTextNode = $oDOMNode->ownerDocument->createTextNode($value);
  1215. $oDOMNode->appendChild($oTextNode);
  1216. }
  1217. public function MakeFileUrl($value)
  1218. {
  1219. return utils::GetAbsoluteUrlModulesRoot().$value;
  1220. }
  1221. public function GetDefaultValue($sClass = 'Contact')
  1222. {
  1223. $sIconPath = MetaModel::GetClassIcon($sClass, false);
  1224. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIconPath);
  1225. return $sIcon;
  1226. }
  1227. }
  1228. class DesignerSortableField extends DesignerFormField
  1229. {
  1230. protected $aAllowedValues;
  1231. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1232. {
  1233. parent::__construct($sCode, $sLabel, $defaultValue);
  1234. $this->aAllowedValues = array();
  1235. }
  1236. public function SetAllowedValues($aAllowedValues)
  1237. {
  1238. $this->aAllowedValues = $aAllowedValues;
  1239. }
  1240. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1241. {
  1242. $bOpen = false;
  1243. $sId = $this->oForm->GetFieldId($this->sCode);
  1244. $sName = $this->oForm->GetFieldName($this->sCode);
  1245. $sReadOnly = $this->IsReadOnly() ? 'readonly="readonly"' : '';
  1246. $aResult = array('label' => $this->sLabel, 'value' => "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" $sReadOnly value=\"".htmlentities($this->defaultValue, ENT_QUOTES, 'UTF-8')."\">");
  1247. $sJSFields = json_encode(array_keys($this->aAllowedValues));
  1248. $oP->add_ready_script(
  1249. "$('#$sId').sortable_field({aAvailableFields: $sJSFields});"
  1250. );
  1251. return $aResult;
  1252. }
  1253. }
  1254. class DesignerFormSelectorField extends DesignerFormField
  1255. {
  1256. protected $aSubForms;
  1257. protected $defaultRealValue; // What's stored as default value is actually the index
  1258. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1259. {
  1260. parent::__construct($sCode, $sLabel, 0);
  1261. $this->defaultRealValue = $defaultValue;
  1262. $this->aSubForms = array();
  1263. }
  1264. public function AddSubForm($oSubForm, $sLabel, $sValue)
  1265. {
  1266. $idx = count($this->aSubForms);
  1267. $this->aSubForms[] = array('form' => $oSubForm, 'label' => $sLabel, 'value' => $sValue);
  1268. if ($sValue == $this->defaultRealValue)
  1269. {
  1270. // Store the index of the selected/default form
  1271. $this->defaultValue = count($this->aSubForms) - 1;
  1272. }
  1273. }
  1274. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1275. {
  1276. $sId = $this->oForm->GetFieldId($this->sCode);
  1277. $sName = $this->oForm->GetFieldName($this->sCode);
  1278. $sReadOnly = $this->IsReadOnly() ? 'disabled="disabled"' : '';
  1279. $this->aCSSClasses[] = 'formSelector';
  1280. $sCSSClasses = '';
  1281. if (count($this->aCSSClasses) > 0)
  1282. {
  1283. $sCSSClasses = 'class="'.implode(' ', $this->aCSSClasses).'"';
  1284. }
  1285. if ($this->IsReadOnly())
  1286. {
  1287. $aSelected = array();
  1288. $aHiddenValues = array();
  1289. $sDisplayValue = '';
  1290. $sHiddenValue = '';
  1291. foreach($this->aSubForms as $iKey => $aFormData)
  1292. {
  1293. if ($iKey == $this->defaultValue) // Default value is actually the index
  1294. {
  1295. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');
  1296. $sHiddenValue = "<input type=\"hidden\" id=\"$sId\" name=\"$sName\" value=\"".htmlentities($iKey, ENT_QUOTES, 'UTF-8')."\"/>";
  1297. break;
  1298. }
  1299. }
  1300. $sHtml = "<span $sCSSClasses>".$sDisplayValue.$sHiddenValue."</span>";
  1301. }
  1302. else
  1303. {
  1304. $sHtml = "<select $sCSSClasses id=\"$sId\" name=\"$sName\" $sReadOnly>";
  1305. foreach($this->aSubForms as $iKey => $aFormData)
  1306. {
  1307. $sDisplayValue = htmlentities($aFormData['label'], ENT_QUOTES, 'UTF-8');;
  1308. $sSelected = ($iKey == $this->defaultValue) ? 'selected' : '';
  1309. $sHtml .= "<option value=\"$iKey\" $sSelected>".$sDisplayValue."</option>";
  1310. }
  1311. $sHtml .= "</select>";
  1312. }
  1313. if ($sRenderMode == 'property')
  1314. {
  1315. $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>';
  1316. }
  1317. foreach($this->aSubForms as $sKey => $aFormData)
  1318. {
  1319. $sId = $this->oForm->GetFieldId($this->sCode);
  1320. $sStyle = (($sKey == $this->defaultValue) && $this->oForm->IsDisplayed()) ? '' : 'style="display:none"';
  1321. $oSubForm = $aFormData['form'];
  1322. $oSubForm->SetParentForm($this->oForm);
  1323. $oSubForm->CopySubmitParams($this->oForm);
  1324. $oSubForm->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1325. if ($sRenderMode == 'property')
  1326. {
  1327. // Note: Managing the visibility of nested subforms had several implications
  1328. // 1) Attributes are displayed in a table and we have to group them in as many tbodys as necessary to hide/show the various options depending on the current selection
  1329. // 2) It is not possible to nest tbody tags. Therefore, it is not possible to manage the visibility the same way as it is done for the dialog mode (using nested divs).
  1330. // The div hierarchy has been emulated by adding attributes to the tbody tags:
  1331. // - data-selector : uniquely identifies the DesignerFormSelectorField that has an impact on the visibility of the node
  1332. // - data-path : uniquely identifies the combination of users choices that must be made to show the node
  1333. // - data-state : records the state, depending on the user choice on the FormSelectorField just above the node, but indepentantly from the visibility in the page (can be visible in the form itself being in a hidden form)
  1334. // Then a series of actions are performed to hide and show the relevant nodes, depending on the user choice
  1335. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1336. $oSubForm->SetHierarchyParent($sSelector);
  1337. $sPath = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix().'-'.$sKey;
  1338. $oSubForm->SetHierarchyPath($sPath);
  1339. $oSubForm->SetDisplayed($sKey == $this->defaultValue);
  1340. $sState = ($sKey == $this->defaultValue) ? 'visible' : 'hidden';
  1341. //$sHtml .= "</tbody><tbody data-selector=\"$sSelector\" data-path=\"$sPath\" data-state=\"$sState\" $sStyle>";
  1342. $sHtml .= $oSubForm->RenderAsPropertySheet($oP, true);
  1343. $sState = $this->oForm->IsDisplayed() ? 'visible' : 'hidden';
  1344. $sParentStyle = '';
  1345. if ($oParent = $this->oForm->GetParentForm())
  1346. {
  1347. $sParentStyle = ($oParent->IsDisplayed()) ? '' : 'style="display:none"';
  1348. $sParentSelector = $oParent->GetHierarchyParent();
  1349. $sParentPath = $oParent->GetHierarchyPath();
  1350. }
  1351. else
  1352. {
  1353. $sParentSelector = '';
  1354. $sParentPath = '';
  1355. }
  1356. //$sHtml .= "</tbody><tbody data-selector=\"$sParentSelector\" data-path=\"$sParentPath\" data-state=\"$sState\" $sParentStyle>";
  1357. }
  1358. else
  1359. {
  1360. $sHtml .= "<div class=\"subform_{$sId} {$sId}_{$sKey}\" $sStyle>";
  1361. $sHtml .= $oSubForm->Render($oP, true);
  1362. $sHtml .= "</div>";
  1363. }
  1364. }
  1365. if ($sRenderMode == 'property')
  1366. {
  1367. $sSelector = $this->oForm->GetHierarchyPath().'/'.$this->sCode.$this->oForm->GetSuffix();
  1368. $oP->add_ready_script("InitFormSelectorField('$sId', '$sSelector');");
  1369. }
  1370. else
  1371. {
  1372. $oP->add_ready_script(
  1373. <<<EOF
  1374. $('#$sId').bind('change reverted', function() { $('.subform_{$sId}').hide(); $('.{$sId}_'+this.value).show(); } );
  1375. EOF
  1376. );
  1377. }
  1378. return array('label' => $this->sLabel, 'value' => $sHtml);
  1379. }
  1380. public function ReadParam(&$aValues)
  1381. {
  1382. parent::ReadParam($aValues);
  1383. $sKey = $aValues[$this->sCode];
  1384. $aValues[$this->sCode] = $this->aSubForms[$sKey]['value'];
  1385. $this->aSubForms[$sKey]['form']->SetPrefix($this->oForm->GetPrefix().$sKey.'_');
  1386. $this->aSubForms[$sKey]['form']->SetParentForm($this->oForm);
  1387. $this->aSubForms[$sKey]['form']->ReadParams($aValues);
  1388. }
  1389. public function SetDefaultValueFrom($aAllDefaultValues)
  1390. {
  1391. if (array_key_exists($this->GetCode(), $aAllDefaultValues))
  1392. {
  1393. $selectedValue = $aAllDefaultValues[$this->GetCode()];
  1394. foreach($this->aSubForms as $iKey => $aFormData)
  1395. {
  1396. $sId = $this->oForm->GetFieldId($this->sCode);
  1397. if ($selectedValue == $aFormData['value'])
  1398. {
  1399. $this->defaultValue =$iKey;
  1400. $aDefaultValues = $this->oForm->GetDefaultValues();
  1401. $oSubForm = $aFormData['form'];
  1402. $oSubForm->SetDefaultValues($aAllDefaultValues);
  1403. }
  1404. }
  1405. }
  1406. }
  1407. public function FindField($sFieldCode)
  1408. {
  1409. $oField = parent::FindField($sFieldCode);
  1410. if ($oField === false)
  1411. {
  1412. // Look in the subforms
  1413. foreach($this->aSubForms as $sKey => $aFormData)
  1414. {
  1415. $oSubForm = $aFormData['form'];
  1416. $oField = $oSubForm->FindField($sFieldCode);
  1417. if ($oField !== false)
  1418. {
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. return $oField;
  1424. }
  1425. }
  1426. class DesignerSubFormField extends DesignerFormField
  1427. {
  1428. protected $oSubForm;
  1429. public function __construct($sLabel, $oSubForm)
  1430. {
  1431. parent::__construct('', $sLabel, '');
  1432. $this->oSubForm = $oSubForm;
  1433. }
  1434. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1435. {
  1436. $this->oSubForm->SetParentForm($this->oForm);
  1437. $this->oSubForm->CopySubmitParams($this->oForm);
  1438. if ($sRenderMode == 'property')
  1439. {
  1440. $sHtml = $this->oSubForm->RenderAsPropertySheet($oP, true);
  1441. }
  1442. else
  1443. {
  1444. $sHtml = $this->oSubForm->Render($oP, true);
  1445. }
  1446. return array('label' => $this->sLabel, 'value' => $sHtml);
  1447. }
  1448. public function ReadParam(&$aValues)
  1449. {
  1450. $this->oSubForm->SetParentForm($this->oForm);
  1451. $this->oSubForm->ReadParams($aValues);
  1452. }
  1453. public function FindField($sFieldCode)
  1454. {
  1455. $oField = parent::FindField($sFieldCode);
  1456. if ($oField === false)
  1457. {
  1458. // Look in the subform
  1459. $oField = $this->oSubForm->FindField($sFieldCode);
  1460. }
  1461. return $oField;
  1462. }
  1463. }
  1464. class DesignerStaticTextField extends DesignerFormField
  1465. {
  1466. public function __construct($sCode, $sLabel = '', $defaultValue = '')
  1467. {
  1468. parent::__construct($sCode, $sLabel, $defaultValue);
  1469. }
  1470. public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
  1471. {
  1472. return array('label' => $this->sLabel, 'value' => $this->defaultValue);
  1473. }
  1474. }