public function execute()
 {
     $type_id = $this->getType();
     if (!$type_id) {
         return;
     }
     $hash = waRequest::post('hash', '', waRequest::TYPE_STRING_TRIM);
     if (!$hash) {
         $product_ids = waRequest::post('product_id', array(), waRequest::TYPE_ARRAY_INT);
         $product_ids = $this->product_model->filterAllowedProductIds($product_ids);
         if (!$product_ids) {
             return;
         }
         $this->product_model->updateType($product_ids, $type_id);
         $this->response['types'] = $this->type_model->getTypes();
     } else {
         if (substr($hash, 0, 5) != 'type/') {
             $collection = new shopProductsCollection($hash);
             $offset = 0;
             $count = 100;
             $total_count = $collection->count();
             while ($offset < $total_count) {
                 $ids = array_keys($collection->getProducts('*', $offset, $count));
                 $filtered = $this->product_model->filterAllowedProductIds($ids);
                 $this->product_model->updateType($filtered, $type_id);
                 $offset += count($ids);
             }
             $this->response['types'] = $this->type_model->getTypes();
         } else {
             $this->product_model->changeType(substr($hash, 5), $type_id);
             $this->response['types'] = $this->type_model->getTypes();
         }
     }
 }
 protected function init()
 {
     try {
         $this->data['timestamp'] = time();
         $this->data['direction'] = waRequest::post('direction', 'import');
         $type_model = new shopTypeModel();
         $this->data['types'] = array_map('intval', array_keys($type_model->getTypes()));
         switch ($this->data['direction']) {
             case 'export':
                 $this->initExport();
                 break;
             case 'import':
             default:
                 $this->data['direction'] = 'import';
                 $this->initImport();
                 break;
         }
         $stages = array_keys($this->data['count']);
         $this->data['current'] = array_fill_keys($stages, 0);
         $value = $this->data['direction'] == 'import' ? $this->emulate(null) ? array('add' => 0, 'found' => 0, 'skip' => 0, 'rights' => 0, 'currency' => 0) : array('new' => 0, 'update' => 0, 'skip' => 0, 'error' => 0, 'rights' => 0, 'currency' => 0) : 0;
         $this->data['processed_count'] = array_fill_keys($stages, $value);
         $this->data['map'] = array();
         $this->data['memory'] = memory_get_peak_usage();
         $this->data['memory_avg'] = memory_get_usage();
         $this->data['timestamp'] = time();
     } catch (waException $ex) {
         $this->error($ex->getMessage());
         echo $this->json(array('error' => $ex->getMessage()));
         exit;
     }
 }
 public function execute()
 {
     $type_model = new shopTypeModel();
     $types = $type_model->getTypes();
     $this->response = array_values($types);
     $this->response['_element'] = 'type';
 }
 public function execute()
 {
     $hide = waRequest::get('hide');
     if (strlen($hide)) {
         wa()->getUser()->setSettings('shop', 'collapse_types', intval($hide));
         exit;
     } else {
         $type_model = new shopTypeModel();
         wa()->getUser()->setSettings('shop', 'collapse_types', 0);
         $this->view->assign('types', $type_model->getTypes());
     }
 }
 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));
 }
 public function execute()
 {
     $model = new shopTypeModel();
     $this->view->assign('types', $model->getTypes());
 }
 /**
  * Get product ids and leave only allowed by rights
  *
  * @param array $product_ids
  * @return array
  */
 public function filterAllowedProductIds(array $product_ids)
 {
     if (wa('shop')->getUser()->getRights('shop', 'type.all')) {
         return $product_ids;
     }
     $type_model = new shopTypeModel();
     $types = $type_model->getTypes();
     $type_ids = array_keys($types);
     if (empty($product_ids) || empty($types)) {
         return array();
     }
     $product_ids = array_keys($this->query("\n            SELECT id FROM `{$this->table}`\n            WHERE id IN(" . implode(',', $product_ids) . ")\n                AND type_id IN (" . implode(',', $type_ids) . ")")->fetchAll('id'));
     return $product_ids;
 }
 public function execute()
 {
     $direction = waRequest::request('direction', 'import') == 'export' ? 'export' : 'import';
     $profile_helper = new shopImportexportHelper('csv:product:' . $direction);
     $this->view->assign('profiles', $profile_helper->getList());
     $profile = $profile_helper->getConfig();
     if ($direction == 'export') {
         //export section TODO
         $profile['config'] += array('encoding' => 'UTF-8', 'images' => true, 'features' => true, 'domain' => null, 'delimiter' => ';', 'hash' => '');
         $info = array();
         if (!empty($profile['id'])) {
             $path = wa()->getTempPath('csv/download/' . $profile['id']);
             $files = waFiles::listdir($path);
             foreach ($files as $file) {
                 $file_path = $path . '/' . $file;
                 $info[] = array('name' => $file, 'mtime' => filemtime($file_path), 'size' => filesize($file_path));
             }
             usort($info, create_function('$a, $b', 'return (max(-1, min(1, $a["mtime"] - $b["mtime"])));'));
         }
         $this->view->assign('info', array_slice($info, -5, 5, true));
         $set_model = new shopSetModel();
         $this->view->assign('sets', $set_model->getAll());
         $routing = wa()->getRouting();
         $settlements = array();
         $current_domain = null;
         $domain_routes = $routing->getByApp('shop');
         foreach ($domain_routes as $domain => $routes) {
             foreach ($routes as $route) {
                 $settlement = $domain . '/' . $route['url'];
                 if ($current_domain === null) {
                     $current_domain = $settlement;
                 } elseif ($settlement == $profile['config']['domain']) {
                     $current_domain = $settlement;
                 }
                 $settlements[] = $settlement;
             }
         }
         $this->view->assign('current_domain', $current_domain);
         $this->view->assign('settlements', $settlements);
     } else {
         $profile['config'] += array('encoding' => 'UTF-8', 'delimiter' => ';', 'map' => array());
         $base_path = wa()->getConfig()->getPath('root');
         $app_path = array('shop' => wa()->getDataPath(null, false, 'shop'), 'site' => wa()->getDataPath(null, true, 'site'));
         foreach ($app_path as &$path) {
             $path = preg_replace('@^([\\\\/])@', '', str_replace($base_path, '', $path . '/'));
         }
         unset($path);
         $this->view->assign('upload_path', waSystem::getSetting('csv.upload_path', 'path/to/folder/with/source/images/'));
         $this->view->assign('upload_app', waSystem::getSetting('csv.upload_app', 'shop'));
         $this->view->assign('app_path', $app_path);
     }
     $this->view->assign('profile', $profile);
     $type_model = new shopTypeModel();
     $this->view->assign('types', $type_model->getTypes());
     $encoding = array_diff(mb_list_encodings(), array('pass', 'wchar', 'byte2be', 'byte2le', 'byte4be', 'byte4le', 'BASE64', 'UUENCODE', 'HTML-ENTITIES', 'Quoted-Printable', '7bit', '8bit', 'auto'));
     $popular = array_intersect(array('UTF-8', 'Windows-1251', 'ISO-8859-1'), $encoding);
     asort($encoding);
     $encoding = array_unique(array_merge($popular, $encoding));
     $this->view->assign('encoding', $encoding);
     $plugins = wa()->getConfig()->getPlugins();
     $this->view->assign('direction', $direction);
     $this->view->assign('plugin', ifempty($plugins['csvproducts'], array()));
 }
 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 getAvailableImages($offset = 0, $limit = null)
 {
     if (!$limit) {
         $limit = (int) $offset;
         $offset = 0;
     } else {
         $offset = (int) $offset;
         $limit = (int) $limit;
     }
     if (wa()->getUser()->getRights('shop', 'type.all')) {
         $sql = "SELECT * FROM `{$this->table}` ORDER BY product_id, id LIMIT {$offset}, {$limit}";
     } else {
         $type_model = new shopTypeModel();
         $types = $type_model->getTypes();
         if (!$types) {
             return array();
         }
         $sql = "SELECT i.* FROM `{$this->table}` i\n                JOIN `shop_products` p ON p.id = i.product_id\n                WHERE p.type_id IN (" . array_keys($types) . ")\n                ORDER BY i.product_id, i.id\n                LIMIT {$offset}, {$limit}";
     }
     return $this->query($sql)->fetchAll('id');
 }