consolesimplefieldrenderer.class.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. });
  97. EOF
  98. );
  99. break;
  100. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  101. $oOutput->AddJs(
  102. <<<EOF
  103. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  104. var me = this;
  105. $(this).closest(".field_set").trigger("field_change", {
  106. id: $(me).attr("id"),
  107. name: $(me).closest(".form_field").attr("data-field-id"),
  108. value: $(me).val()
  109. });
  110. });
  111. EOF
  112. );
  113. break;
  114. }
  115. // JS Form field widget construct
  116. $aValidators = array();
  117. foreach ($this->oField->GetValidators() as $oValidator)
  118. {
  119. $aValidators[$oValidator::GetName()] = array(
  120. 'reg_exp' => $oValidator->GetRegExp(),
  121. 'message' => Dict::S($oValidator->GetErrorMessage())
  122. );
  123. }
  124. $sValidators = json_encode($aValidators);
  125. $sFormFieldOptions =
  126. <<<EOF
  127. {
  128. validators: $sValidators,
  129. on_validation_callback: function(me, oResult) {
  130. var oValidationElement = $(me.element).find('span.form_validation');
  131. if (oResult.is_valid)
  132. {
  133. oValidationElement.html('');
  134. }
  135. else
  136. {
  137. //TODO: escape html entities
  138. var sExplain = oResult.error_messages.join(', ');
  139. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  140. oValidationElement.tooltip({
  141. items: 'span',
  142. tooltipClass: 'form_field_error',
  143. content: function() {
  144. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  145. }
  146. });
  147. }
  148. }
  149. }
  150. EOF
  151. ;
  152. $oOutput->AddJs(
  153. <<<EOF
  154. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  155. EOF
  156. );
  157. switch ($sFieldClass)
  158. {
  159. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  160. $oOutput->AddJs(
  161. <<<EOF
  162. $("[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();});
  163. EOF
  164. );
  165. break;
  166. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  167. case 'Combodo\\iTop\\Form\\Field\\StringField':
  168. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  169. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  170. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  171. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  172. break;
  173. }
  174. return $oOutput;
  175. }
  176. }