DataProvider.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataProviders;
  3. use ModulesGarden\ProxmoxAddon\Core\Http\Request;
  4. /**
  5. *
  6. */
  7. abstract class DataProvider
  8. {
  9. const FILTR_BY_DATE = '\ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Filters\Date';
  10. const FILTR_BY_RAGE = '\ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Filters\Rage';
  11. const FILTR_BY_RAGE_DATE = '\ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Filters\RageDate';
  12. const FILTR_BY_SELECT = '\ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Filters\Select';
  13. const FILTR_BY_TEXT = '\ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Filters\Text';
  14. const FILTR_BY_YESNO = '\ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Filters\YesNo';
  15. const SORT_ASC = 'ASC';
  16. const SORT_DESC = 'DESC';
  17. protected $limit = 10;
  18. protected $offset = 0;
  19. protected $data = null;
  20. protected $orderColumn = null;
  21. protected $orderDir = null;
  22. protected $request = null;
  23. protected $avalibleCols = null;
  24. protected $filter = [];
  25. protected $records = [];
  26. protected $filterFields = [];
  27. protected $toSearch = null;
  28. private $response;
  29. protected $customSearch = false;
  30. protected $defaultOrderColumn = null;
  31. protected $defaultOrderDir = null;
  32. public function __construct()
  33. {
  34. $this->request = Request::build();
  35. $this->response = new DataSet();
  36. $this->loadLimits();
  37. $this->loadSortings();
  38. $this->loadFilter();
  39. $this->loadSearch();
  40. }
  41. protected function loadLimits()
  42. {
  43. if ($this->request->query->get('iDisplayLength'))
  44. {
  45. $this->setLimit((int) $this->request->query->get('iDisplayLength'));
  46. }
  47. if ($this->request->query->get('iDisplayStart'))
  48. {
  49. $this->setOfset((int) $this->request->query->get('iDisplayStart'));
  50. }
  51. }
  52. protected function loadSortings()
  53. {
  54. if ($this->request->query->get('iSortCol_0') && $this->request->query->get('sSortDir_0'))
  55. {
  56. $this->setSortBy($this->request->query->get('iSortCol_0'));
  57. $this->setSortDir($this->request->query->get('sSortDir_0'));
  58. }
  59. }
  60. protected function loadSearch()
  61. {
  62. if ($this->request->query->get('sSearch'))
  63. {
  64. $this->setSearch(html_entity_decode($this->request->query->get('sSearch'), ENT_QUOTES));
  65. }
  66. }
  67. protected function loadFilter()
  68. {
  69. $filters = $this->request->query->get('filter', []);
  70. foreach ($filters as $filter)
  71. {
  72. $this->addFilter($filter['name'], $filter['data']);
  73. }
  74. return $this;
  75. }
  76. protected function addFilter($key, $data = null)
  77. {
  78. if (isset($data))
  79. {
  80. array_set($this->filter, $key, $data);
  81. }
  82. return $this;
  83. }
  84. public function setLimit($limit)
  85. {
  86. $this->limit = (int) $limit;
  87. }
  88. public function setSearch($toSearch)
  89. {
  90. $this->toSearch = $toSearch;
  91. }
  92. protected function useSearch($data = [])
  93. {
  94. if ($this->toSearch && str_replace(" ", "", $this->toSearch) != '' && $this->customSearch === false)
  95. {
  96. $searchable = [];
  97. foreach ($this->avalibleCols as $column)
  98. {
  99. if ($column->searchable === true)
  100. {
  101. $searchable[] = $column->name;
  102. }
  103. }
  104. $removeIds = [];
  105. foreach ($data as $id => $record)
  106. {
  107. $isFind = false;
  108. foreach ($record as $fieldKey => $fieldData)
  109. {
  110. if (strpos(strtolower(str_replace(" ", "", $fieldData)), strtolower(str_replace(" ", "", $this->toSearch))) !== false && in_array($fieldKey, $searchable))
  111. {
  112. $isFind = true;
  113. break;
  114. }
  115. }
  116. if ($isFind === false)
  117. {
  118. $removeIds[] = $id;
  119. }
  120. }
  121. if (is_object($data))
  122. {
  123. foreach ($removeIds as $id)
  124. {
  125. unset($data->$id);
  126. }
  127. }
  128. else
  129. {
  130. foreach ($removeIds as $id)
  131. {
  132. unset($data[$id]);
  133. }
  134. }
  135. }
  136. return $data;
  137. }
  138. public function setOfset($offset)
  139. {
  140. $this->offset = (int) $offset;
  141. }
  142. public function setData(array $data = [])
  143. {
  144. $this->data = $data;
  145. return $this;
  146. }
  147. public function setSortBy($colName)
  148. {
  149. $this->orderColumn = $colName;
  150. }
  151. public function setSortDir($sortDir)
  152. {
  153. $this->orderDir = $sortDir;
  154. }
  155. public function getData(array $avalibleCols = [])
  156. {
  157. $this->avalibleCols = $avalibleCols;
  158. $this->data = $this->useSearch($this->data);
  159. $this->records = $this->data;
  160. $this->response->fullDataLenght = count($this->records);
  161. //$this->useFilter(); //<- todo
  162. $this->sortData();
  163. $this->addLimit($this->records);
  164. $this->response->offset = $this->offset;
  165. $this->response->records = $this->records;
  166. return $this->response;
  167. }
  168. protected function sortData()
  169. {
  170. if ($this->orderColumn && $this->orderDir && $this->avalibleCols[$this->orderColumn])
  171. {
  172. $column = $this->avalibleCols[$this->orderColumn];
  173. $this->sortNow($this->orderColumn, $column->type, strtolower($this->orderDir) === strtolower(DataProvider::SORT_ASC));
  174. }
  175. }
  176. protected function addLimit(&$data)
  177. {
  178. $data = array_slice($data, $this->offset, $this->limit);
  179. }
  180. protected function useSort()
  181. {
  182. foreach ($this->sort as $field => $sort)
  183. {
  184. $this->sortNow($field, $this->getType($field), strtolower($sort) === strtolower(DataProvider::SORT_ASC));
  185. }
  186. return $this;
  187. }
  188. protected function sortNow($fieldName, $type, $asc)
  189. {
  190. if ($type == 'string')
  191. {
  192. usort($this->records, function (array $a, array $b) use ($fieldName, $asc)
  193. {
  194. if ($asc)
  195. {
  196. return strcmp(strtolower($a[$fieldName]), strtolower($b[$fieldName]));
  197. }
  198. return strcmp(strtolower($b[$fieldName]), strtolower($a[$fieldName]));
  199. });
  200. }
  201. elseif ($type == 'int')
  202. {
  203. usort($this->records, function (array $a, array $b) use (&$fieldName, &$asc)
  204. {
  205. if ($a[$fieldName] == $b[$fieldName])
  206. {
  207. $return = 0;
  208. }
  209. elseif ($a[$fieldName] != $b[$fieldName] && $asc)
  210. {
  211. $return = ($a[$fieldName] < $b[$fieldName]) ? -1 : 1;
  212. }
  213. else
  214. {
  215. $return = ($a[$fieldName] > $b[$fieldName]) ? -1 : 1;
  216. }
  217. return $return;
  218. });
  219. }
  220. elseif ($type == 'date')
  221. {
  222. usort($this->records, function (array $a, array $b) use (&$fieldName, &$asc)
  223. {
  224. $a = strtotime($a[$fieldName]);
  225. $b = strtotime($b[$fieldName]);
  226. if ($a == $b)
  227. {
  228. $return = 0;
  229. }
  230. elseif ($a != $b && $asc)
  231. {
  232. $return = $a < $b ? -1 : 1;
  233. }
  234. else
  235. {
  236. $return = $a > $b ? -1 : 1;
  237. }
  238. return $return;
  239. });
  240. }
  241. else
  242. {
  243. foreach ($this->sortFunction as $typeCallBack => $callback)
  244. {
  245. if ($typeCallBack == $type)
  246. {
  247. usort($this->records, $callback);
  248. }
  249. }
  250. }
  251. }
  252. protected function addSortFunction($type, $callback)
  253. {
  254. array_set($this->sortFunction, $type, $callback);
  255. return $this;
  256. }
  257. private function setType($field, $type = self::TYPE_STRING)
  258. {
  259. array_set($this->type, $field, $type);
  260. return $this;
  261. }
  262. protected function getType($field)
  263. {
  264. if (array_key_exists($field, $this->type))
  265. {
  266. return $this->type[$field];
  267. }
  268. return 'string';
  269. }
  270. private function addFilterField($field, $class = self::FILTR_BY_TEXT)
  271. {
  272. array_set($this->filterFields, $field, $class);
  273. return $this;
  274. }
  275. protected function useFilter()
  276. {
  277. foreach ($this->filter as $field => $value)
  278. {
  279. if (isset($this->filterFields[$field]))
  280. {
  281. $class = $this->filterFields[$field];
  282. $this->setData($class::create($this->getRecords(), $field, $value));
  283. }
  284. else
  285. {
  286. $this->setData(Filters\Text::create($this->getRecords(), $field, $value));
  287. }
  288. }
  289. return $this;
  290. }
  291. public function setDefaultSorting($column, $direction)
  292. {
  293. if (!$this->request->query->get('iSortCol_0') && !$this->request->query->get('sSortDir_0'))
  294. {
  295. $this->setSortBy($column);
  296. $this->setSortDir($direction);
  297. }
  298. return $this;
  299. }
  300. public function enableCustomSearch()
  301. {
  302. $this->customSearch = true;
  303. }
  304. }