formmanager.class.inc.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. static function FromJSON($sJson)
  31. {
  32. // Overload in child class when needed
  33. $aJson = json_decode($sJson, true);
  34. $oFormManager = new static();
  35. $sFormRendererClass = $aJson['formrenderer_class'];
  36. $oFormRenderer = new $sFormRendererClass();
  37. $oFormRenderer->SetEndpoint($aJson['formrenderer_endpoint']);
  38. $oFormManager->SetRenderer($oFormRenderer);
  39. return $oFormManager;
  40. }
  41. public function __construct()
  42. {
  43. // Overload in child class when needed
  44. }
  45. /**
  46. * @return Form
  47. */
  48. public function GetForm()
  49. {
  50. return $this->oForm;
  51. }
  52. /**
  53. * @return FormRenderer
  54. */
  55. public function GetRenderer()
  56. {
  57. return $this->oRenderer;
  58. }
  59. public function SetRenderer(FormRenderer $oRenderer)
  60. {
  61. $this->oRenderer = $oRenderer;
  62. return $this;
  63. }
  64. public function GetClass()
  65. {
  66. return get_class($this);
  67. }
  68. public function ToJSON()
  69. {
  70. // Overload in child class when needed
  71. return array(
  72. 'id' => $this->oForm->GetId(),
  73. 'formmanager_class' => $this->GetClass(),
  74. 'formrenderer_class' => get_class($this->GetRenderer()),
  75. 'formrenderer_endpoint' => $this->GetRenderer()->GetEndpoint()
  76. );
  77. }
  78. abstract public function Build();
  79. abstract public function OnUpdate($aArgs = null);
  80. abstract public function OnSubmit($aArgs = null);
  81. abstract public function OnCancel($aArgs = null);
  82. }