consolesimplefieldrenderer.class.inc.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // Copyright (C) 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\Console\FieldRenderer;
  19. use Combodo\iTop\Form\Field\StringField;
  20. use \Dict;
  21. use Combodo\iTop\Renderer\FieldRenderer;
  22. use Combodo\iTop\Renderer\RenderingOutput;
  23. class ConsoleSimpleFieldRenderer extends FieldRenderer
  24. {
  25. public function Render()
  26. {
  27. $oOutput = new RenderingOutput();
  28. $sFieldClass = get_class($this->oField);
  29. if ($sFieldClass == 'Combodo\\iTop\\Form\\Field\\HiddenField')
  30. {
  31. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  32. }
  33. else
  34. {
  35. $oOutput->AddHtml('<table class="form-field-container">');
  36. $oOutput->AddHtml('<tr>');
  37. if ($this->oField->GetLabel() != '')
  38. {
  39. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  40. }
  41. switch ($sFieldClass)
  42. {
  43. case 'Combodo\\iTop\\Form\\Field\\StringField':
  44. $oOutput->AddHtml('<td class="form-field-content">');
  45. if ($this->oField->GetReadOnly())
  46. {
  47. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  48. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'</span>');
  49. }
  50. else
  51. {
  52. $oOutput->AddHtml('<input class="form-field-data" type="text" id="'.$this->oField->GetGlobalId().'" value="'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'" size="30"/>');
  53. }
  54. $oOutput->AddHtml('<span class="form_validation"></span>');
  55. $oOutput->AddHtml('</td>');
  56. break;
  57. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  58. $oOutput->AddHtml('<td class="form-field-content">');
  59. if ($this->oField->GetReadOnly())
  60. {
  61. $aChoices = $this->oField->GetChoices();
  62. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  63. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  64. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  65. }
  66. else
  67. {
  68. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'" '.(($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '').'>');
  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="'.htmlentities($sChoice, ENT_QUOTES, 'UTF-8').'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  74. }
  75. $oOutput->AddHtml('</select>');
  76. }
  77. $oOutput->AddHtml('<span class="form_validation"></span>');
  78. $oOutput->AddHtml('</td>');
  79. break;
  80. }
  81. $oOutput->AddHtml('</tr>');
  82. $oOutput->AddHtml('</table>');
  83. }
  84. switch ($sFieldClass)
  85. {
  86. case 'Combodo\\iTop\\Form\\Field\\StringField':
  87. $oOutput->AddJs(
  88. <<<EOF
  89. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  90. var me = this;
  91. $(this).closest(".field_set").trigger("field_change", {
  92. id: $(me).attr("id"),
  93. name: $(me).closest(".form_field").attr("data-field-id"),
  94. value: $(me).val()
  95. })
  96. .closest('.form_handler').trigger('value_change');
  97. });
  98. EOF
  99. );
  100. break;
  101. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  102. $oOutput->AddJs(
  103. <<<EOF
  104. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  105. var me = this;
  106. $(this).closest(".field_set").trigger("field_change", {
  107. id: $(me).attr("id"),
  108. name: $(me).closest(".form_field").attr("data-field-id"),
  109. value: $(me).val()
  110. })
  111. .closest('.form_handler').trigger('value_change');
  112. });
  113. EOF
  114. );
  115. break;
  116. }
  117. // JS Form field widget construct
  118. $aValidators = array();
  119. foreach ($this->oField->GetValidators() as $oValidator)
  120. {
  121. $aValidators[$oValidator::GetName()] = array(
  122. 'reg_exp' => $oValidator->GetRegExp(),
  123. 'message' => Dict::S($oValidator->GetErrorMessage())
  124. );
  125. }
  126. $sValidators = json_encode($aValidators);
  127. $sFormFieldOptions =
  128. <<<EOF
  129. {
  130. validators: $sValidators,
  131. on_validation_callback: function(me, oResult) {
  132. var oValidationElement = $(me.element).find('span.form_validation');
  133. if (oResult.is_valid)
  134. {
  135. oValidationElement.html('');
  136. }
  137. else
  138. {
  139. //TODO: escape html entities
  140. var sExplain = oResult.error_messages.join(', ');
  141. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  142. oValidationElement.tooltip({
  143. items: 'span',
  144. tooltipClass: 'form_field_error',
  145. content: function() {
  146. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  147. }
  148. });
  149. }
  150. }
  151. }
  152. EOF
  153. ;
  154. $oOutput->AddJs(
  155. <<<EOF
  156. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  157. EOF
  158. );
  159. switch ($sFieldClass)
  160. {
  161. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  162. $oOutput->AddJs(
  163. <<<EOF
  164. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field('option', 'get_current_value_callback', function(me){ return $(me.element).find('select').val();});
  165. EOF
  166. );
  167. break;
  168. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  169. case 'Combodo\\iTop\\Form\\Field\\StringField':
  170. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  171. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  172. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  173. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  174. break;
  175. }
  176. return $oOutput;
  177. }
  178. }