Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: roger
- * Date: 2/26/17
- * Time: 6:46 AM
- * Componente para paginação com VueJS 2.0
- */
- namespace Cake\Controller\Component;
- use Cake\Controller\Component;
- use Cake\Database\Exception;
- use Cake\Datasource\ConnectionManager;
- class PaginationComponent extends Component
- {
- private $totalFields = 0;
- /*
- * Configuração padrão
- */
- protected $_defaultConfig = [
- 'currentPage' => 1,
- 'perPage' => 20,
- 'orderSort' => 'ASC',
- 'defaultCool' => 'id'
- ];
- public function paginate($table,array $fields = [])
- {
- if(!isset($table))
- die('Informe a tabela');
- /*
- * Instancia a conexão
- */
- $conn = ConnectionManager::get('default');
- /*
- * Verifica se o parâmetro registros por página foi passado, se não foi coloca o valor default
- *
- */
- if (isset($_REQUEST['perPage'])) {
- $perPage = $_REQUEST['perPage'];
- } else {
- $perPage = $this->_defaultConfig['perPage'];
- }
- /*
- * Verifica se o parâmetro página atual foi passado, se não foi coloca o valor default
- */
- if (isset($_REQUEST['currentPage'])) {
- $_REQUEST['currentPage'] = $_REQUEST['currentPage'] - 1;
- } else {
- $_REQUEST['currentPage'] = $this->_defaultConfig['currentPage'];
- }
- /*
- * Levanta o ponto de partida da paginação
- */
- $offset = ($_REQUEST['currentPage'] * $perPage);
- /*
- * Verifica se o parâmetro para busca foi pasado e extrai os campos pré configurados
- */
- if (!empty($_REQUEST['searchQuery'])) {
- $concat_sql = '';
- $concat_sql .= "WHERE ";
- $break = 0;
- $this->totalFields = count($fields);
- /*
- * Se não foram passados campos para busca procura pelo campo default
- */
- if (!$this->totalFields) {
- $concat_sql = ' LIKE \'%' . $this->_defaultConfig['defaultCool'] . '%\'';
- } else {
- /*
- * Extrai os campos para busca que foram configurados
- */
- foreach ($fields as $field) {
- $concat_sql .= $field . ' LIKE \'%' . trim($_REQUEST['searchQuery']) . '%\'';
- $break++;
- if ($break < $this->totalFields) {
- $concat_sql .= ' OR ';
- }
- }
- }
- } else {
- $concat_sql = '';
- }
- /*
- * Verifica se os parâmetros para ordenação foram passados
- */
- if ((!empty($_REQUEST['orderSort'])) && (!empty($_REQUEST['orderCool']))) {
- /*
- * se não foi passado a direção de ordenação coloca a default
- */
- if ($_REQUEST['orderSort'] == 'default') {
- $_REQUEST['orderSort'] = $this->_defaultConfig['orderSort'];
- }
- /*
- * se não foi passado o campo para busca coloca o default
- */
- if (empty($_REQUEST['orderCool'])) {
- $_REQUEST['orderCool'] = $this->_defaultConfig['defaultCool'];
- }
- /*
- * query com ordenação
- */
- $results['registries'] = $conn->execute('SELECT * FROM .'.$table.' '. $concat_sql . ' ORDER BY ' . $_REQUEST['orderCool'] . ' ' . $_REQUEST['orderSort'] . ' LIMIT ' . $offset . ',' . $perPage)->fetchAll('assoc');
- } else {
- /*
- * query sem ordenação
- */
- $results['registries'] = $conn->execute('SELECT * FROM .'.$table.' '. $concat_sql . ' LIMIT ' . $offset . ',' . $perPage)->fetchAll('assoc');
- }
- /*
- * Pega o total de registros
- */
- $results['totalRegistries'] = $conn->execute('SELECT * FROM .'.$table.' '. $concat_sql)->count();
- return $results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement