formmanager.class.inc.php 3.5 KB

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