Ejemplo n.º 1
0
 /**
  * Process ordering query.
  *
  * @param Query   $query      The query object.
  * @param string  $ordering   The ordering string.
  * @param string  $direction  ASC or DESC.
  *
  * @return  void
  */
 protected function processOrdering(Query $query, $ordering = null, $direction = null)
 {
     $ordering = $this->state->get('list.ordering', $ordering);
     $this->state->set('list.ordering', $ordering);
     // If no ordering set, ignore this function.
     if (!$ordering) {
         return;
     }
     $self = $this;
     $direction = $this->state->get('list.direction', $direction);
     $this->state->set('list.direction', $direction);
     $ordering = explode(',', $ordering);
     // Add quote
     $ordering = array_map(function ($value) use($query, $self) {
         // Remove extra spaces
         preg_replace('/\\s+/', ' ', $value);
         $value = explode(' ', trim($value));
         // Check it is an allowed field.
         if (!$self->filterField($value[0])) {
             return '';
         }
         $field = $this->mapField($value[0]);
         if (!empty($field) && $field[strlen($field) - 1] != ')') {
             $field = $query->quoteName($field);
         }
         // $value[1] is direction
         if (isset($value[1])) {
             return $field . ' ' . $value[1];
         }
         return $field;
     }, $ordering);
     $ordering = array_filter($ordering, 'strlen');
     $ordering = implode(', ', $ordering);
     if (!$ordering) {
         return;
     }
     $query->order($ordering . ' ' . $direction);
 }