bssimplefieldrenderer.class.inc.php 16 KB

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