bssimplefieldrenderer.class.inc.php 11 KB

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