Created
August 15, 2012 10:53
-
-
Save goldsky/3358864 to your computer and use it in GitHub Desktop.
PHP controller file for dojo's dgrid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
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
Adding range on MODX's GetListProcessor (class based processor) to be used by dojo's JSONREST