Пример #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;
    }
Пример #2
0
 /**
  * Saves the passed configurator option data. If an id passed, the function updates the existing options, otherwise
  * a new option will be created.
  */
 public function saveConfiguratorOptionAction()
 {
     try {
         $id = (int) $this->Request()->getParam('id');
         if (!empty($id)) {
             $option = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Option', $id);
         } else {
             $option = new \Shopware\Models\Article\Configurator\Option();
         }
         $data = $this->Request()->getParams();
         if (empty($data['groupId'])) {
             return;
         }
         $data['group'] = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Group', $data['groupId']);
         $option->fromArray($data);
         Shopware()->Models()->persist($option);
         Shopware()->Models()->flush();
         $data['id'] = $option->getId();
         $this->View()->assign(array('success' => true, 'data' => $data));
     } catch (Exception $e) {
         $this->View()->assign(array('success' => false, 'message' => $e->getMessage()));
     }
 }