/** * Saves the attribute related data to the storage * * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items * @param array $data List of CSV fields with position as key and data as value * @return array List of data which hasn't been imported */ public function process(\Aimeos\MShop\Product\Item\Iface $product, array $data) { $context = $this->getContext(); $manager = \Aimeos\MShop\Factory::createManager($context, 'attribute'); $listManager = \Aimeos\MShop\Factory::createManager($context, 'product/lists'); $separator = $context->getConfig()->get('controller/common/product/import/csv/separator', "\n"); $manager->begin(); try { $pos = 0; $delete = $attrcodes = array(); $map = $this->getMappedChunk($data); $listItems = $product->getListItems('attribute', $this->listTypes); foreach ($listItems as $listId => $listItem) { if (isset($map[$pos])) { if (!isset($map[$pos]['attribute.code']) || !isset($map[$pos]['attribute.type'])) { unset($map[$pos]); continue; } $refItem = $listItem->getRefItem(); if ($refItem !== null && $map[$pos]['attribute.code'] === $refItem->getCode() && $map[$pos]['attribute.type'] === $refItem->getType() && (!isset($map[$pos]['product.lists.type']) || isset($map[$pos]['product.lists.type']) && $map[$pos]['product.lists.type'] === $listItem->getType())) { $pos++; continue; } } $listItems[$listId] = null; $delete[] = $listId; $pos++; } $listManager->deleteItems($delete); foreach ($map as $pos => $list) { if (!isset($list['attribute.code']) || $list['attribute.code'] === '' || $list['attribute.type'] === '' || isset($list['product.lists.type']) && $this->listTypes !== null && !in_array($list['product.lists.type'], (array) $this->listTypes)) { continue; } $codes = explode($separator, $list['attribute.code']); foreach ($codes as $code) { $attrItem = $this->getAttributeItem($code, $list['attribute.type']); $attrItem->fromArray($list); $attrItem->setCode($code); $manager->saveItem($attrItem); if (($listItem = array_shift($listItems)) === null) { $listItem = $listManager->createItem(); } $typecode = isset($list['product.lists.type']) ? $list['product.lists.type'] : 'default'; $list['product.lists.typeid'] = $this->getTypeId('product/lists/type', 'attribute', $typecode); $list['product.lists.refid'] = $attrItem->getId(); $list['product.lists.parentid'] = $product->getId(); $list['product.lists.domain'] = 'attribute'; $listItem->fromArray($this->addListItemDefaults($list, $pos)); $listManager->saveItem($listItem); } } $remaining = $this->getObject()->process($product, $data); $manager->commit(); } catch (\Exception $e) { $manager->rollback(); throw $e; } return $remaining; }
/** * Saves the product related data to the storage * * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items * @param array $data List of CSV fields with position as key and data as value * @return array List of data which hasn't been imported */ public function process(\Aimeos\MShop\Product\Item\Iface $product, array $data) { $context = $this->getContext(); $manager = \Aimeos\MShop\Factory::createManager($context, 'media'); $listManager = \Aimeos\MShop\Factory::createManager($context, 'product/lists'); $separator = $context->getConfig()->get('controller/common/product/import/csv/separator', "\n"); $manager->begin(); try { $map = $this->getMappedChunk($data); $listItems = $product->getListItems('media'); foreach ($map as $pos => $list) { if ($this->checkEntry($list) === false) { continue; } $urls = explode($separator, $list['media.url']); $type = $this->getValue($list, 'media.type', 'default'); $typecode = $this->getValue($list, 'product.lists.type', 'default'); if (($langid = $this->getValue($list, 'media.languageid', null)) === '') { $langid = null; } foreach ($urls as $url) { if (($listItem = array_shift($listItems)) !== null) { $refItem = $listItem->getRefItem(); } else { $listItem = $listManager->createItem(); $refItem = $manager->createItem(); } $list['media.typeid'] = $this->getTypeId('media/type', 'product', $type); $list['media.languageid'] = $langid; $list['media.domain'] = 'product'; $list['media.url'] = $url; $refItem->fromArray($this->addItemDefaults($list)); $manager->saveItem($refItem); $list['product.lists.typeid'] = $this->getTypeId('product/lists/type', 'media', $typecode); $list['product.lists.parentid'] = $product->getId(); $list['product.lists.refid'] = $refItem->getId(); $list['product.lists.domain'] = 'media'; $listItem->fromArray($this->addListItemDefaults($list, $pos++)); $listManager->saveItem($listItem); } } foreach ($listItems as $listItem) { $manager->deleteItem($listItem->getRefItem()->getId()); $listManager->deleteItem($listItem->getId()); } $remaining = $this->getObject()->process($product, $data); $manager->commit(); } catch (\Exception $e) { $manager->rollback(); throw $e; } return $remaining; }
/** * Saves the product related data to the storage * * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items * @param array $data List of CSV fields with position as key and data as value * @return array List of data which hasn't been imported */ public function process(\Aimeos\MShop\Product\Item\Iface $product, array $data) { $listManager = \Aimeos\MShop\Factory::createManager($this->getContext(), 'product/lists'); $manager = \Aimeos\MShop\Factory::createManager($this->getContext(), 'price'); $manager->begin(); try { $listItems = $product->getListItems('price'); $map = $this->getMappedChunk($data); foreach ($map as $pos => $list) { if (!isset($list['price.value']) || $list['price.value'] === '' || isset($list['product.lists.type']) && $this->listTypes !== null && !in_array($list['product.lists.type'], (array) $this->listTypes)) { continue; } if (($listItem = array_shift($listItems)) !== null) { $refItem = $listItem->getRefItem(); } else { $listItem = $listManager->createItem(); $refItem = $manager->createItem(); } $typecode = isset($list['price.type']) ? $list['price.type'] : 'default'; $list['price.typeid'] = $this->getTypeId('price/type', 'product', $typecode); $list['price.domain'] = 'product'; $refItem->fromArray($this->addItemDefaults($list)); $manager->saveItem($refItem); $typecode = isset($list['product.lists.type']) ? $list['product.lists.type'] : 'default'; $list['product.lists.typeid'] = $this->getTypeId('product/lists/type', 'price', $typecode); $list['product.lists.parentid'] = $product->getId(); $list['product.lists.refid'] = $refItem->getId(); $list['product.lists.domain'] = 'price'; $listItem->fromArray($this->addListItemDefaults($list, $pos)); $listManager->saveItem($listItem); } foreach ($listItems as $listItem) { $manager->deleteItem($listItem->getRefItem()->getId()); $listManager->deleteItem($listItem->getId()); } $remaining = $this->getObject()->process($product, $data); $manager->commit(); } catch (\Exception $e) { $manager->rollback(); throw $e; } return $remaining; }
/** * Adds all texts belonging to an product item. * * @param \Aimeos\MW\Container\Content\Iface $contentItem Content item * @param \Aimeos\MShop\Product\Item\Iface $item product item object * @param string $langid Language id */ protected function addItem(\Aimeos\MW\Container\Content\Iface $contentItem, \Aimeos\MShop\Product\Item\Iface $item, $langid) { $listTypes = array(); foreach ($item->getListItems('text') as $listItem) { $listTypes[$listItem->getRefId()] = $listItem->getType(); } foreach ($this->getTextTypes('product') as $textTypeItem) { $textItems = $item->getRefItems('text', $textTypeItem->getCode()); if (!empty($textItems)) { foreach ($textItems as $textItem) { $listType = isset($listTypes[$textItem->getId()]) ? $listTypes[$textItem->getId()] : ''; $items = array($langid, $item->getType(), $item->getCode(), $listType, $textTypeItem->getCode(), '', ''); // use language of the text item because it may be null if (($textItem->getLanguageId() == $langid || is_null($textItem->getLanguageId())) && $textItem->getTypeId() == $textTypeItem->getId()) { $items[0] = $textItem->getLanguageId(); $items[5] = $textItem->getId(); $items[6] = $textItem->getContent(); } $contentItem->add($items); } } else { $items = array($langid, $item->getType(), $item->getCode(), 'default', $textTypeItem->getCode(), '', ''); $contentItem->add($items); } } }
protected function delete(\Aimeos\MShop\Product\Item\Iface $product) { $manager = \Aimeos\MShop\Product\Manager\Factory::createManager($this->context); $listManager = $manager->getSubManager('lists'); foreach ($product->getListItems('attribute') as $listItem) { $listManager->deleteItem($listItem->getId()); } $manager->deleteItem($product->getId()); }
/** * Returns the pool of list items that can be reassigned * * @param \Aimeos\MShop\Product\Item\Iface $product Product item object * @param array $map List of associative arrays containing the chunked properties * @return array List of list items implementing \Aimeos\MShop\Common\Item\Lists\Iface */ protected function getListItemPool(\Aimeos\MShop\Product\Item\Iface $product, array $map) { $pos = 0; $delete = array(); $listItems = $product->getListItems('product', $this->listTypes); foreach ($listItems as $listId => $listItem) { $refItem = $listItem->getRefItem(); if (isset($map[$pos]) && (!isset($map[$pos]['product.code']) || $refItem !== null && $map[$pos]['product.code'] === $refItem->getCode())) { $pos++; continue; } $listItems[$listId] = null; $delete[] = $listId; $pos++; } $listManager = \Aimeos\MShop\Factory::createManager($this->getContext(), 'product/lists'); $listManager->deleteItems($delete); return $listItems; }