public function order_by($orderby, $direction = '', $escape = NULL)
{
$direction = strtoupper(trim($direction));
if ($direction === 'RANDOM') {
$direction = '';
// Do we have a seed value?
$orderby = ctype_digit((string) $orderby) ? sprintf($this->_random_keyword[1], $orderby) : $this->_random_keyword[0];
} elseif (empty($orderby)) {
return $this;
} elseif ($direction !== '') {
$direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' ' . $direction : '';
}
is_bool($escape) or $escape = $this->_protect_identifiers;
if ($escape === FALSE) {
$qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
} else {
$qb_orderby = array();
foreach (explode(',', $orderby) as $field) {
$qb_orderby[] = $direction === '' && preg_match('/\\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE) ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' ' . $match[1][0], 'escape' => TRUE) : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
}
}
$this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
if ($this->qb_caching === TRUE) {
$this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
$this->qb_cache_exists[] = 'orderby';
}
return $this;
}