Skip to content

Instantly share code, notes, and snippets.

@goldsky
Created August 15, 2012 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldsky/3358864 to your computer and use it in GitHub Desktop.
Save goldsky/3358864 to your computer and use it in GitHub Desktop.
PHP controller file for dojo's dgrid
<?php
$out = '';
switch ($_REQUEST['act']) {
// ... more cases above
case 'get-data':
$params = array();
//$headers = apache_request_headers();
//$range = @explode('-', ltrim($headers['Range'], 'items='));
//$start = $range[0];
//$end = isset($range[1]) ? $range[1] : '';
if (isset($_SERVER["HTTP_RANGE"])) {
preg_match('/(\d+)-(\d+)/', $_SERVER["HTTP_RANGE"], $matches);
$start = $matches[1];
$end = $matches[2];
if ($end > 20) {
$end = 20;
}
} else {
$start = 0;
$end = 20;
}
foreach ($_GET as $k => $v) {
if ($k === 'q' || $k === 'act')
continue;
$params[$k] = $v;
}
$params['start'] = $start !== '' ?
intval($start) :
(isset($_GET['start']) ? intval($_GET['start']) : 0);
$params['limit'] = $end !== '' ?
intval($end) - intval($start) + 1 :
(isset($_GET['limit']) ? intval($_GET['limit']) : 0);
//$params['start'] = $start;
//$params['limit'] = $end;
if (isset($params['sort'])) {
$matches = array();
preg_match('/^-{1}/', $params['sort'], $matches);
$params['sort'] = ltrim($params['sort'], '-+ ');
$params['dir'] = isset($matches[0]) && $matches[0] === '-' ? 'DESC' : 'ASC';
}
// $ctrl...
// $ctrl is an instance of the server's query
$res = $ctrl->getData($params);
/**
* $res returns 2 arrays:
* 1. ['total'] without limit
* 2. ['results'] with start-limit
*/
if (!empty($res)) {
$jsonDec = json_decode($res, 1);
$spread = (intval($params['start']) + intval($params['limit'])) - 1;
$end = $spread > 0 ? $spread : $jsonDec['total'] - 1;
$contentRange = intval($params['start']) . '-' . $end . '/' . $jsonDec['total'];
header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
header("Content-Type: application/json");
header("Content-Range: items $contentRange");
$out = json_encode($jsonDec['results']);
}
break;
default:
break;
}
return $out;
@goldsky
Copy link
Author

goldsky commented Jul 23, 2013

Adding range on MODX's GetListProcessor (class based processor) to be used by dojo's JSONREST

<?php
    /**
     * {@inheritDoc}
     * @return boolean
     */
    public function initialize() {
        if (isset($_SERVER["HTTP_RANGE"])) {
            $matches = array();
            preg_match('/(\d+)-(\d+)/', $_SERVER["HTTP_RANGE"], $matches);

            $start = $matches[1];
            $end = $matches[2];
        } else {
            $start = 0;
            $end = 19;
        }

        $scriptProperties = $this->getProperties();
        if (isset($scriptProperties['sort']) && $scriptProperties['sort'] !== '') {
            $matches = array();
            preg_match('/^-{1}/', $scriptProperties['sort'], $matches);
            $sort = ltrim($scriptProperties['sort'], '-+ ');
            $dir = isset($matches[0]) && $matches[0] === '-' ? 'DESC' : 'ASC';
        } else {
            $sort = 'id';
            $dir = 'ASC';
        }
        $this->setProperty('start', $start);
        $this->setProperty('limit', intval($end) - intval($start) + 1);
        $this->setProperty('sort', $sort);
        $this->setProperty('dir', $dir);

        return parent::initialize();
    }

@goldsky
Copy link
Author

goldsky commented Jul 23, 2013

And returned as follow:

<?php

    /**
     * Return arrays of objects (with count) converted to JSON.
     *
     * The JSON result includes two main elements, total and results. This format is used for list
     * results.
     *
     * @access public
     * @param array $array An array of data objects.
     * @param mixed $count The total number of objects. Used for pagination.
     * @return string The JSON output.
     */
    public function outputArray(array $array, $count = false) {
        $scriptProperties = $this->getProperties();

        $spread = (intval($scriptProperties['start']) + intval($scriptProperties['limit']));
        $end = $spread > 0 ? ($spread > $count ? $count : $spread) : $count;
        $contentRange = intval($scriptProperties['start']) . '-' . $end . '/' . $count;

        header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
        header("Content-Type: application/json");
        header("Content-Range: items $contentRange");

        return $this->modx->toJSON($array);
//        return '{"total":"' . $count . '","items":' . $this->modx->toJSON($array) . '}';
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment