1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
<?php
/* --------------------------------------------------------------
ProductImageContainerRepository.inc.php 2016-06-30
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class ProductImageContainerRepository
*
* @category System
* @package Product
* @subpackage Repositories
*/
class ProductImageContainerRepository implements ProductImageContainerRepositoryInterface
{
/**
* Database connection.
*
* @var CI_DB_query_builder
*/
protected $db;
/**
* Used for fetching the language data.
*
* @var LanguageProviderInterface
*/
protected $languageProvider;
/**
* ProductImageContainerRepository constructor.
*
* @param CI_DB_query_builder $db Database connection.
* @param LanguageProviderInterface $languageProvider
*/
public function __construct(CI_DB_query_builder $db, LanguageProviderInterface $languageProvider)
{
$this->db = $db;
$this->languageProvider = $languageProvider;
}
/**
* Stores the product image container.
*
* @param IdType $productId Product ID.
* @param ProductImageContainerInterface $imageContainer Product image container.
*
* @throws InvalidArgumentException On invalid arguments.
*
* @return ProductImageContainerRepository Same instance for method chaining.
*/
public function store(IdType $productId, ProductImageContainerInterface $imageContainer)
{
$this->deleteByProductId($productId);
// Prepare primary image data.
$primaryImageData = array(
'products_image' => $imageContainer->getPrimary()->getFilename(),
'products_image_w' => 0,
'products_image_h' => 0,
'gm_show_image' => $imageContainer->getPrimary()->isVisible()
);
$this->db->where('products_id', $productId->asInt())->update('products', $primaryImageData);
$this->_savePrimaryImageAltText($imageContainer, $productId);
$this->_saveAdditionalImagesAltText($imageContainer, $productId);
return $this;
}
/**
* Returns a product image container based on the product ID given.
*
* @param IdType $productId Product ID.
*
* @throws InvalidArgumentException On invalid arguments.
*
* @return ProductImageContainerInterface Product image container.
*/
public function getByProductId(IdType $productId)
{
$primaryImageQuery = $this->_queryPrimaryImage($productId);
$additionalImagesQuery = $this->_queryAdditionalImages($productId);
$imageContainer = MainFactory::create('ProductImageContainer');
// Create primary image.
if(!empty($primaryImageQuery['products_image']))
{
$primaryImageFilename = new FilenameStringType($primaryImageQuery['products_image']);
$primaryImageIsVisible = new BoolType($primaryImageQuery['gm_show_image']);
$primaryImage = MainFactory::create('ProductImage', $primaryImageFilename);
$primaryImage->setVisible($primaryImageIsVisible);
// Set alternative texts on primary image.
$this->_getPrimaryImageAltText($primaryImage, $productId);
}
else
{
$primaryImage = MainFactory::create('EmptyProductImage');
}
$imageContainer->setPrimary($primaryImage);
// Create additional images and add them to image container.
foreach($additionalImagesQuery as $additionalImageRow)
{
// Create additional product image.
$additionalImageId = new IdType($additionalImageRow['image_id']);
$additionalImageFilename = new FilenameStringType($additionalImageRow['image_name']);
$additionalImageIsVisible = new BoolType($additionalImageRow['gm_show_image']);
$additionalImage = MainFactory::create('ProductImage', $additionalImageFilename);
$additionalImage->setVisible($additionalImageIsVisible);
// Set alternative texts on additional image.
$this->_getAdditionalImagesAltText($additionalImage, $productId, $additionalImageId);
$imageContainer->addAdditional($additionalImage);
}
return $imageContainer;
}
/**
* Deletes a product image container based on the product ID given.
*
* @param IdType $productId Product ID.
*
* @return ProductImageContainerRepository Same instance for method chaining.
*/
public function deleteByProductId(IdType $productId)
{
$this->db->where('products_id', $productId->asInt())->delete('products_images');
// Clear primary image fields.
$updateData = array(
'products_image' => '',
'products_image_w' => 0,
'products_image_h' => 0
);
$this->db->where('products_id', $productId->asInt())->update('products', $updateData);
// Delete alternative texts entries for additional images.
$this->db->where('products_id', $productId->asInt())->delete('gm_prd_img_alt');
return $this;
}
/**
* Saves alternative texts for additional images.
*
* @param ProductImageContainerInterface $imageContainer Image container.
* @param IdType $productId Product ID.
*
* @throws InvalidArgumentException on invalid arguments.
*/
protected function _saveAdditionalImagesAltText(ProductImageContainerInterface $imageContainer, IdType $productId)
{
// Prepare additional images data and insert entries to database.
foreach($imageContainer->getAdditionals()->getArray() as $index => $additionalImage)
{
// Additional image data.
$additionalImageData = array(
'products_id' => $productId->asInt(),
'image_nr' => $index + 1,
'image_name' => $additionalImage->getFilename(),
'gm_show_image' => (bool)$additionalImage->isVisible()
);
// Insert additional image into database.
$this->db->insert('products_images', $additionalImageData);
}
// Get additional image entries after save.
$additionalImagesAfterSave = $this->_queryAdditionalImages($productId);
// Iterate over each additional image fetched from DB.
foreach($additionalImagesAfterSave as $additionalImageData)
{
$imageId = (int)$additionalImageData['image_id'];
$imageIndex = (int)$additionalImageData['image_nr'] - 1;
// Iterate over each language
foreach($this->languageProvider->getCodes() as $languageCode)
{
$languageId = $this->languageProvider->getIdByCode($languageCode);
// Try to get alternative text of additional image for current language.
// If no value is set for current language, iterate to next.
try
{
// Get alternative text.
// Throws InvalidArgumentException if no value is present.
$altText = $imageContainer->getAdditionals()->getItem($imageIndex)->getAltText($languageCode);
// Data array for insert into `gm_prd_img_alt` table.
$productImageAltData = array(
'image_id' => $imageId,
'products_id' => $productId->asInt(),
'language_id' => $languageId,
'gm_alt_text' => $altText
);
$this->db->insert('gm_prd_img_alt', $productImageAltData);
}
catch(Exception $exception)
{
continue;
}
}
}
}
/**
* Saves alternative texts for the primary image.
*
* @param ProductImageContainerInterface $imageContainer Image container.
* @param IdType $productId Product ID.
*
* @throws InvalidArgumentException on invalid arguments.
*/
protected function _savePrimaryImageAltText(ProductImageContainerInterface $imageContainer, IdType $productId)
{
// Iterate over each language and save primary image alternative text.
foreach($this->languageProvider->getCodes() as $languageCode)
{
$languageId = $this->languageProvider->getIdByCode($languageCode);
// Try to get alternative text of primary image for current language.
// If no value is set for current language, iterate to next.
try
{
// Get alternative text.
// Throws InvalidArgumentException if no value is present.
$altText = $imageContainer->getPrimary()->getAltText($languageCode);
// Data array for `products_description` table.
$productsDescriptionData = array(
'gm_alt_text' => $altText
);
// Write value to database.
$this->db->where('products_id', $productId->asInt())
->where('language_id', $languageId)
->update('products_description', $productsDescriptionData);
}
catch(Exception $exception)
{
continue;
}
}
}
/**
* Sets alternative texts for the primary image provided.
*
* @param ProductImageInterface $primaryImage Product primary image.
* @param IdType $productId Product ID.
*
* @throws InvalidArgumentException on invalid arguments.
*/
protected function _getPrimaryImageAltText(ProductImageInterface $primaryImage, IdType $productId)
{
foreach($this->languageProvider->getCodes() as $languageCode)
{
$languageId = $this->languageProvider->getIdByCode($languageCode);;
// Get all alternative texts for primary image.
$alternativeTextsQuery = $this->db->select('language_id, gm_alt_text')
->from('products_description')
->where('products_id', $productId->asInt())
->where('language_id', $languageId)
->get()
->row_array();
if(count($alternativeTextsQuery) > 0 && $alternativeTextsQuery['gm_alt_text'] !== null)
{
$text = new StringType((string)$alternativeTextsQuery['gm_alt_text']);
}
else
{
$text = new StringType('');
}
$primaryImage->setAltText($text, $languageCode);
}
}
/**
* Sets the alternative texts for an additional image.
*
* @param ProductImageInterface $additionalImage Additional Image.
* @param IdType $productId Product ID.
* @param IdType $imageId Image ID.
*
* @throws InvalidArgumentException on invalid arguments.
*/
protected function _getAdditionalImagesAltText(ProductImageInterface $additionalImage,
IdType $productId,
IdType $imageId)
{
foreach($this->languageProvider->getCodes() as $languageCode)
{
$languageId = $this->languageProvider->getIdByCode($languageCode);
// Get all alternative texts for primary image.
$alternativeTextsQuery = $this->db->select('*')
->from('gm_prd_img_alt')
->where('products_id', $productId->asInt())
->where('language_id', $languageId)
->where('image_id', $imageId->asInt())
->get()
->row_array();
if(count($alternativeTextsQuery) > 0 && $alternativeTextsQuery['gm_alt_text'] !== null)
{
$text = new StringType((string)$alternativeTextsQuery['gm_alt_text']);
}
else
{
$text = new StringType('');
}
$additionalImage->setAltText($text, $languageCode);
}
}
/**
* Perform database query to get primary image.
*
* @param IdType $productId Product ID.
*
* @return array Result.
*/
protected function _queryPrimaryImage(IdType $productId)
{
return $this->db->select('products_image, products_image_w, products_image_h, gm_show_image')
->from('products')
->where('products.products_id', $productId->asInt())
->get()
->row_array();
}
/**
* Perform database query to get additional images.
*
* @param IdType $productId Product ID.
*
* @return array Result.
*/
protected function _queryAdditionalImages(IdType $productId)
{
return $this->db->select('*')
->from('products_images')
->where('products_id', $productId->asInt())
->order_by('image_nr', 'ASC')
->get()
->result_array();
}
}