bssimplefieldrenderer.class.inc.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 \Dict;
  20. use \Combodo\iTop\Renderer\FieldRenderer;
  21. use \Combodo\iTop\Renderer\RenderingOutput;
  22. /**
  23. * Description of BsSimpleFieldRenderer
  24. *
  25. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  26. */
  27. class BsSimpleFieldRenderer extends FieldRenderer
  28. {
  29. /**
  30. * Returns a RenderingOutput for the FieldRenderer's Field
  31. *
  32. * @return \Combodo\iTop\Renderer\RenderingOutput
  33. */
  34. public function Render()
  35. {
  36. $oOutput = new RenderingOutput();
  37. $sFieldClass = get_class($this->oField);
  38. $sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
  39. // TODO : Shouldn't we have a field type so we don't have to maintain FQN classname ?
  40. // Rendering field in edition mode
  41. if (!$this->oField->GetReadOnly())
  42. {
  43. switch ($sFieldClass)
  44. {
  45. case 'Combodo\\iTop\\Form\\Field\\StringField':
  46. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  47. if ($this->oField->GetLabel() !== '')
  48. {
  49. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  50. }
  51. $oOutput->AddHtml('<div class="help-block"></div>');
  52. $oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
  53. $oOutput->AddHtml('</div>');
  54. break;
  55. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  56. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  57. if ($this->oField->GetLabel() !== '')
  58. {
  59. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  60. }
  61. $oOutput->AddHtml('<div class="help-block"></div>');
  62. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" class="form-control" rows="8">' . $this->oField->GetCurrentValue() . '</textarea>');
  63. $oOutput->AddHtml('</div>');
  64. break;
  65. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  66. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  67. if ($this->oField->GetLabel() !== '')
  68. {
  69. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  70. }
  71. $oOutput->AddHtml('<div class="help-block"></div>');
  72. $oOutput->AddHtml('<select id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" ' . ( ($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '' ) . ' class="form-control">');
  73. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  74. {
  75. // 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)
  76. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  77. $oOutput->AddHtml('<option value="' . $sChoice . '" ' . $sSelectedAtt . ' >' . $sLabel . '</option>');
  78. }
  79. $oOutput->AddHtml('</select>');
  80. $oOutput->AddHtml('</div>');
  81. break;
  82. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  83. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  84. $sFieldType = ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\RadioField') ? 'radio' : 'checkbox';
  85. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '" id="' . $this->oField->GetGlobalId() . '">');
  86. if ($this->oField->GetLabel() !== '')
  87. {
  88. $oOutput->AddHtml('<div><label class="control-label">' . $this->oField->GetLabel() . '</label></div>');
  89. }
  90. $oOutput->AddHtml('<div class="help-block"></div>');
  91. $oOutput->AddHtml('<div class="btn-group" data-toggle="buttons">');
  92. $i = 0;
  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. $sCheckedAtt = ($this->oField->IsAmongValues($sChoice)) ? 'checked' : '';
  97. $sCheckedClass = ($this->oField->IsAmongValues($sChoice)) ? 'active' : '';
  98. $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>');
  99. $i++;
  100. }
  101. $oOutput->AddHtml('</div>');
  102. $oOutput->AddHtml('</div>');
  103. break;
  104. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  105. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '"/>');
  106. break;
  107. }
  108. }
  109. // ... and in read-only mode
  110. else
  111. {
  112. // ... specific rendering for fields with mulltiple values
  113. if (($this->oField instanceof Combodo\iTop\Form\Field\MultipleChoicesField) && ($this->oField->GetMultipleValuesEnabled()))
  114. {
  115. // TODO
  116. }
  117. // ... clasic rendering for fields with only one value
  118. else
  119. {
  120. switch ($sFieldClass)
  121. {
  122. case 'Combodo\\iTop\\Form\\Field\\StringField':
  123. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  124. $oOutput->AddHtml('<div class="form-group">');
  125. if ($this->oField->GetLabel() !== '')
  126. {
  127. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  128. }
  129. $oOutput->AddHtml('<div class="form-control-static">' . $this->oField->GetCurrentValue() . '</div>');
  130. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
  131. $oOutput->AddHtml('</div>');
  132. break;
  133. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  134. case 'Combodo\\iTop\\Form\\Field\\SelectField': // TODO : This should be check for external key, as we would display it differently
  135. $aFieldChoices = $this->oField->GetChoices();
  136. $sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject');
  137. $oOutput->AddHtml('<div class="form-group">');
  138. if ($this->oField->GetLabel() !== '')
  139. {
  140. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  141. }
  142. $oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
  143. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
  144. $oOutput->AddHtml('</div>');
  145. break;
  146. }
  147. }
  148. }
  149. // JS FieldChange trigger (:input are not always at the same depth)
  150. switch ($sFieldClass)
  151. {
  152. case 'Combodo\\iTop\\Form\\Field\\StringField':
  153. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  154. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  155. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  156. $oOutput->AddJs(
  157. <<<EOF
  158. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  159. var me = this;
  160. $(this).closest(".field_set").trigger("field_change", {
  161. id: $(me).attr("id"),
  162. name: $(me).closest(".form_field").attr("data-field-id"),
  163. value: $(me).val()
  164. });
  165. });
  166. EOF
  167. );
  168. break;
  169. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  170. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  171. $oOutput->AddJs(
  172. <<<EOF
  173. $("#{$this->oField->GetGlobalId()} input").off("change").on("change", function(){
  174. var me = this;
  175. $(this).closest(".field_set").trigger("field_change", {
  176. id: $(me).closest("#{$this->oField->GetGlobalId()}").attr("id"),
  177. name: $(me).attr("name"),
  178. value: $(me).val()
  179. });
  180. });
  181. EOF
  182. );
  183. break;
  184. }
  185. // JS Form field widget construct
  186. $aValidators = array();
  187. foreach ($this->oField->GetValidators() as $oValidator)
  188. {
  189. $aValidators[$oValidator::GetName()] = array(
  190. 'reg_exp' => $oValidator->GetRegExp(),
  191. 'message' => Dict::S($oValidator->GetErrorMessage())
  192. );
  193. }
  194. $sFormFieldOptions = json_encode(array(
  195. 'validators' => $aValidators
  196. ));
  197. switch ($sFieldClass)
  198. {
  199. case 'Combodo\\iTop\\Form\\Field\\StringField':
  200. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  201. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  202. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  203. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  204. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  205. $oOutput->AddJs(
  206. <<<EOF
  207. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field($sFormFieldOptions);
  208. EOF
  209. );
  210. break;
  211. }
  212. return $oOutput;
  213. }
  214. }