public function ajaxProcessUpdateGallaryImagePosition()
 {
     $res = false;
     if ($json = Tools::getValue('json')) {
         $res = true;
         $json = Tools::stripslashes(pSQL($json));
         $images = Tools::jsonDecode($json, true);
         foreach ($images as $id => $position) {
             $img = new SmartBlogGallaryImage((int) $id);
             $img->position = (int) $position;
             $res &= $img->update();
         }
     }
     if ($res) {
         $this->jsonConfirmation($this->_conf[25]);
     } else {
         $this->jsonError(Tools::displayError('An error occurred while attempting to move this picture.'));
     }
 }
 public function ajaxProcessaddGallaryImage()
 {
     self::$currentIndex = 'index.php?tab=AdminSmartBlog&token=' . Tools::getAdminTokenLite('AdminSmartBlog');
     $smart_blog_post = new SmartBlogPost((int) Tools::getValue('id_smart_blog_post'));
     $legends = Tools::getValue('legend');
     if (!is_array($legends)) {
         $legends = (array) $legends;
     }
     if (!Validate::isLoadedObject($smart_blog_post)) {
         $files = array();
         $files[0]['error'] = Tools::displayError('Cannot add image because product creation failed.');
     }
     $image_uploader = new HelperImageUploader('file');
     $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size);
     $files = $image_uploader->process();
     foreach ($files as &$file) {
         $image = new SmartBlogGallaryImage();
         $image->id_smart_blog_post = (int) $smart_blog_post->id;
         $image->position = SmartBlogGallaryImage::getHighestPosition($smart_blog_post->id) + 1;
         foreach ($legends as $key => $legend) {
             if (!empty($legend)) {
                 $image->legend[(int) $key] = $legend;
             }
         }
         if (($validate = $image->validateFieldsLang(false, true)) !== true) {
             $file['error'] = Tools::displayError($validate);
         }
         if (isset($file['error']) && (!is_numeric($file['error']) || $file['error'] != 0)) {
             continue;
         }
         if (!$image->add()) {
             $file['error'] = Tools::displayError('Error while creating additional image');
         } else {
             if (!($new_path = $image->getPathForCreation())) {
                 $file['error'] = Tools::displayError('An error occurred during new folder creation');
                 continue;
             }
             $error = 0;
             if (!ImageManager::resize($file['save_path'], $new_path . '.' . $image->image_format, null, null, 'jpg', false, $error)) {
                 switch ($error) {
                     case ImageManager::ERROR_FILE_NOT_EXIST:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file does not exist anymore.');
                         break;
                     case ImageManager::ERROR_FILE_WIDTH:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file width is 0px.');
                         break;
                     case ImageManager::ERROR_MEMORY_LIMIT:
                         $file['error'] = Tools::displayError('An error occurred while copying image, check your memory limit.');
                         break;
                     default:
                         $file['error'] = Tools::displayError('An error occurred while copying image.');
                         break;
                 }
                 continue;
             } else {
                 $imagesTypes = BlogImageType::GetImageAllType('post');
                 foreach ($imagesTypes as $imageType) {
                     if (!ImageManager::resize($file['save_path'], $new_path . '-' . Tools::stripslashes($imageType['type_name']) . '.' . $image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) {
                         $file['error'] = Tools::displayError('An error occurred while copying image:') . ' ' . Tools::stripslashes($imageType['name']);
                         continue;
                     }
                 }
             }
             unlink($file['save_path']);
             //Necesary to prevent hacking
             unset($file['save_path']);
             Hook::exec('actionWatermark', array('id_smart_blog_post_mage' => $image->id, 'id_smart_blog_post' => $smart_blog_post->id));
             if (!$image->update()) {
                 $file['error'] = Tools::displayError('Error while updating status');
                 continue;
             }
             $file['status'] = 'ok';
             $file['id'] = $image->id;
             $file['position'] = $image->position;
             $file['cover'] = $image->cover;
             $file['legend'] = $image->legend;
             $file['path'] = $image->getExistingImgPath();
             $file['shops'] = array("{$this->context->shop->id}" => true);
             @unlink(_PS_TMP_IMG_DIR_ . 'smart_blog_post_' . (int) $smart_blog_post->id . '.jpg');
             @unlink(_PS_TMP_IMG_DIR_ . 'smart_blog_post__mini_' . (int) $smart_blog_post->id . '_' . $this->context->shop->id . '.jpg');
         }
     }
     die(Tools::jsonEncode(array($image_uploader->getName() => $files)));
 }
    /**
     * Copy images from a product to another
     *
     * @param integer $id_smart_blog_post_old Source product ID
     * @param boolean $id_smart_blog_post_new Destination product ID
     */
    public static function duplicateProductImages($id_smart_blog_post_old, $id_smart_blog_post_new, $combination_images)
    {
        $images_types = BlogImageType::getImagesTypes('post');
        $result = Db::getInstance()->executeS('
		SELECT `id_image`
		FROM `' . _DB_PREFIX_ . 'smart_blog_gallary_images`
		WHERE `id_smart_blog_post` = ' . (int) $id_smart_blog_post_old);
        foreach ($result as $row) {
            $image_old = new Image($row['id_image']);
            $image_new = clone $image_old;
            unset($image_new->id);
            $image_new->id_smart_blog_post = (int) $id_smart_blog_post_new;
            // A new id is generated for the cloned image when calling add()
            if ($image_new->add()) {
                $new_path = $image_new->getPathForCreation();
                foreach ($images_types as $image_type) {
                    if (file_exists(_MODULE_SMARTBLOG_GALLARY_DIR_ . $image_old->getExistingImgPath() . '-' . $image_type['name'] . '.jpg')) {
                        if (!Configuration::get('PS_LEGACY_IMAGES')) {
                            $image_new->createImgFolder();
                        }
                        copy(_MODULE_SMARTBLOG_GALLARY_DIR_ . $image_old->getExistingImgPath() . '-' . $image_type['name'] . '.jpg', $new_path . '-' . $image_type['name'] . '.jpg');
                    }
                }
                if (file_exists(_MODULE_SMARTBLOG_GALLARY_DIR_ . $image_old->getExistingImgPath() . '.jpg')) {
                    copy(_MODULE_SMARTBLOG_GALLARY_DIR_ . $image_old->getExistingImgPath() . '.jpg', $new_path . '.jpg');
                }
                SmartBlogGallaryImage::replaceAttributeImageAssociationId($combination_images, (int) $image_old->id, (int) $image_new->id);
                // Duplicate shop associations for images
                $image_new->duplicateShops($id_smart_blog_post_old);
            } else {
                return false;
            }
        }
        return SmartBlogGallaryImage::duplicateAttributeImageAssociations($combination_images);
    }