bssimplefieldrenderer.class.inc.php 11 KB

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