formmanager.class.inc.php 3.4 KB

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