fieldrenderer.class.inc.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // Copyright (C) 2010-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;
  19. use \Dict;
  20. use \DBObject;
  21. use \Combodo\iTop\Form\Field\Field;
  22. /**
  23. * Description of FieldRenderer
  24. *
  25. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  26. */
  27. abstract class FieldRenderer
  28. {
  29. protected $oField;
  30. protected $sEndpoint;
  31. /**
  32. * Default constructor
  33. *
  34. * @param \Combodo\iTop\Form\Field\Field $oField
  35. */
  36. public function __construct(Field $oField)
  37. {
  38. $this->oField = $oField;
  39. }
  40. /**
  41. *
  42. * @return string
  43. */
  44. public function GetEndpoint()
  45. {
  46. return $this->sEndpoint;
  47. }
  48. /**
  49. *
  50. * @param string $sEndpoint
  51. */
  52. public function SetEndpoint($sEndpoint)
  53. {
  54. $this->sEndpoint = $sEndpoint;
  55. return $this;
  56. }
  57. /**
  58. * Returns a JSON encoded string that contains the field's validators as an array.
  59. *
  60. * eg :
  61. * {
  62. * validator_id_1 : {reg_exp: /[0-9]/, message: "Error message"},
  63. * validator_id_2 : {reg_exp: /[a-z]/, message: "Another error message"},
  64. * ...
  65. * }
  66. *
  67. * @return string
  68. */
  69. protected function GetValidatorsAsJson()
  70. {
  71. $aValidators = array();
  72. foreach ($this->oField->GetValidators() as $oValidator)
  73. {
  74. $aValidators[$oValidator::GetName()] = array(
  75. 'reg_exp' => $oValidator->GetRegExp(),
  76. 'message' => Dict::S($oValidator->GetErrorMessage())
  77. );
  78. }
  79. // - Formatting options
  80. return json_encode($aValidators);
  81. }
  82. /**
  83. * Renders a Field as a RenderingOutput
  84. *
  85. * @return \Combodo\iTop\Renderer\RenderingOutput
  86. */
  87. abstract public function Render();
  88. }