consolesimplefieldrenderer.class.inc.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 \Dict;
  20. use Combodo\iTop\Renderer\FieldRenderer;
  21. use Combodo\iTop\Renderer\RenderingOutput;
  22. class ConsoleSimpleFieldRenderer extends FieldRenderer
  23. {
  24. public function Render()
  25. {
  26. $oOutput = new RenderingOutput();
  27. $sFieldClass = get_class($this->oField);
  28. if ($sFieldClass == 'Combodo\\iTop\\Form\\Field\\HiddenField')
  29. {
  30. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  31. }
  32. else
  33. {
  34. $oOutput->AddHtml('<table class="form-field-container">');
  35. $oOutput->AddHtml('<tr>');
  36. if ($this->oField->GetLabel() != '')
  37. {
  38. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  39. }
  40. switch ($sFieldClass)
  41. {
  42. case 'Combodo\\iTop\\Form\\Field\\StringField':
  43. $oOutput->AddHtml('<td class="form-field-content">');
  44. if ($this->oField->GetReadOnly())
  45. {
  46. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  47. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'</span>');
  48. }
  49. else
  50. {
  51. $oOutput->AddHtml('<input class="form-field-data" type="text" id="'.$this->oField->GetGlobalId().'" value="'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'" size="30"/>');
  52. }
  53. $oOutput->AddHtml('<span class="form_validation"></span>');
  54. $oOutput->AddHtml('</td>');
  55. break;
  56. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  57. $oOutput->AddHtml('<td class="form-field-content">');
  58. if ($this->oField->GetReadOnly())
  59. {
  60. $aChoices = $this->oField->GetChoices();
  61. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  62. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  63. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  64. }
  65. else
  66. {
  67. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'" '.(($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '').'>');
  68. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  69. {
  70. // 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)
  71. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  72. $oOutput->AddHtml('<option value="'.htmlentities($sChoice, ENT_QUOTES, 'UTF-8').'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  73. }
  74. $oOutput->AddHtml('</select>');
  75. }
  76. $oOutput->AddHtml('<span class="form_validation"></span>');
  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. .closest('.form_handler').trigger('value_change');
  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. .closest('.form_handler').trigger('value_change');
  111. });
  112. EOF
  113. );
  114. break;
  115. }
  116. // JS Form field widget construct
  117. $aValidators = array();
  118. foreach ($this->oField->GetValidators() as $oValidator)
  119. {
  120. $aValidators[$oValidator::GetName()] = array(
  121. 'reg_exp' => $oValidator->GetRegExp(),
  122. 'message' => Dict::S($oValidator->GetErrorMessage())
  123. );
  124. }
  125. $sValidators = json_encode($aValidators);
  126. $sFormFieldOptions =
  127. <<<EOF
  128. {
  129. validators: $sValidators,
  130. on_validation_callback: function(me, oResult) {
  131. var oValidationElement = $(me.element).find('span.form_validation');
  132. if (oResult.is_valid)
  133. {
  134. oValidationElement.html('');
  135. }
  136. else
  137. {
  138. //TODO: escape html entities
  139. var sExplain = oResult.error_messages.join(', ');
  140. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  141. oValidationElement.tooltip({
  142. items: 'span',
  143. tooltipClass: 'form_field_error',
  144. content: function() {
  145. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  146. }
  147. });
  148. }
  149. }
  150. }
  151. EOF
  152. ;
  153. $oOutput->AddJs(
  154. <<<EOF
  155. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  156. EOF
  157. );
  158. switch ($sFieldClass)
  159. {
  160. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  161. $oOutput->AddJs(
  162. <<<EOF
  163. $("[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();});
  164. EOF
  165. );
  166. break;
  167. }
  168. return $oOutput;
  169. }
  170. }