bssimplefieldrenderer.class.inc.php 14 KB

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