Esempio n. 1
0
 /**
  * Return data for table view 1s1b and 2s1b
  * @return json
  * @author Ruslan Ushakov
  */
 public function getRanking()
 {
     $this->load->model('ranking_model');
     $this->load->model('sites_model');
     $site_id = $this->input->post('site_id');
     $brand_id = $this->input->post('brand_id');
     $date_of_upload = $this->input->post('date_of_upload');
     $view = $this->input->post('view');
     $compare_site_id = $this->input->post('compare_site_id');
     $compare_date_of_upload = $this->input->post('compare_date_of_upload');
     $term_id = $this->input->post('term_id');
     $all_checked_brand_checkboxes = $this->input->post('all_checked_brand_checkboxes');
     $brand_id = Ranking_model::getBrandIdsFromSelectedGroups($brand_id, $all_checked_brand_checkboxes);
     /*get sorting params*/
     $sort_by = $this->input->post('sort_by');
     $sort_dir = $this->input->post('sort_dir');
     $first_time_flag = $this->input->post('first_time_flag');
     $all_checked_checkboxes = $this->input->post('all_checked_checkboxes');
     $product_lists = $this->input->post('product_lists');
     /*check sorting params*/
     if ($sort_dir != 'asc') {
         $sort_dir = 'desc';
     }
     //Issue #3042 Associate keywords/groups with selected brand
     if (!empty($brand_id)) {
         $this->setAssociatedBrands($brand_id);
         $this->ranking_model->setAssociatedKeywords($brand_id, $all_checked_checkboxes);
     }
     $data = array();
     if (!empty($view) && $view == '1s1b' && !empty($date_of_upload) && !empty($term_id) && !empty($site_id)) {
         if (!$brand_id && !$first_time_flag || $first_time_flag == 'false') {
             $sort_by = 'bsr';
         } elseif (!$first_time_flag || $first_time_flag == 'false') {
             $sort_by = 'ranking';
         } else {
             $sort_by = $this->getSortingField($sort_by, '1s1b1t');
             if (empty($sort_by)) {
                 $sort_by = 'ranking';
             }
         }
         if (is_array($date_of_upload) && count($date_of_upload) > 1) {
             $site = $this->sites_model->get($site_id);
             $summary_products_data = array();
             foreach ($date_of_upload as $date) {
                 $brands = $this->ranking_model->getBrandByIdsArray($brand_id);
                 if (!empty($brands)) {
                     foreach ($brands as $value) {
                         $brand_id = 0;
                         if (!empty($value->id)) {
                             $brand_id = $value->id;
                         }
                         $products = $this->ranking_model->getProductsByTerm($site_id, $date, $term_id, $sort_by, $sort_dir, $brand_id);
                         if ($sort_by == 'rsri.best_seller_ranking' && $sort_dir == 'asc') {
                             uasort($products, array($this, 'userSortByBestSellerAsc'));
                         } elseif ($sort_by == 'rsri.best_seller_ranking' && $sort_dir == 'desc') {
                             uasort($products, array($this, 'userSortByBestSellerDesc'));
                         }
                         if (!empty($products)) {
                             foreach ($products as &$product) {
                                 $product->related_products = $this->ranking_model->getRelatedProductByRsriId($product->rsri_id);
                             }
                         }
                         foreach ($products as $prod) {
                             if (!array_key_exists($prod->title, $summary_products_data)) {
                                 $summary_products_data[$prod->title] = array();
                                 $summary_products_data[$prod->title]['title'] = $prod->title;
                                 $summary_products_data[$prod->title]['url'] = $prod->url;
                             }
                             $summary_products_data[$prod->title][$date] = $prod;
                         }
                     }
                 } else {
                     $brand_id = 0;
                     $products = $this->ranking_model->getProductsByTerm($site_id, $date, $term_id, $sort_by, $sort_dir, $brand_id);
                     if ($sort_by == 'rsri.best_seller_ranking' && $sort_dir == 'asc') {
                         uasort($products, array($this, 'userSortByBestSellerAsc'));
                     } elseif ($sort_by == 'rsri.best_seller_ranking' && $sort_dir == 'desc') {
                         uasort($products, array($this, 'userSortByBestSellerDesc'));
                     }
                     if (!empty($products)) {
                         foreach ($products as &$product) {
                             $product->related_products = $this->ranking_model->getRelatedProductByRsriId($product->rsri_id);
                         }
                     }
                     foreach ($products as $prod) {
                         if (!array_key_exists($prod->title, $summary_products_data)) {
                             $summary_products_data[$prod->title] = array();
                             $summary_products_data[$prod->title]['title'] = $prod->title;
                             $summary_products_data[$prod->title]['url'] = $prod->url;
                         }
                         $summary_products_data[$prod->title][$date] = $prod;
                     }
                 }
             }
             $this->load->library('RankingTable');
             $extended_view = $view == '1s1b' ? '1s1b1r_md' : '1s1bpn_md';
             $columns = $this->rankingtable->getColumnSettings($extended_view);
             $site_name = !empty($site[0]) && !empty($site[0]->name) ? $site[0]->name : '';
             if (!is_array($brand_id)) {
                 $brand = $this->ranking_model->getBrandById($brand_id);
             } else {
                 $brand = $this->ranking_model->getBrandById($brand_id[0]);
             }
             $brand_img = !empty($brand->image_url) ? $brand->image_url : '';
             $table_body = $this->rankingtable->getBody($columns, $extended_view, $summary_products_data, $site_name, '', $date_of_upload, $sort_dir, $sort_by, 0, array(), $brand_img);
             $table_header = $this->rankingtable->getHeader($columns, $extended_view, $date_of_upload, $sort_dir, $sort_by, $site_name);
             $data['table'] = $this->load->view('assess/ranking_table_view_1s1b1r_md', array('products' => $summary_products_data, 'sort_by' => $sort_by, 'sort_dir' => $sort_dir, 'brand_image' => $brand_img, 'site' => $site, 'dates_of_upload' => $date_of_upload, 'table_header' => $table_header, 'table_body' => $table_body), true);
         } else {
             if (is_array($date_of_upload)) {
                 $date_of_upload = $date_of_upload[0];
             }
             $result = array();
             $limit = 30;
             $brands = $this->ranking_model->getBrandByIdsArray($brand_id);
             $summury_products = array();
             if (!empty($brands)) {
                 $ranking = $this->ranking_model->getRanking($site_id, $date_of_upload, $brand_id[0], 'rsri.title', 'asc', $term_id);
                 $this->getRankingResults($ranking, $result);
                 foreach ($brands as $value) {
                     $brand_id = 0;
                     if (!empty($value->id)) {
                         $brand_id = $value->id;
                     }
                     $products = $this->ranking_model->getProductsByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, $brand_id, $limit, true);
                     if (count($products) < $limit) {
                         $add_limit = $limit - count($products);
                         $products_add = $this->ranking_model->getProductsByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, $brand_id, $add_limit, false);
                         if (!empty($products_add)) {
                             $products = array_merge($products, $products_add);
                         }
                     }
                     $summury_products = array_merge($summury_products, $products);
                 }
             } else {
                 $brand_id = 0;
                 $ranking = $this->ranking_model->getRanking($site_id, $date_of_upload, $brand_id, 'rsri.title', 'asc', $term_id);
                 $this->getRankingResults($ranking, $result);
                 $products = $this->ranking_model->getProductsByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, $brand_id, $limit, true);
                 if (count($products) < $limit) {
                     $add_limit = $limit - count($products);
                     $products_add = $this->ranking_model->getProductsByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, $brand_id, $add_limit, false);
                     if (!empty($products_add)) {
                         $products = array_merge($products, $products_add);
                     }
                 }
                 $summury_products = array_merge($summury_products, $products);
             }
             $products = array_unique($summury_products, SORT_REGULAR);
             if ($sort_by == 'rsri.best_seller_ranking' && $sort_dir == 'asc') {
                 uasort($products, array($this, 'userSortByBestSellerAsc'));
             } elseif ($sort_by == 'rsri.best_seller_ranking' && $sort_dir == 'desc') {
                 uasort($products, array($this, 'userSortByBestSellerDesc'));
             }
             if (!empty($products)) {
                 foreach ($products as &$product) {
                     $product->related_products = $this->ranking_model->getRelatedProductByRsriId($product->rsri_id);
                 }
             }
             $this->load->library('RankingTable');
             $extended_view = $view == '1s1b' ? '1s1b1r' : '1s1bpn';
             $columns = $this->rankingtable->getColumnSettings($extended_view);
             if (!is_array($brand_id)) {
                 $brand = $this->ranking_model->getBrandById($brand_id);
             } else {
                 $brand = $this->ranking_model->getBrandById($brand_id[0]);
             }
             $brand_img = !empty($brand->image_url) ? $brand->image_url : '';
             $table_body = $this->rankingtable->getBody($columns, $extended_view, $products, '', '', array(), $sort_dir, $sort_by, 0, $result, $brand_img);
             $table_header = $this->rankingtable->getHeader($columns, $extended_view, $date_of_upload, $sort_dir, $sort_by);
             $data['table'] = $this->load->view('assess/ranking_table_view_1s1b1r', array('products' => $products, 'sort_by' => $sort_by, 'sort_dir' => $sort_dir, 'result' => $result, 'brand_image' => $brand_img, 'table_header' => $table_header, 'table_body' => $table_body, 'view' => $view), true);
         }
     } elseif ($view == '1s1bpn') {
         $sort_by = $this->getSortingField($sort_by, $view);
         if (empty($sort_by)) {
             $sort_by = 'rsri.title';
         }
         if (is_array($date_of_upload) && count($date_of_upload) > 1) {
             $summary_products_data = array();
             foreach ($date_of_upload as $date) {
                 $brand_id = empty($brand_id) ? 0 : $brand_id;
                 $products = $this->ranking_model->getSerchTermInTitleInfo($site_id, $date, $term_id, $sort_by, $sort_dir, $brand_id, $product_lists);
                 foreach ($products as $prod) {
                     if (!array_key_exists($prod->title, $summary_products_data)) {
                         $summary_products_data[$prod->title] = array();
                         $summary_products_data[$prod->title]['title'] = $prod->title;
                         $summary_products_data[$prod->title]['url'] = $prod->url;
                         $summary_products_data[$prod->title]['site_name'] = $prod->site_name;
                         $summary_products_data[$prod->title]['search_term'] = $prod->search_term;
                     }
                     $summary_products_data[$prod->title][$date] = $prod;
                 }
             }
             //sorting new data
             if ($sort_by == 'rsri.search_term_in_title' && $sort_dir == 'asc') {
                 uasort($summary_products_data, array($this, 'userSortBySearchTermInTitleAsc'));
             } elseif ($sort_by == 'rsri.search_term_in_title' && $sort_dir != 'asc') {
                 uasort($summary_products_data, array($this, 'userSortBySearchTermInTitleDesc'));
             } elseif ($sort_by != 'rsri.search_term_in_title' && $sort_dir == 'asc') {
                 uasort($summary_products_data, array($this, 'userSortByTitleAsc'));
             }
             $pager = $this->getPager(count($summary_products_data), 30);
             $resultsNumberString = $this->getResultsNumbersString($pager['total'], $pager['page']);
             $summary_products_data = array_slice($summary_products_data, $pager['offset'], $pager['length']);
             $this->load->library('RankingTable');
             $columns = $this->rankingtable->getColumnSettings('1s1bpn_md');
             $table_body = $this->rankingtable->getBody($columns, '1s1bpn_md', $summary_products_data, '', '', $date_of_upload, $sort_dir, $sort_by, 0, array(), '');
             $table_header = $this->rankingtable->getHeader($columns, '1s1bpn_md', $date_of_upload, $sort_dir, $sort_by, '');
             $data['table'] = $this->load->view('assess/ranking_table_query_product_match_view_md', array('pager' => $pager, 'dates_of_upload' => $date_of_upload, 'table_header' => $table_header, 'table_body' => $table_body, 'resultsNumberString' => $resultsNumberString), true);
         } else {
             $brand_id = empty($brand_id) ? 0 : $brand_id;
             $products = $this->ranking_model->getSerchTermInTitleInfo($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, $brand_id, $product_lists);
             //sorting new data
             if ($sort_by == 'rsri.search_term_in_title' && $sort_dir == 'asc') {
                 uasort($products, array($this, 'userSortBySearchTermInTitleAsc'));
             } elseif ($sort_by == 'rsri.search_term_in_title' && $sort_dir != 'asc') {
                 uasort($products, array($this, 'userSortBySearchTermInTitleDesc'));
             } elseif ($sort_by != 'rsri.search_term_in_title' && $sort_dir == 'asc') {
                 uasort($products, array($this, 'userSortByTitleAsc'));
             } else {
                 uasort($products, array($this, 'userSortByTitleDesc'));
             }
             $topResult = array();
             $topResult['exact'] = 0;
             $topResult['broad'] = 0;
             $topResult['no'] = 0;
             $topResult['all'] = 0;
             foreach ($products as $val) {
                 if (!empty($val) && !empty($val->search_term_in_title) && $val->search_term_in_title == 'exact') {
                     $topResult['exact']++;
                 } elseif (!empty($val) && !empty($val->search_term_in_title) && ($val->search_term_in_title == 'interleaved' || $val->search_term_in_title == 'partial')) {
                     $topResult['broad']++;
                 } else {
                     $topResult['no']++;
                 }
                 $topResult['all']++;
             }
             $pager = $this->getPager(count($products), 30);
             $resultsNumberString = $this->getResultsNumbersString($pager['total'], $pager['page']);
             $products = array_slice($products, $pager['offset'], $pager['length']);
             $this->load->library('RankingTable');
             $columns = $this->rankingtable->getColumnSettings($view);
             $table_body = $this->rankingtable->getBody($columns, $view, $products, '', '', array(), $sort_dir, $sort_by, 0, array(), '');
             $table_header = $this->rankingtable->getHeader($columns, $view, $date_of_upload, $sort_dir, $sort_by);
             $data['table'] = $this->load->view('assess/ranking_table_query_product_match_view', array('pager' => $pager, 'topResult' => $topResult, 'table_header' => $table_header, 'table_body' => $table_body, 'resultsNumberString' => $resultsNumberString), true);
         }
     } else {
         if (!empty($date_of_upload) && is_array($date_of_upload)) {
             $date_of_upload = $date_of_upload[0];
         }
         $sort_by = $this->getSortingField($sort_by, $view);
         if (empty($sort_by)) {
             $sort_by = 'stq.title';
         }
         $site_name = '';
         $site_compare_name = '';
         if (is_array($brand_id)) {
             $brands = $this->ranking_model->getBrandByIdsArray($brand_id);
             $data = array();
             if (!empty($brands)) {
                 foreach ($brands as $value) {
                     $brand_id = 0;
                     if (!empty($value->id)) {
                         $brand_id = $value->id;
                     }
                     $result = $this->ranking_model->getRanking($site_id, $date_of_upload, $brand_id, $sort_by, $sort_dir, $term_id);
                     $tmp_data = array();
                     $this->getRankingResults($result, $tmp_data);
                     foreach ($tmp_data as $k => $d) {
                         $key = $k . ' - ' . $value->name;
                         $data[$key] = $d;
                     }
                 }
             }
         } else {
             $result = $this->ranking_model->getRanking($site_id, $date_of_upload, $brand_id, $sort_by, $sort_dir, $term_id);
             $data = array();
             $this->getRankingResults($result, $data);
         }
         if ($sort_by == 'rsris.on_first_page' && $sort_dir == 'asc') {
             uasort($data, array($this, 'userSort'));
         }
         if (!empty($view) && $view == '2s1b') {
             $result = $this->ranking_model->getRanking($compare_site_id, $compare_date_of_upload, $brand_id, $sort_by, $sort_dir, $term_id);
             $this->getRankingResults($result, $data, '1');
             $this->load->model('sites_model');
             //get site name
             if ($site_id != $compare_site_id) {
                 $site = $this->sites_model->get($site_id);
                 if (!empty($site[0]->name)) {
                     $site_name = $site[0]->name;
                 }
                 $site_compare = $this->sites_model->get($compare_site_id);
                 if (!empty($site_compare[0]->name)) {
                     $site_compare_name = $site_compare[0]->name;
                 }
             } else {
                 $site_name = $date_of_upload;
                 $site_compare_name = $compare_date_of_upload;
             }
         }
         $this->load->library('RankingTable');
         $brand = $this->ranking_model->getBrandById($brand_id);
         $columns = $this->rankingtable->getColumnSettings($view, $brand->name);
         $brand_img = !empty($brand->image_url) ? $brand->image_url : '';
         $body_tmp = $this->rankingtable->getBody($columns, $view, $data, $site_name, $site_compare_name, array(), $sort_dir, $sort_by, 0, array(), $brand_img);
         $combined_data_header = $this->rankingtable->getHeader($columns, $view, array(), $sort_dir, $sort_by, '', $brand->name);
         $combined_data_body = (isset($combined_data_body) ? $combined_data_body : '') . "\n" . $body_tmp;
     }
     $data['table_body'] = $combined_data_body;
     $data['table_header'] = $combined_data_header;
     $this->output->set_content_type('application/json')->set_output(json_encode($data));
 }