bssimplefieldrenderer.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // TODO : Shouldn't we have a field type so we don't have to maintain FQN classname ?
  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\\StringField':
  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('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" maxlength="255" />');
  57. $oOutput->AddHtml('</div>');
  58. break;
  59. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  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. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" class="form-control" rows="8">' . $this->oField->GetCurrentValue() . '</textarea>');
  68. $oOutput->AddHtml('</div>');
  69. // Some additional stuff if we are displaying it with a rich editor
  70. if ($bRichEditor)
  71. {
  72. $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  73. $oOutput->AddJs(
  74. <<<EOF
  75. $('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
  76. $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
  77. EOF
  78. );
  79. if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
  80. {
  81. $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
  82. }
  83. }
  84. break;
  85. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  86. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  87. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  88. if ($this->oField->GetLabel() !== '')
  89. {
  90. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  91. }
  92. $oOutput->AddHtml('<div class="help-block"></div>');
  93. $oOutput->AddHtml('<select id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" ' . ( ($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '' ) . ' class="form-control">');
  94. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  95. {
  96. // 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)
  97. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  98. $oOutput->AddHtml('<option value="' . $sChoice . '" ' . $sSelectedAtt . ' >')->AddHtml($sLabel, true)->AddHtml('</option>');
  99. }
  100. $oOutput->AddHtml('</select>');
  101. $oOutput->AddHtml('</div>');
  102. break;
  103. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  104. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  105. $sFieldType = ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\RadioField') ? 'radio' : 'checkbox';
  106. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '" id="' . $this->oField->GetGlobalId() . '">');
  107. if ($this->oField->GetLabel() !== '')
  108. {
  109. $oOutput->AddHtml('<div><label class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label></div>');
  110. }
  111. $oOutput->AddHtml('<div class="help-block"></div>');
  112. $oOutput->AddHtml('<div class="btn-group" data-toggle="buttons">');
  113. $i = 0;
  114. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  115. {
  116. // 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)
  117. $sCheckedAtt = ($this->oField->IsAmongValues($sChoice)) ? 'checked' : '';
  118. $sCheckedClass = ($this->oField->IsAmongValues($sChoice)) ? 'active' : '';
  119. $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>');
  120. $i++;
  121. }
  122. $oOutput->AddHtml('</div>');
  123. $oOutput->AddHtml('</div>');
  124. break;
  125. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  126. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('"/>');
  127. break;
  128. }
  129. }
  130. // ... and in read-only mode (or hidden)
  131. else
  132. {
  133. // ... specific rendering for fields with mulltiple values
  134. if (($this->oField instanceof Combodo\iTop\Form\Field\MultipleChoicesField) && ($this->oField->GetMultipleValuesEnabled()))
  135. {
  136. // TODO
  137. }
  138. // ... clasic rendering for fields with only one value
  139. else
  140. {
  141. switch ($sFieldClass)
  142. {
  143. case 'Combodo\\iTop\\Form\\Field\\LabelField':
  144. $oOutput->AddHtml('<div class="form-group">');
  145. // Showing label / value only if read-only but not hidden
  146. if (!$this->oField->GetHidden())
  147. {
  148. if ($this->oField->GetLabel() !== '')
  149. {
  150. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  151. }
  152. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('</div>');
  153. }
  154. $oOutput->AddHtml('</div>');
  155. break;
  156. case 'Combodo\\iTop\\Form\\Field\\StringField':
  157. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  158. $bEncodeHtmlEntities = (($sFieldClass === 'Combodo\\iTop\\Form\\Field\\TextAreaField') && ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML)) ? false : true;
  159. $oOutput->AddHtml('<div class="form-group">');
  160. // Showing label / value only if read-only but not hidden
  161. if (!$this->oField->GetHidden())
  162. {
  163. if ($this->oField->GetLabel() !== '')
  164. {
  165. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  166. }
  167. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetCurrentValue(), $bEncodeHtmlEntities)->AddHtml('</div>');
  168. }
  169. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" />');
  170. $oOutput->AddHtml('</div>');
  171. break;
  172. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  173. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  174. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  175. case 'Combodo\\iTop\\Form\\Field\\SelectObjectField': // TODO : This should be check for external key, as we would display it differently
  176. $aFieldChoices = $this->oField->GetChoices();
  177. $sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject');
  178. $oOutput->AddHtml('<div class="form-group">');
  179. // Showing label / value only if read-only but not hidden
  180. if (!$this->oField->GetHidden())
  181. {
  182. if ($this->oField->GetLabel() !== '')
  183. {
  184. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  185. }
  186. $oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
  187. }
  188. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
  189. $oOutput->AddHtml('</div>');
  190. break;
  191. }
  192. }
  193. }
  194. // JS FieldChange trigger (:input are not always at the same depth)
  195. switch ($sFieldClass)
  196. {
  197. case 'Combodo\\iTop\\Form\\Field\\StringField':
  198. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  199. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  200. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  201. case 'Combodo\\iTop\\Form\\Field\\SelectObjectField':
  202. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  203. $oOutput->AddJs(
  204. <<<EOF
  205. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  206. var me = this;
  207. $(this).closest(".field_set").trigger("field_change", {
  208. id: $(me).attr("id"),
  209. name: $(me).closest(".form_field").attr("data-field-id"),
  210. value: $(me).val()
  211. });
  212. });
  213. EOF
  214. );
  215. break;
  216. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  217. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  218. $oOutput->AddJs(
  219. <<<EOF
  220. $("#{$this->oField->GetGlobalId()} input").off("change").on("change", function(){
  221. var me = this;
  222. $(this).closest(".field_set").trigger("field_change", {
  223. id: $(me).closest("#{$this->oField->GetGlobalId()}").attr("id"),
  224. name: $(me).attr("name"),
  225. value: $(me).val()
  226. });
  227. });
  228. EOF
  229. );
  230. break;
  231. }
  232. // JS Form field widget construct
  233. $aValidators = array();
  234. foreach ($this->oField->GetValidators() as $oValidator)
  235. {
  236. $aValidators[$oValidator::GetName()] = array(
  237. 'reg_exp' => $oValidator->GetRegExp(),
  238. 'message' => Dict::S($oValidator->GetErrorMessage())
  239. );
  240. }
  241. $sFormFieldOptions = json_encode(array(
  242. 'validators' => $aValidators
  243. ));
  244. switch ($sFieldClass)
  245. {
  246. case 'Combodo\\iTop\\Form\\Field\\StringField':
  247. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  248. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  249. case 'Combodo\\iTop\\Form\\Field\\SelectObjectField':
  250. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  251. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  252. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  253. $oOutput->AddJs(
  254. <<<EOF
  255. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field($sFormFieldOptions);
  256. EOF
  257. );
  258. break;
  259. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  260. $oOutput->AddJs(
  261. <<<EOF
  262. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field_html($sFormFieldOptions);
  263. EOF
  264. );
  265. // MagnificPopup on images
  266. $oOutput->AddJs(InlineImage::FixImagesWidth());
  267. break;
  268. }
  269. return $oOutput;
  270. }
  271. }