consoleselectobjectfieldrenderer.class.inc.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\Validator\MandatoryValidator;
  20. use \Dict;
  21. use \DBObjectSet;
  22. use Combodo\iTop\Renderer\FieldRenderer;
  23. use Combodo\iTop\Renderer\RenderingOutput;
  24. use Combodo\iTop\Form\Field\SelectObjectField;
  25. class ConsoleSelectObjectFieldRenderer extends FieldRenderer
  26. {
  27. public function Render()
  28. {
  29. $oOutput = new RenderingOutput();
  30. $oOutput->AddHtml('<table class="form-field-container">');
  31. $oOutput->AddHtml('<tr>');
  32. if ($this->oField->GetLabel() != '')
  33. {
  34. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  35. }
  36. $oOutput->AddHtml('<td class="form-field-content">');
  37. $sEditType = 'none';
  38. if ($this->oField->GetReadOnly())
  39. {
  40. $oSearch = $this->oField->GetSearch()->DeepClone();
  41. $oSearch->AddCondition('id', $this->oField->GetCurrentValue());
  42. $oSet = new DBObjectSet($oSearch);
  43. $oObject = $oSet->Fetch();
  44. if ($oObject)
  45. {
  46. $sCurrentLabel = $oObject->Get('friendlyname');
  47. }
  48. else
  49. {
  50. $sCurrentLabel = '';
  51. }
  52. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  53. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  54. }
  55. else
  56. {
  57. $oSearch = $this->oField->GetSearch()->DeepClone();
  58. $oSearch->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true);
  59. $oSet = new \DBObjectSet($oSearch);
  60. $oSet->ApplyParameters();
  61. $oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array('friendlyname')));
  62. $sTargetClass = $oSearch->GetClass();
  63. $oAllowedValues = new DBObjectSet($oSearch);
  64. $iMaxComboLength = $this->oField->GetMaximumComboLength();
  65. $iCount = $oAllowedValues->Count();
  66. if ($iCount > $iMaxComboLength)
  67. {
  68. // Auto-complete
  69. //
  70. $sEditType = 'autocomplete';
  71. $aExtKeyParams = array();
  72. $aExtKeyParams['iFieldSize'] = 10;
  73. $aExtKeyParams['iMinChars'] = $this->oField->GetMinAutoCompleteChars();
  74. $sFieldName = $this->oField->GetGlobalId();
  75. $sFieldId = $sFieldName;
  76. $sFormPrefix = '';
  77. $oWidget = new \UIExtKeyWidget($sTargetClass, $sFieldId, '', true);
  78. $aArgs = array();
  79. $sDisplayStyle = 'select';
  80. $sTitle = $this->oField->GetLabel();
  81. require_once(APPROOT.'application/capturewebpage.class.inc.php');
  82. $oPage = new \CaptureWebPage();
  83. $sHTMLValue = $oWidget->Display($oPage, $iMaxComboLength, false /* $bAllowTargetCreation */, $sTitle, $oSet, $this->oField->GetCurrentValue(), $sFieldId, $this->oField->GetMandatory(), $sFieldName, $sFormPrefix, $aArgs, null, $sDisplayStyle);
  84. $oOutput->AddHtml($sHTMLValue);
  85. $oOutput->AddHtml($oPage->GetHtml());
  86. $oOutput->AddJs($oPage->GetJS());
  87. $oOutput->AddJs($oPage->GetReadyJS());
  88. foreach ($oPage->GetCSS() as $sCss)
  89. {
  90. $oOutput->AddCss($sCss);
  91. }
  92. foreach ($oPage->GetJSFiles() as $sFile)
  93. {
  94. $oOutput->AddJsFile($sFile);
  95. }
  96. foreach ($oPage->GetCSSFiles() as $sFile)
  97. {
  98. $oOutput->AddCssFile($sFile);
  99. }
  100. }
  101. elseif($this->oField->GetControlType() == SelectObjectField::CONTROL_RADIO_VERTICAL)
  102. {
  103. // Radio buttons (vertical)
  104. //
  105. $sEditType = 'radio';
  106. $bVertical = true;
  107. $idx = 0;
  108. $bMandatory = $this->oField->GetMandatory();
  109. $value = $this->oField->GetCurrentValue();
  110. $sId = $this->oField->GetGlobalId();
  111. $oOutput->AddHtml('<div>');
  112. while ($oObject = $oSet->Fetch())
  113. {
  114. $iObject = $oObject->GetKey();
  115. $sLabel = $oObject->Get('friendlyname');
  116. if (($iCount == 1) && $bMandatory)
  117. {
  118. // When there is only once choice, select it by default
  119. $sSelected = ' checked';
  120. }
  121. else
  122. {
  123. $sSelected = ($value == $iObject) ? ' checked' : '';
  124. }
  125. $oOutput->AddHtml("<input type=\"radio\" id=\"{$sId}_{$iObject}\" name=\"radio_$sId\" onChange=\"$('#{$sId}').val(this.value).trigger('change');\" value=\"$iObject\"$sSelected><label class=\"radio\" for=\"{$sId}_{$iObject}\">&nbsp;".htmlentities($sLabel, ENT_QUOTES, 'UTF-8')."</label>&nbsp;");
  126. if ($bVertical)
  127. {
  128. $oOutput->AddHtml("<br>\n");
  129. }
  130. $idx++;
  131. }
  132. $oOutput->AddHtml('</div>');
  133. $oOutput->AddHtml("<input type=\"hidden\" id=\"$sId\" name=\"$sId\" value=\"$value\"/>");
  134. }
  135. else
  136. {
  137. // Drop-down select
  138. //
  139. $sEditType = 'select';
  140. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'">');
  141. $oOutput->AddHtml('<option value="">'.Dict::S('UI:SelectOne').'</option>');
  142. while ($oObject = $oSet->Fetch())
  143. {
  144. $iObject = $oObject->GetKey();
  145. $sLabel = $oObject->Get('friendlyname');
  146. // 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)
  147. $sSelectedAtt = ($this->oField->GetCurrentValue() == $iObject) ? 'selected' : '';
  148. $oOutput->AddHtml('<option value="'.$iObject.'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  149. }
  150. $oOutput->AddHtml('</select>');
  151. }
  152. $oOutput->AddJs(
  153. <<<EOF
  154. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  155. var me = this;
  156. $(this).closest(".field_set").trigger("field_change", {
  157. id: $(me).attr("id"),
  158. name: $(me).closest(".form_field").attr("data-field-id"),
  159. value: $(me).val()
  160. })
  161. .closest('.form_handler').trigger('value_change');
  162. });
  163. EOF
  164. );
  165. }
  166. $oOutput->AddHtml('<span class="form_validation"></span>');
  167. $oOutput->AddHtml('</td>');
  168. $oOutput->AddHtml('</tr>');
  169. $oOutput->AddHtml('</table>');
  170. // JS Form field widget construct
  171. $aValidators = array();
  172. foreach ($this->oField->GetValidators() as $oValidator)
  173. {
  174. if ($oValidator::GetName() == 'notemptyextkey')
  175. {
  176. // The autocomplete widget returns an empty string if the value is undefined (and the select has been aligned with this behavior)
  177. $oValidator = new MandatoryValidator();
  178. }
  179. $aValidators[$oValidator::GetName()] = array(
  180. 'reg_exp' => $oValidator->GetRegExp(),
  181. 'message' => Dict::S($oValidator->GetErrorMessage())
  182. );
  183. }
  184. $sValidators = json_encode($aValidators);
  185. $sFormFieldOptions =
  186. <<<EOF
  187. {
  188. validators: $sValidators,
  189. on_validation_callback: function(me, oResult) {
  190. var oValidationElement = $(me.element).find('span.form_validation');
  191. if (oResult.is_valid)
  192. {
  193. oValidationElement.html('');
  194. }
  195. else
  196. {
  197. //TODO: escape html entities
  198. var sExplain = oResult.error_messages.join(', ');
  199. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  200. oValidationElement.tooltip({
  201. items: 'span',
  202. tooltipClass: 'form_field_error',
  203. content: function() {
  204. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  205. }
  206. });
  207. }
  208. }
  209. }
  210. EOF
  211. ;
  212. $oOutput->AddJs(
  213. <<<EOF
  214. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  215. EOF
  216. );
  217. switch ($sEditType)
  218. {
  219. case 'autocomplete':
  220. case 'radio':
  221. $oOutput->AddJs(
  222. <<<EOF
  223. $("[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('#{$this->oField->GetGlobalId()}').val();});
  224. EOF
  225. );
  226. break;
  227. case 'select':
  228. $oOutput->AddJs(
  229. <<<EOF
  230. $("[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();});
  231. EOF
  232. );
  233. break;
  234. case 'none':
  235. default:
  236. // Not editable
  237. }
  238. return $oOutput;
  239. }
  240. }