bssimplefieldrenderer.class.inc.php 11 KB

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