bssimplefieldrenderer.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. // Copyright (C) 2010-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\Bootstrap\FieldRenderer;
  19. use \utils;
  20. use \Dict;
  21. use \UserRights;
  22. use \InlineImage;
  23. use \Combodo\iTop\Renderer\FieldRenderer;
  24. use \Combodo\iTop\Renderer\RenderingOutput;
  25. use \Combodo\iTop\Form\Field\TextAreaField;
  26. /**
  27. * Description of BsSimpleFieldRenderer
  28. *
  29. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  30. */
  31. class BsSimpleFieldRenderer extends FieldRenderer
  32. {
  33. /**
  34. * Returns a RenderingOutput for the FieldRenderer's Field
  35. *
  36. * @return \Combodo\iTop\Renderer\RenderingOutput
  37. */
  38. public function Render()
  39. {
  40. $oOutput = new RenderingOutput();
  41. $sFieldClass = get_class($this->oField);
  42. $sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
  43. // Rendering field in edition mode
  44. if (!$this->oField->GetReadOnly() && !$this->oField->GetHidden())
  45. {
  46. switch ($sFieldClass)
  47. {
  48. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  49. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  50. if ($this->oField->GetLabel() !== '')
  51. {
  52. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  53. }
  54. $oOutput->AddHtml('<div class="help-block"></div>');
  55. $oOutput->AddHtml('<div class="input-group date" id="datepicker_' . $this->oField->GetGlobalId() . '">');
  56. $oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('" class="form-control" maxlength="255" />');
  57. $oOutput->AddHtml('<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>');
  58. $oOutput->AddHtml('</div>');
  59. $oOutput->AddHtml('</div>');
  60. $sJSFormat = json_encode($this->oField->GetJSDateTimeFormat());
  61. $oOutput->AddJs(
  62. <<<EOF
  63. $('#datepicker_{$this->oField->GetGlobalId()}').datetimepicker({format: $sJSFormat});
  64. EOF
  65. );
  66. break;
  67. case 'Combodo\\iTop\\Form\\Field\\PasswordField':
  68. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  69. if ($this->oField->GetLabel() !== '')
  70. {
  71. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  72. }
  73. $oOutput->AddHtml('<div class="help-block"></div>');
  74. $oOutput->AddHtml('<input type="password" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" maxlength="255" />');
  75. $oOutput->AddHtml('</div>');
  76. break;
  77. case 'Combodo\\iTop\\Form\\Field\\StringField':
  78. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  79. if ($this->oField->GetLabel() !== '')
  80. {
  81. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  82. }
  83. $oOutput->AddHtml('<div class="help-block"></div>');
  84. $oOutput->AddHtml('<input type="text" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" maxlength="255" />');
  85. $oOutput->AddHtml('</div>');
  86. break;
  87. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  88. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  89. $bRichEditor = ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML);
  90. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  91. if ($this->oField->GetLabel() !== '')
  92. {
  93. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  94. }
  95. $oOutput->AddHtml('<div class="help-block"></div>');
  96. // First the edition area
  97. $oOutput->AddHtml('<div>');
  98. $oOutput->AddHtml('<textarea id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" class="form-control" rows="8">' . $this->oField->GetCurrentValue() . '</textarea>');
  99. $oOutput->AddHtml('</div>');
  100. // Then the previous entries if necessary
  101. if ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\CaseLogField')
  102. {
  103. $aEntries = $this->oField->GetEntries();
  104. if (count($aEntries) > 0)
  105. {
  106. $oOutput->AddHtml('<div>');
  107. for ($i = 0; $i < count($aEntries); $i++)
  108. {
  109. $sEntryDate = $aEntries[$i]['date'];
  110. $sEntryUser = $aEntries[$i]['user_login'];
  111. $sEntryHeader = Dict::Format('UI:CaseLog:Header_Date_UserName', $sEntryDate, $sEntryUser);
  112. // Only the last 2 entries are expanded by default
  113. $sEntryContentExpanded = ($i < 2) ? 'true' : 'false';
  114. $sEntryHeaderButtonClass = ($i < 2) ? '' : 'collapsed';
  115. $sEntryContentClass = ($i < 2) ? 'in' : '';
  116. $sEntryContentId = 'caselog_field_entry_content-' . $this->oField->GetGlobalId() . '-' . $i;
  117. // Note : We use CKEditor stylesheet to format this
  118. $oOutput->AddHtml(
  119. <<<EOF
  120. <div class="caselog_field_entry cke_inner">
  121. <div class="caselog_field_entry_header">
  122. {$sEntryHeader}
  123. <div class="pull-right">
  124. <span class="caselog_field_entry_button {$sEntryHeaderButtonClass}" data-toggle="collapse" href="#{$sEntryContentId}" aria-expanded="{$sEntryContentExpanded}" aria-controls="{$sEntryContentId}"></span>
  125. </div>
  126. </div>
  127. <div class="caselog_field_entry_content collapse {$sEntryContentClass}" id="{$sEntryContentId}">
  128. {$aEntries[$i]['message_html']}
  129. </div>
  130. </div>
  131. EOF
  132. );
  133. }
  134. $oOutput->AddHtml('</div>');
  135. }
  136. }
  137. $oOutput->AddHtml('</div>');
  138. // Some additional stuff if we are displaying it with a rich editor
  139. if ($bRichEditor)
  140. {
  141. $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  142. $oOutput->AddJs(
  143. <<<EOF
  144. $('#{$this->oField->GetGlobalId()}').addClass('htmlEditor');
  145. $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage'});
  146. EOF
  147. );
  148. if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null))
  149. {
  150. $oOutput->AddJs(InlineImage::EnableCKEditorImageUpload($this->oField->GetObject(), utils::GetUploadTempId($this->oField->GetTransactionId())));
  151. }
  152. }
  153. break;
  154. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  155. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  156. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  157. if ($this->oField->GetLabel() !== '')
  158. {
  159. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  160. }
  161. $oOutput->AddHtml('<div class="help-block"></div>');
  162. $oOutput->AddHtml('<select id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" ' . ( ($this->oField->GetMultipleValuesEnabled()) ? 'multiple' : '' ) . ' class="form-control">');
  163. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  164. {
  165. // 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)
  166. $sSelectedAtt = ($this->oField->GetCurrentValue() == $sChoice) ? 'selected' : '';
  167. $oOutput->AddHtml('<option value="' . $sChoice . '" ' . $sSelectedAtt . ' >')->AddHtml($sLabel)->AddHtml('</option>');
  168. }
  169. $oOutput->AddHtml('</select>');
  170. $oOutput->AddHtml('</div>');
  171. break;
  172. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  173. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  174. $sFieldType = ($sFieldClass === 'Combodo\\iTop\\Form\\Field\\RadioField') ? 'radio' : 'checkbox';
  175. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '" id="' . $this->oField->GetGlobalId() . '">');
  176. if ($this->oField->GetLabel() !== '')
  177. {
  178. $oOutput->AddHtml('<div><label class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label></div>');
  179. }
  180. $oOutput->AddHtml('<div class="help-block"></div>');
  181. $oOutput->AddHtml('<div class="btn-group" data-toggle="buttons">');
  182. $i = 0;
  183. foreach ($this->oField->GetChoices() as $sChoice => $sLabel)
  184. {
  185. // 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)
  186. $sCheckedAtt = ($this->oField->IsAmongValues($sChoice)) ? 'checked' : '';
  187. $sCheckedClass = ($this->oField->IsAmongValues($sChoice)) ? 'active' : '';
  188. $oOutput->AddHtml('<label class="btn btn-default ' . $sCheckedClass . '"><input type="' . $sFieldType . '" name="' . $this->oField->GetId() . '" id="' . $this->oField->GetId() . $i . '" value="' . $sChoice . '" ' . $sCheckedAtt . ' />' . $sLabel . '</label>');
  189. $i++;
  190. }
  191. $oOutput->AddHtml('</div>');
  192. $oOutput->AddHtml('</div>');
  193. break;
  194. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  195. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('"/>');
  196. break;
  197. }
  198. }
  199. // ... and in read-only mode (or hidden)
  200. else
  201. {
  202. // ... specific rendering for fields with multiple values
  203. if (($this->oField instanceof Combodo\iTop\Form\Field\MultipleChoicesField) && ($this->oField->GetMultipleValuesEnabled()))
  204. {
  205. // TODO
  206. }
  207. // ... clasic rendering for fields with only one value
  208. else
  209. {
  210. switch ($sFieldClass)
  211. {
  212. case 'Combodo\\iTop\\Form\\Field\\LabelField':
  213. $oOutput->AddHtml('<div class="form-group">');
  214. // Showing label / value only if read-only but not hidden
  215. if (!$this->oField->GetHidden())
  216. {
  217. if ($this->oField->GetLabel() !== '')
  218. {
  219. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  220. }
  221. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('</div>');
  222. }
  223. $oOutput->AddHtml('</div>');
  224. break;
  225. case 'Combodo\\iTop\\Form\\Field\\StringField':
  226. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  227. $bEncodeHtmlEntities = (($sFieldClass === 'Combodo\\iTop\\Form\\Field\\TextAreaField') && ($this->oField->GetFormat() === TextAreaField::ENUM_FORMAT_HTML)) ? false : true;
  228. $oOutput->AddHtml('<div class="form-group">');
  229. // Showing label / value only if read-only but not hidden
  230. if (!$this->oField->GetHidden())
  231. {
  232. if ($this->oField->GetLabel() !== '')
  233. {
  234. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  235. }
  236. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetCurrentValue(), $bEncodeHtmlEntities)->AddHtml('</div>');
  237. }
  238. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetCurrentValue(), true)->AddHtml('" class="form-control" />');
  239. $oOutput->AddHtml('</div>');
  240. break;
  241. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  242. $oOutput->AddHtml('<div class="form-group">');
  243. // Showing label / value only if read-only but not hidden
  244. if (!$this->oField->GetHidden())
  245. {
  246. if ($this->oField->GetLabel() !== '')
  247. {
  248. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  249. }
  250. $oOutput->AddHtml('<div class="form-control-static">')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('</div>');
  251. }
  252. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="')->AddHtml($this->oField->GetDisplayValue(), true)->AddHtml('" class="form-control" />');
  253. $oOutput->AddHtml('</div>');
  254. break;
  255. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  256. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  257. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  258. $aFieldChoices = $this->oField->GetChoices();
  259. $sFieldValue = (isset($aFieldChoices[$this->oField->GetCurrentValue()])) ? $aFieldChoices[$this->oField->GetCurrentValue()] : Dict::S('UI:UndefinedObject');
  260. $oOutput->AddHtml('<div class="form-group">');
  261. // Showing label / value only if read-only but not hidden
  262. if (!$this->oField->GetHidden())
  263. {
  264. if ($this->oField->GetLabel() !== '')
  265. {
  266. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')->AddHtml($this->oField->GetLabel(), true)->AddHtml('</label>');
  267. }
  268. $oOutput->AddHtml('<div class="form-control-static">' . $sFieldValue . '</div>');
  269. }
  270. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $this->oField->GetCurrentValue() . '" class="form-control" />');
  271. $oOutput->AddHtml('</div>');
  272. break;
  273. }
  274. }
  275. }
  276. // JS FieldChange trigger (:input are not always at the same depth)
  277. switch ($sFieldClass)
  278. {
  279. case 'Combodo\\iTop\\Form\\Field\\PasswordField':
  280. case 'Combodo\\iTop\\Form\\Field\\StringField':
  281. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  282. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  283. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  284. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  285. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  286. $oOutput->AddJs(
  287. <<<EOF
  288. $("#{$this->oField->GetGlobalId()}").off("change keyup").on("change keyup", function(){
  289. var me = this;
  290. $(this).closest(".field_set").trigger("field_change", {
  291. id: $(me).attr("id"),
  292. name: $(me).closest(".form_field").attr("data-field-id"),
  293. value: $(me).val()
  294. });
  295. });
  296. EOF
  297. );
  298. break;
  299. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  300. // We need the focusout event has the datepicker widget seems to override the change event
  301. $oOutput->AddJs(
  302. <<<EOF
  303. $("#{$this->oField->GetGlobalId()}").off("change keyup focusout").on("change keyup focusout", function(){
  304. var me = this;
  305. $(this).closest(".field_set").trigger("field_change", {
  306. id: $(me).attr("id"),
  307. name: $(me).closest(".form_field").attr("data-field-id"),
  308. value: $(me).val()
  309. });
  310. });
  311. EOF
  312. );
  313. break;
  314. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  315. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  316. $oOutput->AddJs(
  317. <<<EOF
  318. $("#{$this->oField->GetGlobalId()} input").off("change").on("change", function(){
  319. var me = this;
  320. $(this).closest(".field_set").trigger("field_change", {
  321. id: $(me).closest("#{$this->oField->GetGlobalId()}").attr("id"),
  322. name: $(me).attr("name"),
  323. value: $(me).val()
  324. });
  325. });
  326. EOF
  327. );
  328. break;
  329. }
  330. // JS Form field widget construct
  331. $aValidators = array();
  332. foreach ($this->oField->GetValidators() as $oValidator)
  333. {
  334. $aValidators[$oValidator::GetName()] = array(
  335. 'reg_exp' => $oValidator->GetRegExp(),
  336. 'message' => Dict::S($oValidator->GetErrorMessage())
  337. );
  338. }
  339. $sFormFieldOptions = json_encode(array(
  340. 'validators' => $aValidators
  341. ));
  342. switch ($sFieldClass)
  343. {
  344. case 'Combodo\\iTop\\Form\\Field\\PasswordField':
  345. case 'Combodo\\iTop\\Form\\Field\\StringField':
  346. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  347. case 'Combodo\\iTop\\Form\\Field\\MultipleSelectField':
  348. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  349. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  350. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  351. case 'Combodo\\iTop\\Form\\Field\\DateTimeField':
  352. $oOutput->AddJs(
  353. <<<EOF
  354. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field($sFormFieldOptions);
  355. EOF
  356. );
  357. break;
  358. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  359. case 'Combodo\\iTop\\Form\\Field\\CaseLogField':
  360. $oOutput->AddJs(
  361. <<<EOF
  362. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field_html($sFormFieldOptions);
  363. EOF
  364. );
  365. // MagnificPopup on images
  366. $oOutput->AddJs(InlineImage::FixImagesWidth());
  367. break;
  368. }
  369. return $oOutput;
  370. }
  371. }