/** * Import 'images' value * * @param \XLite\Model\Product $model Product * @param array $value Value * @param array $column Column info * * @return void */ protected function importImagesColumn(\XLite\Model\Product $model, array $value, array $column) { if ($value) { foreach ($value as $index => $path) { if ($this->verifyValueAsFile($path)) { $image = $model->getImages() ? $model->getImages()->get($index) : null; $isNew = false; if (!$image) { $isNew = true; $image = new \XLite\Model\Image\Product\Image(); } if (1 < count(parse_url($path))) { $success = $image->loadFromURL($path, true); } else { $dir = \Includes\Utils\FileManager::getRealPath(LC_DIR_VAR . $this->importer->getOptions()->dir); $success = $image->loadFromLocalFile($dir . LC_DS . $path); } if (!$success) { $this->addError('PRODUCT-IMG-LOAD-FAILED', array('column' => $column, 'value' => $path)); } else { $image->setNeedProcess(1); if ($isNew) { $image->setProduct($model); $model->getImages()->add($image); \XLite\Core\Database::getEM()->persist($image); } } } } while (count($model->getImages()) > count($value)) { $image = $model->getImages()->last(); \XLite\Core\Database::getRepo('XLite\\Model\\Image\\Product\\Image')->delete($image, false); $model->getImages()->removeElement($image); } } }
/** * Import images * * @param \XLite\Model\Product $product Product * @param string $data Data * * @return void */ protected function importImages(\XLite\Model\Product $product, $data) { // Save old images ids' list $oldImageIds = array(); foreach ($product->getImages() as $image) { $oldImageIds[] = $image->getId(); } if ($data) { // Load images foreach (explode(';', $data) as $url) { $url = trim($url); $hash = \Includes\Utils\FileManager::getHash($url); $image = null; if ($hash) { foreach ($product->getImages() as $i) { if ($i->getHash() == $hash) { $image = $i; $key = array_search($i->getId(), $oldImageIds); unset($oldImageIds[$key]); break; } } } if (!$image) { $image = new \XLite\Model\Image\Product\Image(); $image->setProduct($product); $result = preg_match('/^(https?|ftp):\\/\\//Ss', $url) ? $image->loadFromURL($url) : $image->loadFromLocalFile($url); if ($result) { $product->addImages($image); \XLite\Core\Database::getEM()->persist($image); } else { $this->logImportWarning(static::t('X image unable to load', array('url' => $url)), $this->importCell['position'], 'images', $url, $product); } } } } // Remove old images foreach ($product->getImages() as $image) { if (in_array($image->getId(), $oldImageIds)) { $product->getImages()->removeElement($image); \XLite\Core\Database::getEM()->remove($image); } } }
/** * Create image * * @param \XLite\Model\Product $product Product * @param array $image Image info * * @return \XLite\Model\Image\Product\Image */ protected function createImage(\XLite\Model\Product $product, array $image) { $model = new \XLite\Model\Image\Product\Image(); $model->setProduct($product); if ($model->loadFromURL($image['url'], $this->isLoadImageToLocalFileSystem())) { $product->addImages($model); \XLite\Core\Database::getEM()->persist($model); } else { \XLite\Logger::getInstance()->log('The picture of the product (' . $image['url'] . ') was not imported.', LOG_ERR); } return $model; }
/** * Common handler for product images. * * @param string $methodToLoad Method to use for getting images * @param array $paramsToLoad Parameters to use in image getter method * * @return void */ protected function doActionSelectProductImages($methodToLoad, array $paramsToLoad) { $productId = intval(\XLite\Core\Request::getInstance()->objectId); $product = \XLite\Core\Database::getRepo('\\XLite\\Model\\Product')->find($productId); if (!isset($product)) { $product = new \XLite\Model\Product(); } $image = new \XLite\Model\Image\Product\Image(); // TODO: methodToLoad - move to this controller from Image model if (call_user_func_array(array($image, $methodToLoad), $paramsToLoad)) { $image->setProduct($product); $product->getImages()->add($image); \XLite\Core\Database::getEM()->persist($image); \XLite\Core\Database::getEM()->flush(); \XLite\Core\TopMessage::addInfo('The detailed image has been added successfully'); } else { \XLite\Core\TopMessage::addError('Failed to add detailed image'); } }
protected function getProduct() { if (!isset($this->product)) { $this->product = \XLite\Core\Database::getRepo('XLite\\Model\\Product')->findOneByEnabled(true); if (!$this->product) { $this->fail('Enabled product not found'); } elseif (!$this->product->getProductId()) { $this->fail('Product has not product_id'); } // Remove old detailed images foreach ($this->product->getImages() as $i) { \XLite\Core\Database::getEM()->remove($i); } $this->product->getImages()->clear(); \XLite\Core\Database::getEM()->flush(); foreach ($this->images as $path) { $i = new \XLite\Model\Image\Product\Image(); $i->setProduct($this->product); $this->product->getImages()->add($i); $i->loadFromLocalFile(__DIR__ . LC_DS . $path); $i->setAlt($path); $i->setOrderby(1); \XLite\Core\Database::getEM()->persist($i); } \XLite\Core\Database::getEM()->flush(); } return $this->product; }