public function execute()
 {
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     $data = $this->getData($id);
     if (!isset($data['product_id']) && $id) {
         $data['product_id'] = $this->pages_model->select('product_id')->where('id=' . (int) $id)->fetchField('product_id');
     }
     $product = $this->getProduct($data['product_id']);
     // check rights
     if (!$this->product_model->checkRights($product)) {
         throw new waException(_w("Access denied"));
     }
     if ($id) {
         if (!$this->pages_model->update($id, $data)) {
             $this->errors[] = _w('Error saving product page');
             return;
         }
     } else {
         $id = $this->pages_model->add($data);
         if (!$id) {
             $this->errors[] = _w('Error saving product page');
             return;
         }
     }
     $page = $this->pages_model->getById($id);
     $page['name'] = htmlspecialchars($data['name']);
     $page['frontend_url'] = rtrim(wa()->getRouteUrl('/frontend/productPage', array('product_url' => $product['url'], 'page_url' => ''), true), '/');
     $page['preview_hash'] = $this->pages_model->getPreviewHash();
     $page['url_escaped'] = htmlspecialchars($data['url']);
     $this->response = $page;
 }
Example #2
0
 /**
  * Verifies current user's access rights to product by its type id.
  *
  * @throws waException
  * @return boolean
  */
 public function checkRights()
 {
     if (isset($this->data['type_id'])) {
         return $this->model->checkRights($this->data);
     } else {
         return $this->model->checkRights($this->getId());
     }
 }
 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' => '');
 }
 public function execute()
 {
     $product_model = new shopProductModel();
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     $product = $product_model->getById($id);
     if (!$product) {
         $this->errors[] = _w("Unknown product");
         return;
     }
     if (!$product_model->checkRights($product)) {
         throw new waException(_w("Access denied"));
     }
     $product_model->updateById($id, array('badge' => null));
 }
 public function execute()
 {
     $id = waRequest::post('id', 0, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException(_w("Unknown image"));
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException(_w("Unknown image"));
     }
     // check rights
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($image['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     $product_images_model->updateById($id, $this->getData());
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException(_w("Unknown page"));
     }
     $product_pages_model = new shopProductPagesModel();
     $page = $product_pages_model->getById($id);
     if (!$page) {
         throw new waException(_w("Unknown page"));
     }
     // check rights
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($page['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     $product_pages_model->delete($id);
 }
 public function execute()
 {
     $product_model = new shopProductModel();
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     $product = $product_model->getById($id);
     if (!$product) {
         throw new waException(_w("Unknown product"));
     }
     if (!$product_model->checkRights($product)) {
         throw new waException(_w("Access denied"));
     }
     $code = waRequest::post('code', null, waRequest::TYPE_STRING_TRIM);
     if (!$code) {
         throw new waException(_w("Empty code"));
     }
     $product_model->updateById($id, array('badge' => $code));
     $badges = shopProductModel::badges();
     $this->response = isset($badges[$code]) ? $badges[$code]['code'] : $code;
 }
 public function execute()
 {
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException(_w("Unknown image"));
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException(_w("Unknown image"));
     }
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($image['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     if (!$product_images_model->delete($id)) {
         throw new waException(_w("Coudn't delete image"));
     }
     $this->response['id'] = $id;
 }
 public function execute()
 {
     $this->product_id = waRequest::get('product_id', null, waRequest::TYPE_INT);
     $this->service_id = waRequest::get('service_id', null, waRequest::TYPE_INT);
     if (!$this->product_id) {
         $this->errors[] = _w("Unknown product");
         return;
     }
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($this->product_id)) {
         throw new waException(_w("Access denied"));
     }
     // check rights
     if (!$this->service_id) {
         $this->errors = _w("Unkown service");
         return;
     }
     $product_services_model = new shopProductServicesModel();
     $product_services_model->save($this->product_id, $this->service_id, $this->getData());
     $this->response = array('status' => $product_services_model->getProductStatus($this->product_id, $this->service_id), 'count' => $product_services_model->countServices($this->product_id));
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException(_w("Unknown page"));
     }
     $before_id = waRequest::post('before_id', null, waRequest::TYPE_INT);
     if ($id == $before_id) {
         $this->errors[] = _w("Page couldn't be inserted before itself");
     }
     $product_page_model = new shopProductPagesModel();
     $page = $product_page_model->getById($id);
     if (!$page) {
         throw new waException(_w("Unknown page"));
     }
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($page['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     if (!$product_page_model->move($id, $before_id)) {
         $this->errors[] = _w("Error when move");
     }
 }
 public function execute()
 {
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Unknown image");
     }
     $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM);
     if (!isset($this->angles[$direction])) {
         throw new waException("Can't rotate image");
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException("Unknown image");
     }
     // check rights
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($image['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     $image_path = shopImage::getPath($image);
     $paths = array();
     try {
         $result_image_path = preg_replace('/(\\.[^\\.]+)$/', '.result$1', $image_path);
         $backup_image_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $image_path);
         $paths[] = $result_image_path;
         if ($this->rotate($image_path, $result_image_path, $this->angles[$direction])) {
             $count = 0;
             while (!file_exists($result_image_path) && ++$count < 5) {
                 sleep(1);
             }
             if (!file_exists($result_image_path)) {
                 throw new waException(_w("Error while rotate. I/O error"));
             }
             if (!waFiles::move($image_path, $backup_image_path)) {
                 throw new waException(_w("Error while rotate. Operation canceled"));
             }
             $paths[] = $backup_image_path;
             if (!waFiles::move($result_image_path, $image_path)) {
                 if (!waFiles::move($backup_image_path, $image_path)) {
                     throw new waException(_w("Error while rotate. Original file corupted but backuped"));
                 }
                 throw new waException(_w("Error while rotate. Operation canceled"));
             }
             $datetime = date('Y-m-d H:i:s');
             $data = array('edit_datetime' => $datetime, 'width' => $image['height'], 'height' => $image['width']);
             $product_images_model->updateById($id, $data);
             $image = array_merge($image, $data);
             $thumb_dir = shopImage::getThumbsPath($image);
             $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
             $paths[] = $back_thumb_dir;
             waFiles::delete($back_thumb_dir);
             if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
                 throw new waException(_w("Error while rebuild thumbnails"));
             }
             $config = $this->getConfig();
             try {
                 shopImage::generateThumbs($image, $config->getImageSizes());
             } catch (Exception $e) {
                 waLog::log($e->getMessage());
             }
             $this->response = $image;
             $edit_datetime_ts = strtotime($image['edit_datetime']);
             $this->response['url_big'] = shopImage::getUrl($image, $config->getImageSize('big')) . '?' . $edit_datetime_ts;
             $this->response['url_crop'] = shopImage::getUrl($image, $config->getImageSize('crop')) . '?' . $edit_datetime_ts;
         }
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
     } catch (Exception $e) {
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
         throw $e;
     }
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't restore image");
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException("Can't restore image");
     }
     // check rights
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($image['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     $original_image_path = shopImage::getOriginalPath($image);
     if (!wa('shop')->getConfig()->getOption('image_save_original') || !file_exists($original_image_path)) {
         throw new waException("Can't restore image. Original image doesn't exist");
     }
     $image_path = shopImage::getPath($image);
     $paths = array();
     try {
         $backup_image_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $image_path);
         if (!waFiles::move($image_path, $backup_image_path)) {
             throw new waException("Error while restore. Operation canceled");
         }
         $paths[] = $backup_image_path;
         if (!waFiles::move($original_image_path, $image_path)) {
             if (!waFiles::move($backup_image_path, $image_path)) {
                 throw new waException("Error while restore. Current file corupted but backuped");
             }
             throw new waException("Error while restore. Operation canceled");
         }
         $data = $this->getData($image_path);
         $product_images_model->updateById($id, $data);
         $image = array_merge($image, $data);
         $thumb_dir = shopImage::getThumbsPath($image);
         $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
         $paths[] = $back_thumb_dir;
         waFiles::delete($back_thumb_dir);
         // old backups
         if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
             throw new waException(_w("Error while rebuild thumbnails"));
         }
         /**
          * @var shopConfig $config
          */
         $config = $this->getConfig();
         try {
             shopImage::generateThumbs($image, $config->getImageSizes());
         } catch (Exception $e) {
             waLog::log($e->getMessage());
         }
         $this->response = $image;
         $edit_datetime_ts = strtotime($image['edit_datetime']);
         $this->response['url_big'] = shopImage::getUrl($image, $config->getImageSize('big')) . '?' . $edit_datetime_ts;
         $this->response['url_crop'] = shopImage::getUrl($image, $config->getImageSize('crop')) . '?' . $edit_datetime_ts;
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
     } catch (Exception $e) {
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
         throw $e;
     }
 }
 public function update($data)
 {
     $id = waRequest::get('id', 0, waRequest::TYPE_INT);
     if (!$id) {
         return;
     }
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($id)) {
         throw new waException(_w("Access denied"));
     }
     // available fields
     $fields = array('name');
     $update = array();
     foreach ($data as $name => $value) {
         if (in_array($name, $fields) !== false) {
             $update[$name] = $value;
         }
     }
     if ($update) {
         $product_model->updateById($id, $update);
         $this->logAction('product_edit', $id);
     }
 }