isLast()) { $level = $this->getTable()->createQuery() ->addWhere('plan_id = ?', $this->getPlanId()) ->addWhere('position = ?', $this->getPosition() + 1) ->fetchOne(); if($level) { $level->setPosition($this->getPosition()); $level->save(); } $this->setPosition($this->getPosition() + 1); $this->save(); } } /** * Sníží pozici */ public function demote() { if(!$this->isFirst()) { $level = $this->getTable()->createQuery() ->addWhere('plan_id = ?', $this->getPlanId()) ->addWhere('position = ?', $this->getPosition() - 1) ->fetchOne(); if($level) { $level->setPosition($this->getPosition()); $level->save(); } $this->setPosition($this->getPosition() - 1); $this->save(); } } /** * * @return boolean */ public function isFirst() { return $this->getPosition() == 1; } /** * * @return boolean */ public function isLast() { $last = $this->getTable()->getLast($this->getPlan()); if($last) { return $last->getId() == $this->getId(); } return true; } public function preInsert($event) { parent::preInsert($event); $last = $this->getTable()->getLast($this->getPlan()); if($last) { $this->setPosition($last->getPosition() + 1); } else { $this->setPosition(1); } } }