/** * Common handler for product attachments * * @param string $methodToLoad Method to use for getting images * @param array $paramsToLoad Parameters to use in attachment getter method * * @return void */ protected function doActionSelectProductAttachments($methodToLoad, array $paramsToLoad) { $productId = intval(\XLite\Core\Request::getInstance()->objectId); $product = \XLite\Core\Database::getRepo('XLite\\Model\\Product')->find($productId); if (isset($product)) { $attachment = new \XLite\Module\CDev\FileAttachments\Model\Product\Attachment(); $attachment->setProduct($product); if (call_user_func_array(array($attachment->getStorage(), $methodToLoad), $paramsToLoad)) { $found = false; foreach ($attachment->getStorage()->getDuplicates() as $duplicate) { if ($duplicate instanceof \XLite\Module\CDev\FileAttachments\Model\Product\Attachment\Storage && $duplicate->getAttachment()->getProduct()->getProductId() == $product->getProductId()) { $found = true; break; } } if ($found) { \XLite\Core\TopMessage::addError('The same file can not be assigned to one product'); } else { $product->addAttachments($attachment); \XLite\Core\Database::getEM()->persist($attachment); \XLite\Core\Database::getEM()->flush(); \XLite\Core\TopMessage::addInfo('The attachment has been added successfully'); } } elseif ('extension' == $attachment->getStorage()->getLoadError()) { // Forbid extension \XLite\Core\TopMessage::addError('Failed to add the attachment. The file download is forbidden'); } else { \XLite\Core\TopMessage::addError('Failed to add the attachment'); } } else { \XLite\Core\TopMessage::addError('Failed to add the attachment'); } }
public function testGetStorage() { \Includes\Utils\FileManager::unlinkRecursive(LC_DIR_FILES . 'attachments'); $attach = new \XLite\Module\CDev\FileAttachments\Model\Product\Attachment(); $this->assertInstanceOf('XLite\\Module\\CDev\\FileAttachments\\Model\\Product\\Attachment\\Storage', $attach->getStorage(), 'check class'); $this->assertEquals($attach, $attach->getStorage()->getAttachment(), 'check attachment'); }
/** * Import 'attachments' value * * @param \XLite\Model\Product $model Product * @param array $value Value * @param array $column Column info * * @return void */ protected function importAttachmentsColumn(\XLite\Model\Product $model, array $value, array $column) { if ($value) { foreach ($value as $index => $path) { if ($this->verifyValueAsFile($path)) { $attachment = $model->getAttachments()->get($index); if (!$attachment) { $attachment = new \XLite\Module\CDev\FileAttachments\Model\Product\Attachment(); $attachment->setProduct($model); $model->getAttachments()->add($attachment); \XLite\Core\Database::getEM()->persist($attachment); } if (1 < count(parse_url($path))) { $attachment->getStorage()->loadFromURL($path, true); } else { $attachment->getStorage()->loadFromLocalFile($this->importer->getOptions()->dir . LC_DS . $path); } } } while (count($model->getAttachments()) > count($value)) { $attachment = $model->getAttachments()->last(); \XLite\Core\Database::getRepo('XLite\\Module\\CDev\\FileAttachments\\Model\\Product\\Attachment')->delete($attachment, false); $model->getAttachments()->removeElement($attachment); } } }