Пример #1
0
 public function productsAction()
 {
     $request = $this->request;
     $persistent = $this->persistent;
     // Reset
     $reset = $this->request->getQuery('reset', 'int');
     if (isset($reset)) {
         $persistent->productsParams = null;
     }
     // Persistent parameters
     if (!is_array($persistent->productsParams)) {
         $persistent->productsParams = ['qry' => ['conditions' => "id IS NOT NULL", 'order' => 'id ASC'], 'sort' => ['page' => 1, 'perpage' => 50]];
     }
     $parameters = $persistent->productsParams;
     $parameters['sort']['perpage'] = 50;
     $parameters['sort']['page'] = 1;
     if ($request->getQuery('page', 'int')) {
         $parameters['sort']['page'] = $request->getQuery('page', 'int');
     }
     if ($request->getQuery('perpage', 'int')) {
         $parameters['sort']['perpage'] = $request->getQuery('perpage', 'int');
     }
     // ----------------------------------------
     // Search
     if ($request->isPost()) {
         $parameters['search_cat'] = null;
         $parameters['search_virtcat'] = null;
         $parameters['search_maker'] = null;
         $parameters['search_material'] = null;
         $parameters['search_hold'] = null;
         $parameters['search_id'] = null;
         // Category
         if (!empty($_POST['search_cat'])) {
             $search_cat_get = $request->getPost('search_cat', 'int');
             $parameters['search_cat'] = $search_cat_get;
             $search_cats = PCategory::getCatTree($search_cat_get);
             $search_cat = " AND category_id IN ({$search_cats})";
         } else {
             $search_cat = null;
         }
         // Virtual category
         if (!empty($_POST['search_virtcat'])) {
             $search_virtcat_get = $request->getPost('search_virtcat', 'int');
             $parameters['search_virtcat'] = $search_virtcat_get;
             $search_virtcat = " AND tmaterial_id = {$search_virtcat_get}";
         } else {
             $search_virtcat = null;
         }
         // Maker
         if (!empty($_POST['search_maker'])) {
             $search_maker_get = $request->getPost('search_maker', 'int');
             $parameters['search_maker'] = $search_maker_get;
             $search_maker = " AND maker_id = {$search_maker_get}";
         } else {
             $search_maker = null;
         }
         // Material
         if (!empty($_POST['search_material'])) {
             $search_material_get = $request->getPost('search_material', 'int');
             $parameters['search_material'] = $search_material_get;
             $search_material = " AND cmaterial_id = {$search_material_get}";
         } else {
             $search_material = null;
         }
         // Hold
         if (!empty($_POST['search_hold'])) {
             $search_hold_get = $request->getPost('search_hold', 'int');
             $parameters['search_hold'] = $search_hold_get;
             $search_hold = $search_hold_get == 1 ? " AND hold = 1" : " AND hold = 0";
         } else {
             $search_hold = null;
         }
         // ID
         if (!empty($_POST['search_id'])) {
             $parameters['search_id'] = $request->getPost('search_id', 'string');
             $search_id_get = preg_split('/[,\\s]/', $request->getPost('search_id', 'string'), null, PREG_SPLIT_NO_EMPTY);
             $search_id = join(', ', $search_id_get);
             $search_id = " AND id IN ({$search_id})";
         } else {
             $search_id = null;
         }
         // Search parameters
         $parameters['qry'] = ['conditions' => "id IS NOT NULL\n                    {$search_cat}\n                    {$search_virtcat}\n                    {$search_maker}\n                    {$search_material}\n                    {$search_hold}\n                    {$search_id}"];
     }
     // Other parameters
     $parameters['query_count'] = PProductMain::count($parameters['qry']);
     $parameters['qry']['order'] = "id ASC";
     $parameters['qry']['limit'] = 10000;
     $persistent->productsParams = $parameters;
     //        var_dump($parameters);
     // Find all placements that correspond to set parameters
     $products = PProductMain::find($parameters['qry']);
     // Paginator
     $paginator = new Paginator(["data" => $products, "limit" => $parameters['sort']['perpage'], "page" => $parameters['sort']['page']]);
     $this->view->page = $paginator->getPaginate();
     // Set selected search values
     $this->view->selected_cat = isset($parameters['search_cat']) && !empty($parameters['search_cat']) ? $parameters['search_cat'] : null;
     $this->view->selected_virtcat = isset($parameters['search_virtcat']) && !empty($parameters['search_virtcat']) ? $parameters['search_virtcat'] : null;
     $this->view->selected_maker = isset($parameters['search_maker']) && !empty($parameters['search_maker']) ? $parameters['search_maker'] : null;
     $this->view->selected_material = isset($parameters['search_material']) && !empty($parameters['search_material']) ? $parameters['search_material'] : null;
     $this->view->selected_hold = isset($parameters['search_hold']) && !empty($parameters['search_hold']) ? $parameters['search_hold'] : null;
     $this->view->selected_id = isset($parameters['search_id']) && !empty($parameters['search_id']) ? $parameters['search_id'] : null;
     // Products count (overall or filtered)
     $this->view->products_count = $parameters['query_count'];
     // All categories
     $this->view->cats_list = PCategory::getCatList();
     // All virtual categories
     $virtcats = PCategoryGroup::find(['order' => 'title ASC']);
     foreach ($virtcats as $virtcat) {
         $virtcats_array[$virtcat->id] = $virtcat->title;
     }
     $this->view->virtcats_list = $virtcats_array;
     // All makers
     //        $makers = PMaker::find(["name IS NOT NULL AND isseller=1 AND approved=1 AND banned=0", 'order' => 'name ASC']);
     $makers = PMaker::find(["name IS NOT NULL AND isseller=1", 'order' => 'name ASC']);
     foreach ($makers as $maker) {
         $makers_array[$maker->id] = $maker->name;
     }
     $this->view->makers_list = $makers_array;
     // All materials
     $materials = PProdMaterial::find(['order' => 'title ASC']);
     foreach ($materials as $material) {
         $materials_array[$material->id] = $material->title;
     }
     $this->view->materials_list = $materials_array;
     // Langs stat
     $langs = PProdInfo::count(["coder_status = 5", 'group' => 'lang', 'order' => 'id']);
     foreach ($langs as $lang) {
         $langs_stat[$lang->lang] = $lang->rowcount;
     }
     $this->view->langs_stat = $langs_stat;
     // Check if user can put products on hold
     $this->view->can_hold = AccRoles::findFirst("user_id={$this->auth->id} AND (role_id=5 OR role_id=1000)");
 }
