consolesimplefieldrenderer.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  112. $oOutput->AddHtml('<td class="form-field-content">');
  113. if ($this->oField->GetReadOnly())
  114. {
  115. $aChoices = $this->oField->GetChoices();
  116. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  117. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  118. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  119. }
  120. else
  121. {
  122. $bVertical = true;
  123. $idx = 0;
  124. $bMandatory = $this->oField->GetMandatory();
  125. $value = $this->oField->GetCurrentValue();
  126. $sId = $this->oField->GetGlobalId();
  127. $oOutput->AddHtml('<div>');
  128. $aChoices = $this->oField->GetChoices();
  129. foreach ($aChoices as $sChoice => $sLabel)
  130. {
  131. if ((count($aChoices)== 1) && $bMandatory)
  132. {
  133. // When there is only once choice, select it by default
  134. $sSelected = ' checked';
  135. }
  136. else
  137. {
  138. $sSelected = ($value == $sChoice) ? ' checked' : '';
  139. }
  140. $oOutput->AddHtml("<input type=\"radio\" id=\"{$sId}_{$idx}\" name=\"radio_$sId\" onChange=\"$('#{$sId}').val(this.value).trigger('change');\" value=\"".htmlentities($sChoice, ENT_QUOTES, 'UTF-8')."\"$sSelected><label class=\"radio\" for=\"{$sId}_{$idx}\">&nbsp;".htmlentities($sLabel, ENT_QUOTES, 'UTF-8')."</label>&nbsp;");
  141. if ($bVertical)
  142. {
  143. $oOutput->AddHtml("<br>\n");
  144. }
  145. $idx++;
  146. }
  147. $oOutput->AddHtml('</div>');
  148. $oOutput->AddHtml("<input type=\"hidden\" id=\"$sId\" name=\"$sId\" value=\"$value\"/>");
  149. }
  150. $oOutput->AddHtml('<span class="form_validation"></span>');
  151. $oOutput->AddHtml('</td>');
  152. break;
  153. }
  154. $oOutput->AddHtml('</tr>');
  155. $oOutput->AddHtml('</table>');
  156. }
  157. switch ($sFieldClass)
  158. {
  159. case 'Combodo\\iTop\\Form\\Field\\StringField':
  160. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  161. $oOutput->AddJs(
  162. <<<EOF
  163. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  164. var me = this;
  165. $(this).closest(".field_set").trigger("field_change", {
  166. id: $(me).attr("id"),
  167. name: $(me).closest(".form_field").attr("data-field-id"),
  168. value: $(me).val()
  169. })
  170. .closest('.form_handler').trigger('value_change');
  171. });
  172. EOF
  173. );
  174. break;
  175. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  176. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  177. $oOutput->AddJs(
  178. <<<EOF
  179. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  180. var me = this;
  181. $(this).closest(".field_set").trigger("field_change", {
  182. id: $(me).attr("id"),
  183. name: $(me).closest(".form_field").attr("data-field-id"),
  184. value: $(me).val()
  185. })
  186. .closest('.form_handler').trigger('value_change');
  187. });
  188. EOF
  189. );
  190. break;
  191. }
  192. // JS Form field widget construct
  193. $aValidators = array();
  194. foreach ($this->oField->GetValidators() as $oValidator)
  195. {
  196. $aValidators[$oValidator::GetName()] = array(
  197. 'reg_exp' => $oValidator->GetRegExp(),
  198. 'message' => Dict::S($oValidator->GetErrorMessage())
  199. );
  200. }
  201. $sValidators = json_encode($aValidators);
  202. $sFormFieldOptions =
  203. <<<EOF
  204. {
  205. validators: $sValidators,
  206. on_validation_callback: function(me, oResult) {
  207. var oValidationElement = $(me.element).find('span.form_validation');
  208. if (oResult.is_valid)
  209. {
  210. oValidationElement.html('');
  211. }
  212. else
  213. {
  214. //TODO: escape html entities
  215. var sExplain = oResult.error_messages.join(', ');
  216. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  217. oValidationElement.tooltip({
  218. items: 'span',
  219. tooltipClass: 'form_field_error',
  220. content: function() {
  221. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  222. }
  223. });
  224. }
  225. }
  226. }
  227. EOF
  228. ;
  229. $oOutput->AddJs(
  230. <<<EOF
  231. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  232. EOF
  233. );
  234. switch ($sFieldClass)
  235. {
  236. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  237. $oOutput->AddJs(
  238. <<<EOF
  239. $("[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();});
  240. EOF
  241. );
  242. break;
  243. }
  244. return $oOutput;
  245. }
  246. }