RawDataQuery.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders;
  3. use \Illuminate\Database\Capsule\Manager as DB;
  4. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\DataProvider;
  5. /**
  6. *
  7. */
  8. class RawDataQuery
  9. {
  10. private $query = null;
  11. private $params = [];
  12. private $limit = 10;
  13. private $offset = 0;
  14. private $orderColumn = false;
  15. private $orderDir = false;
  16. private $avalibleCols = [];
  17. private $toSearch = false;
  18. private $response;
  19. public function __construct($query, $params = [])
  20. {
  21. $this->query = $query;
  22. $this->params = $params;
  23. $this->response = new DataSet();
  24. }
  25. public function getData(array $avalibleCols = [])
  26. {
  27. $this->avalibleCols = $avalibleCols;
  28. if ($this->query)
  29. {
  30. $this->addSearch();
  31. $this->addSorting();
  32. $this->countRawResults();
  33. $this->addLimit();
  34. $statement = DB::connection()
  35. ->getPdo()
  36. ->prepare($this->query);
  37. $statement->execute($this->params);
  38. while ($row = $statement->fetch(\PDO::FETCH_ASSOC))
  39. {
  40. $this->response->records[] = $row;
  41. }
  42. return $this->response;
  43. }
  44. return $this->response;
  45. }
  46. public function countRawResults()
  47. {
  48. $statement = DB::connection()
  49. ->getPdo()
  50. ->prepare($this->query);
  51. $statement->execute($this->params);
  52. $this->response->fullDataLenght = $statement->rowCount();
  53. }
  54. public function addSearch()
  55. {
  56. if (!$this->toSearch)
  57. {
  58. return false;
  59. }
  60. $searchQuery = '';
  61. foreach ($this->avalibleCols as $column)
  62. {
  63. if ($column->searchable === true)
  64. {
  65. $searchQuery .= 'OR ' . $column->name . ' LIKE :' . $column->name . ' ';
  66. $this->params[$column->name] = '%' . $this->toSearch . '%';
  67. }
  68. }
  69. if ($searchQuery != '')
  70. {
  71. if (substr($searchQuery, 0, 2) === 'OR')
  72. {
  73. $searchQuery = substr($searchQuery, 2);
  74. }
  75. $this->query .= 'AND ( ' . $searchQuery . ' ) ';
  76. }
  77. }
  78. public function setLimit($limit)
  79. {
  80. $this->limit = (int) $limit;
  81. }
  82. public function setSearch($toSearch)
  83. {
  84. $this->toSearch = $toSearch;
  85. }
  86. public function setOffset($offset)
  87. {
  88. $this->offset = (int) $offset;
  89. }
  90. protected function addLimit()
  91. {
  92. $this->query .= ' LIMIT ' . $this->limit . ' OFFSET ' . $this->offset . ' ';
  93. $this->response->offset = $this->offset;
  94. }
  95. protected function addSorting()
  96. {
  97. if ($this->orderColumn && $this->orderDir && $this->isProperColumn($this->orderColumn))
  98. {
  99. $this->query .= ' ORDER BY ' . $this->orderColumn . ' ' . $this->orderDir . ' ';
  100. }
  101. }
  102. public function setSorting($colName, $sortDir = DataProvider::SORT_ASC)
  103. {
  104. $this->orderColumn = $colName;
  105. $this->orderDir = ($sortDir === strtolower(DataProvider::SORT_DESC)) ? DataProvider::SORT_DESC : DataProvider::SORT_ASC;
  106. }
  107. protected function isProperColumn($colName)
  108. {
  109. foreach ($this->avalibleCols as $column)
  110. {
  111. if ($colName === $column->name)
  112. {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. }