DataTable.php 8.3 KB

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