Exemplo n.º 1
0
 /**
  * Resolves the passed configuratorOptions parameter for a single variant.
  * Each passed configurator option, has to be configured in the article configurator set.
  *
  * @param array $data
  * @param ArticleModel $article
  * @param Detail $variant
  * @return \Doctrine\Common\Collections\Collection
  * @throws \Shopware\Components\Api\Exception\CustomValidationException
  */
 protected function prepareConfigurator(array $data, ArticleModel $article, Detail $variant)
 {
     if (!$article->getConfiguratorSet()) {
         throw new ApiException\CustomValidationException('A configurator set has to be defined');
     }
     $availableGroups = $article->getConfiguratorSet()->getGroups();
     $options = new ArrayCollection();
     foreach ($data['configuratorOptions'] as $optionData) {
         $availableGroup = $this->getAvailableGroup($availableGroups, array('id' => $optionData['groupId'], 'name' => $optionData['group']));
         //group is in the article configurator set configured?
         if (!$availableGroup) {
             continue;
         }
         //check if the option is available in the configured article configurator set.
         $option = $this->getAvailableOption($availableGroup->getOptions(), array('id' => $optionData['optionId'], 'name' => $optionData['option']));
         if (!$option) {
             if (!$optionData['option']) {
                 throw new ApiException\CustomValidationException('A new configurator option requires a name');
             }
             $option = new Option();
             $option->setPosition(0);
             $option->setName($optionData['option']);
             $option->setGroup($availableGroup);
             $this->getManager()->persist($option);
         }
         $options->add($option);
     }
     $data['configuratorOptions'] = $options;
     $variant->setConfiguratorOptions($options);
     return $data;
 }
Exemplo n.º 2
0
 private function insertConfiguratorData($groups)
 {
     $pos = 1;
     $data = array();
     foreach ($groups as $groupName => $options) {
         $group = new Models\Article\Configurator\Group();
         $group->setName($groupName);
         $group->setPosition($groups);
         $this->db->executeQuery("DELETE FROM s_article_configurator_groups WHERE name = ?", array($groupName));
         $collection = array();
         $optionPos = 1;
         foreach ($options as $optionName) {
             $this->db->executeQuery("DELETE FROM s_article_configurator_options WHERE name = ?", array($optionName));
             $option = new Models\Article\Configurator\Option();
             $option->setName($optionName);
             $option->setPosition($optionPos);
             $collection[] = $option;
             $optionPos++;
         }
         $group->setOptions($collection);
         $pos++;
         $data[] = $group;
         $this->entityManager->persist($group);
         $this->entityManager->flush();
         $this->entityManager->clear();
         $this->createdConfiguratorGroups[] = $group->getId();
     }
     return $data;
 }
Exemplo n.º 3
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 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 = [];
     $allGroups = [];
     $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 Configurator\Group();
                 $group->setPosition($groupPosition);
             }
         } else {
             throw new ApiException\CustomValidationException('At least the groupname is required');
         }
         $groupOptions = [];
         $optionPosition = 0;
         foreach ($groupData['options'] as $optionData) {
             $option = null;
             if ($group->getId() > 0) {
                 if (isset($optionData['id'])) {
                     $option = $this->getManager()->find('Shopware\\Models\\Article\\Configurator\\Option', $optionData['id']);
                     if (!$option) {
                         throw new ApiException\CustomValidationException(sprintf("ConfiguratorOption by id %s not found", $optionData['id']));
                     }
                 } else {
                     $option = $this->getManager()->getRepository('Shopware\\Models\\Article\\Configurator\\Option')->findOneBy(['name' => $optionData['name'], 'groupId' => $group->getId()]);
                 }
             }
             if (!$option) {
                 $option = new Configurator\Option();
             }
             $option->fromArray($optionData);
             $option->setGroup($group);
             if (!isset($optionData['position'])) {
                 $option->setPosition($optionPosition++);
             }
             $allOptions[] = $option;
             $groupOptions[] = $option;
         }
         $groupData['options'] = $groupOptions;
         $group->fromArray($groupData);
         $allGroups[] = $group;
     }
     // Clear needed in order to allow updates on configuratorSet. When removed constraints in
     // s_article_configurator_set_group_relations and s_article_configurator_set_option_relations
     // might fail.
     $configuratorSet->getOptions()->clear();
     $configuratorSet->setOptions($allOptions);
     $configuratorSet->getGroups()->clear();
     $configuratorSet->setGroups($allGroups);
     $this->getManager()->persist($configuratorSet);
     $data['configuratorSet'] = $configuratorSet;
     return $data;
 }