/** * Imports the values */ protected function importValues() { if (!$this->Group) { return; } /** * @var PlentySoapObject_GetItemAttributesAttributeValue $Value * @var Shopware\Models\Article\Configurator\Option $Option */ foreach ($this->Attribute->Values->item as $Value) { try { $SHOPWARE_optionId = PlentymarketsMappingController::getAttributeOptionByPlentyID($Value->ValueId); } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } foreach ($this->Group->getOptions() as $Option) { if ($Option->getId() != $SHOPWARE_optionId) { continue; } if (!is_null($Value->FrontendName)) { $Option->setName($Value->FrontendName); } elseif (!is_null($Value->BackendName)) { // use backend-name if frontend-name doesn't exists $Option->setName($Value->BackendName); } else { continue; } $Option->setPosition($Value->Position); } } }
/** * @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; }
/** * Saves the passed configurator group data. If an id passed, the function updates the existing group, otherwise * a new group will be created. */ public function saveConfiguratorGroupAction() { try { $id = (int) $this->Request()->getParam('id'); if (!empty($id)) { $group = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Group', $id); } else { $group = new \Shopware\Models\Article\Configurator\Group(); } $data = $this->Request()->getParams(); unset($data['options']); $group->fromArray($data); Shopware()->Models()->persist($group); Shopware()->Models()->flush(); $data['id'] = $group->getId(); $this->View()->assign(array('success' => true, 'data' => $data)); } catch (Exception $e) { $this->View()->assign(array('success' => false, 'message' => $e->getMessage())); } }