DataProvider.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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($query = 0, array $data = [])
  143. {
  144. $this->query = $query;
  145. $this->data = $data;
  146. return $this;
  147. }
  148. public function setSortBy($colName)
  149. {
  150. $this->orderColumn = $colName;
  151. }
  152. public function setSortDir($sortDir)
  153. {
  154. $this->orderDir = $sortDir;
  155. }
  156. public function getData(array $avalibleCols = [])
  157. {
  158. $this->avalibleCols = $avalibleCols;
  159. $this->data = $this->useSearch($this->data);
  160. $this->records = $this->data;
  161. $this->response->fullDataLenght = count($this->records);
  162. //$this->useFilter(); //<- todo
  163. $this->sortData();
  164. $this->addLimit($this->records);
  165. $this->response->offset = $this->offset;
  166. $this->response->records = $this->records;
  167. return $this->response;
  168. }
  169. protected function sortData()
  170. {
  171. if ($this->orderColumn && $this->orderDir && $this->avalibleCols[$this->orderColumn])
  172. {
  173. $column = $this->avalibleCols[$this->orderColumn];
  174. $this->sortNow($this->orderColumn, $column->type, strtolower($this->orderDir) === strtolower(DataProvider::SORT_ASC));
  175. }
  176. }
  177. protected function addLimit(&$data)
  178. {
  179. $data = array_slice($data, $this->offset, $this->limit);
  180. }
  181. protected function useSort()
  182. {
  183. foreach ($this->sort as $field => $sort)
  184. {
  185. $this->sortNow($field, $this->getType($field), strtolower($sort) === strtolower(DataProvider::SORT_ASC));
  186. }
  187. return $this;
  188. }
  189. protected function sortNow($fieldName, $type, $asc)
  190. {
  191. if ($type == 'string')
  192. {
  193. usort($this->records, function (array $a, array $b) use ($fieldName, $asc)
  194. {
  195. if ($asc)
  196. {
  197. return strcmp(strtolower($a[$fieldName]), strtolower($b[$fieldName]));
  198. }
  199. return strcmp(strtolower($b[$fieldName]), strtolower($a[$fieldName]));
  200. });
  201. }
  202. elseif ($type == 'int')
  203. {
  204. usort($this->records, function (array $a, array $b) use (&$fieldName, &$asc)
  205. {
  206. if ($a[$fieldName] == $b[$fieldName])
  207. {
  208. $return = 0;
  209. }
  210. elseif ($a[$fieldName] != $b[$fieldName] && $asc)
  211. {
  212. $return = ($a[$fieldName] < $b[$fieldName]) ? -1 : 1;
  213. }
  214. else
  215. {
  216. $return = ($a[$fieldName] > $b[$fieldName]) ? -1 : 1;
  217. }
  218. return $return;
  219. });
  220. }
  221. elseif ($type == 'date')
  222. {
  223. usort($this->records, function (array $a, array $b) use (&$fieldName, &$asc)
  224. {
  225. $a = strtotime($a[$fieldName]);
  226. $b = strtotime($b[$fieldName]);
  227. if ($a == $b)
  228. {
  229. $return = 0;
  230. }
  231. elseif ($a != $b && $asc)
  232. {
  233. $return = $a < $b ? -1 : 1;
  234. }
  235. else
  236. {
  237. $return = $a > $b ? -1 : 1;
  238. }
  239. return $return;
  240. });
  241. }
  242. else
  243. {
  244. foreach ($this->sortFunction as $typeCallBack => $callback)
  245. {
  246. if ($typeCallBack == $type)
  247. {
  248. usort($this->records, $callback);
  249. }
  250. }
  251. }
  252. }
  253. protected function addSortFunction($type, $callback)
  254. {
  255. array_set($this->sortFunction, $type, $callback);
  256. return $this;
  257. }
  258. private function setType($field, $type = self::TYPE_STRING)
  259. {
  260. array_set($this->type, $field, $type);
  261. return $this;
  262. }
  263. protected function getType($field)
  264. {
  265. if (array_key_exists($field, $this->type))
  266. {
  267. return $this->type[$field];
  268. }
  269. return 'string';
  270. }
  271. private function addFilterField($field, $class = self::FILTR_BY_TEXT)
  272. {
  273. array_set($this->filterFields, $field, $class);
  274. return $this;
  275. }
  276. protected function useFilter()
  277. {
  278. foreach ($this->filter as $field => $value)
  279. {
  280. if (isset($this->filterFields[$field]))
  281. {
  282. $class = $this->filterFields[$field];
  283. $this->setData($class::create($this->getRecords(), $field, $value));
  284. }
  285. else
  286. {
  287. $this->setData(Filters\Text::create($this->getRecords(), $field, $value));
  288. }
  289. }
  290. return $this;
  291. }
  292. public function setDefaultSorting($column, $direction)
  293. {
  294. if (!$this->request->query->get('iSortCol_0') && !$this->request->query->get('sSortDir_0'))
  295. {
  296. $this->setSortBy($column);
  297. $this->setSortDir($direction);
  298. }
  299. return $this;
  300. }
  301. public function enableCustomSearch()
  302. {
  303. $this->customSearch = true;
  304. }
  305. }