Example #1
0
    /**
     * @param array $data
     * @param \Shopware\Models\Article\Article $article
     * @throws \Shopware\Components\Api\Exception\CustomValidationException
     * @throws \Exception
     * @return array
     */
    protected function prepareConfiguratorSet($data, ArticleModel $article)
    {
        if (!isset($data['configuratorSet'])) {
            return $data;
        }

        $configuratorSet = $article->getConfiguratorSet();
        if (!$configuratorSet) {
            $configuratorSet = new \Shopware\Models\Article\Configurator\Set();
            if (isset($data['mainDetail']['number'])) {
                $number = $data['mainDetail']['number'];
            } else {
                $number = $article->getMainDetail()->getNumber();
            }

            $configuratorSet->setName('Set-' . $number);
            $configuratorSet->setPublic(false);
        }

        if (isset($data['configuratorSet']['type'])) {
            $configuratorSet->setType($data['configuratorSet']['type']);
        }

        if (isset($data['configuratorSet']['name'])) {
            $configuratorSet->setName($data['configuratorSet']['name']);
        }

        $allOptions = array();
        $allGroups = array();

        $groupPosition = 0;

        foreach ($data['configuratorSet']['groups'] as $groupData) {
            $group = null;
            if (isset($groupData['id'])) {
                $group = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Group')->find($groupData['id']);
                if (!$group) {
                    throw new ApiException\CustomValidationException(sprintf("ConfiguratorGroup by id %s not found", $groupData['id']));
                }
            } elseif (isset($groupData['name'])) {
                $group = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Group')->findOneBy(array('name' => $groupData['name']));

                if (!$group) {
                    $group = new \Shopware\Models\Article\Configurator\Group();
                    $group->setPosition($groupPosition);
                }
            } else {
                throw new ApiException\CustomValidationException('At least the groupname is required');
            }

            $groupOptions = array();
            $optionPosition = 0;
            foreach ($groupData['options'] as $optionData) {
                $option = null;
                if ($group->getId() > 0) {
                    $option = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Option')->findOneBy(array(
                        'name'    => $optionData['name'],
                        'groupId' => $group->getId()
                    ));
                }

                if (!$option) {
                    $option = new \Shopware\Models\Article\Configurator\Option();
                }

                $option->fromArray($optionData);
                $option->setGroup($group);
                $option->setPosition($optionPosition++);
                $allOptions[]   = $option;
                $groupOptions[] = $option;
            }

            $groupData['options'] = $groupOptions;
            $group->fromArray($groupData);
            $allGroups[] = $group;
        }

        $configuratorSet->setOptions($allOptions);
        $configuratorSet->setGroups($allGroups);

        $data['configuratorSet'] = $configuratorSet;

        return $data;
    }
Example #2
0
 /**
  * Internal helper function to check if the article is configured as
  * multiple dimensional article (Configurator activated).
  * The following scenarios are possible:
  * <code>
  *  - New Article
  *    --> Checkbox activated
  *    --> "isConfigurator" = true  / configuratorSetId = null
  *    --> A new configurator set will be created with the name "Set-ArticleNumber"
  *
  *  - Existing Article
  *    --> Checkbox wasn't activated before, now the user activated the checkbox
  *    --> "isConfigurator" = true  / configuratorSetId = null
  *    --> A new configurator set will be created with the name "Set-ArticleNumber"
  *
  *  - Existing Article
  *    --> Checkbox was activated before, now the user deactivated the checkbox
  *    --> "isConfigurator" = false / configuratorSetId = Some Numeric value
  *    --> The old configurator set will be deleted.
  *
  * </code>
  * @param $data
  * @param $article \Shopware\Models\Article\Article
  * @return array
  */
 protected function prepareConfiguratorAssociatedData($data, $article)
 {
     if (!empty($data['configuratorSetId'])) {
         $data['configuratorSet'] = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Set', $data['configuratorSetId']);
     } elseif ($data['isConfigurator']) {
         $set = new \Shopware\Models\Article\Configurator\Set();
         $set->setName('Set-' . $data['mainDetail']['number']);
         $set->setPublic(false);
         $data['configuratorSet'] = $set;
     } else {
         //if the article has an configurator set, we have to remove this set if it isn't used for other articles
         if ($article->getConfiguratorSet() && $article->getConfiguratorSet()->getId()) {
             $builder = Shopware()->Models()->createQueryBuilder();
             $articles = $builder->select(array('articles'))->from('Shopware\\Models\\Article\\Article', 'articles')->where('articles.configuratorSetId = ?1')->setParameter(1, $article->getConfiguratorSet()->getId())->getQuery()->getArrayResult();
             if (count($articles) <= 1) {
                 $set = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Set', $article->getConfiguratorSet()->getId());
                 Shopware()->Models()->remove($set);
             }
         }
         $data['configuratorSet'] = null;
     }
     return $data;
 }