BaseDataProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\DataProviders;
  3. use \ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\FormDataProviderInterface;
  4. /**
  5. * BaseDataProvider - form controler witch CRUD implementation
  6. *
  7. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  8. */
  9. abstract class BaseDataProvider implements FormDataProviderInterface
  10. {
  11. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\RequestObjectHandler;
  12. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\RequestFormDataHandler;
  13. protected $data = [];
  14. protected $availableValues = [];
  15. protected $loaded = false;
  16. protected $disabledList = [];
  17. protected $parentFormType = null;
  18. public function __construct()
  19. {
  20. $this->loadFormDataFromRequest();
  21. }
  22. public function create()
  23. {
  24. //to be overwritten if needed
  25. }
  26. abstract public function read();
  27. abstract public function update();
  28. public function delete()
  29. {
  30. //to be overwritten if needed
  31. }
  32. public function reload()
  33. {
  34. //to be overwritten if needed
  35. $this->read();
  36. }
  37. public function getValueById($id)
  38. {
  39. if ($this->data[$id] || $this->data[$id] === 0)
  40. {
  41. return $this->data[$id];
  42. }
  43. return null;
  44. }
  45. public function getAvailableValuesById($id)
  46. {
  47. if (is_array($this->availableValues[$id]) || count($this->availableValues[$id]) > 0)
  48. {
  49. return $this->availableValues[$id];
  50. }
  51. return null;
  52. }
  53. public function getData()
  54. {
  55. return $this->data;
  56. }
  57. public function isDisabledById($id)
  58. {
  59. if (in_array($id, $this->disabledList))
  60. {
  61. return true;
  62. }
  63. return false;
  64. }
  65. public function initData()
  66. {
  67. if ($this->loaded === false)
  68. {
  69. $this->read();
  70. $this->loaded = true;
  71. }
  72. }
  73. protected function setData($data)
  74. {
  75. $this->data = $data;
  76. return $this;
  77. }
  78. protected function setDisabled($id)
  79. {
  80. if (!in_array($id, $this->disabledList))
  81. {
  82. $this->disabledList[] = $id;
  83. }
  84. }
  85. protected function removeFromDisabled($id)
  86. {
  87. if (in_array($id, $this->disabledList))
  88. {
  89. $key = array_search($id, $this->disabledList[]);
  90. if ($key)
  91. {
  92. unset($this->disabledList[$key]);
  93. }
  94. }
  95. }
  96. public function setParentFormType($formType = null)
  97. {
  98. if (is_string($formType) && $formType !== '')
  99. {
  100. $this->parentFormType = $formType;
  101. }
  102. return $this;
  103. }
  104. public function getParentFormType()
  105. {
  106. return $this->parentFormType;
  107. }
  108. }