1 <?php
2 3 4 5 6 7 8 9 10
11
12 13 14 15 16 17 18
19 class ProductImageContainerRepository implements ProductImageContainerRepositoryInterface
20 {
21 22 23 24 25
26 protected $db;
27
28
29 30 31 32 33
34 public function __construct(CI_DB_query_builder $db)
35 {
36 $this->db = $db;
37 }
38
39
40 41 42 43 44 45 46 47 48 49
50 public function store(IdType $productId, ProductImageContainerInterface $imageContainer)
51 {
52 $this->deleteByProductId($productId);
53
54
55 $primaryImageData = array(
56 'products_image' => $imageContainer->getPrimary()->getFilename(),
57 'products_image_w' => 0,
58 'products_image_h' => 0,
59 'gm_show_image' => $imageContainer->getPrimary()->isVisible()
60 );
61
62 $this->db->where('products_id', $productId->asInt())->update('products', $primaryImageData);
63
64 $this->_savePrimaryImageAltText($imageContainer, $productId);
65
66 $this->_saveAdditionalImagesAltText($imageContainer, $productId);
67
68 return $this;
69 }
70
71
72 73 74 75 76 77 78 79 80
81 public function getByProductId(IdType $productId)
82 {
83 $primaryImageQuery = $this->_queryPrimaryImage($productId);
84
85 $additionalImagesQuery = $this->_queryAdditionalImages($productId);
86
87 $imageContainer = MainFactory::create('ProductImageContainer');
88
89
90 if(!empty($primaryImageQuery['products_image']))
91 {
92 $primaryImageFilename = new FilenameStringType($primaryImageQuery['products_image']);
93 $primaryImageIsVisible = new BoolType($primaryImageQuery['gm_show_image']);
94 $primaryImage = MainFactory::create('ProductImage', $primaryImageFilename);
95 $primaryImage->setVisible($primaryImageIsVisible);
96
97
98 $this->_getPrimaryImageAltText($primaryImage, $productId);
99 }
100 else
101 {
102 $primaryImage = MainFactory::create('EmptyProductImage');
103 }
104
105 $imageContainer->setPrimary($primaryImage);
106
107
108 foreach($additionalImagesQuery as $additionalImageRow)
109 {
110
111 $additionalImageId = new IdType($additionalImageRow['image_id']);
112 $additionalImageFilename = new FilenameStringType($additionalImageRow['image_name']);
113 $additionalImageIsVisible = new BoolType($additionalImageRow['gm_show_image']);
114 $additionalImage = MainFactory::create('ProductImage', $additionalImageFilename);
115 $additionalImage->setVisible($additionalImageIsVisible);
116
117
118 $this->_getAdditionalImagesAltText($additionalImage, $productId, $additionalImageId);
119
120 $imageContainer->addAdditional($additionalImage);
121 }
122
123 return $imageContainer;
124 }
125
126
127 128 129 130 131 132 133
134 public function deleteByProductId(IdType $productId)
135 {
136 $this->db->where('products_id', $productId->asInt())->delete('products_images');
137
138
139 $updateData = array(
140 'products_image' => '',
141 'products_image_w' => 0,
142 'products_image_h' => 0
143 );
144 $this->db->where('products_id', $productId->asInt())->update('products', $updateData);
145
146
147 $this->db->where('products_id', $productId->asInt())->delete('gm_prd_img_alt');
148
149 return $this;
150 }
151
152
153 154 155 156 157 158 159 160
161 protected function _saveAdditionalImagesAltText(ProductImageContainerInterface $imageContainer, IdType $productId)
162 {
163 $languages = $this->_getLanguages();
164
165
166 foreach($imageContainer->getAdditionals()->getArray() as $index => $additionalImage)
167 {
168
169 $additionalImageData = array(
170 'products_id' => $productId->asInt(),
171 'image_nr' => $index + 1,
172 'image_name' => $additionalImage->getFilename(),
173 'gm_show_image' => (bool)$additionalImage->isVisible()
174 );
175
176
177 $this->db->insert('products_images', $additionalImageData);
178 }
179
180
181 $additionalImagesAfterSave = $this->db->select('*')
182 ->from('products_images')
183 ->where('products_id', $productId->asInt())
184 ->get()
185 ->result_array();
186
187
188 foreach($additionalImagesAfterSave as $additionalImageData)
189 {
190 $imageId = (int)$additionalImageData['image_id'];
191 $imageIndex = (int)$additionalImageData['image_nr'] - 1;
192
193
194 foreach($languages as $language)
195 {
196 $languageId = new IdType($language['languages_id']);
197 $languageCode = new LanguageCode(new NonEmptyStringType($language['code']));
198
199
200
201 try
202 {
203
204
205 $altText = $imageContainer->getAdditionals()->getItem($imageIndex)->getAltText($languageCode);
206
207
208 $productImageAltData = array(
209 'image_id' => $imageId,
210 'products_id' => $productId->asInt(),
211 'language_id' => $languageId->asInt(),
212 'gm_alt_text' => $altText
213 );
214
215 $this->db->insert('gm_prd_img_alt', $productImageAltData);
216 }
217 catch(Exception $exception)
218 {
219 continue;
220 }
221 }
222 }
223 }
224
225
226 227 228 229 230 231 232 233
234 protected function _savePrimaryImageAltText(ProductImageContainerInterface $imageContainer, IdType $productId)
235 {
236 $languages = $this->_getLanguages();
237
238
239 foreach($languages as $language)
240 {
241 $languageId = new IdType($language['languages_id']);
242 $languageCode = new LanguageCode(new NonEmptyStringType($language['code']));
243
244
245
246 try
247 {
248
249
250 $altText = $imageContainer->getPrimary()->getAltText($languageCode);
251
252
253 $productsDescriptionData = array(
254 'gm_alt_text' => $altText
255 );
256
257
258 $this->db->where('products_id', $productId->asInt())
259 ->where('language_id', $languageId->asInt())
260 ->update('products_description', $productsDescriptionData);
261 }
262 catch(Exception $exception)
263 {
264 continue;
265 }
266 }
267 }
268
269
270 271 272 273 274 275 276 277
278 protected function _getPrimaryImageAltText(ProductImageInterface $primaryImage, IdType $productId)
279 {
280 $languages = $this->_getLanguages();
281
282 foreach($languages as $language)
283 {
284 $languageId = new IdType($language['languages_id']);
285 $languageCode = new LanguageCode(new NonEmptyStringType($language['code']));
286
287
288 $alternativeTextsQuery = $this->db->select('language_id, gm_alt_text')
289 ->from('products_description')
290 ->where('products_id', $productId->asInt())
291 ->where('language_id', $languageId->asInt())
292 ->get()
293 ->row_array();
294
295 if(count($alternativeTextsQuery) > 0 && $alternativeTextsQuery['gm_alt_text'] !== null)
296 {
297 $text = new StringType((string)$alternativeTextsQuery['gm_alt_text']);
298 }
299 else
300 {
301 $text = new StringType('');
302 }
303 $primaryImage->setAltText($text, $languageCode);
304 }
305 }
306
307
308 309 310 311 312 313 314 315 316
317 protected function _getAdditionalImagesAltText(ProductImageInterface $additionalImage,
318 IdType $productId,
319 IdType $imageId)
320 {
321 $languages = $this->_getLanguages();
322
323 foreach($languages as $language)
324 {
325 $languageId = new IdType($language['languages_id']);
326 $languageCode = new LanguageCode(new NonEmptyStringType($language['code']));
327
328
329 $alternativeTextsQuery = $this->db->select('*')
330 ->from('gm_prd_img_alt')
331 ->where('products_id', $productId->asInt())
332 ->where('language_id', $languageId->asInt())
333 ->where('image_id', $imageId->asInt())
334 ->get()
335 ->row_array();
336
337 if(count($alternativeTextsQuery) > 0 && $alternativeTextsQuery['gm_alt_text'] !== null)
338 {
339 $text = new StringType((string)$alternativeTextsQuery['gm_alt_text']);
340 }
341 else
342 {
343 $text = new StringType('');
344 }
345 $additionalImage->setAltText($text, $languageCode);
346 }
347 }
348
349
350 351 352 353 354 355 356
357 protected function _getLanguages()
358 {
359 return $this->db->get_where('languages')->result_array();
360 }
361
362
363 364 365 366 367 368 369
370 protected function _queryPrimaryImage(IdType $productId)
371 {
372 return $this->db->select('products_image, products_image_w, products_image_h, gm_show_image')
373 ->from('products')
374 ->where('products.products_id', $productId->asInt())
375 ->get()
376 ->row_array();
377 }
378
379
380 381 382 383 384 385 386
387 protected function _queryAdditionalImages(IdType $productId)
388 {
389 return $this->db->select('*')
390 ->from('products_images')
391 ->where('products_id', $productId->asInt())
392 ->get()
393 ->result_array();
394 }
395 }