1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Media\Observers\FileUploadObserver |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-product-media |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Media\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Media\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Observer that uploads the file specified in a CSV file's column 'image_path' to a |
28
|
|
|
* configurable directoy. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/techdivision/import-product-media |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class FileUploadObserver extends AbstractProductImportObserver |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Process the observer's business logic. |
41
|
|
|
* |
42
|
|
|
* @return array The processed row |
43
|
|
|
*/ |
44
|
|
|
protected function process() |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// query whether or not, the image changed |
48
|
|
|
if ($this->isParentImage($image = $this->getValue(ColumnKeys::IMAGE_PATH))) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// initialize the image path |
53
|
|
|
$imagePath = $this->getValue(ColumnKeys::IMAGE_PATH); |
54
|
|
|
|
55
|
|
|
// query whether or not we've to upload the image files |
56
|
|
|
if ($this->hasCopyImages()) { |
57
|
|
|
// upload the file and set the new image path |
58
|
|
|
$imagePath = $this->uploadFile($image); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// temoprarily persist the image path |
62
|
|
|
$this->setValue(ColumnKeys::IMAGE_PATH_NEW, $imagePath); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Upload's the file with the passed name to the Magento |
67
|
|
|
* media directory. If the file already exists, the will |
68
|
|
|
* be given a new name that will be returned. |
69
|
|
|
* |
70
|
|
|
* @param string $filename The name of the file to be uploaded |
71
|
|
|
* |
72
|
|
|
* @return string The name of the uploaded file |
73
|
|
|
*/ |
74
|
|
|
protected function uploadFile($filename) |
75
|
|
|
{ |
76
|
|
|
return $this->getSubject()->uploadFile($filename); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return's the name of the created image. |
81
|
|
|
* |
82
|
|
|
* @return string The name of the created image |
83
|
|
|
*/ |
84
|
|
|
protected function getParentImage() |
85
|
|
|
{ |
86
|
|
|
return $this->getSubject()->getParentImage(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return's TRUE if the passed image is the parent one. |
91
|
|
|
* |
92
|
|
|
* @param string $image The imageD to check |
93
|
|
|
* |
94
|
|
|
* @return boolean TRUE if the passed image is the parent one |
95
|
|
|
*/ |
96
|
|
|
protected function isParentImage($image) |
97
|
|
|
{ |
98
|
|
|
return $this->getParentImage() === $image; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return's the flag to copy images or not. |
103
|
|
|
* |
104
|
|
|
* @return booleas The flag |
105
|
|
|
*/ |
106
|
|
|
protected function hasCopyImages() |
107
|
|
|
{ |
108
|
|
|
return $this->getSubject()->hasCopyImages(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|