formrenderer.class.inc.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 \Combodo\iTop\Form\Form;
  20. /**
  21. * Description of FormRenderer
  22. *
  23. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  24. */
  25. abstract class FormRenderer
  26. {
  27. protected $oForm;
  28. protected $sEndpoint;
  29. protected $aSupportedFields;
  30. protected $sBaseLayout;
  31. protected $aOutputs;
  32. /**
  33. * Default constructor
  34. *
  35. * @param \Combodo\iTop\Form\Form $oForm
  36. */
  37. public function __construct(Form $oForm = null)
  38. {
  39. if ($oForm !== null)
  40. {
  41. $this->oForm = $oForm;
  42. }
  43. $this->sBaseLayout = '';
  44. $this->InitOutputs();
  45. }
  46. public function GetForm()
  47. {
  48. return $this->oForm;
  49. }
  50. public function SetForm($oForm)
  51. {
  52. $this->oForm = $oForm;
  53. return $this;
  54. }
  55. public function GetEndpoint()
  56. {
  57. return $this->sEndpoint;
  58. }
  59. public function SetEndpoint($sEndpoint)
  60. {
  61. $this->sEndpoint = $sEndpoint;
  62. return $this;
  63. }
  64. public function GetBaseLayout()
  65. {
  66. return $this->sBaseLayout;
  67. }
  68. public function SetBaseLayout($sBaseLayout)
  69. {
  70. $this->sBaseLayout = $sBaseLayout;
  71. return $this;
  72. }
  73. public function GetFieldRendererClass($oField)
  74. {
  75. if (array_key_exists(get_class($oField), $this->aSupportedFields))
  76. {
  77. return $this->aSupportedFields[get_class($oField)];
  78. }
  79. else
  80. {
  81. // TODO : We might want to throw an exception.
  82. return null;
  83. }
  84. }
  85. /**
  86. * Returns the field identified by the id $sId in $this->oForm.
  87. *
  88. * @param string $sId
  89. * @return Combodo\iTop\Renderer\FieldRenderer
  90. */
  91. public function GetFieldRendererClassFromId($sId)
  92. {
  93. return $this->GetFieldRendererClass($this->oForm->GetField($sId));
  94. }
  95. /**
  96. *
  97. * @return array
  98. */
  99. public function GetOutputs()
  100. {
  101. return $this->aOutputs;
  102. }
  103. /**
  104. * Registers a Renderer class for the specified Field class.
  105. *
  106. * If the Field class is not fully qualified, the default "Combodo\iTop\Form\Field" will be prepend.
  107. * If the Field class already had a registered Renderer, it is replaced.
  108. *
  109. * @param string $sFieldClass
  110. * @param string $sRendererClass
  111. */
  112. public function AddSupportedField($sFieldClass, $sRendererClass)
  113. {
  114. $sFieldClass = (strpos($sFieldClass, '\\') !== false) ? $sFieldClass : 'Combodo\\iTop\\Form\\Field\\' . $sFieldClass;
  115. $this->aSupportedFields[$sFieldClass] = $sRendererClass;
  116. return $this;
  117. }
  118. public function InitOutputs()
  119. {
  120. $this->aOutputs = array();
  121. return $this;
  122. }
  123. abstract public function Render();
  124. }