consolesimplefieldrenderer.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. use \AttributeDuration;
  26. class ConsoleSimpleFieldRenderer extends FieldRenderer
  27. {
  28. public function Render()
  29. {
  30. $oOutput = new RenderingOutput();
  31. $sFieldClass = get_class($this->oField);
  32. if ($sFieldClass == 'Combodo\\iTop\\Form\\Field\\HiddenField')
  33. {
  34. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  35. }
  36. else
  37. {
  38. $oOutput->AddHtml('<table class="form-field-container">');
  39. $oOutput->AddHtml('<tr>');
  40. if ($this->oField->GetLabel() != '')
  41. {
  42. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  43. }
  44. switch ($sFieldClass)
  45. {
  46. case 'Combodo\\iTop\\Form\\Field\\StringField':
  47. $oOutput->AddHtml('<td class="form-field-content">');
  48. if ($this->oField->GetReadOnly())
  49. {
  50. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  51. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'</span>');
  52. }
  53. else
  54. {
  55. $oOutput->AddHtml('<input class="form-field-data" type="text" id="'.$this->oField->GetGlobalId().'" value="'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'" size="30"/>');
  56. }
  57. $oOutput->AddHtml('<span class="form_validation"></span>');
  58. $oOutput->AddHtml('</td>');
  59. break;
  60. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  61. $bRichEditor = ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML);
  62. $oOutput->AddHtml('<td class="form-field-content">');
  63. if ($this->oField->GetReadOnly())
  64. {
  65. $oOutput->AddHtml('<textarea disabled="disabled" id="' . $this->oField->GetGlobalId() . '" class="form-field-data resizable" rows="8" cols="40">' . $this->oField->GetCurrentValue() . '</textarea>');
  66. }
  67. else
  68. {
  69. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" class="form-field-data resizable" rows="8" cols="40">' . $this->oField->GetCurrentValue() . '</textarea>');
  70. // Some additional stuff if we are displaying it with a rich editor
  71. if ($bRichEditor)
  72. {
  73. $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  74. $oOutput->AddJs(
  75. <<<EOF
  76. $('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
  77. $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
  78. EOF
  79. );
  80. if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
  81. {
  82. $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
  83. }
  84. }
  85. }
  86. $oOutput->AddHtml('<span class="form_validation"></span>');
  87. $oOutput->AddHtml('</td>');
  88. break;
  89. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  90. $oOutput->AddHtml('<td class="form-field-content">');
  91. if ($this->oField->GetReadOnly())
  92. {
  93. $aChoices = $this->oField->GetChoices();
  94. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  95. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  96. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  97. }
  98. else
  99. {
  100. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'" '.(($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '').'>');
  101. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  102. {
  103. // 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)
  104. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  105. $oOutput->AddHtml('<option value="'.htmlentities($sChoice, ENT_QUOTES, 'UTF-8').'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  106. }
  107. $oOutput->AddHtml('</select>');
  108. }
  109. $oOutput->AddHtml('<span class="form_validation"></span>');
  110. $oOutput->AddHtml('</td>');
  111. break;
  112. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  113. $oOutput->AddHtml('<td class="form-field-content">');
  114. if ($this->oField->GetReadOnly())
  115. {
  116. $aChoices = $this->oField->GetChoices();
  117. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  118. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  119. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  120. }
  121. else
  122. {
  123. $bVertical = true;
  124. $idx = 0;
  125. $bMandatory = $this->oField->GetMandatory();
  126. $value = $this->oField->GetCurrentValue();
  127. $sId = $this->oField->GetGlobalId();
  128. $oOutput->AddHtml('<div>');
  129. $aChoices = $this->oField->GetChoices();
  130. foreach ($aChoices as $sChoice => $sLabel)
  131. {
  132. if ((count($aChoices)== 1) && $bMandatory)
  133. {
  134. // When there is only once choice, select it by default
  135. $sSelected = ' checked';
  136. }
  137. else
  138. {
  139. $sSelected = ($value == $sChoice) ? ' checked' : '';
  140. }
  141. $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;");
  142. if ($bVertical)
  143. {
  144. $oOutput->AddHtml("<br>\n");
  145. }
  146. $idx++;
  147. }
  148. $oOutput->AddHtml('</div>');
  149. $oOutput->AddHtml("<input type=\"hidden\" id=\"$sId\" name=\"$sId\" value=\"$value\"/>");
  150. }
  151. $oOutput->AddHtml('<span class="form_validation"></span>');
  152. $oOutput->AddHtml('</td>');
  153. break;
  154. case 'Combodo\\iTop\\Form\\Field\\DurationField':
  155. $oOutput->AddHtml('<td class="form-field-content">');
  156. $value = $this->oField->GetCurrentValue();
  157. if ($this->oField->GetReadOnly())
  158. {
  159. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '"/>');
  160. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities(\AttributeDuration::FormatDuration($value), ENT_QUOTES, 'UTF-8').'</span>');
  161. }
  162. else
  163. {
  164. $sId = $this->oField->GetGlobalId();
  165. $aVal = AttributeDuration::SplitDuration($value);
  166. $sDays = "<input type=\"text\" size=\"3\" name=\"{$sId}[d]\" value=\"{$aVal['days']}\" id=\"{$sId}_d\"/>";
  167. $sHours = "<input type=\"text\" size=\"2\" name=\"{$sId}[h]\" value=\"{$aVal['hours']}\" id=\"{$sId}_h\"/>";
  168. $sMinutes = "<input type=\"text\" size=\"2\" name=\"{$sId}[m]\" value=\"{$aVal['minutes']}\" id=\"{$sId}_m\"/>";
  169. $sSeconds = "<input type=\"text\" size=\"2\" name=\"{$sId}[s]\" value=\"{$aVal['seconds']}\" id=\"{$sId}_s\"/>";
  170. $oOutput->AddHtml(Dict::Format('UI:DurationForm_Days_Hours_Minutes_Seconds', $sDays, $sHours, $sMinutes, $sSeconds));
  171. $oOutput->AddHtml("<input type=\"hidden\" id=\"{$sId}\" value=\"".htmlentities($value, ENT_QUOTES, 'UTF-8')."\"/>");
  172. $oOutput->AddJs("$('#{$sId}_d').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  173. $oOutput->AddJs("$('#{$sId}_h').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  174. $oOutput->AddJs("$('#{$sId}_m').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  175. $oOutput->AddJs("$('#{$sId}_s').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  176. $oOutput->AddJs("$('#{$sId}').bind('update', function(evt, sFormId) { return ToggleDurationField('$sId'); });");
  177. }
  178. $oOutput->AddHtml('<span class="form_validation"></span>');
  179. $oOutput->AddHtml('</td>');
  180. break;
  181. }
  182. $oOutput->AddHtml('</tr>');
  183. $oOutput->AddHtml('</table>');
  184. }
  185. switch ($sFieldClass)
  186. {
  187. case 'Combodo\\iTop\\Form\\Field\\StringField':
  188. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  189. $oOutput->AddJs(
  190. <<<EOF
  191. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  192. var me = this;
  193. $(this).closest(".field_set").trigger("field_change", {
  194. id: $(me).attr("id"),
  195. name: $(me).closest(".form_field").attr("data-field-id"),
  196. value: $(me).val()
  197. })
  198. .closest('.form_handler').trigger('value_change');
  199. });
  200. EOF
  201. );
  202. break;
  203. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  204. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  205. case 'Combodo\\iTop\\Form\\Field\\DurationField':
  206. $oOutput->AddJs(
  207. <<<EOF
  208. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  209. var me = this;
  210. $(this).closest(".field_set").trigger("field_change", {
  211. id: $(me).attr("id"),
  212. name: $(me).closest(".form_field").attr("data-field-id"),
  213. value: $(me).val()
  214. })
  215. .closest('.form_handler').trigger('value_change');
  216. });
  217. EOF
  218. );
  219. break;
  220. }
  221. // JS Form field widget construct
  222. $aValidators = array();
  223. foreach ($this->oField->GetValidators() as $oValidator)
  224. {
  225. $aValidators[$oValidator::GetName()] = array(
  226. 'reg_exp' => $oValidator->GetRegExp(),
  227. 'message' => Dict::S($oValidator->GetErrorMessage())
  228. );
  229. }
  230. $sValidators = json_encode($aValidators);
  231. $sFormFieldOptions =
  232. <<<EOF
  233. {
  234. validators: $sValidators,
  235. on_validation_callback: function(me, oResult) {
  236. var oValidationElement = $(me.element).find('span.form_validation');
  237. if (oResult.is_valid)
  238. {
  239. oValidationElement.html('');
  240. }
  241. else
  242. {
  243. //TODO: escape html entities
  244. var sExplain = oResult.error_messages.join(', ');
  245. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  246. oValidationElement.tooltip({
  247. items: 'span',
  248. tooltipClass: 'form_field_error',
  249. content: function() {
  250. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  251. }
  252. });
  253. }
  254. }
  255. }
  256. EOF
  257. ;
  258. $oOutput->AddJs(
  259. <<<EOF
  260. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  261. EOF
  262. );
  263. switch ($sFieldClass)
  264. {
  265. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  266. $oOutput->AddJs(
  267. <<<EOF
  268. $("[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();});
  269. EOF
  270. );
  271. break;
  272. }
  273. return $oOutput;
  274. }
  275. }