bssimplefieldrenderer.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. // Copyright (C) 2010-2016 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. namespace Combodo\iTop\Renderer\Bootstrap\FieldRenderer;
  19. use \utils;
  20. use \Dict;
  21. use \UserRights;
  22. use \AttributeDateTime;
  23. use \InlineImage;
  24. use \Combodo\iTop\Renderer\FieldRenderer;
  25. use \Combodo\iTop\Renderer\RenderingOutput;
  26. use \Combodo\iTop\Form\Field\TextAreaField;
  27. /**
  28. * Description of BsSimpleFieldRenderer
  29. *
  30. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  31. */
  32. class BsSimpleFieldRenderer extends FieldRenderer
  33. {
  34. /**
  35. * Returns a RenderingOutput for the FieldRenderer's Field
  36. *
  37. * @return \Combodo\iTop\Renderer\RenderingOutput
  38. */
  39. public function Render()
  40. {
  41. $oOutput = new RenderingOutput();
  42. $sFieldClass = get_class($this->oField);
  43. $sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
  44. // Rendering field in edition mode
  45. if (!$this->oField->GetReadOnly() && !$this->oField->GetHidden())
  46. {
  47. switch ($sFieldClass)
  48. {
  49. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  50. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  51. if ($this->oField->GetLabel() !== '')
  52. {
  53. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  54. }
  55. $oOutput->AddHtml('<div class="help-block"></div>');
  56. $oOutput->AddHtml('<div class="input-group date" id="datepicker_' . $this->oField->GetGlobalId() . '">');
  57. $oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('" class="form-control" maxlength="255" />');
  58. $oOutput->AddHtml('<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>');
  59. $oOutput->AddHtml('</div>');
  60. $oOutput->AddHtml('</div>');
  61. $sJSFormat = json_encode($this->oField->GetJSDateTimeFormat());
  62. $oOutput->AddJs(
  63. <<<EOF
  64. $('#datepicker_{$this->oField->GetGlobalId()}').datetimepicker({format: $sJSFormat});
  65. EOF
  66. );
  67. break;
  68. case 'Combodo\\iTop\\Form\\Field\\PasswordField':
  69. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  70. if ($this->oField->GetLabel() !== '')
  71. {
  72. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  73. }
  74. $oOutput->AddHtml('<div class="help-block"></div>');
  75. $oOutput->AddHtml('<input type="password" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" maxlength="255" autocomplete="off" />');
  76. $oOutput->AddHtml('</div>');
  77. break;
  78. case 'Combodo\\iTop\\Form\\Field\\StringField':
  79. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  80. if ($this->oField->GetLabel() !== '')
  81. {
  82. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  83. }
  84. $oOutput->AddHtml('<div class="help-block"></div>');
  85. $oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" maxlength="255" />');
  86. $oOutput->AddHtml('</div>');
  87. break;
  88. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  89. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  90. $bRichEditor = ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML);
  91. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  92. if ($this->oField->GetLabel() !== '')
  93. {
  94. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  95. }
  96. $oOutput->AddHtml('<div class="help-block"></div>');
  97. // First the edition area
  98. $oOutput->AddHtml('<div>');
  99. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" class="form-control" rows="8">' . $this->oField->GetCurrentValue() . '</textarea>');
  100. $oOutput->AddHtml('</div>');
  101. // Then the previous entries if necessary
  102. if ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\CaseLogField')
  103. {
  104. $this->PreparingCaseLogEntries($oOutput);
  105. }
  106. $oOutput->AddHtml('</div>');
  107. // Some additional stuff if we are displaying it with a rich editor
  108. if ($bRichEditor)
  109. {
  110. $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  111. $oOutput->AddJs(
  112. <<<EOF
  113. $('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
  114. $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
  115. EOF
  116. );
  117. if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
  118. {
  119. $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
  120. }
  121. }
  122. break;
  123. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  124. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  125. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  126. if ($this->oField->GetLabel() !== '')
  127. {
  128. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  129. }
  130. $oOutput->AddHtml('<div class="help-block"></div>');
  131. $oOutput->AddHtml('<select id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" ' . ( ($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '' ) . ' class="form-control">');
  132. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  133. {
  134. // Note : The test is a double equal on purpose as the type of the value received from the XHR is not always the same as the type of the allowed values. (eg : string vs int)
  135. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  136. $oOutput->AddHtml('<option value="' . $sChoice . '" ' . $sSelectedAtt . ' >')->AddHtml($sLabel)->AddHtml('</option>');
  137. }
  138. $oOutput->AddHtml('</select>');
  139. $oOutput->AddHtml('</div>');
  140. break;
  141. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  142. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  143. $sFieldType = ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\RadioField') ? 'radio' : 'checkbox';
  144. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '" id="' . $this->oField->GetGlobalId() . '">');
  145. if ($this->oField->GetLabel() !== '')
  146. {
  147. $oOutput->AddHtml('<div><label class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label></div>');
  148. }
  149. $oOutput->AddHtml('<div class="help-block"></div>');
  150. $oOutput->AddHtml('<div class="btn-group" data-toggle="buttons">');
  151. $i = 0;
  152. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  153. {
  154. // Note : The test is a double equal on purpose as the type of the value received from the XHR is not always the same as the type of the allowed values. (eg : string vs int)
  155. $sCheckedAtt = ($this->oField->IsAmongValues($sChoice)) ? 'checked' : '';
  156. $sCheckedClass = ($this->oField->IsAmongValues($sChoice)) ? 'active' : '';
  157. $oOutput->AddHtml('<label class="btn btn-default ' . $sCheckedClass . '"><input type="' . $sFieldType . '" name="' . $this->oField->GetId() . '" id="' . $this->oField->GetId() . $i . '" value="' . $sChoice . '" ' . $sCheckedAtt . ' />' . $sLabel . '</label>');
  158. $i++;
  159. }
  160. $oOutput->AddHtml('</div>');
  161. $oOutput->AddHtml('</div>');
  162. break;
  163. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  164. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('"/>');
  165. break;
  166. }
  167. }
  168. // ... and in read-only mode (or hidden)
  169. else
  170. {
  171. // ... specific rendering for fields with multiple values
  172. if (($this->oField instanceof Combodo\iTop\Form\Field\MultipleChoicesField) && ($this->oField->GetMultipleValuesEnabled()))
  173. {
  174. // TODO
  175. }
  176. // ... clasic rendering for fields with only one value
  177. else
  178. {
  179. switch ($sFieldClass)
  180. {
  181. case 'Combodo\\iTop\\Form\\Field\\LabelField':
  182. $oOutput->AddHtml('<div class="form-group">');
  183. // Showing label / value only if read-only but not hidden
  184. if (!$this->oField->GetHidden())
  185. {
  186. if ($this->oField->GetLabel() !== '')
  187. {
  188. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  189. }
  190. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('</div>');
  191. }
  192. $oOutput->AddHtml('</div>');
  193. break;
  194. case 'Combodo\\iTop\\Form\\Field\\StringField':
  195. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  196. $bEncodeHtmlEntities = (($sFieldClass === 'Combodo\\iTop\\Form\\Field\\TextAreaField') && ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML)) ? false : true;
  197. $oOutput->AddHtml('<div class="form-group">');
  198. // Showing label / value only if read-only but not hidden
  199. if (!$this->oField->GetHidden())
  200. {
  201. if ($this->oField->GetLabel() !== '')
  202. {
  203. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  204. }
  205. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetCurrentValue(), $bEncodeHtmlEntities)->AddHtml('</div>');
  206. }
  207. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" />');
  208. $oOutput->AddHtml('</div>');
  209. break;
  210. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  211. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  212. if ($this->oField->GetLabel() !== '')
  213. {
  214. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  215. }
  216. // Entries if necessary
  217. $this->PreparingCaseLogEntries($oOutput);
  218. $oOutput->AddHtml('</div>');
  219. break;
  220. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  221. $oOutput->AddHtml('<div class="form-group">');
  222. // Showing label / value only if read-only but not hidden
  223. if (!$this->oField->GetHidden())
  224. {
  225. if ($this->oField->GetLabel() !== '')
  226. {
  227. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  228. }
  229. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('</div>');
  230. }
  231. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('" class="form-control" />');
  232. $oOutput->AddHtml('</div>');
  233. break;
  234. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  235. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  236. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  237. $aFieldChoices = $this->oField->GetChoices();
  238. $sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject');
  239. $oOutput->AddHtml('<div class="form-group">');
  240. // Showing label / value only if read-only but not hidden
  241. if (!$this->oField->GetHidden())
  242. {
  243. if ($this->oField->GetLabel() !== '')
  244. {
  245. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  246. }
  247. $oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
  248. }
  249. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
  250. $oOutput->AddHtml('</div>');
  251. break;
  252. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  253. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('"/>');
  254. break;
  255. }
  256. }
  257. }
  258. // JS FieldChange trigger (:input are not always at the same depth)
  259. switch ($sFieldClass)
  260. {
  261. case 'Combodo\\iTop\\Form\\Field\\PasswordField':
  262. case 'Combodo\\iTop\\Form\\Field\\StringField':
  263. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  264. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  265. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  266. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  267. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  268. $oOutput->AddJs(
  269. <<<EOF
  270. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  271. var me = this;
  272. $(this).closest(".field_set").trigger("field_change", {
  273. id: $(me).attr("id"),
  274. name: $(me).closest(".form_field").attr("data-field-id"),
  275. value: $(me).val()
  276. });
  277. });
  278. EOF
  279. );
  280. break;
  281. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  282. // We need the focusout event has the datepicker widget seems to override the change event
  283. $oOutput->AddJs(
  284. <<<EOF
  285. $("#{$this->oField->GetGlobalId()}").off("change keyup focusout").on("change keyup focusout", function(){
  286. var me = this;
  287. $(this).closest(".field_set").trigger("field_change", {
  288. id: $(me).attr("id"),
  289. name: $(me).closest(".form_field").attr("data-field-id"),
  290. value: $(me).val()
  291. });
  292. });
  293. EOF
  294. );
  295. break;
  296. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  297. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  298. $oOutput->AddJs(
  299. <<<EOF
  300. $("#{$this->oField->GetGlobalId()} input").off("change").on("change", function(){
  301. var me = this;
  302. $(this).closest(".field_set").trigger("field_change", {
  303. id: $(me).closest("#{$this->oField->GetGlobalId()}").attr("id"),
  304. name: $(me).attr("name"),
  305. value: $(me).val()
  306. });
  307. });
  308. EOF
  309. );
  310. break;
  311. }
  312. // JS Form field widget construct
  313. $aValidators = array();
  314. foreach ($this->oField->GetValidators() as $oValidator)
  315. {
  316. $aValidators[$oValidator::GetName()] = array(
  317. 'reg_exp' => $oValidator->GetRegExp(),
  318. 'message' => Dict::S($oValidator->GetErrorMessage())
  319. );
  320. }
  321. $sFormFieldOptions = json_encode(array(
  322. 'validators' => $aValidators
  323. ));
  324. switch ($sFieldClass)
  325. {
  326. case 'Combodo\\iTop\\Form\\Field\\PasswordField':
  327. case 'Combodo\\iTop\\Form\\Field\\StringField':
  328. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  329. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  330. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  331. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  332. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  333. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  334. $oOutput->AddJs(
  335. <<<EOF
  336. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field($sFormFieldOptions);
  337. EOF
  338. );
  339. break;
  340. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  341. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  342. $oOutput->AddJs(
  343. <<<EOF
  344. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field_html($sFormFieldOptions);
  345. EOF
  346. );
  347. // MagnificPopup on images
  348. $oOutput->AddJs(InlineImage::FixImagesWidth());
  349. break;
  350. }
  351. return $oOutput;
  352. }
  353. protected function PreparingCaseLogEntries(RenderingOutput &$oOutput)
  354. {
  355. $aEntries = $this->oField->GetEntries();
  356. if (count($aEntries) > 0)
  357. {
  358. $oOutput->AddHtml('<div>');
  359. for ($i = 0; $i < count($aEntries); $i++)
  360. {
  361. $sEntryDate = AttributeDateTime::GetFormat()->Format($aEntries[$i]['date']);
  362. $sEntryUser = $aEntries[$i]['user_login'];
  363. $sEntryHeader = Dict::Format('UI:CaseLog:Header_Date_UserName', $sEntryDate, $sEntryUser);
  364. // Only the last 2 entries are expanded by default
  365. $sEntryContentExpanded = ($i < 2) ? 'true' : 'false';
  366. $sEntryHeaderButtonClass = ($i < 2) ? '' : 'collapsed';
  367. $sEntryContentClass = ($i < 2) ? 'in' : '';
  368. $sEntryContentId = 'caselog_field_entry_content-' . $this->oField->GetGlobalId() . '-' . $i;
  369. // Note : We use CKEditor stylesheet to format this
  370. $oOutput->AddHtml(
  371. <<<EOF
  372. <div class="caselog_field_entry cke_inner">
  373. <div class="caselog_field_entry_header">
  374. {$sEntryHeader}
  375. <div class="pull-right">
  376. <span class="caselog_field_entry_button {$sEntryHeaderButtonClass}" data-toggle="collapse" href="#{$sEntryContentId}" aria-expanded="{$sEntryContentExpanded}" aria-controls="{$sEntryContentId}"></span>
  377. </div>
  378. </div>
  379. <div class="caselog_field_entry_content collapse {$sEntryContentClass}" id="{$sEntryContentId}">
  380. {$aEntries[$i]['message_html']}
  381. </div>
  382. </div>
  383. EOF
  384. );
  385. }
  386. $oOutput->AddHtml('</div>');
  387. }
  388. }
  389. }