protected function save($type) { $value = waRequest::post($type); $product = new shopProduct($this->product_id); $product->save(array($type => $value)); if ($value != 2) { $related_model = new shopProductRelatedModel(); $related_model->deleteByField(array('product_id' => $this->product_id, 'type' => $type)); } }
public function execute() { $id = $this->get('id', true); $product = $this->getProduct($id); $data = waRequest::post(); if (isset($data['type_id'])) { $this->checkRights($data['type_id']); } $this->checkSku($data); $p = new shopProduct($product); if ($p->save($data, true, $errors)) { $_GET['id'] = $p->getId(); $method = new shopProductGetInfoMethod(); $this->response = $method->getResponse(true); } else { throw new waAPIException('server_error', implode(",\n", $errors), 500); } }
public function execute() { $data = waRequest::post(); $exclude = array('id', 'sku_type'); foreach ($exclude as $k) { if (isset($data[$k])) { unset($data[$k]); } } $this->post("name", true); $this->post("skus", true); $this->checkSku($data); // check access rights $this->checkRights($this->post("type_id", true)); $p = new shopProduct(); if ($p->save($data, true, $errors)) { $_GET['id'] = $p->getId(); $method = new shopProductGetInfoMethod(); $this->response = $method->getResponse(true); } else { throw new waAPIException('server_error', implode(",\n", $errors), 500); } }
/** * @param array $options * @return shopProduct * @throws waException */ public function duplicate($options = array()) { if (!$this->checkRights()) { throw new waRightsException('Access denied'); } $data = $this->data; $skip = array('id', 'create_datetime', 'id_1c', 'rating', 'rating_count', 'total_sales', 'image_id', 'contact_id', 'ext', 'count', 'sku_count'); foreach ($skip as $field) { if (isset($data[$field])) { unset($data[$field]); } } $duplicate = new shopProduct(); $this->getStorage(null); $sku_files = array(); $sku_images = array(); $ignore_select = true; foreach (self::$data_storages as $key => $i) { $raw = $this->getStorage($key)->getData($this); switch ($key) { case 'features_selectable': $storage_data = array(); if (!$ignore_select) { if ($this->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) { if (!is_array($raw)) { $raw = array(); } foreach ($raw as $id => $f) { if (!empty($f['selected'])) { foreach ($f['values'] as $value_id => &$value) { if (!empty($value['selected'])) { $value = array('id' => $value_id); } else { unset($f['values'][$value_id]); } } $storage_data[$id] = $f; } } } } break; case 'skus': $storage_data = array(); $i = 0; foreach ($raw as $sku_id => $sku) { if (!empty($sku['virtual']) || $ignore_select) { if ($file_path = shopProductSkusModel::getPath($sku)) { $sku_files[$sku['id']] = array('file_name' => $sku['file_name'], 'file_description' => $sku['file_description'], 'file_size' => $sku['file_size'], 'file_path' => $file_path); } if (!empty($sku['image_id'])) { $sku_images[$sku['id']] = $sku['image_id']; } foreach (array('id', 'id_1c', 'product_id', 'image_id', 'file_name', 'file_size', 'file_description') as $field) { if (isset($sku[$field])) { unset($sku[$field]); } } $storage_data[--$i] = $sku; } } break; case 'tags': $storage_data = array_values($raw); break; case 'categories': $storage_data = array_keys($raw); break; default: $storage_data = $raw; break; } $duplicate->{$key} = $storage_data; } $counter = 0; $data['url'] = shopHelper::genUniqueUrl($this->url, $this->model, $counter); $data['name'] = $this->name . sprintf('(%d)', $counter ? $counter : 1); $duplicate->save($data); $product_id = $duplicate->getId(); $sku_map = array_combine(array_keys($this->skus), array_keys($duplicate->skus)); $config = wa('shop')->getConfig(); $image_thumbs_on_demand = $config->getOption('image_thumbs_on_demand'); /** * @var shopConfig $config */ if ($this->pages) { $product_pages_model = new shopProductPagesModel(); foreach ($this->pages as $page) { unset($page['id']); unset($page['create_time']); unset($page['update_datetime']); unset($page['create_contact_id']); $page['product_id'] = $duplicate->getId(); $product_pages_model->add($page); } } #duplicate images $product_skus_model = new shopProductSkusModel(); $images_model = new shopProductImagesModel(); $images = $images_model->getByField('product_id', $this->getId(), $images_model->getTableId()); $callback = create_function('$a, $b', 'return (max(-1, min(1, $a["sort"] - $b["sort"])));'); usort($images, $callback); foreach ($images as $id => $image) { $source_path = shopImage::getPath($image); $original_file = shopImage::getOriginalPath($image); $image['product_id'] = $duplicate->getId(); if ($sku_id = array_search($image['id'], $sku_images)) { $sku_id = $sku_map[$sku_id]; } unset($image['id']); try { if ($image['id'] = $images_model->add($image, $id == $this->image_id)) { waFiles::copy($source_path, shopImage::getPath($image)); if (file_exists($original_file)) { waFiles::copy($original_file, shopImage::getOriginalPath($image)); } if ($sku_id) { $product_skus_model->updateById($sku_id, array('image_id' => $image['id'])); } if (!$image_thumbs_on_demand) { shopImage::generateThumbs($image, $config->getImageSizes()); //TODO use dummy copy with rename files } } } catch (waDbException $ex) { //just ignore it waLog::log('Error during copy product: ' . $ex->getMessage(), 'shop.log'); } catch (waException $ex) { if (!empty($image['id'])) { $images_model->deleteById($image['id']); } waLog::log('Error during copy product: ' . $ex->getMessage(), 'shop.log'); } } foreach ($sku_files as $sku_id => $data) { $source_path = $data['file_path']; unset($data['file_path']); $sku_id = $sku_map[$sku_id]; $sku = array_merge($duplicate->skus[$sku_id], $data); $product_skus_model->updateById($sku_id, $data); $target_path = shopProductSkusModel::getPath($sku); try { waFiles::copy($source_path, $target_path); } catch (waException $ex) { $data = array('file_name' => '', 'file_description' => '', 'file_size' => 0); $product_skus_model->updateById($sku_id, $data); print $ex->getMessage(); } } $product_features_model = new shopProductFeaturesModel(); $skus_features = $product_features_model->getSkuFeatures($this->id); $skus_features_data = array(); foreach ($skus_features as $sku_id => $features) { $sku_id = $sku_map[$sku_id]; foreach ($features as $feature_id => $feature_value_id) { $skus_features_data[] = compact('product_id', 'sku_id', 'feature_id', 'feature_value_id'); } } if ($skus_features_data) { $product_features_model->multipleInsert($skus_features_data); } if ($this->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) { $product_features_selectable_model = new shopProductFeaturesSelectableModel(); if ($features_selectable = $product_features_selectable_model->getByField('product_id', $this->id, true)) { foreach ($features_selectable as &$feature_selectable) { $feature_selectable['product_id'] = $product_id; } unset($feature_selectable); $product_features_selectable_model->multipleInsert($features_selectable); } } $product_services_model = new shopProductServicesModel(); if ($services = $product_services_model->getByField('product_id', $this->id, true)) { foreach ($services as &$service) { unset($service['id']); $service['product_id'] = $product_id; $service['sku_id'] = ifset($sku_map[$service['sku_id']]); unset($service); } $product_services_model->multipleInsert($services); } $product_related_model = new shopProductRelatedModel(); if ($related = $product_related_model->getByField('product_id', $this->id, true)) { foreach ($related as &$row) { $row['product_id'] = $product_id; } unset($row); $product_related_model->multipleInsert($related); } $params = array('product' => &$this, 'duplicate' => &$duplicate); /** * @wa-event product_duplicate */ wa()->event('product_duplicate', $params); return $duplicate; }
public function execute() { $update = waRequest::post('update'); // just update one or any field of product if ($update) { $this->update($update); return; } $data = waRequest::post('product'); $id = empty($data['id']) || !intval($data['id']) ? null : $data['id']; if (!$id && isset($data['id'])) { unset($data['id']); } # edit product info - check rights $product_model = new shopProductModel(); if ($id) { if (!$product_model->checkRights($id)) { throw new waRightsException(_w("Access denied")); } } else { if (!$product_model->checkRights($data)) { throw new waRightsException(_w("Access denied")); } } $skus = waRequest::post('skus', array()); if (isset($data['skus'])) { foreach ($skus as $s_id => $s) { if (isset($data['skus'][$s_id])) { $data['skus'][$s_id] += $s; } else { $data['skus'][$s_id] = $s; } } } else { $data['skus'] = $skus; } if (empty($data['categories'])) { $data['categories'] = array(); } if (empty($data['tags'])) { $data['tags'] = array(); } if (empty($data['features_selectable'])) { $data['features_selectable'] = array(); } # verify sku_type before save if ($data['type_id']) { $features_model = new shopFeatureModel(); if ($features_model->isTypeMultipleSelectable($data['type_id'])) { if ($data['sku_type'] == shopProductModel::SKU_TYPE_SELECTABLE) { if (empty($data['features_selectable'])) { throw new waException(_w("Check at least one feature value")); } } } else { $data['sku_type'] = shopProductModel::SKU_TYPE_FLAT; } } else { $data['sku_type'] = shopProductModel::SKU_TYPE_FLAT; } if ($data['sku_type'] == shopProductModel::SKU_TYPE_FLAT) { $data['features_selectable'] = array(); } try { $product = new shopProduct($id); // for logging changes in stocks shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_PRODUCT); if ($product->save($data, true, $this->errors)) { $features_counts = null; if ($product->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) { $features_counts = array(); foreach ($product->features_selectable as $f) { if (isset($f['selected'])) { $features_counts[] = $f['selected']; } else { $features_counts[] = count($f['values']); } } $features_total_count = array_product($features_counts); $this->response['features_selectable_strings'] = array('options' => implode(' x ', $features_counts) . ' ' . _w('option', 'options', $features_total_count), 'skus' => _w('%d SKU in total', '%d SKUs in total', $features_total_count)); } shopProductStocksLogModel::clearContext(); if ($id) { $this->logAction('product_edit', $id); } else { $this->logAction('product_add', $product->getId()); } $this->response['id'] = $product->getId(); $this->response['name'] = $product->name; $this->response['url'] = $product->url; $this->response['frontend_urls'] = $this->getUrl($product); $this->response['raw'] = $this->workupData($product->getData()); $sales_rate = waRequest::post('sales_rate', 0, waRequest::TYPE_STRING_TRIM); $sales_rate = (double) str_replace(',', '.', $sales_rate); $runout = $product->getRunout($sales_rate); if (!empty($runout['product'])) { $runout['product']['date_str'] = wa_date("humandate", $runout['product']['date']); $runout['product']['days_str'] = _w('%d day', '%d days', $runout['product']['days']); if ($runout['product']['days'] < 3 * 365 && $runout['product']['days'] > 0) { $runout['product_str'] = sprintf(_w('Based on last 30 days sales dynamic (%d items of %s sold during last 30 days), you will run out of %s in <strong>%d days</strong> (on %s)'), $sales_rate * 30, $product->name, $product->name, $runout['product']['days'], wa_date("humandate", $runout['product']['date'])); } } else { $runout['product'] = new stdClass(); /* {} */ } if (!empty($runout['sku'])) { foreach ($runout['sku'] as &$sk_r) { if (empty($sk_r['stock'])) { $sk_r['date_str'] = wa_date("humandate", $sk_r['date']); $sk_r['days_str'] = _w('%d day', '%d days', $sk_r['days']); } else { foreach ($sk_r['stock'] as &$st_r) { $st_r['date_str'] = wa_date("humandate", $st_r['date']); $st_r['days_str'] = _w('%d day', '%d days', $st_r['days']); } } } unset($sk_r, $st_r); } else { $runout['sku'] = new stdClass(); /* {} */ } $this->response['raw']['runout'] = $runout; $this->response['storefront_map'] = $product_model->getStorefrontMap($product->id); } } catch (Exception $ex) { $this->setError($ex->getMessage()); } }
public function addNewProduct($data) { $product = new shopProduct(); $product->save($data); return $product->getId(); }