示例#1
0
    /**
     * Checks if the position changed and update the position of the old and new level.
     * If the parent changed the old and next level must updated.
     *  OldLevel =>  from OldPosition to MAX -1          <br>
     *  NewLevel =>  from newPosition to MAX +1
     *
     * @param Shopware\Models\Media\Album $album
     * @param $newPosition
     * @param $newParent
     */
    private function changePosition(Album $album, $newPosition, $newParent)
    {
        //position or level not changed?
        if ($newParent == $album->getParentId() && $newPosition == $album->getPosition()) {
            return;
        }

        //0 can not be used, because Doctrine would otherwise check if the album with the id 0 exists.
        $newParent = ($newParent == 0) ? null : $newParent;

        //moved up or down? down => -1 / up = +1
        $step = ($newPosition < $album->getPosition()) ? 1 : -1;

        //Start and end values ​​determined.
        $fromPosition = ($step == 1) ? $newPosition - 1 : $album->getPosition();
        $toPosition = ($step == 1) ? $album->getPosition() : $newPosition + 1;

        //same parent? Then change only the position of the current album level.
        if ($newParent == $album->getParentId()) {
            $this->changePositionBetween($fromPosition, $toPosition, $step, $album->getParentId());
        } else {
            //change the position of the old level
            $this->changePositionBetween($album->getPosition(), 99999, -1, $album->getParentId());

            //change the position of the new level
            $this->changePositionBetween($newPosition - 1, 99999, +1, $newParent);
        }
    }