/** * Saves the attribute related data to the storage * * @param MShop_Product_Item_Interface $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(MShop_Product_Item_Interface $product, array $data) { $context = $this->_getContext(); $manager = MShop_Factory::createManager($context, 'attribute'); $listManager = MShop_Factory::createManager($context, 'product/list'); $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.list.type']) || isset($map[$pos]['product.list.type']) && $map[$pos]['product.list.type'] === $listItem->getType())) { $pos++; continue; } } $listItems[$listId] = null; $delete[] = $listId; $pos++; } $listManager->deleteItems($delete); foreach ($map as $pos => $list) { if ($list['attribute.code'] === '' || $list['attribute.type'] === '' || isset($list['product.list.type']) && $this->_listTypes !== null && !in_array($list['product.list.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.list.type']) ? $list['product.list.type'] : 'default'; $list['product.list.typeid'] = $this->_getTypeId('product/list/type', 'attribute', $typecode); $list['product.list.refid'] = $attrItem->getId(); $list['product.list.parentid'] = $product->getId(); $list['product.list.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 MShop_Product_Item_Interface $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(MShop_Product_Item_Interface $product, array $data) { $context = $this->_getContext(); $manager = MShop_Factory::createManager($context, 'media'); $listManager = MShop_Factory::createManager($context, 'product/list'); $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 (!isset($list['media.url']) || $list['media.url'] === '' || isset($list['product.list.type']) && $this->_listTypes !== null && !in_array($list['product.list.type'], (array) $this->_listTypes)) { continue; } $urls = explode($separator, $list['media.url']); $type = isset($list['media.type']) ? $list['media.type'] : 'default'; $typecode = isset($list['product.list.type']) ? $list['product.list.type'] : 'default'; 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.domain'] = 'product'; $list['media.url'] = $url; $refItem->fromArray($this->_addItemDefaults($list)); $manager->saveItem($refItem); $list['product.list.typeid'] = $this->_getTypeId('product/list/type', 'media', $typecode); $list['product.list.parentid'] = $product->getId(); $list['product.list.refid'] = $refItem->getId(); $list['product.list.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 property related data to the storage * * @param MShop_Product_Item_Interface $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(MShop_Product_Item_Interface $product, array $data) { $manager = MShop_Factory::createManager($this->_getContext(), 'product/property'); $manager->begin(); try { $pos = 0; $delete = array(); $map = $this->_getMappedChunk($data); $items = $this->_getPropertyItems($product->getId()); foreach ($items as $id => $item) { if (isset($map[$pos])) { if (!isset($map[$pos]['product.property.type']) || !isset($map[$pos]['product.property.value'])) { unset($map[$pos]); continue; } if ($map[$pos]['product.property.type'] === $item->getType() && $map[$pos]['product.property.value'] === $item->getValue() && (!isset($map[$pos]['product.property.languageid']) || isset($map[$pos]['product.property.languageid']) && $map[$pos]['product.property.languageid'] === $item->getLanguageId())) { $pos++; continue; } } $items[$id] = null; $delete[] = $id; $pos++; } $manager->deleteItems($delete); foreach ($map as $pos => $list) { if ($list['product.property.type'] == '' || $list['product.property.value'] == '') { continue; } $typecode = $list['product.property.type']; $list['product.property.typeid'] = $this->_getTypeId('product/property/type', 'product/property', $typecode); $list['product.property.parentid'] = $product->getId(); if (($item = array_shift($items)) === null) { $item = $manager->createItem(); } $item->fromArray($list); $manager->saveItem($item); } $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 MShop_Product_Item_Interface $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(MShop_Product_Item_Interface $product, array $data) { $listManager = MShop_Factory::createManager($this->_getContext(), 'product/list'); $manager = 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.list.type']) && $this->_listTypes !== null && !in_array($list['product.list.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.list.type']) ? $list['product.list.type'] : 'default'; $list['product.list.typeid'] = $this->_getTypeId('product/list/type', 'price', $typecode); $list['product.list.parentid'] = $product->getId(); $list['product.list.refid'] = $refItem->getId(); $list['product.list.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; }
/** * Saves the product stock related data to the storage * * @param MShop_Product_Item_Interface $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(MShop_Product_Item_Interface $product, array $data) { $manager = MShop_Factory::createManager($this->_getContext(), 'product/stock'); $manager->begin(); try { $pos = 0; $delete = array(); $map = $this->_getMappedChunk($data); $items = $this->_getStockItems($product->getId()); foreach ($map as $pos => $list) { if (!array_key_exists('product.stock.stocklevel', $list)) { continue; } $whcode = isset($list['product.stock.warehouse']) ? $list['product.stock.warehouse'] : 'default'; if (!isset($list['product.stock.warehouseid'])) { $list['product.stock.warehouseid'] = $this->_cache->get($whcode); } if (isset($list['product.stock.dateback']) && $list['product.stock.dateback'] === '') { $list['product.stock.dateback'] = null; } if ($list['product.stock.stocklevel'] === '') { $list['product.stock.stocklevel'] = null; } $list['product.stock.productid'] = $product->getId(); if (($item = array_pop($items)) === null) { $item = $manager->createItem(); } $item->fromArray($list); $manager->saveItem($item); } $manager->deleteItems(array_keys($items)); $remaining = $this->_getObject()->process($product, $data); $manager->commit(); } catch (Exception $e) { $manager->rollback(); throw $e; } return $remaining; }
/** * Copys all data from a given product item. * * @param MShop_Product_Item_Interface $product Product item to copy from */ public function copyFrom(MShop_Product_Item_Interface $product) { $this->setName($product->getName()); $this->setType($product->getType()); $this->setSupplierCode($product->getSupplierCode()); $this->setProductCode($product->getCode()); $this->setProductId($product->getId()); $items = $product->getRefItems('media', 'default', 'default'); if (($item = reset($items)) !== false) { $this->setMediaUrl($item->getUrl()); } $this->setModified(); }
protected function _delete(MShop_Product_Item_Interface $product) { $manager = MShop_Product_Manager_Factory::createManager($this->_context); $listManager = $manager->getSubManager('list'); foreach ($product->getListItems('attribute') as $listItem) { $listManager->deleteItem($listItem->getId()); } $manager->deleteItem($product->getId()); }
/** * Adds all texts belonging to an product item. * * @param MW_Container_Content_Interface $contentItem Content item * @param MShop_Product_Item_Interface $item product item object * @param string $langid Language id */ protected function _addItem(MW_Container_Content_Interface $contentItem, MShop_Product_Item_Interface $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); } } }
/** * Create new product item object initialized with given parameters. * * @param MShop_Product_Item_Interface $item Product item object * @return array Associative list of key/value pairs suitable for product item constructor */ protected function _createArray(MShop_Product_Item_Interface $item) { return array('id' => $item->getId(), 'typeid' => $item->getTypeId(), 'type' => $item->getType(), 'status' => $item->getStatus(), 'label' => $item->getLabel(), 'start' => $item->getDateStart(), 'end' => $item->getDateEnd(), 'code' => $item->getCode(), 'suppliercode' => $item->getSupplierCode(), 'ctime' => $item->getTimeCreated(), 'mtime' => $item->getTimeModified(), 'editor' => $item->getEditor()); }
/** * Returns the product variants of a selection product that match the given attributes. * * @param MShop_Product_Item_Interface $productItem Product item including sub-products * @param array $variantAttributeIds IDs for the variant-building attributes * @param array $domains Names of the domain items that should be fetched too * @return array List of products matching the given attributes */ protected function _getProductVariants(MShop_Product_Item_Interface $productItem, array $variantAttributeIds, array $domains = array('attribute', 'media', 'price', 'text')) { $subProductIds = array(); foreach ($productItem->getRefItems('product', 'default', 'default') as $item) { $subProductIds[] = $item->getId(); } if (count($subProductIds) === 0) { return array(); } $productManager = MShop_Factory::createManager($this->_getContext(), 'product'); $search = $productManager->createSearch(true); $expr = array($search->compare('==', 'product.id', $subProductIds), $search->getConditions()); if (count($variantAttributeIds) > 0) { $listTypeItem = $this->_getProductListTypeItem('attribute', 'variant'); $param = array('attribute', $listTypeItem->getId(), $variantAttributeIds); $cmpfunc = $search->createFunction('product.contains', $param); $expr[] = $search->compare('==', $cmpfunc, count($variantAttributeIds)); } $search->setConditions($search->combine('&&', $expr)); return $productManager->searchItems($search, $domains); }
/** * Returns the pool of list items that can be reassigned * * @param MShop_Product_Item_Interface $product Product item object * @param array $map List of associative arrays containing the chunked properties * @return array List of list items implementing MShop_Common_Item_List_Interface */ protected function _getListItemPool(MShop_Product_Item_Interface $product, array $map) { $pos = 0; $delete = array(); $listItems = $this->_getListItems($product->getId(), $this->_listTypes); foreach ($listItems as $listId => $listItem) { if (isset($map[$pos]) && (!isset($map[$pos]['catalog.code']) || $this->_cache->get($map[$pos]['catalog.code']) == $listItem->getParentId())) { $pos++; continue; } $listItems[$listId] = null; $delete[] = $listId; $pos++; } $listManager = MShop_Factory::createManager($this->_getContext(), 'catalog/list'); $listManager->deleteItems($delete); return $listItems; }
/** * Returns the pool of list items that can be reassigned * * @param MShop_Product_Item_Interface $product Product item object * @param array $map List of associative arrays containing the chunked properties * @return array List of list items implementing MShop_Common_Item_List_Interface */ protected function _getListItemPool(MShop_Product_Item_Interface $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 = MShop_Factory::createManager($this->_getContext(), 'product/list'); $listManager->deleteItems($delete); return $listItems; }
protected function _delete(MShop_Product_Item_Interface $product) { $manager = MShop_Product_Manager_Factory::createManager($this->_context); $manager->deleteItem($product->getId()); }