BaseDataProvider.php 2.8 KB

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