Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: roger
  5. * Date: 2/26/17
  6. * Time: 6:46 AM
  7. * Componente para paginação com VueJS 2.0
  8. */
  9.  
  10. namespace Cake\Controller\Component;
  11.  
  12. use Cake\Controller\Component;
  13. use Cake\Database\Exception;
  14. use Cake\Datasource\ConnectionManager;
  15.  
  16. class PaginationComponent extends Component
  17. {
  18.  
  19. private $totalFields = 0;
  20. /*
  21. * Configuração padrão
  22. */
  23. protected $_defaultConfig = [
  24. 'currentPage' => 1,
  25. 'perPage' => 20,
  26. 'orderSort' => 'ASC',
  27. 'defaultCool' => 'id'
  28. ];
  29.  
  30. public function paginate($table,array $fields = [])
  31. {
  32.  
  33. if(!isset($table))
  34. die('Informe a tabela');
  35. /*
  36. * Instancia a conexão
  37. */
  38. $conn = ConnectionManager::get('default');
  39. /*
  40. * Verifica se o parâmetro registros por página foi passado, se não foi coloca o valor default
  41. *
  42. */
  43. if (isset($_REQUEST['perPage'])) {
  44. $perPage = $_REQUEST['perPage'];
  45. } else {
  46. $perPage = $this->_defaultConfig['perPage'];
  47. }
  48. /*
  49. * Verifica se o parâmetro página atual foi passado, se não foi coloca o valor default
  50. */
  51. if (isset($_REQUEST['currentPage'])) {
  52. $_REQUEST['currentPage'] = $_REQUEST['currentPage'] - 1;
  53. } else {
  54. $_REQUEST['currentPage'] = $this->_defaultConfig['currentPage'];
  55. }
  56. /*
  57. * Levanta o ponto de partida da paginação
  58. */
  59. $offset = ($_REQUEST['currentPage'] * $perPage);
  60. /*
  61. * Verifica se o parâmetro para busca foi pasado e extrai os campos pré configurados
  62. */
  63. if (!empty($_REQUEST['searchQuery'])) {
  64. $concat_sql = '';
  65. $concat_sql .= "WHERE ";
  66. $break = 0;
  67. $this->totalFields = count($fields);
  68. /*
  69. * Se não foram passados campos para busca procura pelo campo default
  70. */
  71. if (!$this->totalFields) {
  72. $concat_sql = ' LIKE \'%' . $this->_defaultConfig['defaultCool'] . '%\'';
  73. } else {
  74. /*
  75. * Extrai os campos para busca que foram configurados
  76. */
  77. foreach ($fields as $field) {
  78. $concat_sql .= $field . ' LIKE \'%' . trim($_REQUEST['searchQuery']) . '%\'';
  79. $break++;
  80. if ($break < $this->totalFields) {
  81. $concat_sql .= ' OR ';
  82. }
  83. }
  84. }
  85.  
  86. } else {
  87. $concat_sql = '';
  88. }
  89. /*
  90. * Verifica se os parâmetros para ordenação foram passados
  91. */
  92. if ((!empty($_REQUEST['orderSort'])) && (!empty($_REQUEST['orderCool']))) {
  93.  
  94. /*
  95. * se não foi passado a direção de ordenação coloca a default
  96. */
  97. if ($_REQUEST['orderSort'] == 'default') {
  98. $_REQUEST['orderSort'] = $this->_defaultConfig['orderSort'];
  99. }
  100. /*
  101. * se não foi passado o campo para busca coloca o default
  102. */
  103. if (empty($_REQUEST['orderCool'])) {
  104. $_REQUEST['orderCool'] = $this->_defaultConfig['defaultCool'];
  105. }
  106. /*
  107. * query com ordenação
  108. */
  109. $results['registries'] = $conn->execute('SELECT * FROM .'.$table.' '. $concat_sql . ' ORDER BY ' . $_REQUEST['orderCool'] . ' ' . $_REQUEST['orderSort'] . ' LIMIT ' . $offset . ',' . $perPage)->fetchAll('assoc');
  110. } else {
  111. /*
  112. * query sem ordenação
  113. */
  114. $results['registries'] = $conn->execute('SELECT * FROM .'.$table.' '. $concat_sql . ' LIMIT ' . $offset . ',' . $perPage)->fetchAll('assoc');
  115. }
  116. /*
  117. * Pega o total de registros
  118. */
  119. $results['totalRegistries'] = $conn->execute('SELECT * FROM .'.$table.' '. $concat_sql)->count();
  120. return $results;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement