/**
  * @inheritdoc
  */
 public function process(array $settings = [])
 {
     $this->processOrders($settings);
     $this->processFilters($settings);
     $this->source->limit($settings['limit']);
     $this->source->skip(($settings['page'] - 1) * $settings['limit']);
     // run queries
     $gridData = new Grid\Data($this->source->find());
     // get all records to set grid total
     // @todo: cache result of total query for few minutes
     $this->source = new ParseQuery($this->collectionName);
     $this->processOrders($settings);
     $this->processFilters($settings);
     $gridData->setTotal(sizeof($this->source->limit(1000)->find()));
     return $gridData;
 }
Example #2
0
 /**
  * Process
  *
  * @param  array $settings
  * @return \Bluz\Grid\Data
  */
 public function process(array $settings = [])
 {
     // process filters
     $where = [];
     if (!empty($settings['filters'])) {
         foreach ($settings['filters'] as $column => $filters) {
             foreach ($filters as $filter => $value) {
                 if ($filter == Grid\Grid::FILTER_LIKE) {
                     $value = '%' . $value . '%';
                 }
                 $where[] = $column . ' ' . $this->filters[$filter] . ' ' . Proxy\Db::quote($value);
             }
         }
     }
     // process orders
     $orders = [];
     if (!empty($settings['orders'])) {
         // Obtain a list of columns
         foreach ($settings['orders'] as $column => $order) {
             $column = Proxy\Db::quoteIdentifier($column);
             $orders[] = $column . ' ' . $order;
         }
     }
     // process pages
     $limit = ' LIMIT ' . ($settings['page'] - 1) * $settings['limit'] . ', ' . $settings['limit'];
     // prepare query
     $connect = Proxy\Config::getData('db', 'connect');
     if (strtolower($connect['type']) == 'mysql') {
         // MySQL
         $dataSql = preg_replace('/SELECT\\s(.*?)\\sFROM/is', 'SELECT SQL_CALC_FOUND_ROWS $1 FROM', $this->source, 1);
         $countSql = 'SELECT FOUND_ROWS()';
     } else {
         // other
         $dataSql = $this->source;
         $countSql = preg_replace('/SELECT\\s(.*?)\\sFROM/is', 'SELECT COUNT(*) FROM', $this->source, 1);
         if (sizeof($where)) {
             $countSql .= ' WHERE ' . join(' AND ', $where);
         }
     }
     if (sizeof($where)) {
         $dataSql .= ' WHERE ' . join(' AND ', $where);
     }
     if (sizeof($orders)) {
         $dataSql .= ' ORDER BY ' . join(', ', $orders);
     }
     $dataSql .= $limit;
     // run queries
     $data = Proxy\Db::fetchAll($dataSql);
     $total = Proxy\Db::fetchOne($countSql);
     $gridData = new Grid\Data($data);
     $gridData->setTotal($total);
     return $gridData;
 }
Example #3
0
 /**
  * Process
  *
  * @param  array $settings
  * @return \Bluz\Grid\Data
  */
 public function process(array $settings = [])
 {
     // process filters
     if (!empty($settings['filters'])) {
         foreach ($settings['filters'] as $column => $filters) {
             foreach ($filters as $filter => $value) {
                 if ($filter == Grid\Grid::FILTER_LIKE) {
                     $value = '%' . $value . '%';
                 }
                 $this->source->andWhere($column . ' ' . $this->filters[$filter] . ' ?', $value);
             }
         }
     }
     // process orders
     if (!empty($settings['orders'])) {
         // Obtain a list of columns
         foreach ($settings['orders'] as $column => $order) {
             $this->source->addOrderBy($column, $order);
         }
     }
     // process pages
     $this->source->setLimit($settings['limit']);
     $this->source->setPage($settings['page']);
     // prepare query
     $connect = Proxy\Config::getData('db', 'connect');
     if (strtolower($connect['type']) == 'mysql') {
         // MySQL
         $select = $this->source->getQueryPart('select');
         $this->source->select('SQL_CALC_FOUND_ROWS ' . current($select));
         // run queries
         $data = $this->source->execute();
         $total = Proxy\Db::fetchOne('SELECT FOUND_ROWS()');
     } else {
         // other
         $totalSource = clone $this->source;
         $totalSource->select('COUNT(*)');
         // run queries
         $data = $this->source->execute();
         $total = $totalSource->execute();
     }
     $gridData = new Grid\Data($data);
     $gridData->setTotal($total);
     return $gridData;
 }
Example #4
0
 /**
  * Process
  *
  * @param  array $settings
  * @return \Bluz\Grid\Data
  */
 public function process(array $settings = [])
 {
     $data = $this->source;
     // process filters
     if (!empty($settings['filters'])) {
         $data = array_filter($data, function ($row) use($settings) {
             foreach ($settings['filters'] as $column => $filters) {
                 foreach ($filters as $filter => $value) {
                     // switch statement for filter
                     switch ($filter) {
                         case Grid\Grid::FILTER_EQ:
                             if ($row[$column] != $value) {
                                 return false;
                             }
                             break;
                         case Grid\Grid::FILTER_NE:
                             if ($row[$column] == $value) {
                                 return false;
                             }
                             break;
                         case Grid\Grid::FILTER_GT:
                             if ($row[$column] <= $value) {
                                 return false;
                             }
                             break;
                         case Grid\Grid::FILTER_GE:
                             if ($row[$column] < $value) {
                                 return false;
                             }
                             break;
                         case Grid\Grid::FILTER_LT:
                             if ($row[$column] >= $value) {
                                 return false;
                             }
                             break;
                         case Grid\Grid::FILTER_LE:
                             if ($row[$column] > $value) {
                                 return false;
                             }
                             break;
                         case Grid\Grid::FILTER_LIKE:
                             if (!preg_match('/' . $value . '/', $row[$column])) {
                                 return false;
                             }
                             break;
                     }
                 }
             }
             return true;
         });
     }
     // process orders
     if (!empty($settings['orders'])) {
         // Create empty column stack
         $orders = [];
         foreach ($settings['orders'] as $column => $order) {
             $orders[$column] = [];
         }
         // Obtain a list of columns
         foreach ($data as $key => $row) {
             foreach ($settings['orders'] as $column => $order) {
                 $orders[$column][$key] = $row[$column];
             }
         }
         // Prepare array of arguments
         $funcArgs = [];
         foreach ($settings['orders'] as $column => $order) {
             $funcArgs[] = $orders[$column];
             $funcArgs[] = $order == Grid\Grid::ORDER_ASC ? SORT_ASC : SORT_DESC;
         }
         $funcArgs[] =& $data;
         // Sort the data with volume descending, edition ascending
         // Add $data as the last parameter, to sort by the common key
         array_multisort(...$funcArgs);
     }
     $total = sizeof($data);
     // process pages
     $data = array_slice($data, $settings['limit'] * ($settings['page'] - 1), $settings['limit']);
     $gridData = new Grid\Data($data);
     $gridData->setTotal($total);
     return $gridData;
 }