Beispiel #1
0
    /**
     * The saveAlbumAction is used to save a new album and update an existing album.
     * The function expects the following parameters:
     * <code>
     *  id               => [int]    May be null if a new album to be saved.
     *  text             => [string] Name of the album to be saved.
     *  parentId         => [int]    Id of the parent album. 0 if the album is to be stored at the highest level.
     *  position         => [int]    Position of the album within the tree.
     *  iconCls          => [string] Css class for the album tree node
     *  thumbnailSize    => [int]    Flag if thumbnails should be created.
     *  createThumbnails => [array]  Array of thumbnail sizes
     * </code>
     * @return bool
     */
    public function saveAlbumAction()
    {
        $params = $this->Request()->getParams();

        //check if the album id passed. If no id passed, create a new album and album settings.
        if (isset($params['id']) && !empty($params['id']) && $params['id'] !== 0) {
            $album = Shopware()->Models()->find('Shopware\Models\Media\Album', $params['id']);
            $settings = $album->getSettings();
        } else {
            $album = new Album();
            $settings = new Settings();
            $settings->setCreateThumbnails(0);
            $settings->setIcon('sprite-blue-folder');
            $settings->setThumbnailSize('');
        }

        //check if the album is loaded.
        if ($album === null || $settings === null) {
            $this->View()->assign(array('success' => false, 'message' => 'Album/Settings not found'));
            return false;
        }

        //validate name
        if (isset($params['text']) && !empty($params['text'])) {
            $album->setName($params['text']);
        } else {
            $this->View()->assign(array('success' => false, 'message' => 'No name passed!'));
            return false;
        }

        //validate parent ID
        if (isset($params['parentId']) && !empty($params['parentId']) && $params['parentId'] !== 0 && $params['parentId'] !== 'root') {
            $parentId = $params['parentId'];
        } else {
            $parentId = null;
        }

        //validate position
        if (isset($params['position']) && !empty($params['position'])) {
            if ($album->getId() !== 0) {
                $this->changePosition($album, $params['position'], $parentId);
            }
            $album->setPosition($params['position']);
        } else {
            //get last position + 1
            if ($parentId === null) {
                $sql= "SELECT MAX(position) + 1 FROM s_media_album WHERE parentID IS NULL";
                $position = Shopware()->Db()->fetchOne($sql);
            } else {
                $sql= "SELECT MAX(position) + 1 FROM s_media_album WHERE parentID = ?";
                $position = Shopware()->Db()->fetchOne($sql, array($parentId));
            }
            if ($position === null) {
                $position = 1;
            }
            $album->setPosition($position);
        }

        //Important! Parent must be set only after updating the position.
        $album->setParentId($parentId);

        //validate icon property of the album settings
        if (isset($params['iconCls']) && !empty($params['iconCls'])) {
            $settings->setIcon($params['iconCls']);
        }

        //validate create thumbnail property of the album settings
        if (isset($params['createThumbnails']) && !empty($params['createThumbnails'])) {
            $settings->setCreateThumbnails($params['createThumbnails']);
        }

        //validate thumbnail size property of the album settings
        if (isset($params['thumbnailSize'])) {
            $sizes = array();
            foreach ($params['thumbnailSize'] as $size) {
                if (!empty($size['value']) && $size['value'] !== '') {
                    $sizes[] = $size['value'];
                }
            }
            if (count($sizes) > 0) {
                $settings->setThumbnailSize($sizes);
            } else {
                $settings->setThumbnailSize('');
            }
        }

        if ($parentId !== null && empty($params['id'])) {
            $builder = Shopware()->Models()->createQueryBuilder();
            $builder->select(array('settings'))
                    ->from('Shopware\Models\Media\Settings', 'settings')
                    ->where('settings.albumId = :albumId')
                    ->setParameter('albumId', $parentId);

            $settingData = $builder->getQuery()->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
            $settings->fromArray($settingData);
        }

        //try save
        try {
            Shopware()->Models()->persist($album);
            Shopware()->Models()->flush();
            
            //after the album is saved, set the album id to the settings model and save the settings
            $settings->setAlbumId($album->getId());
            $settings->setAlbum($album);
            Shopware()->Models()->persist($settings);
            Shopware()->Models()->flush();

            //return the album node properties to refresh the tree.
            $node = $this->toTree(array($album));
            $this->View()->assign(array('success' => true, 'data' => $node));
        }
        catch (\Doctrine\ORM\ORMException $e) {
            $this->View()->assign(array('success' => false, 'message' => $e->getMessage()));
        }
    }
 /**
  * This method resolves the given request data and returns
  * an array with album data
  *
  * @param $data
  * @param Album $album
  * @throws Exception
  * @return array|bool
  */
 protected function resolveAlbumData($data, $album)
 {
     $settings = $album->getSettings();
     if (!$settings) {
         $settings = new Settings();
         $settings->setAlbum($album);
     }
     // validate album name
     if (empty($data['text'])) {
         throw new Exception('No valid album name passed!');
     }
     $data['name'] = $data['text'];
     $data['parent'] = null;
     if (!empty($data['parentId']) && $data['parentId'] != 'root') {
         $parent = $this->getManager()->find('Shopware\\Models\\Media\\Album', $data['parentId']);
         if (!$parent) {
             throw new Exception('No valid parent album passed!');
         }
         $data['parent'] = $parent;
     }
     $thumbnailSizes = '';
     $createThumbnails = 0;
     $icon = 'sprite-blue-folder';
     if (isset($data['createThumbnails'])) {
         $createThumbnails = (int) $data['createThumbnails'];
     }
     if (isset($data['thumbnailSize'])) {
         $thumbnailSizes = array();
         foreach ($data['thumbnailSize'] as $size) {
             if (!empty($size['value']) && $size['value'] !== '') {
                 $thumbnailSizes[] = $size['value'];
             }
         }
     }
     if (isset($data['iconCls']) && !empty($data['iconCls'])) {
         $icon = $data['iconCls'];
     }
     $albumId = $album->getId();
     if (empty($albumId) && $data['parent'] !== null) {
         /** @var Settings $parentSettings */
         $parentSettings = $data['parent']->getSettings();
         $thumbnailSizes = $parentSettings->getThumbnailSize();
         $createThumbnails = $parentSettings->getCreateThumbnails();
     }
     $settings->setCreateThumbnails($createThumbnails);
     $settings->setThumbnailSize(empty($thumbnailSizes) ? '' : $thumbnailSizes);
     $settings->setIcon($icon);
     $data['settings'] = $settings;
     return $data;
 }