Пример #2
0
 public function productsAction($campaign_id = null, $show_campaign = null)
 {
     $request = $this->request;
     $response = $this->response;
     $persistent = $this->persistent;
     if ($campaign_id == null) {
         $response->redirect('adv/index');
     }
     // Find campaign info
     $this->view->campaign = $campaign = AdvCampaigns::findFirst("id={$campaign_id}");
     // Find all products for the specified campaign
     if ($campaign_products = AdvProducts::find("campaign_id={$campaign_id}")) {
         foreach ($campaign_products as $product) {
             $list_of_products[] = $product->product_id;
         }
     }
     // Campaign currency code
     $this->view->campaign_currencyCode = $campaign_currencyCode = $campaign->currencyCode != null ? $campaign->currencyCode : '$';
     // Currency rate for the campaign currency
     $this->view->curr_rate = $campaign->currencyCode != null ? PCrosscurrency::findFirst("title='{$campaign->currencyCode}'")->currencyrate : 1;
     $selected_products = isset($list_of_products) ? " AND id NOT IN (" . implode(',', $list_of_products) . ")" : null;
     // Set titles
     if ($campaign->type == 2) {
         Tag::setTitle('Catalog');
         $this->view->hdr = 'Каталог';
     } else {
         Tag::setTitle('Advertising campaign');
         $this->view->hdr = 'Рекламная кампания';
     }
     // Check if the tab is selected
     $this->view->show_campaign = $show_campaign == 'campaign' ? true : false;
     // Language
     $this->view->lang = $persistent->campaign_lang = $campaign_lang = $campaign->langcode;
     // Discount
     $this->view->discount = $campaign_discount = $campaign->discount;
     // Coefficient
     $this->view->coeff = $campaign_coeff = $campaign->coeff;
     // Setting product title and description according to campaign language
     //        $this->view->prod_title = $prod_title = ($campaign_lang == 'ru') ? "exporttitle" : "exporttitle_$campaign_lang";
     //        $this->view->prod_description = $prod_description = ($campaign_lang == 'ru') ? "exportabout" : "exportabout_$campaign_lang";
     // Reset
     $reset = $this->request->getQuery('reset', 'int');
     if (isset($reset)) {
         $persistent->searchAdvParams = null;
     }
     $persistent->last_added_product = '';
     // Persistent parameters
     if (!is_array($persistent->searchAdvParams)) {
         $persistent->searchAdvParams = ['qry' => ['conditions' => "hold = 0 AND lang = '{$campaign_lang}' AND coder_status = 5\n                        {$selected_products}\n                        {$persistent->last_added_product}", 'order' => 'rating desc'], 'sort' => ['page' => 1, 'perpage' => 20, 'sortField' => 'rating', 'sortOrder' => 'desc', 'cpage' => 1, 'cperpage' => 20]];
     }
     $parameters = $persistent->searchAdvParams;
     $parameters['sort']['perpage'] = isset($parameters['sort']['perpage']) ? $parameters['sort']['perpage'] : 20;
     $parameters['sort']['page'] = isset($parameters['sort']['page']) ? $parameters['sort']['page'] : 1;
     $parameters['sort']['cpage'] = isset($parameters['sort']['cpage']) ? $parameters['sort']['cpage'] : 1;
     $parameters['sort']['cperpage'] = isset($parameters['sort']['cperpage']) ? $parameters['sort']['cperpage'] : 20;
     if ($request->getQuery('sort', 'string')) {
         $parameters['sort']['sortField'] = $request->getQuery('sort', 'string');
         $parameters['sort']['sortOrder'] = $request->getQuery('order', 'string');
     }
     if ($request->getQuery('page', 'int')) {
         $parameters['sort']['page'] = $request->getQuery('page', 'int');
     }
     if ($request->getQuery('perpage', 'int')) {
         $parameters['sort']['perpage'] = $request->getQuery('perpage', 'int');
     }
     if ($request->getQuery('cpage', 'int')) {
         $parameters['sort']['cpage'] = $request->getQuery('cpage', 'int');
     }
     if ($request->getQuery('cperpage', 'int')) {
         $parameters['sort']['cperpage'] = $request->getQuery('cperpage', 'int');
     }
     $sortField = $parameters['sort']['sortField'];
     $sortOrder = $parameters['sort']['sortOrder'];
     $persistent->search_query = null;
     // If this is a post request
     if ($request->isPost()) {
         // Checks if a category was selected
         if (!empty($_POST['category_id'])) {
             $selected_cat = $request->getPost('category_id', 'int');
             // Find children categories
             $search_cats = PCategory::getCatTree($selected_cat, $campaign_lang);
             $persistent->search_cats = " AND category_id IN ({$search_cats})";
         } else {
             $persistent->search_cats = null;
         }
         // Checks if a virtual category was selected
         if (!empty($_POST['virtcat_id'])) {
             $sel_virtcat = $request->getPost('virtcat_id', 'int');
             // Find children categories
             $search_virtcat = " AND tmaterial_id = {$sel_virtcat}";
         } else {
             $search_virtcat = null;
         }
         // Checks if price range is set
         $price_from = !empty($_POST['price_from']) ? $request->getPost('price_from', 'float') : 0;
         $price_to = !empty($_POST['price_to']) ? $request->getPost('price_to', 'float') : 999999;
         // Checks if search request is set and whether the search is conducted by title or id
         if (!empty($_POST['search'])) {
             $search_get = $request->getPost('search', 'string');
             // If the search request consists of numbers, commas and whitespace only
             if (preg_match('/^[0-9]+/', $search_get)) {
                 $search_split = preg_split('/[,\\s]/', $search_get, 0, PREG_SPLIT_NO_EMPTY);
                 $search_request = implode(', ', $search_split);
                 $search_request = preg_replace('/[^\\d,]/', '', $search_request);
                 $search_request = " AND id IN ({$search_request})";
                 $persistent->search_query = $search_request;
             } else {
                 $search_request = " AND title LIKE '%{$search_get}%'";
                 $persistent->search_query = null;
             }
         } else {
             $search_request = null;
             $persistent->search_query = null;
         }
         // Search parameters
         $parameters['qry'] = ['conditions' => "hold = 0 AND lang = '{$campaign_lang}' AND coder_status = 5\n                    AND (price BETWEEN {$price_from} AND {$price_to})\n                    {$search_request}\n                    {$persistent->search_cats}\n                    {$search_virtcat}\n                    {$selected_products}\n                    {$persistent->last_added_product}"];
         // Count search results
         $parameters['search_query'] = $request->getPost('search');
         $parameters['category_id'] = $request->getPost('category_id');
         $parameters['virtcat_id'] = $request->getPost('virtcat_id');
         $parameters['price_from'] = $request->getPost('price_from');
         $parameters['price_to'] = $request->getPost('price_to');
         if (!empty($parameters['search_query']) || !empty($parameters['category_id']) || !empty($parameters['price_from']) || !empty($parameters['price_to'])) {
             $parameters['products_count_search'] = PSelection::count($parameters['qry']);
         } else {
             $parameters['products_count_search'] = null;
         }
     }
     // Other parameters
     //        $parameters['qry']['limit'] = $parameters['sort']['perpage'];
     //        $parameters['qry']['offset'] = ($parameters['sort']['page'] - 1) * $parameters['sort']['perpage'];
     $parameters['products_count'] = PSelection::count($parameters['qry']);
     $parameters['qry']['order'] = "{$sortField} {$sortOrder}";
     $parameters['qry']['limit'] = 1000;
     $persistent->searchAdvParams = $parameters;
     //        var_dump($parameters);
     // Find all products according to set parameters
     $products = PSelection::find($parameters['qry']);
     // Paginator
     $paginator = new Paginator(["data" => $products, "limit" => $parameters['sort']['perpage'], "page" => $parameters['sort']['page']]);
     // Sorting and table headers mapping
     $headMapping = ['title' => 'Название', 'intro' => 'Описание', '__category_id' => 'Категория', 'price' => "Цена, {$campaign_currencyCode}"];
     if ($campaign->showMakerPrice == 1) {
         $headMapping['__priceMaker'] = "Вход. цена, {$campaign->currencyCode}";
     }
     if ($campaign->showPriceDiscount == 1) {
         $headMapping['__priceDiscount'] = "Цена со<br> скидкой, {$campaign->currencyCode}";
     }
     if ($campaign->showCoeff == 1) {
         $headMapping['__priceCoeff'] = "Цена с<br> наценкой, {$campaign->currencyCode}";
     }
     if ($campaign->showMarkup == 1) {
         $headMapping['__markup'] = "Mark-up";
     }
     // Sorting header and order arrow
     $headings = array();
     foreach ($headMapping as $field => $name) {
         $headings[$field] = ['field' => $field, 'name' => $name, 'sort' => $sortField == $field ? $sortOrder == 'desc' ? 'asc' : 'desc' : false, 'arrow' => $sortField == $field ? $sortOrder == 'desc' ? '&nbsp;&#8595;' : '&nbsp;&#8593;' : false];
     }
     $this->view->headings = $headings;
     $this->view->page = $paginator->getPaginate();
     //        $this->view->total_pages = ceil(PSelection::count() / $parameters['sort']['perpage']);
     // Show products count
     $this->view->count = isset($parameters['products_count_search']) && $parameters['products_count_search'] != null ? "Товаров, отвечающих параметрам: {$parameters['products_count']}" : "Всего товаров: {$parameters['products_count']}";
     // Show products count in campaign
     $this->view->campaign_count = AdvProducts::count("campaign_id = {$campaign_id}");
     // ---------------------------------
     // Categories
     $this->view->categories = PCategory::getCatList($campaign_lang);
     // Virtual categories
     $this->view->virtcats = array_column(PCategoryGroup::find()->toArray(), 'title', 'id');
     // ---------------------------------
     // Campaign #
     // ---------------------------------
     // Campaign paginator
     //        $persistent->cpage = $cpage = $this->request->getQuery('cpage', 'int') ? $this->request->getQuery('cpage', 'int') : 1;
     $campaign_paginator = new Paginator(["data" => $campaign_products, "limit" => $parameters['sort']['cperpage'], "page" => $parameters['sort']['cpage']]);
     $this->view->campaign_page = $campaign_paginator->getPaginate();
     // Campaign variables
     $this->view->langs = $this->langs;
     $this->view->rnd = $campaign_currencyCode == 'UAH' || $campaign_currencyCode == 'RUB' ? 0 : 2;
     $this->view->currencies = $this->currencies;
     $this->view->category_id = !empty($parameters['category_id']) ? $parameters['category_id'] : false;
     $this->view->virtcat_id = !empty($parameters['virtcat_id']) ? $parameters['virtcat_id'] : false;
     $this->view->search_products = null != $persistent->search_query ? $parameters['search_query'] : false;
     $this->view->search_query = !empty($parameters['search_query']) ? $parameters['search_query'] : false;
     $this->view->price_from = !empty($parameters['price_from']) ? $parameters['price_from'] : false;
     $this->view->price_to = !empty($parameters['price_to']) ? $parameters['price_to'] : false;
 }