public function save(waRequestFile $file, $data)
 {
     // check image
     if (!($image = $file->waImage())) {
         throw new waException(_w('Incorrect image'));
     }
     $plugin = wa()->getPlugin('publicgallery');
     $min_size = $plugin->getSettings('min_size');
     if ($min_size && ($image->height < $min_size || $image->width < $min_size)) {
         throw new waException(sprintf(_w("Image is too small. Minimum image size is %d px"), $min_size));
     }
     $max_size = $plugin->getSettings('max_size');
     if ($max_size && ($image->height > $max_size || $image->width > $max_size)) {
         throw new waException(sprintf(_w("Image is too big. Maximum image size is %d px"), $max_size));
     }
     $id = $this->model->add($file, $data);
     if (!$id) {
         throw new waException(_w("Save error"));
     }
     $tag = $plugin->getSettings('assign_tag');
     if ($tag) {
         $photos_tag_model = new photosPhotoTagsModel();
         $photos_tag_model->set($id, $tag);
     }
     return array('name' => $file->name, 'type' => $file->type, 'size' => $file->size);
 }
 protected function save(waRequestFile $file)
 {
     $product_id = waRequest::post('product_id', null, waRequest::TYPE_INT);
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($product_id)) {
         throw new waException(_w("Access denied"));
     }
     // check image
     if (!($image = $file->waImage())) {
         throw new waException('Incorrect image');
     }
     $image_changed = false;
     /**
      * Extend upload proccess
      * Make extra workup
      * @event image_upload
      */
     $event = wa()->event('image_upload', $image);
     if ($event) {
         foreach ($event as $plugin_id => $result) {
             if ($result) {
                 $image_changed = true;
             }
         }
     }
     if (!$this->model) {
         $this->model = new shopProductImagesModel();
     }
     $data = array('product_id' => $product_id, 'upload_datetime' => date('Y-m-d H:i:s'), 'width' => $image->width, 'height' => $image->height, 'size' => $file->size, 'original_filename' => basename($file->name), 'ext' => $file->extension);
     $image_id = $data['id'] = $this->model->add($data);
     if (!$image_id) {
         throw new waException("Database error");
     }
     /**
      * @var shopConfig $config
      */
     $config = $this->getConfig();
     $image_path = shopImage::getPath($data);
     if (file_exists($image_path) && !is_writable($image_path) || !file_exists($image_path) && !waFiles::create($image_path)) {
         $this->model->deleteById($image_id);
         throw new waException(sprintf("The insufficient file write permissions for the %s folder.", substr($image_path, strlen($config->getRootPath()))));
     }
     if ($image_changed) {
         $image->save($image_path);
         // save original
         $original_file = shopImage::getOriginalPath($data);
         if ($config->getOption('image_save_original') && $original_file) {
             $file->moveTo($original_file);
         }
     } else {
         $file->moveTo($image_path);
     }
     unset($image);
     // free variable
     shopImage::generateThumbs($data, $config->getImageSizes());
     return array('id' => $image_id, 'name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'url_thumb' => shopImage::getUrl($data, $config->getImageSize('thumb')), 'url_crop' => shopImage::getUrl($data, $config->getImageSize('crop')), 'url_crop_small' => shopImage::getUrl($data, $config->getImageSize('crop_small')), 'description' => '');
 }
 protected function save(waRequestFile $file)
 {
     // check image
     if (!($image = $file->waImage())) {
         throw new waException(_w('Incorrect image'));
     }
     $exif_data = photosExif::getInfo($file->tmp_name);
     $image_changed = false;
     if (!empty($exif_data['Orientation'])) {
         $image_changed = $this->correctOrientation($exif_data['Orientation'], $image);
     }
     /**
      * Extend upload proccess
      * Make extra workup
      * @event photo_upload
      */
     $event = wa()->event('photo_upload', $image);
     if ($event && !$image_changed) {
         foreach ($event as $plugin_id => $result) {
             if ($result) {
                 $image_changed = true;
                 break;
             }
         }
     }
     $data = array('name' => preg_replace('/\\.[^\\.]+$/', '', basename($file->name)), 'ext' => $file->extension, 'size' => $file->size, 'type' => $image->type, 'width' => $image->width, 'height' => $image->height, 'contact_id' => $this->getUser()->getId(), 'status' => $this->status, 'upload_datetime' => date('Y-m-d H:i:s'));
     if ($this->status <= 0) {
         $data['hash'] = md5(uniqid(time(), true));
     }
     $photo_id = $data['id'] = $this->model->insert($data);
     if (!$photo_id) {
         throw new waException(_w('Database error'));
     }
     // update url
     $url = $this->generateUrl($data['name'], $photo_id);
     $this->model->updateById($photo_id, array('url' => $url));
     // check rigths to upload folder
     $photo_path = photosPhoto::getPhotoPath($data);
     if (file_exists($photo_path) && !is_writable($photo_path) || !file_exists($photo_path) && !waFiles::create($photo_path)) {
         $this->model->deleteById($photo_id);
         throw new waException(sprintf(_w("The insufficient file write permissions for the %s folder."), substr($photo_path, strlen($this->getConfig()->getRootPath()))));
     }
     if ($image_changed) {
         $image->save($photo_path);
         // save original
         if ($this->getConfig()->getOption('save_original')) {
             $original_file = photosPhoto::getOriginalPhotoPath($photo_path);
             $file->moveTo($original_file);
         }
     } else {
         $file->moveTo($photo_path);
     }
     unset($image);
     // free variable
     // add to album
     if ($photo_id && $this->album_id) {
         $album_photos_model = new photosAlbumPhotosModel();
         // update note if album is empty and note is yet null
         $r = $album_photos_model->getByField('album_id', $this->album_id);
         if (!$r) {
             $album_model = new photosAlbumModel();
             $sql = "UPDATE " . $album_model->getTableName() . " SET note = IFNULL(note, s:note) WHERE id = i:album_id";
             $time = !empty($exif_data['DateTimeOriginal']) ? strtotime($exif_data['DateTimeOriginal']) : time();
             $album_model->query($sql, array('note' => mb_strtolower(_ws(date('F', $time))) . ' ' . _ws(date('Y', $time)), 'album_id' => $this->album_id));
         }
         // add to album iteself
         $sort = (int) $album_photos_model->query("SELECT sort + 1 AS sort FROM " . $album_photos_model->getTableName() . " WHERE album_id = i:album_id ORDER BY sort DESC LIMIT 1", array('album_id' => $this->album_id))->fetchField('sort');
         $album_photos_model->insert(array('photo_id' => $photo_id, 'album_id' => $this->album_id, 'sort' => $sort));
     }
     // save rights for groups
     if ($this->groups) {
         $rights_model = new photosPhotoRightsModel();
         $rights_model->multiInsert(array('photo_id' => $photo_id, 'group_id' => $this->groups));
     }
     // save exif data
     if (!empty($exif_data)) {
         $exif_model = new photosPhotoExifModel();
         $exif_model->save($photo_id, $exif_data);
     }
     $sizes = $this->getConfig()->getSizes();
     photosPhoto::generateThumbs($data, $sizes);
     return array('name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'thumbnail_url' => photosPhoto::getPhotoUrl($data, photosPhoto::getThumbPhotoSize()), 'url' => '#/photo/' . $photo_id . '/');
 }