public function execute() { $product = new shopProduct(waRequest::get('id', 0, waRequest::TYPE_INT)); if (!$product->id) { if (waRequest::get('id') == 'new') { $product->name = ''; $product->id = 'new'; $product->status = 1; } else { throw new waException("Product not found", 404); } } $counters = array('reviews' => 0, 'images' => 0, 'pages' => 0, 'services' => 0); $sidebar_counters = array(); $config = $this->getConfig(); /** * @var shopConfig $config */ #load product types $type_model = new shopTypeModel(); $product_types = $type_model->getTypes(true); $product_types_count = count($product_types); if (intval($product->id)) { # 1 fill extra product data # 1.1 fill product reviews $product_reviews_model = new shopProductReviewsModel(); $product['reviews'] = $product_reviews_model->getReviews($product->id, 0, $config->getOption('reviews_per_page_product'), 'datetime DESC', array('is_new' => true)); $counters['reviews'] = $product_reviews_model->count($product->id); $sidebar_counters['reviews'] = array('new' => $product_reviews_model->countNew()); $counters['images'] = count($product['images']); $product_pages_model = new shopProductPagesModel(); $counters['pages'] = $product_pages_model->count($product->id); $product_services_model = new shopProductServicesModel(); $counters['services'] = $product_services_model->countServices($product->id); $product_stocks_log_model = new shopProductStocksLogModel(); $counters['stocks_log'] = $product_stocks_log_model->countByField('product_id', $product->id); $this->view->assign('edit_rights', $product->checkRights()); } else { $counters += array_fill_keys(array('images', 'services', 'pages', 'reviews'), 0); $product['images'] = array(); reset($product_types); $product->type_id = 0; if ($product_types_count) { if (!$product_types) { throw new waRightsException(_w("Access denied")); } else { reset($product_types); $product->type_id = key($product_types); } } elseif (!$product->checkRights()) { throw new waRightsException(_w("Access denied")); } $this->view->assign('edit_rights', true); $product['skus'] = array('-1' => array('id' => -1, 'sku' => '', 'available' => 1, 'name' => '', 'price' => 0.0, 'purchase_price' => 0.0, 'count' => null, 'stock' => array(), 'virtual' => 0)); $product->currency = $config->getCurrency(); } $this->assignReportsData($product); $stock_model = new shopStockModel(); $taxes_mode = new shopTaxModel(); $this->view->assign('stocks', $stock_model->getAll('id')); $this->view->assign(array('use_product_currency' => wa()->getSetting('use_product_currency'), 'currencies' => $this->getCurrencies(), 'primary_currency' => $config->getCurrency(), 'taxes' => $taxes_mode->getAll())); $category_model = new shopCategoryModel(); $categories = $category_model->getFullTree('id, name, depth, url, full_url, parent_id', true); $frontend_urls = array(); if (intval($product->id)) { $routing = wa()->getRouting(); $domain_routes = $routing->getByApp($this->getAppId()); foreach ($domain_routes as $domain => $routes) { foreach ($routes as $r) { if (!empty($r['private'])) { continue; } if (empty($r['type_id']) || in_array($product->type_id, (array) $r['type_id'])) { $routing->setRoute($r, $domain); $params = array('product_url' => $product->url); if ($product->category_id && isset($categories[$product->category_id])) { if (!empty($r['url_type']) && $r['url_type'] == 1) { $params['category_url'] = $categories[$product->category_id]['url']; } else { $params['category_url'] = $categories[$product->category_id]['full_url']; } } $frontend_url = $routing->getUrl('/frontend/product', $params, true); $frontend_urls[] = array('url' => $frontend_url); } } } } else { $frontend_urls[] = array('url' => wa()->getRouteUrl('/frontend/product', array('product_url' => '%product_url%'), true)); } $stuff = intval($product->id) ? $product->url : '%product_url%'; foreach ($frontend_urls as &$frontend_url) { $pos = strrpos($frontend_url['url'], $stuff); $frontend_url['base'] = $pos !== false ? rtrim(substr($frontend_url['url'], 0, $pos), '/') . '/' : $frontend_url['url']; } unset($frontend_url); $product_model = new shopProductModel(); $this->view->assign('storefront_map', $product_model->getStorefrontMap($product->id)); /** * Backend product profile page * UI hook allow extends product profile page * @event backend_product * @param shopProduct $entry * @return array[string][string]string $return[%plugin_id%]['title_suffix'] html output * @return array[string][string]string $return[%plugin_id%]['action_button'] html output * @return array[string][string]string $return[%plugin_id%]['toolbar_section'] html output * @return array[string][string]string $return[%plugin_id%]['image_li'] html output */ $this->view->assign('backend_product', wa()->event('backend_product', $product)); /** * @event backend_product_edit */ $this->view->assign('backend_product_edit', wa()->event('backend_product_edit', $product)); $this->view->assign('categories', $categories); $this->view->assign('counters', $counters); $this->view->assign('product', $product); $this->view->assign('current_author', shopProductReviewsModel::getAuthorInfo(wa()->getUser()->getId())); $this->view->assign('reply_allowed', true); $this->view->assign('review_allowed', true); $this->view->assign('sidebar_counters', $sidebar_counters); $this->view->assign('lang', substr(wa()->getLocale(), 0, 2)); $this->view->assign('frontend_urls', $frontend_urls); $tag_model = new shopTagModel(); $this->view->assign('popular_tags', $tag_model->popularTags()); $counts = array(); // Selectable features $features_selectable = $product->features_selectable; if (is_array($features_selectable)) { foreach ($features_selectable as $f) { if ($f['selected']) { $counts[] = $f['selected']; } } } $feature_model = new shopTypeFeaturesModel(); $features_selectable_types = $feature_model->getSkuTypeSelectableTypes(); foreach ($product_types as $type_id => &$type) { $type['sku_type'] = empty($features_selectable_types[$type_id]) ? shopProductModel::SKU_TYPE_FLAT : shopProductModel::SKU_TYPE_SELECTABLE; } $this->view->assign('features', $features_selectable); $this->view->assign('duble', '???'); $this->view->assign('features_counts', $counts); #load product types $this->view->assign('product_types', $product_types); $this->view->assign('sidebar_width', $config->getSidebarWidth()); }
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()); } }