예제 #1
0
 /**
  * Method to set new item ordering as first or last.
  *
  * @param   Record $record    Item table to save.
  * @param   string $position `first` or other are `last`.
  *
  * @return  void
  */
 public function setOrderPosition(Record $record, $position = self::ORDER_POSITION_LAST)
 {
     $orderField = $this->state->get('order.column', 'ordering');
     if (!$record->hasField($orderField)) {
         return;
     }
     $position = $this->get('order.position', $position);
     if ($position == static::ORDER_POSITION_FIRST) {
         if (empty($record->{$orderField})) {
             $record->{$orderField} = 1;
             $this->state->set('order.position', static::ORDER_POSITION_FIRST);
         }
     } else {
         // Set ordering to the last item if not set
         if (empty($record->{$orderField})) {
             $query = $this->db->getQuery(true)->select(sprintf('MAX(%s)', $orderField))->from($record->getTableName());
             $condition = $this->getReorderConditions($record);
             // Condition should be an array.
             if (count($condition)) {
                 $query->where($this->getReorderConditions($record));
             }
             $max = $this->db->setQuery($query)->loadResult();
             $record->{$orderField} = $max + 1;
         }
     }
 }