consolesimplefieldrenderer.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. use \DateTimeFormat;
  27. use \AttributeDateTime;
  28. use \AttributeDate;
  29. class ConsoleSimpleFieldRenderer extends FieldRenderer
  30. {
  31. public function Render()
  32. {
  33. $oOutput = new RenderingOutput();
  34. $sFieldClass = get_class($this->oField);
  35. if ($sFieldClass == 'Combodo\\iTop\\Form\\Field\\HiddenField')
  36. {
  37. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  38. }
  39. else
  40. {
  41. $oOutput->AddHtml('<table class="form-field-container">');
  42. $oOutput->AddHtml('<tr>');
  43. if ($this->oField->GetLabel() != '')
  44. {
  45. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  46. }
  47. switch ($sFieldClass)
  48. {
  49. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  50. $sDateTimeFormat = $this->oField->GetPHPDateTimeFormat();
  51. $oFormat = new DateTimeFormat($sDateTimeFormat);
  52. $sPlaceHolder = $oFormat->ToPlaceholder();
  53. $oOutput->AddHtml('<td class="form-field-content">');
  54. if ($this->oField->GetReadOnly())
  55. {
  56. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  57. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'</span>');
  58. }
  59. else
  60. {
  61. $oOutput->AddHtml('<input class="form-field-data datetime-pick" size="15" type="text" placeholder="'.htmlentities($sPlaceHolder, ENT_QUOTES, 'UTF-8').'" id="'.$this->oField->GetGlobalId().'" value="'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'" size="30"/>');
  62. }
  63. $oOutput->AddHtml('<span class="form_validation"></span>');
  64. $oOutput->AddHtml('</td>');
  65. break;
  66. case 'Combodo\\iTop\\Form\\Field\\StringField':
  67. $oOutput->AddHtml('<td class="form-field-content">');
  68. if ($this->oField->GetReadOnly())
  69. {
  70. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  71. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'</span>');
  72. }
  73. else
  74. {
  75. $oOutput->AddHtml('<input class="form-field-data" type="text" id="'.$this->oField->GetGlobalId().'" value="'.htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8').'" size="30"/>');
  76. }
  77. $oOutput->AddHtml('<span class="form_validation"></span>');
  78. $oOutput->AddHtml('</td>');
  79. break;
  80. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  81. $bRichEditor = ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML);
  82. $oOutput->AddHtml('<td class="form-field-content">');
  83. if ($this->oField->GetReadOnly())
  84. {
  85. $oOutput->AddHtml('<textarea disabled="disabled" id="' . $this->oField->GetGlobalId() . '" class="form-field-data resizable" rows="8" cols="40">' . $this->oField->GetCurrentValue() . '</textarea>');
  86. }
  87. else
  88. {
  89. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" class="form-field-data resizable" rows="8" cols="40">' . $this->oField->GetCurrentValue() . '</textarea>');
  90. // Some additional stuff if we are displaying it with a rich editor
  91. if ($bRichEditor)
  92. {
  93. $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  94. $oOutput->AddJs(
  95. <<<EOF
  96. $('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
  97. $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
  98. EOF
  99. );
  100. if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
  101. {
  102. $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
  103. }
  104. }
  105. }
  106. $oOutput->AddHtml('<span class="form_validation"></span>');
  107. $oOutput->AddHtml('</td>');
  108. break;
  109. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  110. $oOutput->AddHtml('<td class="form-field-content">');
  111. if ($this->oField->GetReadOnly())
  112. {
  113. $aChoices = $this->oField->GetChoices();
  114. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  115. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  116. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  117. }
  118. else
  119. {
  120. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'" '.(($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '').'>');
  121. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  122. {
  123. // 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)
  124. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  125. $oOutput->AddHtml('<option value="'.htmlentities($sChoice, ENT_QUOTES, 'UTF-8').'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  126. }
  127. $oOutput->AddHtml('</select>');
  128. }
  129. $oOutput->AddHtml('<span class="form_validation"></span>');
  130. $oOutput->AddHtml('</td>');
  131. break;
  132. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  133. $oOutput->AddHtml('<td class="form-field-content">');
  134. if ($this->oField->GetReadOnly())
  135. {
  136. $aChoices = $this->oField->GetChoices();
  137. $sCurrentLabel = isset($aChoices[$this->oField->GetCurrentValue()]) ? $aChoices[$this->oField->GetCurrentValue()] : '' ;
  138. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  139. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  140. }
  141. else
  142. {
  143. $bVertical = true;
  144. $idx = 0;
  145. $bMandatory = $this->oField->GetMandatory();
  146. $value = $this->oField->GetCurrentValue();
  147. $sId = $this->oField->GetGlobalId();
  148. $oOutput->AddHtml('<div>');
  149. $aChoices = $this->oField->GetChoices();
  150. foreach ($aChoices as $sChoice => $sLabel)
  151. {
  152. if ((count($aChoices)== 1) && $bMandatory)
  153. {
  154. // When there is only once choice, select it by default
  155. $sSelected = ' checked';
  156. }
  157. else
  158. {
  159. $sSelected = ($value == $sChoice) ? ' checked' : '';
  160. }
  161. $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;");
  162. if ($bVertical)
  163. {
  164. $oOutput->AddHtml("<br>\n");
  165. }
  166. $idx++;
  167. }
  168. $oOutput->AddHtml('</div>');
  169. $oOutput->AddHtml("<input type=\"hidden\" id=\"$sId\" name=\"$sId\" value=\"$value\"/>");
  170. }
  171. $oOutput->AddHtml('<span class="form_validation"></span>');
  172. $oOutput->AddHtml('</td>');
  173. break;
  174. case 'Combodo\\iTop\\Form\\Field\\DurationField':
  175. $oOutput->AddHtml('<td class="form-field-content">');
  176. $value = $this->oField->GetCurrentValue();
  177. if ($this->oField->GetReadOnly())
  178. {
  179. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($value, ENT_QUOTES, 'UTF-8') . '"/>');
  180. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities(\AttributeDuration::FormatDuration($value), ENT_QUOTES, 'UTF-8').'</span>');
  181. }
  182. else
  183. {
  184. $sId = $this->oField->GetGlobalId();
  185. $aVal = AttributeDuration::SplitDuration($value);
  186. $sDays = "<input type=\"text\" size=\"3\" name=\"{$sId}[d]\" value=\"{$aVal['days']}\" id=\"{$sId}_d\"/>";
  187. $sHours = "<input type=\"text\" size=\"2\" name=\"{$sId}[h]\" value=\"{$aVal['hours']}\" id=\"{$sId}_h\"/>";
  188. $sMinutes = "<input type=\"text\" size=\"2\" name=\"{$sId}[m]\" value=\"{$aVal['minutes']}\" id=\"{$sId}_m\"/>";
  189. $sSeconds = "<input type=\"text\" size=\"2\" name=\"{$sId}[s]\" value=\"{$aVal['seconds']}\" id=\"{$sId}_s\"/>";
  190. $oOutput->AddHtml(Dict::Format('UI:DurationForm_Days_Hours_Minutes_Seconds', $sDays, $sHours, $sMinutes, $sSeconds));
  191. $oOutput->AddHtml("<input type=\"hidden\" id=\"{$sId}\" value=\"".htmlentities($value, ENT_QUOTES, 'UTF-8')."\"/>");
  192. $oOutput->AddJs("$('#{$sId}_d').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  193. $oOutput->AddJs("$('#{$sId}_h').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  194. $oOutput->AddJs("$('#{$sId}_m').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  195. $oOutput->AddJs("$('#{$sId}_s').bind('keyup change', function(evt, sFormId) { return UpdateDuration('$sId'); });");
  196. $oOutput->AddJs("$('#{$sId}').bind('update', function(evt, sFormId) { return ToggleDurationField('$sId'); });");
  197. }
  198. $oOutput->AddHtml('<span class="form_validation"></span>');
  199. $oOutput->AddHtml('</td>');
  200. break;
  201. }
  202. $oOutput->AddHtml('</tr>');
  203. $oOutput->AddHtml('</table>');
  204. }
  205. switch ($sFieldClass)
  206. {
  207. case 'Combodo\\iTop\Form\\Field\\DateTimeField':
  208. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  209. $sDateTimeFormat = $this->oField->GetPHPDateTimeFormat();
  210. $oFormat = new DateTimeFormat($sDateTimeFormat);
  211. $sDatePickerFormat = $oFormat->ToDatePicker();
  212. $sJSDaysMin = json_encode(array(Dict::S('DayOfWeek-Sunday-Min'), Dict::S('DayOfWeek-Monday-Min'), Dict::S('DayOfWeek-Tuesday-Min'), Dict::S('DayOfWeek-Wednesday-Min'),
  213. Dict::S('DayOfWeek-Thursday-Min'), Dict::S('DayOfWeek-Friday-Min'), Dict::S('DayOfWeek-Saturday-Min')));
  214. $sJSMonthsShort = json_encode(array(Dict::S('Month-01-Short'), Dict::S('Month-02-Short'), Dict::S('Month-03-Short'), Dict::S('Month-04-Short'), Dict::S('Month-05-Short'), Dict::S('Month-06-Short'),
  215. Dict::S('Month-07-Short'), Dict::S('Month-08-Short'), Dict::S('Month-09-Short'), Dict::S('Month-10-Short'), Dict::S('Month-11-Short'), Dict::S('Month-12-Short')));
  216. $iFirstDayOfWeek = (int) Dict::S('Calendar-FirstDayOfWeek');
  217. $sJSDateFormat = json_encode(AttributeDate::GetFormat()->ToDatePicker());
  218. $sTimeFormat = AttributeDateTime::GetFormat()->ToTimeFormat();
  219. $oTimeFormat = new DateTimeFormat($sTimeFormat);
  220. $sJSTimeFormat = json_encode($oTimeFormat->ToDatePicker());
  221. $sJSLangShort = json_encode(strtolower(substr(Dict::GetUserLanguage(), 0, 2)));
  222. $sJSOk = json_encode(Dict::S('UI:Button:Ok'));
  223. if ($this->oField->IsDateOnly())
  224. {
  225. $oOutput->AddJs(
  226. <<<EOF
  227. $("#{$this->oField->GetGlobalId()}").datepicker({
  228. showOn: 'button',
  229. buttonImage: '../images/calendar.png',
  230. buttonImageOnly: true,
  231. dateFormat: $sJSDateFormat,
  232. constrainInput: false,
  233. changeMonth: true,
  234. changeYear: true,
  235. dayNamesMin: $sJSDaysMin,
  236. monthNamesShort: $sJSMonthsShort,
  237. firstDay: $iFirstDayOfWeek
  238. });
  239. EOF
  240. );
  241. }
  242. else
  243. {
  244. $oOutput->AddJs(
  245. <<<EOF
  246. $("#{$this->oField->GetGlobalId()}").datetimepicker({
  247. showOn: 'button',
  248. buttonImage: '../images/calendar.png',
  249. buttonImageOnly: true,
  250. dateFormat: $sJSDateFormat,
  251. constrainInput: false,
  252. changeMonth: true,
  253. changeYear: true,
  254. dayNamesMin: $sJSDaysMin,
  255. monthNamesShort: $sJSMonthsShort,
  256. firstDay: $iFirstDayOfWeek,
  257. // time picker options
  258. timeFormat: $sJSTimeFormat,
  259. controlType: 'select',
  260. closeText: $sJSOk
  261. });
  262. EOF
  263. );
  264. }
  265. $oOutput->AddJs(
  266. <<<EOF
  267. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  268. var me = this;
  269. $(this).closest(".field_set").trigger("field_change", {
  270. id: $(me).attr("id"),
  271. name: $(me).closest(".form_field").attr("data-field-id"),
  272. value: $(me).val()
  273. })
  274. .closest('.form_handler').trigger('value_change');
  275. });
  276. EOF
  277. );
  278. break;
  279. break;
  280. case 'Combodo\\iTop\\Form\\Field\\StringField':
  281. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  282. $oOutput->AddJs(
  283. <<<EOF
  284. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  285. var me = this;
  286. $(this).closest(".field_set").trigger("field_change", {
  287. id: $(me).attr("id"),
  288. name: $(me).closest(".form_field").attr("data-field-id"),
  289. value: $(me).val()
  290. })
  291. .closest('.form_handler').trigger('value_change');
  292. });
  293. EOF
  294. );
  295. break;
  296. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  297. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  298. case 'Combodo\\iTop\\Form\\Field\\DurationField':
  299. $oOutput->AddJs(
  300. <<<EOF
  301. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  302. var me = this;
  303. $(this).closest(".field_set").trigger("field_change", {
  304. id: $(me).attr("id"),
  305. name: $(me).closest(".form_field").attr("data-field-id"),
  306. value: $(me).val()
  307. })
  308. .closest('.form_handler').trigger('value_change');
  309. });
  310. EOF
  311. );
  312. break;
  313. }
  314. // JS Form field widget construct
  315. $aValidators = array();
  316. foreach ($this->oField->GetValidators() as $oValidator)
  317. {
  318. $aValidators[$oValidator::GetName()] = array(
  319. 'reg_exp' => $oValidator->GetRegExp(),
  320. 'message' => Dict::S($oValidator->GetErrorMessage())
  321. );
  322. }
  323. $sValidators = json_encode($aValidators);
  324. $sFormFieldOptions =
  325. <<<EOF
  326. {
  327. validators: $sValidators,
  328. on_validation_callback: function(me, oResult) {
  329. var oValidationElement = $(me.element).find('span.form_validation');
  330. if (oResult.is_valid)
  331. {
  332. oValidationElement.html('');
  333. }
  334. else
  335. {
  336. //TODO: escape html entities
  337. var sExplain = oResult.error_messages.join(', ');
  338. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  339. oValidationElement.tooltip({
  340. items: 'span',
  341. tooltipClass: 'form_field_error',
  342. content: function() {
  343. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  344. }
  345. });
  346. }
  347. }
  348. }
  349. EOF
  350. ;
  351. $oOutput->AddJs(
  352. <<<EOF
  353. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  354. EOF
  355. );
  356. switch ($sFieldClass)
  357. {
  358. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  359. $oOutput->AddJs(
  360. <<<EOF
  361. $("[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();});
  362. EOF
  363. );
  364. break;
  365. }
  366. return $oOutput;
  367. }
  368. }