1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dealer\Loop; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Dealer\Model\DealerImage; |
7
|
|
|
use Dealer\Model\DealerImageQuery; |
8
|
|
|
use Thelia\Core\Event\Image\ImageEvent; |
9
|
|
|
use Thelia\Core\Event\TheliaEvents; |
10
|
|
|
use Thelia\Core\Template\Element\LoopResult; |
11
|
|
|
use Thelia\Core\Template\Element\LoopResultRow; |
12
|
|
|
use Thelia\Core\Template\Element\PropelSearchLoopInterface; |
13
|
|
|
use Thelia\Core\Template\Loop\Argument\Argument; |
14
|
|
|
use Thelia\Core\Template\Loop\Image; |
15
|
|
|
use Thelia\Log\Tlog; |
16
|
|
|
|
17
|
|
|
class DealerCustomImagesLoop extends Image implements PropelSearchLoopInterface |
18
|
|
|
{ |
19
|
|
|
const ARG_IMAGE_TYPE = "image_type"; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array Possible standard image sources |
23
|
|
|
*/ |
24
|
|
|
protected $possible_sources = array('product', 'category'); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return DealerImageQuery|\Propel\Runtime\ActiveQuery\ModelCriteria|\Thelia\Model\ProductDocumentQuery |
28
|
|
|
*/ |
29
|
|
|
public function buildModelCriteria() |
30
|
|
|
{ |
31
|
|
|
$this->objectType = null; |
32
|
|
|
if (!empty($this->getProduct())) { |
33
|
|
|
$this->objectType = $this->possible_sources[0]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (is_null($this->objectType)) { |
37
|
|
|
throw new \InvalidArgumentException("Argument type must be specified"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$type = $this->getArgValue(self::ARG_IMAGE_TYPE); |
41
|
|
|
|
42
|
|
|
$search = DealerImageQuery::create(); |
43
|
|
|
$typeIndex = array_search($type, DealerImage::POSSIBLE_TYPE); |
44
|
|
|
$search->filterByType($typeIndex); |
45
|
|
|
$search->filterByDealerId($this->getProduct()); |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->configureI18nProcessing($search); |
49
|
|
|
return $search; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getArgDefinitions() |
53
|
|
|
{ |
54
|
|
|
$args = parent::getArgDefinitions(); |
55
|
|
|
$args->addArgument(Argument::createAlphaNumStringTypeArgument(self::ARG_IMAGE_TYPE)); |
56
|
|
|
return $args; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param LoopResult $loopResult |
61
|
|
|
* @return LoopResult |
62
|
|
|
*/ |
63
|
|
|
public function parseResults(LoopResult $loopResult) |
64
|
|
|
{ |
65
|
|
|
// Create image processing event |
66
|
|
|
$event = new ImageEvent(); |
67
|
|
|
|
68
|
|
|
// Prepare tranformations |
69
|
|
|
$width = $this->getWidth(); |
70
|
|
|
$height = $this->getHeight(); |
71
|
|
|
$rotation = $this->getRotation(); |
72
|
|
|
$background_color = $this->getBackgroundColor(); |
73
|
|
|
$quality = $this->getQuality(); |
74
|
|
|
$effects = $this->getEffects(); |
75
|
|
|
|
76
|
|
|
$event->setAllowZoom($this->getAllowZoom()); |
77
|
|
|
|
78
|
|
|
if (! is_null($effects)) { |
79
|
|
|
$effects = explode(',', $effects); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
switch ($this->getResizeMode()) { |
83
|
|
|
case 'crop': |
84
|
|
|
$resizeMode = \Thelia\Action\Image::EXACT_RATIO_WITH_CROP; |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case 'borders': |
88
|
|
|
$resizeMode = \Thelia\Action\Image::EXACT_RATIO_WITH_BORDERS; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'none': |
92
|
|
|
default: |
93
|
|
|
$resizeMode = \Thelia\Action\Image::KEEP_IMAGE_RATIO; |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** @var DealerImage $result */ |
98
|
|
|
foreach ($loopResult->getResultDataCollection() as $result) { |
99
|
|
|
// Setup required transformations |
100
|
|
|
if (! is_null($width)) { |
101
|
|
|
$event->setWidth($width); |
102
|
|
|
} |
103
|
|
|
if (! is_null($height)) { |
104
|
|
|
$event->setHeight($height); |
105
|
|
|
} |
106
|
|
|
$event->setResizeMode($resizeMode); |
107
|
|
|
if (! is_null($rotation)) { |
108
|
|
|
$event->setRotation($rotation); |
109
|
|
|
} |
110
|
|
|
if (! is_null($background_color)) { |
111
|
|
|
$event->setBackgroundColor($background_color); |
112
|
|
|
} |
113
|
|
|
if (! is_null($quality)) { |
114
|
|
|
$event->setQuality($quality); |
115
|
|
|
} |
116
|
|
|
if (! is_null($effects)) { |
117
|
|
|
$event->setEffects($effects); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Put source image file path |
121
|
|
|
$sourceFilePath = sprintf( |
122
|
|
|
'%s/%s', |
123
|
|
|
$result->getUploadDir(), |
124
|
|
|
$result->getFile() |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$event->setSourceFilepath($sourceFilePath); |
128
|
|
|
$event->setCacheSubdirectory($this->objectType); |
129
|
|
|
|
130
|
|
|
$loopResultRow = new LoopResultRow($result); |
131
|
|
|
|
132
|
|
|
$loopResultRow |
133
|
|
|
->set("ID", $result->getId()) |
134
|
|
|
->set("LOCALE", $this->locale) |
135
|
|
|
->set("ORIGINAL_IMAGE_PATH", $sourceFilePath) |
136
|
|
|
->set("TITLE", $result->getVirtualColumn('i18n_TITLE')) |
137
|
|
|
->set("CHAPO", $result->getVirtualColumn('i18n_CHAPO')) |
138
|
|
|
->set("DESCRIPTION", $result->getVirtualColumn('i18n_DESCRIPTION')) |
139
|
|
|
->set("POSTSCRIPTUM", $result->getVirtualColumn('i18n_POSTSCRIPTUM')) |
140
|
|
|
->set("OBJECT_TYPE", $this->objectType) |
141
|
|
|
->set("OBJECT_ID", $this->objectId) |
142
|
|
|
; |
143
|
|
|
|
144
|
|
|
$addRow = true; |
145
|
|
|
|
146
|
|
|
$returnErroredImages = $this->getBackendContext() || ! $this->getIgnoreProcessingErrors(); |
147
|
|
|
|
148
|
|
|
try { |
149
|
|
|
// Dispatch image processing event |
150
|
|
|
$this->dispatcher->dispatch(TheliaEvents::IMAGE_PROCESS, $event); |
151
|
|
|
|
152
|
|
|
$loopResultRow |
153
|
|
|
->set("IMAGE_URL", $event->getFileUrl()) |
154
|
|
|
->set("ORIGINAL_IMAGE_URL", $event->getOriginalFileUrl()) |
155
|
|
|
->set("IMAGE_PATH", $event->getCacheFilepath()) |
156
|
|
|
->set("PROCESSING_ERROR", false) |
157
|
|
|
; |
158
|
|
|
} catch (\Exception $ex) { |
159
|
|
|
// Ignore the result and log an error |
160
|
|
|
Tlog::getInstance()->addError(sprintf("Failed to process image in lapelerine_product_image loop: %s", $ex->getMessage())); |
161
|
|
|
|
162
|
|
|
if ($returnErroredImages) { |
163
|
|
|
$loopResultRow |
164
|
|
|
->set("IMAGE_URL", '') |
165
|
|
|
->set("ORIGINAL_IMAGE_URL", '') |
166
|
|
|
->set("IMAGE_PATH", '') |
167
|
|
|
->set("PROCESSING_ERROR", true) |
168
|
|
|
; |
169
|
|
|
} else { |
170
|
|
|
$addRow = false; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($addRow) { |
175
|
|
|
$this->addOutputFields($loopResultRow, $result); |
176
|
|
|
|
177
|
|
|
$loopResult->addRow($loopResultRow); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $loopResult; |
182
|
|
|
} |
183
|
|
|
} |