DataQuery.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders;
  3. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\DataProvider;
  4. /**
  5. *
  6. */
  7. class DataQuery
  8. {
  9. private $query = null;
  10. private $limit = 10;
  11. private $offset = 0;
  12. private $orderColumn = false;
  13. private $orderDir = false;
  14. private $avalibleCols = [];
  15. private $toSearch = false;
  16. private $response;
  17. //\Illuminate\Database\Query\Builder
  18. public function __construct($query)
  19. {
  20. $this->query = $query;
  21. $this->response = new DataSet();
  22. }
  23. public function getData(array $avalibleCols = [])
  24. {
  25. $this->avalibleCols = $avalibleCols;
  26. if ($this->query)
  27. {
  28. $this->addSearch()
  29. ->addSorting()
  30. ->countRawResults()
  31. ->addLimit();
  32. $this->response->records = $this->query->get();
  33. return $this->response;
  34. }
  35. return $this->response;
  36. }
  37. public function countRawResults()
  38. {
  39. $this->response->fullDataLenght = $this->query->getCountForPagination();
  40. return $this;
  41. }
  42. public function addSearch()
  43. {
  44. if (!$this->toSearch)
  45. {
  46. return $this;
  47. }
  48. $searchable = [];
  49. foreach ($this->avalibleCols as $column)
  50. {
  51. if ($column->searchable === true)
  52. {
  53. $searchable[] = $column;
  54. }
  55. }
  56. if (count($searchable) > 0)
  57. {
  58. $this->query->where(function($query) use ($searchable)
  59. {
  60. foreach ($searchable as $key => $value)
  61. {
  62. $searchWraper = $value->type === $value::TYPE_INT ? '' : '%';
  63. if (($key === 0 && $value->type !== $value::TYPE_INT) || ($key === 0 && $value->type === $value::TYPE_INT && is_numeric($this->toSearch)))
  64. {
  65. $query->whereRaw( ( $value->type === $value::TYPE_INT ? '' : 'LOWER(') . $value->getFullName(true) . ( $value->type === $value::TYPE_INT ? ' LIKE ? ' : ') LIKE ? '),
  66. [$searchWraper . ( $value->type === $value::TYPE_INT ? $this->toSearch : strtolower($this->toSearch)) . $searchWraper]);
  67. }
  68. elseif (($value->type !== $value::TYPE_INT) || ($value->type === $value::TYPE_INT && is_numeric($this->toSearch)))
  69. {
  70. $query->orWhereRaw(( $value->type === $value::TYPE_INT ? '' : 'LOWER(') . $value->getFullName(true) . ( $value->type === $value::TYPE_INT ? ' LIKE ? ' : ') LIKE ? '),
  71. [$searchWraper . strtolower($this->toSearch) . $searchWraper]);
  72. }
  73. }
  74. });
  75. }
  76. return $this;
  77. }
  78. public function setLimit($limit)
  79. {
  80. $this->limit = (int) $limit;
  81. return $this;
  82. }
  83. public function setSearch($toSearch)
  84. {
  85. $this->toSearch = $toSearch;
  86. return $this;
  87. }
  88. public function setOffset($offset)
  89. {
  90. $this->offset = (int) $offset;
  91. return $this;
  92. }
  93. protected function addLimit()
  94. {
  95. $this->query->offset($this->offset);
  96. $this->query->limit($this->limit);
  97. $this->response->offset = $this->offset;
  98. return $this;
  99. }
  100. protected function addSorting()
  101. {
  102. if ($this->orderColumn && $this->orderDir) //&& $this->isProperColumn($this->orderColumn))
  103. {
  104. $find = null;
  105. foreach($this->avalibleCols as $column)
  106. {
  107. if ($this->orderColumn === $column->name)
  108. {
  109. $find = $column;
  110. break;
  111. }
  112. }
  113. if ($find)
  114. {
  115. $this->query->orderBy($find->getFullName(false), $this->orderDir);
  116. }
  117. }
  118. return $this;
  119. }
  120. public function setSorting($colName, $sortDir = DataProvider::SORT_ASC)
  121. {
  122. $this->orderColumn = $colName;
  123. $this->orderDir = (strtolower($sortDir) === strtolower(DataProvider::SORT_DESC)) ? DataProvider::SORT_DESC : DataProvider::SORT_ASC;
  124. return $this;
  125. }
  126. protected function isProperColumn($colName)
  127. {
  128. foreach ($this->avalibleCols as $column)
  129. {
  130. if ($colName === $column->name)
  131. {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. }