bssimplefieldrenderer.class.inc.php 11 KB

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