consolesimplefieldrenderer.class.inc.php 9.4 KB

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