DataTable.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable;
  3. use \ModulesGarden\ProxmoxAddon\Core\UI\Builder\BaseContainer;
  4. use \ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataProviders;
  5. use \ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates;
  6. use \ModulesGarden\ProxmoxAddon\Core\DependencyInjection\DependencyInjection;
  7. /**
  8. * Description of Service
  9. *
  10. * @author inbs
  11. */
  12. class DataTable extends BaseContainer implements \ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AjaxElementInterface
  13. {
  14. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\DatatableActionButtons;
  15. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\DatatableMassActionButtons;
  16. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\VSortable;
  17. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\TitleButtons;
  18. use \ModulesGarden\ProxmoxAddon\Core\UI\Traits\TableLength;
  19. protected $name = 'dataTable';
  20. protected $key = 'id';
  21. protected $type = ['id' => 'int'];
  22. protected $recordsSet = [];
  23. protected $sort = [];
  24. protected $columns = [];
  25. protected $isActive = true;
  26. protected $html = '';
  27. protected $config = [];
  28. protected $dataProvider = null;
  29. protected $searchable = true;
  30. protected $vueComponent = true;
  31. protected $defaultVueComponentName = 'mg-datatable';
  32. protected $searchBarButtonsVisible = 1;
  33. protected $dropdawnWrapper = null;
  34. protected $elementsContainers = ['elements', 'buttons'];
  35. public function __construct($baseId = null)
  36. {
  37. parent::__construct($baseId);
  38. $this->_construct();
  39. }
  40. protected function _construct()
  41. {
  42. $this->loadHtml();
  43. $this->customTplVars['columns'] = $this->columns;
  44. $this->customTplVars['jsDrawFunctions'] = $this->getJsDrawFunctions();
  45. }
  46. protected function getJsDrawFunctions()
  47. {
  48. $functionsList = [];
  49. foreach ($this->columns as $column)
  50. {
  51. if ($column->getCustomJsDrawFunction() !== null)
  52. {
  53. $functionsList[$column->name] = $column->getCustomJsDrawFunction();
  54. }
  55. }
  56. return $functionsList;
  57. }
  58. public function returnAjaxData()
  59. {
  60. $this->loadHtml();
  61. $this->loadData($this->columns);
  62. $this->parseDataRecords();
  63. $returnTemplate = self::getVueComponents();
  64. return (new ResponseTemplates\RawDataJsonResponse(['recordsSet' => $this->recordsSet, 'template' => $returnTemplate,
  65. 'registrations' => self::getVueComponentsRegistrations()]))->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
  66. }
  67. public function setName($name = null)
  68. {
  69. $this->name = $name;
  70. return $this;
  71. }
  72. public function getName()
  73. {
  74. return $this->name;
  75. }
  76. protected function setKey($key)
  77. {
  78. $this->key = $key;
  79. return $this;
  80. }
  81. protected function setStatus($status)
  82. {
  83. $this->isActive = $status;
  84. return $this;
  85. }
  86. protected function addColumn(Column $column)
  87. {
  88. if (!array_key_exists($column->name, $this->columns))
  89. {
  90. $this->columns[$column->name] = $column;
  91. }
  92. return $this;
  93. }
  94. public function setData(array $data = [])
  95. {
  96. $this->recordsSet = $data;
  97. return $this;
  98. }
  99. protected function loadData()
  100. {
  101. //do nothing
  102. }
  103. protected function loadHtml()
  104. {
  105. //do nothing
  106. }
  107. protected function getCount()
  108. {
  109. return count($this->recordsSet['records']);
  110. }
  111. protected function getRecords()
  112. {
  113. return $this->recordsSet;
  114. }
  115. protected function setHtml($html)
  116. {
  117. $this->html = $html;
  118. return $this;
  119. }
  120. public function setDataProvider(DataProviders\DataProvider $dataProv)
  121. {
  122. $this->dataProvider = $dataProv;
  123. if (!$this->columns)
  124. {
  125. $this->loadHtml();
  126. }
  127. $this->setData((array)$this->dataProvider->getData($this->columns));
  128. }
  129. protected function parseDataRecords()
  130. {
  131. $replacementFunctions = $this->getReplacementFunctions();
  132. if (count($replacementFunctions) === 0)
  133. {
  134. return false;
  135. }
  136. foreach ($this->recordsSet['records'] as $key => $row)
  137. {
  138. $this->recordsSet['records'][$key] = $this->replaceRowData($row, $replacementFunctions);
  139. }
  140. }
  141. protected function replaceRowData($row, $replacementFunctions)
  142. {
  143. foreach ($replacementFunctions as $colName => $functionName)
  144. {
  145. if (method_exists($this, $functionName))
  146. {
  147. $this->setValueForDataRow($row, $colName, $this->{$functionName}($colName, $row));
  148. }
  149. }
  150. return $row;
  151. }
  152. protected function getReplacementFunctions()
  153. {
  154. $replacementFunctions = [];
  155. foreach ($this->columns as $column)
  156. {
  157. if (method_exists($this, 'replaceField' . ucfirst($column->name)))
  158. {
  159. $replacementFunctions[$column->name] = 'replaceField' . ucfirst($column->name);
  160. }
  161. }
  162. return $replacementFunctions;
  163. }
  164. protected function setValueForDataRow(&$row, $colName, $value)
  165. {
  166. if (is_array($row))
  167. {
  168. $row[$colName] = $value;
  169. return $this;
  170. }
  171. if (is_object($row))
  172. {
  173. $row->$colName = $value;
  174. }
  175. return $this;
  176. }
  177. public function hasCustomColumnHtml($colName)
  178. {
  179. if (method_exists($this, 'customColumnHtml' . ucfirst($colName)))
  180. {
  181. return true;
  182. }
  183. return false;
  184. }
  185. public function getCustomColumnHtml($colName)
  186. {
  187. if ($this->hasCustomColumnHtml($colName))
  188. {
  189. return $this->{'customColumnHtml' . ucfirst($colName)}();
  190. }
  191. return false;
  192. }
  193. public function getSearchBarButtonsVisible()
  194. {
  195. return $this->searchBarButtonsVisible;
  196. }
  197. public function addButton($button)
  198. {
  199. if ($this->getButtonsCount() < $this->getSearchBarButtonsVisible())
  200. {
  201. parent::addButton($button);
  202. return $this;
  203. }
  204. $this->addButtonToDropdawn($button);
  205. return $this;
  206. }
  207. public function addButtonToDropdawn($button)
  208. {
  209. if ($this->dropdawnWrapper === null)
  210. {
  211. $this->dropdawnWrapper = DependencyInjection::call(\ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdown::class);
  212. $this->registerMainContainerAdditions($this->dropdawnWrapper);
  213. }
  214. $this->dropdawnWrapper->addButton($button);
  215. return $this;
  216. }
  217. public function hasDropdawnButton()
  218. {
  219. return $this->dropdawnWrapper !== null;
  220. }
  221. public function getDropdawnButtonHtml()
  222. {
  223. return $this->dropdawnWrapper->getHtml();
  224. }
  225. }