public function execute()
 {
     $product_tags_model = new shopProductTagsModel();
     $tags = array();
     $hash = waRequest::post('hash', '');
     // get tags by products
     if (!$hash) {
         $product_ids = waRequest::post('product_id', array(), waRequest::TYPE_ARRAY_INT);
         if (!$product_ids) {
             return;
         }
         $tags = $product_tags_model->getTags($product_ids);
         // get tags by hash of collection
     } else {
         // add all products of collection with this hash
         $collection = new shopProductsCollection($hash);
         $offset = 0;
         $count = 100;
         $total_count = $collection->count();
         while ($offset < $total_count) {
             $ids = array_keys($collection->getProducts('*', $offset, $count));
             $tags += $product_tags_model->getTags($ids);
             $offset += count($ids);
         }
     }
     $tag_model = new shopTagModel();
     $this->view->assign(array('tags' => $tags, 'popular_tags' => $tag_model->popularTags()));
 }
 public function execute()
 {
     $type = waRequest::get('type', '', waRequest::TYPE_STRING_TRIM);
     $parent_id = waRequest::get('parent_id', 0, waRequest::TYPE_INT);
     if ($parent_id) {
         $category_model = new shopCategoryModel();
         $parent = $category_model->getById($parent_id);
     }
     $this->template = 'DialogProduct' . ucfirst($type) . 'Create';
     $this->view->assign(array('type' => $type, 'parent' => $parent_id ? $parent : array()));
     if ($type == 'category') {
         $tag_model = new shopTagModel();
         $stuff = '%category_url%';
         $frontend_url = wa()->getRouteUrl('/frontend/category', array('category_url' => $stuff), true);
         $pos = strrpos($frontend_url, $stuff);
         $fontend_base_url = $pos !== false ? rtrim(substr($frontend_url, 0, $pos), '/') . '/' : $frontend_url;
         $feature_model = new shopFeatureModel();
         $features = $feature_model->getFeatures('selectable', 1);
         $features += $feature_model->getFeatures('type', 'boolean');
         $features = $feature_model->getValues($features);
         $this->view->assign(array('cloud' => $tag_model->getCloud(), 'currency' => wa()->getConfig()->getCurrency(), 'frontend_base_url' => $fontend_base_url, 'lang' => substr(wa()->getLocale(), 0, 2), 'features' => $features));
     } else {
         if ($type == 'set') {
             $this->view->assign('default_count', $this->set_dynamic_default_count);
         }
     }
 }
 public function execute()
 {
     $tag_model = new shopTagModel();
     $limit = waRequest::get('limit', 100, 'int');
     if ($limit < 0) {
         throw new waAPIException('invalid_param', 'Param limit must be greater than or equal to zero');
     }
     if ($limit > 1000) {
         throw new waAPIException('invalid_param', 'Param limit must be less or equal 1000');
     }
     $this->response = $tag_model->getCloud(null, $limit);
 }
 public function execute()
 {
     $limit = 10;
     $term = waRequest::get('term', '', waRequest::TYPE_STRING_TRIM);
     $tag_model = new shopTagModel();
     $term = $tag_model->escape($term, 'like');
     $tags = array();
     foreach ($tag_model->select('name')->where("name LIKE '{$term}%'")->limit($limit)->fetchAll() as $tag) {
         $tags[] = array('value' => $tag['name'], 'label' => htmlspecialchars($tag['name']));
     }
     echo json_encode($tags);
 }
 public function execute()
 {
     $tag_model = new shopTagModel();
     $product_tags_model = new shopProductTagsModel();
     $delete_tags = waRequest::post('delete_tags', array(), waRequest::TYPE_ARRAY_INT);
     $tags = waRequest::post('tags', '', waRequest::TYPE_STRING_TRIM);
     $tags = $tags ? explode(',', $tags) : array();
     if (!$delete_tags && !$tags) {
         return;
     }
     $hash = waRequest::post('hash', '');
     // delete tags of selected products
     if (!$hash) {
         $product_ids = waRequest::post('product_id', array(), waRequest::TYPE_ARRAY_INT);
         if (!$product_ids) {
             return;
         }
         // delete tags of selected products
         if ($delete_tags) {
             $product_tags_model->delete($product_ids, $delete_tags);
         }
         // assign tags to selected products
         if ($tags) {
             $tag_ids = $tag_model->getIds($tags);
             $product_tags_model->assign($product_ids, $tag_ids);
         }
     } else {
         // maintain all products of collection with this hash
         $collection = new shopProductsCollection($hash);
         $offset = 0;
         $count = 100;
         $total_count = $collection->count();
         $tag_ids = array();
         if ($offset < $total_count) {
             $tag_ids = $tag_model->getIds($tags);
         }
         while ($offset < $total_count) {
             $product_ids = array_keys($collection->getProducts('*', $offset, $count));
             // delete tags
             if ($delete_tags) {
                 $product_tags_model->delete($product_ids, $delete_tags);
             }
             // assign tags
             if ($tag_ids) {
                 $product_tags_model->assign($product_ids, $tag_ids);
             }
             $offset += count($product_ids);
         }
     }
     $this->response['cloud'] = $tag_model->getCloud('id');
 }
 public function execute()
 {
     if (!$this->getUser()->isAdmin('shop') && !wa()->getUser()->getRights('shop', 'type.%')) {
         throw new waRightsException('Access denied');
     }
     $this->setLayout(new shopBackendLayout());
     $this->getResponse()->setTitle(_w('Products'));
     $this->view->assign('categories', new shopCategories());
     $tag_model = new shopTagModel();
     $this->view->assign('cloud', $tag_model->getCloud());
     $set_model = new shopSetModel();
     $this->view->assign('sets', $set_model->getAll());
     $collapse_types = wa()->getUser()->getSettings('shop', 'collapse_types');
     if (empty($collapse_types)) {
         $type_model = new shopTypeModel();
         $this->view->assign('types', $type_model->getTypes());
     } else {
         $this->view->assign('types', false);
     }
     $product_model = new shopProductModel();
     $this->view->assign('count_all', $product_model->countAll());
     $review_model = new shopProductReviewsModel();
     $this->view->assign('count_reviews', array('all' => $review_model->count(null, false), 'new' => $review_model->countNew(true)));
     $product_services = new shopServiceModel();
     $this->view->assign('count_services', $product_services->countAll());
     $config = $this->getConfig();
     $this->view->assign('default_view', $config->getOption('products_default_view'));
     /*
      * @event backend_products
      * @return array[string]array $return[%plugin_id%] array of html output
      * @return array[string][string]string $return[%plugin_id%]['sidebar_top_li'] html output
      * @return array[string][string]string $return[%plugin_id%]['sidebar_section'] html output
      */
     $this->view->assign('backend_products', wa()->event('backend_products'));
     $this->view->assign('sidebar_width', $config->getSidebarWidth());
     $this->view->assign('lang', substr(wa()->getLocale(), 0, 2));
 }
 protected function upsellingPrepare($product_id, $auto_title = false)
 {
     $model = $this->getModel();
     if (isset($this->options['product'])) {
         $product = $this->options['product'];
         $conditions = $this->options['conditions'];
     } else {
         $product = new shopProduct($product_id);
         $type_upselling_model = new shopTypeUpsellingModel();
         $conditions = $type_upselling_model->getByField('type_id', $product['type'], true);
     }
     $this->where[] = 'p.id != ' . (int) $product_id;
     $sum = array();
     foreach ($conditions as $row) {
         if ($row['feature'] == 'tag') {
             $tag_model = new shopTagModel();
             $tag = $tag_model->getByName($row['value']);
             if ($tag) {
                 $this->where[] = 'pt.tag_id = ' . (int) $tag['id'];
                 $this->joins[] = array('table' => 'shop_product_tags', 'alias' => 'pt');
             }
             continue;
         } elseif ($row['feature'] == 'type_id') {
             if ($row['cond'] == 'same') {
                 $this->where[] = 'p.type_id = ' . (int) $product['type_id'];
             } elseif ($row['cond'] == 'notsame') {
                 $this->where[] = 'p.type_id != ' . (int) $product['type_id'];
             } elseif ($row['cond'] == 'is') {
                 $this->where[] = 'p.type_id = ' . (int) $row['value'];
             }
             continue;
         }
         switch ($row['cond']) {
             case 'between':
                 list($min, $max) = explode(',', $row['value']);
                 if ($model->fieldExists($row['feature'])) {
                     $v = $product[$row['feature']];
                 } else {
                     $v = isset($product['features'][$row['feature']]) ? $product['features'][$row['feature']] : null;
                 }
                 if (!$v) {
                     continue;
                 }
                 $min = $v * (double) (100 + $min) / 100;
                 $max = $v * (double) (100 + $max) / 100;
                 $v = str_replace(',', '.', $v);
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . ' > ' . str_replace(',', '.', $min);
                     $this->where[] = 'p.' . $row['feature'] . ' < ' . str_replace(',', '.', $max);
                     $sum[] = 'ABS(p.' . $row['feature'] . ' - ' . $v . ')/' . $v;
                 }
                 break;
             case 'is':
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . " = '" . $model->escape($row['value']) . "'";
                 } else {
                     $this->addJoin('shop_product_features', null, ":table.feature_id = " . (int) $row['feature_id'] . " AND :table.feature_value_id = " . (int) $row['value']);
                     $this->group_by = 'p.id';
                 }
                 break;
             case 'any':
             case 'all':
                 if ($model->fieldExists($row['feature'])) {
                     //$this->where[] = 'p.'.$row['feture']." = '".$model->escape($row['value'])."'";
                 } else {
                     if ($row['value']) {
                         $this->addJoin('shop_product_features', null, ":table.feature_id = " . (int) $row['feature_id'] . " AND :table.feature_value_id IN (" . $row['value'] . ")");
                         $this->group_by = 'p.id';
                     } else {
                         $this->where[] = '0';
                     }
                 }
                 break;
             case 'notsame':
             case 'same':
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . " " . ($row['cond'] == 'notsame' ? '!' : '') . "= '" . $model->escape($product->features[$row['feature']]) . "'";
                 } else {
                     $product_features_model = new shopProductFeaturesModel();
                     $rows = $product_features_model->getByField(array('product_id' => $product['id'], 'sku_id' => null, 'feature_id' => $row['feature_id']), true);
                     $values = array();
                     foreach ($rows as $r) {
                         $values[] = $r['feature_value_id'];
                     }
                     if ($values) {
                         $alias = $this->addJoin('shop_product_features');
                         $this->where[] = $alias . ".feature_id = " . $row['feature_id'];
                         $this->where[] = $alias . ".feature_value_id " . (count($values) == 1 ? ($row['cond'] == 'notsame' ? '!' : '') . "= " . $values[0] : ($row['cond'] == 'notsame' ? 'NOT ' : '') . "IN (" . implode(',', $values) . ")");
                         $this->group_by = 'p.id';
                     }
                 }
                 break;
         }
     }
     if ($sum) {
         $this->fields[] = '(' . implode(' + ', $sum) . ') AS upselling_deviation';
         $this->order_by = 'upselling_deviation';
     }
 }
 private function getCategorySettings($id)
 {
     $category_model = new shopCategoryModel();
     $category_params_model = new shopCategoryParamsModel();
     $settings = $category_model->getById($id);
     if (!$settings) {
         return array();
     }
     /**
      * @event backend_category_dialog
      * @param array $category
      * @return array[string][string] $return[%plugin_id%] html output for dialog
      */
     $this->view->assign('event_dialog', wa()->event('backend_category_dialog', $settings));
     $category_routes_model = new shopCategoryRoutesModel();
     $settings['routes'] = $category_routes_model->getRoutes($id);
     $settings['frontend_urls'] = array();
     foreach ($category_model->getFrontendUrls($id) as $frontend_url) {
         $pos = strrpos($frontend_url, $settings['url']);
         $settings['frontend_urls'][] = array('url' => $frontend_url, 'base' => $pos !== false ? rtrim(substr($frontend_url, 0, $pos), '/') . '/' : '');
     }
     $settings['params'] = $category_params_model->get($id);
     if (isset($settings['params']['enable_sorting'])) {
         $settings['enable_sorting'] = 1;
         unset($settings['params']['enable_sorting']);
     } else {
         $settings['enable_sorting'] = 0;
     }
     $feature_model = new shopFeatureModel();
     $selectable_and_boolean_features = $feature_model->select('*')->where("(selectable=1 OR type='boolean' OR type='double' OR type LIKE 'dimension\\.%' OR type LIKE 'range\\.%') AND parent_id IS NULL")->fetchAll('id');
     if ($settings['type'] == shopCategoryModel::TYPE_DYNAMIC) {
         if ($settings['conditions']) {
             $settings['conditions'] = shopProductsCollection::parseConditions($settings['conditions']);
         } else {
             $settings['conditions'] = array();
         }
         $tag_model = new shopTagModel();
         $cloud = $tag_model->getCloud('name');
         if (!empty($settings['conditions']['tag'][1])) {
             foreach ($settings['conditions']['tag'][1] as $tag_name) {
                 $cloud[$tag_name]['checked'] = true;
             }
         }
         $settings['cloud'] = $cloud;
         // extract conditions for features
         foreach ($settings['conditions'] as $name => $value) {
             if (substr($name, -9) === '.value_id') {
                 unset($settings['conditions'][$name]);
                 $settings['conditions']['feature'][substr($name, 0, -9)] = $value;
             }
         }
         $settings['custom_conditions'] = $this->extractCustomConditions($settings['conditions']);
         $settings['features'] = $selectable_and_boolean_features;
         $settings['features'] = $feature_model->getValues($settings['features']);
     }
     $filter = $settings['filter'] !== null ? explode(',', $settings['filter']) : null;
     $feature_filter = array();
     $features['price'] = array('id' => 'price', 'name' => 'Price');
     $features += $selectable_and_boolean_features;
     if (!empty($filter)) {
         foreach ($filter as $feature_id) {
             $feature_id = trim($feature_id);
             if (isset($features[$feature_id])) {
                 $feature_filter[$feature_id] = $features[$feature_id];
                 $feature_filter[$feature_id]['checked'] = true;
                 unset($features[$feature_id]);
             }
         }
     }
     $settings['allow_filter'] = (bool) $filter;
     $settings['filter'] = $feature_filter + $features;
     return $settings;
 }
 /**
  * @param int|array $product_id
  * @param int|array $tag_id
  */
 public function delete($product_id, $tag_id)
 {
     if (!$product_id) {
         return false;
     }
     $product_id = (array) $product_id;
     // delete tags
     $this->deleteByField(array('product_id' => $product_id, 'tag_id' => $tag_id));
     // decrease count for tags
     $tag_model = new shopTagModel();
     $tag_model->recount($tag_id);
     // clear cache
     if ($cache = wa()->getCache()) {
         $cache->delete('tags');
     }
 }
Beispiel #10
0
 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 tags($limit = 50)
 {
     if ($limit == 50 && ($cache = $this->wa->getCache())) {
         $tags = $cache->get('tags');
         if ($tags !== null) {
             return $tags;
         }
     }
     $tag_model = new shopTagModel();
     $tags = $tag_model->getCloud(null, $limit);
     if (!empty($cache)) {
         $cache->set('tags', $tags, 7200);
     }
     return $tags;
 }