consoleselectobjectfieldrenderer.class.inc.php 9.0 KB

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