consolesimplefieldrenderer.class.inc.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. use \Combodo\iTop\Form\Field\TextAreaField;
  23. use \InlineImage;
  24. use \UserRights;
  25. class ConsoleSimpleFieldRenderer extends FieldRenderer
  26. {
  27. public function Render()
  28. {
  29. $oOutput = new RenderingOutput();
  30. $sFieldClass = get_class($this->oField);
  31. if ($sFieldClass == 'Combodo\\iTop\\Form\\Field\\HiddenField')
  32. {
  33. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  34. }
  35. else
  36. {
  37. $oOutput->AddHtml('<table class="form-field-container">');
  38. $oOutput->AddHtml('<tr>');
  39. if ($this->oField->GetLabel() != '')
  40. {
  41. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  42. }
  43. switch ($sFieldClass)
  44. {
  45. case 'Combodo\\iTop\\Form\\Field\\StringField':
  46. $oOutput->AddHtml('<td class="form-field-content">');
  47. if ($this->oField->GetReadOnly())
  48. {
  49. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  50. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'</span>');
  51. }
  52. else
  53. {
  54. $oOutput->AddHtml('<input class="form-field-data" type="text" id="'.$this->oField->GetGlobalId().'" value="'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'" size="30"/>');
  55. }
  56. $oOutput->AddHtml('<span class="form_validation"></span>');
  57. $oOutput->AddHtml('</td>');
  58. break;
  59. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  60. $bRichEditor = ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML);
  61. $oOutput->AddHtml('<td class="form-field-content">');
  62. if ($this->oField->GetReadOnly())
  63. {
  64. $oOutput->AddHtml('<textarea disabled="disabled" id="' . $this->oField->GetGlobalId() . '" class="form-field-data resizable" rows="8" cols="40">' . $this->oField->GetCurrentValue() . '</textarea>');
  65. }
  66. else
  67. {
  68. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" class="form-field-data resizable" rows="8" cols="40">' . $this->oField->GetCurrentValue() . '</textarea>');
  69. // Some additional stuff if we are displaying it with a rich editor
  70. if ($bRichEditor)
  71. {
  72. $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  73. $oOutput->AddJs(
  74. <<<EOF
  75. $('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
  76. $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
  77. EOF
  78. );
  79. if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
  80. {
  81. $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
  82. }
  83. }
  84. }
  85. $oOutput->AddHtml('<span class="form_validation"></span>');
  86. $oOutput->AddHtml('</td>');
  87. break;
  88. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  89. $oOutput->AddHtml('<td class="form-field-content">');
  90. if ($this->oField->GetReadOnly())
  91. {
  92. $aChoices = $this->oField->GetChoices();
  93. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  94. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  95. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  96. }
  97. else
  98. {
  99. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'" '.(($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '').'>');
  100. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  101. {
  102. // 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)
  103. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  104. $oOutput->AddHtml('<option value="'.htmlentities($sChoice, ENT_QUOTES, 'UTF-8').'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  105. }
  106. $oOutput->AddHtml('</select>');
  107. }
  108. $oOutput->AddHtml('<span class="form_validation"></span>');
  109. $oOutput->AddHtml('</td>');
  110. break;
  111. }
  112. $oOutput->AddHtml('</tr>');
  113. $oOutput->AddHtml('</table>');
  114. }
  115. switch ($sFieldClass)
  116. {
  117. case 'Combodo\\iTop\\Form\\Field\\StringField':
  118. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  119. $oOutput->AddJs(
  120. <<<EOF
  121. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  122. var me = this;
  123. $(this).closest(".field_set").trigger("field_change", {
  124. id: $(me).attr("id"),
  125. name: $(me).closest(".form_field").attr("data-field-id"),
  126. value: $(me).val()
  127. })
  128. .closest('.form_handler').trigger('value_change');
  129. });
  130. EOF
  131. );
  132. break;
  133. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  134. $oOutput->AddJs(
  135. <<<EOF
  136. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  137. var me = this;
  138. $(this).closest(".field_set").trigger("field_change", {
  139. id: $(me).attr("id"),
  140. name: $(me).closest(".form_field").attr("data-field-id"),
  141. value: $(me).val()
  142. })
  143. .closest('.form_handler').trigger('value_change');
  144. });
  145. EOF
  146. );
  147. break;
  148. }
  149. // JS Form field widget construct
  150. $aValidators = array();
  151. foreach ($this->oField->GetValidators() as $oValidator)
  152. {
  153. $aValidators[$oValidator::GetName()] = array(
  154. 'reg_exp' => $oValidator->GetRegExp(),
  155. 'message' => Dict::S($oValidator->GetErrorMessage())
  156. );
  157. }
  158. $sValidators = json_encode($aValidators);
  159. $sFormFieldOptions =
  160. <<<EOF
  161. {
  162. validators: $sValidators,
  163. on_validation_callback: function(me, oResult) {
  164. var oValidationElement = $(me.element).find('span.form_validation');
  165. if (oResult.is_valid)
  166. {
  167. oValidationElement.html('');
  168. }
  169. else
  170. {
  171. //TODO: escape html entities
  172. var sExplain = oResult.error_messages.join(', ');
  173. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  174. oValidationElement.tooltip({
  175. items: 'span',
  176. tooltipClass: 'form_field_error',
  177. content: function() {
  178. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  179. }
  180. });
  181. }
  182. }
  183. }
  184. EOF
  185. ;
  186. $oOutput->AddJs(
  187. <<<EOF
  188. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  189. EOF
  190. );
  191. switch ($sFieldClass)
  192. {
  193. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  194. $oOutput->AddJs(
  195. <<<EOF
  196. $("[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();});
  197. EOF
  198. );
  199. break;
  200. }
  201. return $oOutput;
  202. }
  203. }