formmanager.class.inc.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Form;
  19. use \Combodo\iTop\Form\Form;
  20. use \Combodo\iTop\Renderer\FormRenderer;
  21. /**
  22. * Description of formmanager
  23. *
  24. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  25. */
  26. abstract class FormManager
  27. {
  28. protected $oForm;
  29. protected $oRenderer;
  30. /**
  31. * Creates an instance of \Combodo\iTop\Form\FormManager from JSON data that must contain at least :
  32. * - formrenderer_class : The class of the FormRenderer to use in the FormManager
  33. * - formrenderer_endpoint : The endpoint of the renderer
  34. *
  35. * @param string $sJson
  36. * @return \Combodo\iTop\Form\FormManager
  37. */
  38. static function FromJSON($sJson)
  39. {
  40. // Overload in child class when needed
  41. $aJson = json_decode($sJson, true);
  42. $oFormManager = new static();
  43. $sFormRendererClass = $aJson['formrenderer_class'];
  44. $oFormRenderer = new $sFormRendererClass();
  45. $oFormRenderer->SetEndpoint($aJson['formrenderer_endpoint']);
  46. $oFormManager->SetRenderer($oFormRenderer);
  47. return $oFormManager;
  48. }
  49. public function __construct()
  50. {
  51. // Overload in child class when needed
  52. }
  53. /**
  54. *
  55. * @return \Combodo\iTop\Form\Form
  56. */
  57. public function GetForm()
  58. {
  59. return $this->oForm;
  60. }
  61. /**
  62. *
  63. * @param \Combodo\iTop\Form\Form $oForm
  64. * @return \Combodo\iTop\Form\FormManager
  65. */
  66. public function SetForm(Form $oForm)
  67. {
  68. $this->oForm = $oForm;
  69. return $this;
  70. }
  71. /**
  72. *
  73. * @return \Combodo\iTop\Renderer\FormRenderer
  74. */
  75. public function GetRenderer()
  76. {
  77. return $this->oRenderer;
  78. }
  79. /**
  80. *
  81. * @param \Combodo\iTop\Renderer\FormRenderer $oRenderer
  82. * @return \Combodo\iTop\Form\FormManager
  83. */
  84. public function SetRenderer(FormRenderer $oRenderer)
  85. {
  86. $this->oRenderer = $oRenderer;
  87. return $this;
  88. }
  89. /**
  90. *
  91. * @return string
  92. */
  93. public function GetClass()
  94. {
  95. return get_class($this);
  96. }
  97. /**
  98. * Creates a JSON string from the current object including :
  99. * - id : Id of the current Form
  100. * - formmanager_class
  101. * - formrenderer_class
  102. * - formrenderer_endpoint
  103. *
  104. * @return string
  105. */
  106. public function ToJSON()
  107. {
  108. // Overload in child class when needed
  109. return array(
  110. 'id' => $this->oForm->GetId(),
  111. 'formmanager_class' => $this->GetClass(),
  112. 'formrenderer_class' => get_class($this->GetRenderer()),
  113. 'formrenderer_endpoint' => $this->GetRenderer()->GetEndpoint()
  114. );
  115. }
  116. abstract public function Build();
  117. abstract public function OnUpdate($aArgs = null);
  118. abstract public function OnSubmit($aArgs = null);
  119. abstract public function OnCancel($aArgs = null);
  120. }