consolesimplefieldrenderer.class.inc.php 7.1 KB

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