/** * 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); } } }
/** * Calculate hashes for current version * * @return array */ protected function loadHashesForInstalledFiles() { $result = array(); $module = $this->getModuleInstalled(); if ($module) { $pack = new \XLite\Core\Pack\Module($module); foreach ($pack->getDirectoryIterator() as $file) { if ($file->isFile()) { $relativePath = \Includes\Utils\FileManager::getRelativePath($file->getPathname(), LC_DIR_ROOT); if ($relativePath) { $result[$relativePath] = \Includes\Utils\FileManager::getHash($file->getPathname(), true); } } } } return $result ?: $this->getHashes(true); }
/** * Create list of archive file hashes and add it to the pack * * @param \PharData $phar PHAR archive * @param \Iterator $iterator Directory iterator * * @return void */ protected static function addPackHash(\PharData $phar, \Iterator $iterator) { $data = array(); foreach ($iterator as $filePath => $fileInfo) { $data[\Includes\Utils\FileManager::getRelativePath($filePath, LC_DIR_ROOT)] = \Includes\Utils\FileManager::getHash($filePath); } $phar->addFromString('.hash', json_encode($data)); }
/** * Renew properties by path * * @param string $path Path * * @return boolean */ protected function renewByPath($path) { $result = parent::renewByPath($path); if ($result) { $data = @getimagesize($path); if (is_array($data)) { $this->setWidth($data[0]); $this->setHeight($data[1]); $this->setMime($data['mime']); $hash = \Includes\Utils\FileManager::getHash($path); if ($hash) { $this->setHash($hash); } } else { $result = false; } } return $result; }
/** * Detect image * * @param \XLite\Model\Product $product Product * @param array $image Image info * * @return \XLite\Model\Image\Product\Image */ protected function detectImage(\XLite\Model\Product $product, array $image) { $hash = \Includes\Utils\FileManager::getHash($image['url']); $model = null; if ($hash) { foreach ($product->getImages() as $oldImage) { if ($oldImage->getHash() == $hash) { $model = $oldImage; break; } } } return $model; }