/** * This performs the actual doctrine query and returns a json object * * @return json object */ public function getGridJson($q = NULL) { // If this is not a request for this grids data, exit if (empty($_REQUEST['gridName']) || $_REQUEST['gridName'] != $this->gridName) { return false; } /** * * THIS SECTION INITIALIZES ANY VARS EXPECTED FROM THE GRID * */ self::_orderColumns(); // These are the standard sort and page requests from the grid $currentPage = empty($_REQUEST['page']) ? 1 : $_REQUEST['page']; $resultsPerPage = empty($_REQUEST['rows']) ? 10 : $_REQUEST['rows']; $sidx =& $_REQUEST['sidx']; $sord =& $_REQUEST['sord']; // If the user has not supplied a Doctrine query then create one if (empty($q)) { $q = self::_autoQuery(); } /** * * THIS SECTION GETS A DOCTRINE QUERY, ADDS A ORDERBY ARGUMENT AND WRAPS IT IN A PAGER * */ // This handles the possible search request from the grid if (!empty($_REQUEST['_search'])) { self::_search($q); } // Add order to this mess :) if (!empty($sidx) && !empty($sord)) { $q->orderby($sidx . ' ' . $sord); } // Set up doctrine page for pagnation $pager = new Doctrine_Pager($q, $currentPage, $resultsPerPage); // Execute the query Kohana::log('debug', "JGRID QUERY IS: " . $q->getSqlQuery()); $results = $pager->execute(array(), Doctrine::HYDRATE_SCALAR); /** * * THIS SECTION BUILDS AN ARRAY THAT REPRESENTS A JSON RESPONSE TO THE GRID * */ $encodeArray['page'] = $pager->getPage(); $encodeArray['total'] = $pager->getLastPage(); $encodeArray['records'] = $pager->getNumResults(); // For each of our results start building an array we can use to generate json foreach ($results as $result) { // Build a pointer to a new rows array $ptEncodeArray =& $encodeArray['rows'][]; // Loop through each requested column foreach ($this->query['columns'] as $hydrateName => $model) { // Load a new cell with the value from the query, or blank if empty $cell =& $ptEncodeArray['cell'][]; $cell = empty($result[$hydrateName]) ? '' : $result[$hydrateName]; if (is_string($cell)) { $cell = htmlspecialchars($cell); } // Check if this field has a callback... if (!empty($this->query['callbacks'][$hydrateName])) { $cell = self::_cellCallback($cell, $this->query['callbacks'][$hydrateName], $result); } // Check if this field should be displayed as a link... if (!empty($cell) && !empty($this->query['links'][$hydrateName])) { $cell = self::_cellToAnchor($cell, $this->query['links'][$hydrateName], $result); } } if (!empty($this->query['actions'])) { $ptEncodeArray['cell'][] = self::_cellActions($this->query['actions'], $result); } } // Encode the results into a json object $json = json_encode($encodeArray); // These hacks let you pass an anchor in the json without it exploding.... $json = str_replace('\\/', '/', $json); $json = str_replace('\\"', '', $json); // Return the json return $json; }