consoleselectobjectfieldrenderer.class.inc.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Combodo\iTop\Form\Validator\MandatoryValidator;
  21. use Combodo\iTop\Form\Validator\Validator;
  22. use \Dict;
  23. use \DBObjectSet;
  24. use Combodo\iTop\Renderer\FieldRenderer;
  25. use Combodo\iTop\Renderer\RenderingOutput;
  26. class ConsoleSelectObjectFieldRenderer extends FieldRenderer
  27. {
  28. public function Render()
  29. {
  30. $oOutput = new RenderingOutput();
  31. $oOutput->AddHtml('<table class="form-field-container">');
  32. $oOutput->AddHtml('<tr>');
  33. if ($this->oField->GetLabel() != '')
  34. {
  35. $oOutput->AddHtml('<td class="form-field-label label"><span><label for="'.$this->oField->GetGlobalId().'">'.$this->oField->GetLabel().'</label></span></td>');
  36. }
  37. $oOutput->AddHtml('<td class="form-field-content">');
  38. $sEditType = 'none';
  39. if ($this->oField->GetReadOnly())
  40. {
  41. $oSearch = $this->oField->GetSearch()->DeepClone();
  42. $oSearch->AddCondition('id', $this->oField->GetCurrentValue());
  43. $oSet = new DBObjectSet($oSearch);
  44. $oObject = $oSet->Fetch();
  45. if ($oObject)
  46. {
  47. $sCurrentLabel = $oObject->Get('friendlyname');
  48. }
  49. else
  50. {
  51. $sCurrentLabel = '';
  52. }
  53. $oOutput->AddHtml('<input type="hidden" id="'.$this->oField->GetGlobalId().'" value="' . htmlentities($this->oField->GetCurrentValue(), ENT_QUOTES, 'UTF-8') . '"/>');
  54. $oOutput->AddHtml('<span class="form-field-data">'.htmlentities($sCurrentLabel, ENT_QUOTES, 'UTF-8').'</span>');
  55. }
  56. else
  57. {
  58. $oSearch = $this->oField->GetSearch()->DeepClone();
  59. $oSearch->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true);
  60. $oSet = new \DBObjectSet($oSearch);
  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. $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. else
  98. {
  99. // Drop-down select
  100. $sEditType = 'select';
  101. $oOutput->AddHtml('<select class="form-field-data" id="'.$this->oField->GetGlobalId().'">');
  102. $oOutput->AddHtml('<option value="">'.Dict::S('UI:SelectOne').'</option>');
  103. while ($oObject = $oSet->Fetch())
  104. {
  105. $iObject = $oObject->GetKey();
  106. $sLabel = $oObject->Get('friendlyname');
  107. // 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)
  108. $sSelectedAtt = ($this->oField->GetCurrentValue() == $iObject) ? 'selected' : '';
  109. $oOutput->AddHtml('<option value="'.$iObject.'" '.$sSelectedAtt.' >'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>');
  110. }
  111. $oOutput->AddHtml('</select>');
  112. }
  113. $oOutput->AddJs(
  114. <<<EOF
  115. $("#{$this->oField->GetGlobalId()}").off("change").on("change", function(){
  116. var me = this;
  117. $(this).closest(".field_set").trigger("field_change", {
  118. id: $(me).attr("id"),
  119. name: $(me).closest(".form_field").attr("data-field-id"),
  120. value: $(me).val()
  121. })
  122. .closest('.form_handler').trigger('value_change');
  123. });
  124. EOF
  125. );
  126. }
  127. $oOutput->AddHtml('<span class="form_validation"></span>');
  128. $oOutput->AddHtml('</td>');
  129. $oOutput->AddHtml('</tr>');
  130. $oOutput->AddHtml('</table>');
  131. // JS Form field widget construct
  132. $aValidators = array();
  133. foreach ($this->oField->GetValidators() as $oValidator)
  134. {
  135. if ($oValidator::GetName() == 'notemptyextkey')
  136. {
  137. // The autocomplete widget returns an empty string if the value is undefined (and the select has been aligned with this behavior)
  138. $oValidator = new MandatoryValidator();
  139. }
  140. $aValidators[$oValidator::GetName()] = array(
  141. 'reg_exp' => $oValidator->GetRegExp(),
  142. 'message' => Dict::S($oValidator->GetErrorMessage())
  143. );
  144. }
  145. $sValidators = json_encode($aValidators);
  146. $sFormFieldOptions =
  147. <<<EOF
  148. {
  149. validators: $sValidators,
  150. on_validation_callback: function(me, oResult) {
  151. var oValidationElement = $(me.element).find('span.form_validation');
  152. if (oResult.is_valid)
  153. {
  154. oValidationElement.html('');
  155. }
  156. else
  157. {
  158. //TODO: escape html entities
  159. var sExplain = oResult.error_messages.join(', ');
  160. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  161. oValidationElement.tooltip({
  162. items: 'span',
  163. tooltipClass: 'form_field_error',
  164. content: function() {
  165. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  166. }
  167. });
  168. }
  169. }
  170. }
  171. EOF
  172. ;
  173. $oOutput->AddJs(
  174. <<<EOF
  175. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  176. EOF
  177. );
  178. switch ($sEditType)
  179. {
  180. case 'autocomplete':
  181. $oOutput->AddJs(
  182. <<<EOF
  183. $("[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();});
  184. EOF
  185. );
  186. break;
  187. case 'select':
  188. $oOutput->AddJs(
  189. <<<EOF
  190. $("[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();});
  191. EOF
  192. );
  193. break;
  194. case 'none':
  195. default:
  196. // Not editable
  197. }
  198. return $oOutput;
  199. }
  200. }