formmanager.class.inc.php 3.6 KB

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