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; }
/** * @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; }