Skip to content

Commit

Permalink
- fixes #32
Browse files Browse the repository at this point in the history
- added new feature to render output as raw aside from the default: json as pointed out by @ikarius6 . See WIKI [https://github.com/IgnitedDatatables/Ignited-Datatables/wiki/Function-Reference#this-datatables-generatecharset] for more info.
  • Loading branch information
cryogenix committed Jul 24, 2013
1 parent 2675d34 commit bcce6d7
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions application/libraries/Datatables.php
Expand Up @@ -225,15 +225,18 @@ public function unset_column($column)
/**
* Builds all the necessary query segments and performs the main query based on results set from chained statements
*
* @param string charset
* @param string $output
* @param string $charset
* @return string
*/
public function generate($charset = 'UTF-8')
public function generate($output = 'json', $charset = 'UTF-8')
{
$this->get_paging();
if(strtolower($output) == 'json')
$this->get_paging();

$this->get_ordering();
$this->get_filtering();
return $this->produce_output($charset);
return $this->produce_output(strtolower($output), strtolower($charset));
}

/**
Expand Down Expand Up @@ -311,7 +314,7 @@ private function get_filtering()
{
if(preg_match("/(<=|>=|=|<|>)(\s*)(.+)/i", trim($val), $matches))
$this->ci->db->where($this->select[$mColArray[$i]].' '.$matches[1], $matches[3]);
elseif(preg_match("/(.*)$sRangeSeparator(.*)/i", trim($val), $matches))
elseif(!empty($sRangeSeparator) && preg_match("/(.*)$sRangeSeparator(.*)/i", trim($val), $matches))
{
$rangeQuery = '';

Expand Down Expand Up @@ -345,17 +348,22 @@ private function get_display_result()
}

/**
* Builds a JSON encoded string data
* Builds an encoded string data. Returns JSON by default, and an array of aaData and sColumns if output is set to raw.
*
* @param string charset
* @return string
* @param string $output
* @param string $charset
* @return mixed
*/
private function produce_output($charset)
private function produce_output($output, $charset)
{
$aaData = array();
$rResult = $this->get_display_result();
$iTotal = $this->get_total_results();
$iFilteredTotal = $this->get_total_results(TRUE);

if($output == 'json')
{
$iTotal = $this->get_total_results();
$iFilteredTotal = $this->get_total_results(TRUE);
}

foreach($rResult->result_array() as $row_key => $row_val)
{
Expand All @@ -380,19 +388,24 @@ private function produce_output($charset)
$sColumns = array_diff($this->columns, $this->unset_columns);
$sColumns = array_merge_recursive($sColumns, array_keys($this->add_columns));

$sOutput = array
(
'sEcho' => intval($this->ci->input->post('sEcho')),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData' => $aaData,
'sColumns' => implode(',', $sColumns)
);

if(strtolower($charset) == 'utf-8')
return json_encode($sOutput);
if($output == 'json')
{
$sOutput = array
(
'sEcho' => intval($this->ci->input->post('sEcho')),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData' => $aaData,
'sColumns' => implode(',', $sColumns)
);

if($charset == 'utf-8')
return json_encode($sOutput);
else
return $this->jsonify($sOutput);
}
else
return $this->jsonify($sOutput);
return array('aaData' => $aaData, 'sColumns' => $sColumns);
}

/**
Expand Down Expand Up @@ -549,7 +562,7 @@ private function explode($delimiter, $str, $open = '(', $close=')')
/**
* Workaround for json_encode's UTF-8 encoding if a different charset needs to be used
*
* @param mixed result
* @param mixed $result
* @return string
*/
private function jsonify($result = FALSE)
Expand Down Expand Up @@ -607,4 +620,4 @@ private function jsonify($result = FALSE)
}
}
/* End of file Datatables.php */
/* Location: ./application/libraries/Datatables.php */
/* Location: ./application/libraries/Datatables.php */

0 comments on commit bcce6d7

Please sign in to comment.