Example #1
0
 public function getOrderByStatement($extra = '', $table = '', $direction = 'ASC')
 {
     if (!$this->createDestination()) {
         return parent::getOrderByStatement();
     }
     if (!empty($table)) {
         $table = $table . '_AE_' . $this->fieldName();
     } else {
         $table = $this->fieldName();
     }
     if (!empty($extra) && in_array($extra, $this->m_listColumns)) {
         return $this->getDestination()->getAttribute($extra)->getOrderByStatement('', $table, $direction);
     }
     $order = $this->m_destInstance->getOrder();
     if (!empty($order)) {
         $newParts = [];
         $parts = explode(',', $order);
         foreach ($parts as $part) {
             $split = preg_split('/\\s+/', trim($part));
             $field = isset($split[0]) ? $split[0] : null;
             $fieldDirection = empty($split[1]) ? 'ASC' : strtoupper($split[1]);
             // if our default direction is DESC (the opposite of the default ASC)
             // we always have to switch the given direction to be the opposite, e.g.
             // DESC => ASC and ASC => DESC, this way we respect the default ordering
             // in the destination node even if the default is descending
             if ($fieldDirection == 'DESC') {
                 $fieldDirection = $direction == 'DESC' ? 'ASC' : 'DESC';
             } else {
                 $fieldDirection = $direction;
             }
             if (strpos($field, '.') !== false) {
                 list(, $field) = explode('.', $field);
             }
             $newPart = $this->getDestination()->getAttribute($field)->getOrderByStatement('', $table, $fieldDirection);
             // realias if destination order contains the wrong tablename.
             if (strpos($newPart, $this->m_destInstance->m_table . '.') !== false) {
                 $newPart = str_replace($this->m_destInstance->m_table . '.', $table . '.', $newPart);
             }
             $newParts[] = $newPart;
         }
         return implode(', ', $newParts);
     } else {
         $fields = $this->m_destInstance->descriptorFields();
         if (count($fields) == 0) {
             $fields = array($this->m_destInstance->primaryKeyField());
         }
         $order = '';
         foreach ($fields as $field) {
             $order .= (empty($order) ? '' : ', ') . $table . '.' . $field;
         }
         return $order;
     }
 }