consolesimplefieldrenderer.class.inc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 \Dict;
  20. use Combodo\iTop\Renderer\FieldRenderer;
  21. use Combodo\iTop\Renderer\RenderingOutput;
  22. class ConsoleSimpleFieldRenderer extends FieldRenderer
  23. {
  24. public function Render()
  25. {
  26. $oOutput = new RenderingOutput();
  27. $sFieldClass = get_class($this->oField);
  28. // TODO : Shouldn't we have a field type so we don't have to maintain FQN classname ?
  29. // Rendering field in edition mode
  30. if (!$this->oField->GetReadOnly())
  31. {
  32. switch ($sFieldClass)
  33. {
  34. case 'Combodo\\iTop\\Form\\Field\\StringField':
  35. if ($this->oField->GetLabel() !== '')
  36. {
  37. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  38. }
  39. $oOutput->AddHtml('<input type="text" id="'.$this->oField->GetGlobalId().'" value="' . $this->oField->GetCurrentValue() . '" size="30" />');
  40. $oOutput->AddHtml('<span class="form_validation"></span>');
  41. break;
  42. }
  43. }
  44. // ... and in read-only mode
  45. else
  46. {
  47. switch ($sFieldClass)
  48. {
  49. case 'Combodo\\iTop\\Form\\Field\\StringField':
  50. if ($this->oField->GetLabel() !== '')
  51. {
  52. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">' . $this->oField->GetLabel() . '</label>');
  53. }
  54. $oOutput->AddHtml('<div class="form-control-static">' . $this->oField->GetCurrentValue() . '</div>');
  55. break;
  56. }
  57. }
  58. switch ($sFieldClass)
  59. {
  60. case 'Combodo\\iTop\\Form\\Field\\StringField':
  61. $oOutput->AddJs(
  62. <<<EOF
  63. $("#{$this->oField->GetGlobalId()}").off("change").on("change keyup", function(){
  64. var me = this;
  65. $(this).closest(".field_set").trigger("field_change", {
  66. id: $(me).attr("id"),
  67. name: $(me).closest(".form_field").attr("data-field-id"),
  68. value: $(me).val()
  69. });
  70. });
  71. EOF
  72. );
  73. break;
  74. }
  75. // JS Form field widget construct
  76. $aValidators = array();
  77. foreach ($this->oField->GetValidators() as $oValidator)
  78. {
  79. $aValidators[$oValidator::GetName()] = array(
  80. 'reg_exp' => $oValidator->GetRegExp(),
  81. 'message' => Dict::S($oValidator->GetErrorMessage())
  82. );
  83. }
  84. $sValidators = json_encode($aValidators);
  85. $sFormFieldOptions =
  86. <<<EOF
  87. {
  88. validators: $sValidators,
  89. on_validation_callback: function(me, oResult) {
  90. var oValidationElement = $(me.element).find('span.form_validation');
  91. if (oResult.is_valid)
  92. {
  93. oValidationElement.html('');
  94. }
  95. else
  96. {
  97. //TODO: escape html entities
  98. var sExplain = oResult.error_messages.join(', ');
  99. oValidationElement.html('<img src="../images/validation_error.png" style="vertical-align:middle" data-tooltip="'+sExplain+'"/>');
  100. oValidationElement.tooltip({
  101. items: 'span',
  102. tooltipClass: 'form_field_error',
  103. content: function() {
  104. return $(this).find('img').attr('data-tooltip'); // As opposed to the default 'content' handler, do not escape the contents of 'title'
  105. }
  106. });
  107. }
  108. }
  109. }
  110. EOF
  111. ;
  112. switch ($sFieldClass)
  113. {
  114. case 'Combodo\\iTop\\Form\\Field\\StringField':
  115. case 'Combodo\\iTop\\Form\\Field\\TextAreaField':
  116. case 'Combodo\\iTop\\Form\\Field\\SelectField':
  117. case 'Combodo\\iTop\\Form\\Field\\HiddenField':
  118. case 'Combodo\\iTop\\Form\\Field\\RadioField':
  119. case 'Combodo\\iTop\\Form\\Field\\CheckboxField':
  120. $oOutput->AddJs(
  121. <<<EOF
  122. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").form_field($sFormFieldOptions);
  123. EOF
  124. );
  125. break;
  126. }
  127. return $oOutput;
  128. }
  129. }