コード例 #1
0
ファイル: shopProduct.class.php プロジェクト: Lazary/webasyst
 /**
  * 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;
 }
コード例 #2
0
ファイル: shopProduct.model.php プロジェクト: Lazary/webasyst
 /**
  * Update type of product and update relation information
  * @param int|array $id product ID
  * @param int $type_id new type ID
  * @return boolean
  */
 public function updateType($id, $type_id)
 {
     if (!is_array($id)) {
         $item = $this->getById($id);
         if (!$item) {
             return false;
         }
         if ($item['type_id'] == $type_id) {
             return true;
         }
         if (!$this->updateById($id, array('type_id' => $type_id))) {
             return false;
         }
         $type_model = new shopTypeModel();
         $type_model->incCounters(array($item['type_id'] => '-1', $type_id => '+1'));
     } else {
         if (!$this->updateById($id, array('type_id' => $type_id))) {
             return false;
         }
         $type_model = new shopTypeModel();
         $type_model->recount();
     }
     return true;
 }