/**
  * @param ShopProduct $product
  * @param CUploadedFile $image
  */
 public function __construct(ShopProduct $product, CUploadedFile $image)
 {
     $name = ShopUploadedImage::createName($product, $image);
     $fullPath = ShopUploadedImage::getSavePath() . '/' . $name;
     $image->saveAs($fullPath);
     @chmod($fullPath, 0666);
     // Check if product has main image
     $is_main = (int) ShopProductImage::model()->countByAttributes(array('product_id' => $product->id, 'is_main' => 1));
     $imageModel = new ShopProductImage();
     $imageModel->product_id = $product->id;
     $imageModel->name = $name;
     $imageModel->is_main = $is_main == 0 ? true : false;
     $imageModel->uploaded_by = Yii::app()->user->id;
     //$imageModel->date_uploaded = date('Y-m-d H:i:s');
     $imageModel->save(false, false);
     $this->resize($fullPath);
     $this->watermark($fullPath);
 }
 /**
  * Creates copy of product images
  *
  * @param ShopProduct $original
  * @param ShopProduct $copy
  */
 protected function copyImages(ShopProduct $original, ShopProduct $copy)
 {
     $images = $original->images;
     if (!empty($images)) {
         foreach ($images as $image) {
             $image_copy = new ShopProductImage();
             $image_copy->product_id = $copy->id;
             $image_copy->name = $copy->id . '_' . $image->name;
             $image_copy->is_main = $image->is_main;
             $image_copy->uploaded_by = $image->uploaded_by;
             $image_copy->title = $image->title;
             $image_copy->date_create = date('Y-m-d H:i:s');
             if ($image_copy->validate()) {
                 if ($image_copy->save(false, false)) {
                     copy($image->filePath, $image_copy->filePath);
                 } else {
                     die(__FUNCTION__ . ': Error save');
                 }
             } else {
                 die(__FUNCTION__ . ': Error validate');
             }
         }
     }
 }