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()
 {
     $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;
 }
 private function recalcProductRating($product_id, $rate, $inc = true)
 {
     if ($rate <= 0) {
         return;
     }
     $product_model = new shopProductModel();
     $product = $product_model->getById($product_id);
     if ($inc) {
         $update = array('rating' => ($product['rating'] * $product['rating_count'] + $rate) / ($product['rating_count'] + 1), 'rating_count' => $product['rating_count'] + 1);
     } else {
         $update = array('rating' => ($product['rating'] * $product['rating_count'] - $rate) / ($product['rating_count'] - 1), 'rating_count' => $product['rating_count'] - 1);
     }
     $product_model->updateById($product_id, $update);
 }
Example #4
0
 /**
  * Saves product data to database.
  *
  * @param array $data
  * @return bool Whether saved successfully
  */
 public function save($data = array(), $validate = true, &$errors = array())
 {
     $result = false;
     $id = $this->getId();
     $search = new shopIndexSearch();
     foreach ($data as $name => $value) {
         // name have to be not empty
         if ($name == 'name' && !$value) {
             $value = _w('New product');
         }
         // url have to be not empty
         if ($name == 'url' && !$value && $id) {
             $value = $id;
         }
         $this->__set($name, $value);
     }
     if ($this->is_dirty) {
         $product = array();
         $id_changed = !empty($this->is_dirty['id']);
         foreach ($this->is_dirty as $field => $v) {
             if ($this->model->fieldExists($field)) {
                 $product[$field] = $this->data[$field];
                 unset($this->is_dirty[$field]);
             }
         }
         if ($id && !$id_changed) {
             if (!isset($product['edit_datetime'])) {
                 $product['edit_datetime'] = date('Y-m-d H:i:s');
             }
             if (isset($product['type_id'])) {
                 $this->model->updateType($id, $product['type_id']);
                 unset($product['type_id']);
             }
             if ($this->model->updateById($id, $product)) {
                 $this->saveData($errors);
                 $search->onUpdate($id);
                 $this->is_dirty = array();
                 $result = true;
             }
         } else {
             if (!isset($product['contact_id'])) {
                 $product['contact_id'] = wa()->getUser()->getId();
             }
             if (!isset($product['create_datetime'])) {
                 $product['create_datetime'] = date('Y-m-d H:i:s');
             }
             if (!isset($product['currency'])) {
                 $product['currency'] = wa('shop')->getConfig()->getCurrency();
             }
             if ($id = $this->model->insert($product)) {
                 $this->data['id'] = $id;
                 // update empty url by ID
                 if (empty($product['url'])) {
                     $this->data['url'] = $id;
                     $this->model->updateById($id, array('url' => $this->data['url']));
                 }
                 $this->saveData();
                 $search->onAdd($id);
                 $this->is_dirty = array();
                 if (!empty($this->data['type_id'])) {
                     $type_model = new shopTypeModel();
                     // increment on +1
                     $type_model->incCounters(array($this->data['type_id'] => 1));
                 }
                 $result = true;
             }
         }
     } else {
         $result = true;
     }
     $params = array('data' => $this->getData(), 'instance' => &$this);
     /**
      * Plugin hook for handling product entry saving event
      * @event product_save
      *
      * @param array [string]mixed $params
      * @param array [string][string]mixed $params['data'] raw product data fields (see shop_product table description and related storages)
      * @param array [string][string]int $data['data']['id'] product ID
      * @param array [string]shopProduct $params['instance'] current shopProduct entry instance (avoid recursion)
      * @return void
      */
     wa()->event('product_save', $params);
     return $result;
 }
 public function setData(shopProduct $product, $data)
 {
     $primary_currency = wa()->getConfig()->getCurrency();
     $sort = 0;
     $default_sku_id = null;
     $result = array();
     foreach ($data as $sku_id => $sku) {
         $sku['sort'] = ++$sort;
         if (empty($sku['available'])) {
             $sku['available'] = 0;
         }
         if (isset($sku['price'])) {
             if ($product->currency == $primary_currency) {
                 $sku['primary_price'] = $sku['price'];
             } else {
                 $sku['primary_price'] = $this->convertPrice($sku['price'], $product->currency);
             }
         }
         $sku['product_id'] = $product->id;
         $sku = $this->updateSku($sku_id > 0 ? $sku_id : 0, $sku, false, $product);
         $result[$sku['id']] = $sku;
         if (!empty($sku['features'])) {
             foreach ($sku['features'] as $code => $value) {
                 if (!isset($features)) {
                     $features = $product->features;
                 }
                 if (!isset($features[$code])) {
                     $features[$code] = array();
                 }
                 if (is_array($features[$code])) {
                     if (is_array($value)) {
                         if (isset($value['id'])) {
                             if (!isset($features[$code][$value['id']])) {
                                 $features[$code][$value['id']] = $value['value'];
                             }
                         }
                     } else {
                         $features[$code][] = $value;
                     }
                 }
             }
         }
         if ($product->sku_id == $sku_id) {
             $default_sku_id = $sku['id'];
         }
     }
     $model = new shopProductModel();
     if ($default_sku_id === null && $result) {
         $default_sku_id = current(array_keys($result));
     }
     $model->updateById($product->id, array('sku_id' => $default_sku_id));
     $model->correct($product->id);
     $product_data = $model->getById($product->id);
     $product->min_price = $product_data['min_price'];
     $product->max_price = $product_data['max_price'];
     $product->price = $product_data['price'];
     $product->compare_price = $product_data['compare_price'];
     $product->count = $product_data['count'];
     $product->sku_count = count($data);
     $product->sku_id = $default_sku_id;
     if (isset($features)) {
         $product->features = $features;
     }
     return $result;
 }
 public function setData(shopProduct $product, $data)
 {
     $data = array_unique(array_map('intval', $data));
     $key = array_search(0, $data, true);
     if ($key !== false) {
         unset($data[$key]);
     }
     $category_ids = array_keys($this->getByField('product_id', $product->id, 'category_id'));
     if ($obsolete = array_diff($category_ids, $data)) {
         $this->deleteByField(array('product_id' => $product->id, 'category_id' => $obsolete));
         // correct counter
         $category_model = new shopCategoryModel();
         $category_model->recount($obsolete);
     }
     if ($added = array_diff($data, $category_ids)) {
         $this->add($product->id, $added);
     }
     //$product_model = new shopProductModel();
     //$product_model->correctMainCategory($product->id);
     if ($data) {
         $product->category_id = reset($data);
     } else {
         $product->category_id = null;
     }
     $product_model = new shopProductModel();
     $product_model->updateById($product->id, array('category_id' => $product->category_id));
     return $data;
 }
 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);
     }
 }
 /**
  * Delete one image
  * @param int $id ID of image
  */
 public function delete($id)
 {
     $id = (int) $id;
     if (!$id) {
         return false;
     }
     $image = $this->getById($id);
     if (!$image) {
         return false;
     }
     $product_id = $image['product_id'];
     // first of all try delete files from disk
     waFiles::delete(shopImage::getThumbsPath($image));
     waFiles::delete(shopImage::getPath($image));
     waFiles::delete(shopImage::getOriginalPath($image));
     if (!$this->deleteById($id)) {
         return false;
     }
     // first image for this product is main image for this product
     $main_image = $this->query("SELECT id AS image_id, ext FROM {$this->table} WHERE product_id = {$product_id} ORDER BY sort LIMIT 1")->fetchAssoc();
     if (!$main_image) {
         $main_image = array('image_id' => null, 'ext' => null);
     }
     $product_model = new shopProductModel();
     $product_model->updateById($product_id, $main_image);
     // make NULL image_id for that skus of this product which have image_id equals this image ID
     $this->exec("\n            UPDATE `shop_product_skus` SET image_id = NULL\n            WHERE product_id = {$product_id} AND image_id = {$id}\n        ");
     return true;
 }