DataTable.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable;
  3. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Builder\BaseContainer;
  4. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataProviders;
  5. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates;
  6. use \ModulesGarden\Servers\ZimbraEmail\Core\DependencyInjection\DependencyInjection;
  7. /**
  8. * Description of Service
  9. *
  10. * @author inbs
  11. */
  12. class DataTable extends BaseContainer implements \ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\AjaxElementInterface
  13. {
  14. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\DatatableActionButtons;
  15. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\DatatableMassActionButtons;
  16. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\VSortable;
  17. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\TitleButtons;
  18. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\TableLength;
  19. use \ModulesGarden\Servers\ZimbraEmail\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 = null)
  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(array $data = [])
  105. {
  106. $this->recordsSet = $data;
  107. logModuleCall(
  108. 'zimbraEmail',
  109. __FUNCTION__,
  110. $data,
  111. 'Debug data',
  112. $this->recordsSet
  113. );
  114. return $this;
  115. }
  116. protected function getCount()
  117. {
  118. return count($this->recordsSet);
  119. }
  120. protected function getRecords()
  121. {
  122. return $this->recordsSet;
  123. }
  124. protected function setHtml($html)
  125. {
  126. $this->html = $html;
  127. return $this;
  128. }
  129. public function setDataProvider(DataProviders\DataProvider $dataProv)
  130. {
  131. $this->dataProvider = $dataProv;
  132. if (!$this->columns)
  133. {
  134. $this->loadHtml();
  135. }
  136. $this->setData((array)$this->dataProvider->getData($this->columns));
  137. }
  138. protected function parseDataRecords()
  139. {
  140. $replacementFunctions = $this->getReplacementFunctions();
  141. if (count($replacementFunctions) === 0)
  142. {
  143. return false;
  144. }
  145. foreach ($this->recordsSet as $key => $row)
  146. {
  147. $this->recordsSet[$key] = $this->replaceRowData($row, $replacementFunctions);
  148. }
  149. }
  150. protected function replaceRowData($row, $replacementFunctions)
  151. {
  152. foreach ($replacementFunctions as $colName => $functionName)
  153. {
  154. if (method_exists($this, $functionName))
  155. {
  156. $this->setValueForDataRow($row, $colName, $this->{$functionName}($colName, $row));
  157. }
  158. }
  159. return $row;
  160. }
  161. protected function getReplacementFunctions()
  162. {
  163. $replacementFunctions = [];
  164. foreach ($this->columns as $column)
  165. {
  166. if (method_exists($this, 'replaceField' . ucfirst($column->name)))
  167. {
  168. $replacementFunctions[$column->name] = 'replaceField' . ucfirst($column->name);
  169. }
  170. }
  171. return $replacementFunctions;
  172. }
  173. protected function setValueForDataRow(&$row, $colName, $value)
  174. {
  175. if (is_array($row))
  176. {
  177. $row[$colName] = $value;
  178. return $this;
  179. }
  180. if (is_object($row))
  181. {
  182. $row->$colName = $value;
  183. }
  184. return $this;
  185. }
  186. public function hasCustomColumnHtml($colName)
  187. {
  188. if (method_exists($this, 'customColumnHtml' . ucfirst($colName)))
  189. {
  190. return true;
  191. }
  192. return false;
  193. }
  194. public function getCustomColumnHtml($colName)
  195. {
  196. if ($this->hasCustomColumnHtml($colName))
  197. {
  198. return $this->{'customColumnHtml' . ucfirst($colName)}();
  199. }
  200. return false;
  201. }
  202. public function getSearchBarButtonsVisible()
  203. {
  204. return $this->searchBarButtonsVisible;
  205. }
  206. public function addButton($button)
  207. {
  208. //if datatable pulls only data, there is no point creating this button
  209. if ($this->getRequestValue('ajax') !== false && $this->getRequestValue('iDisplayLength') !== false
  210. && $this->getRequestValue('iDisplayStart') !== false)
  211. {
  212. return $this;
  213. }
  214. if ($this->getButtonsCount() < $this->getSearchBarButtonsVisible())
  215. {
  216. parent::addButton($button);
  217. return $this;
  218. }
  219. $this->addButtonToDropdawn($button);
  220. return $this;
  221. }
  222. public function addButtonToDropdawn($button)
  223. {
  224. if ($this->dropdawnWrapper === null)
  225. {
  226. $this->dropdawnWrapper = DependencyInjection::call(\ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdown::class);
  227. $this->registerMainContainerAdditions($this->dropdawnWrapper);
  228. }
  229. $this->dropdawnWrapper->addButton($button);
  230. return $this;
  231. }
  232. public function hasDropdawnButton()
  233. {
  234. return $this->dropdawnWrapper !== null;
  235. }
  236. public function getDropdawnButtonHtml()
  237. {
  238. return $this->dropdawnWrapper->getHtml();
  239. }
  240. protected function preInitContent()
  241. {
  242. $this->loadHtml();
  243. }
  244. protected function afterInitContent()
  245. {
  246. parent::afterInitContent();
  247. $this->customTplVars['columns'] = $this->columns;
  248. $this->customTplVars['jsDrawFunctions'] = $this->getJsDrawFunctions();
  249. }
  250. public function enableAutoloadDataAfterCreated()
  251. {
  252. $this->autoloadDataAfterCreated = true;
  253. }
  254. public function disableAutoloadDataAfterCreated()
  255. {
  256. $this->autoloadDataAfterCreated = false;
  257. }
  258. public function isAutoloadDataAfterCreated()
  259. {
  260. return $this->autoloadDataAfterCreated;
  261. }
  262. public function getActionIdColumnName()
  263. {
  264. return $this->actionIdColumnName;
  265. }
  266. }