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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And returned as follow: