Bluz\Grid\Source\SqlSource::process PHP Method

process() public method

Process
public process ( array $settings = [] ) : Bluz\Grid\Data
$settings array
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
        $type = Proxy\Db::getOption('connect', 'type');
        if (strtolower($type) == 'mysql') {
            // MySQL
            $dataSql = preg_replace('/SELECT\\s(.*?)\\sFROM/is', 'SELECT SQL_CALC_FOUND_ROWS $1 FROM', $this->source, 1);
            $totalSql = 'SELECT FOUND_ROWS()';
        } else {
            // other
            $dataSql = $this->source;
            $totalSql = preg_replace('/SELECT\\s(.*?)\\sFROM/is', 'SELECT COUNT(*) FROM', $this->source, 1);
            if (sizeof($where)) {
                $totalSql .= ' WHERE ' . join(' AND ', $where);
            }
        }
        if (sizeof($where)) {
            $dataSql .= ' WHERE ' . join(' AND ', $where);
        }
        if (sizeof($orders)) {
            $dataSql .= ' ORDER BY ' . join(', ', $orders);
        }
        $dataSql .= $limit;
        // run queries
        // use transaction to avoid errors
        Proxy\Db::transaction(function () use(&$data, &$total, $dataSql, $totalSql) {
            $data = Proxy\Db::fetchAll($dataSql);
            $total = Proxy\Db::fetchOne($totalSql);
        });
        $gridData = new Grid\Data($data);
        $gridData->setTotal($total);
        return $gridData;
    }