/** * @param $new_summary_data_array * @param $siteIds * @param $dates * @param $terms * @param $brandIds * @param $plistIds * * @return mixed */ private function populateShareOfShelfData(&$new_summary_data_array, $siteIds, $dates, $terms, $brandIds, $plistIds) { $termIds = []; foreach ($terms as $term) { $termIds[] = $term->id; if (!in_array($term->id, $new_summary_data_array['all_results_for_body']['search_terms_list'])) { $new_summary_data_array['all_results_for_body']['search_terms_list'][] = $term; } } foreach ($siteIds as $site) { $all_results_for_body = array(); $data_prepared = $this->ranking_model->getProductsByTerm($site, $dates, $termIds, 'rsri.ranking', 'asc', $brandIds, 0, false, $plistIds); $new_summary_data_array['curr_group_terms_count'] += Ranking_model::get_ofp_total_count($data_prepared, $all_results_for_body); foreach (['terms', 'brands', 'brand_terms'] as $fieldKey) { if (!empty($all_results_for_body[$fieldKey])) { foreach ($all_results_for_body[$fieldKey] as $key => $arfb) { if (!array_key_exists($key, $new_summary_data_array['all_results_for_body'][$fieldKey])) { $new_summary_data_array['all_results_for_body'][$fieldKey][$key] = 0; } $new_summary_data_array['all_results_for_body'][$fieldKey][$key] += $arfb; } } } foreach ($terms as $cur_term) { $group_name = !empty($cur_term->title) ? $cur_term->title : ''; $ofp_count = $this->ranking_model->getOfpCountByTerm($site, $dates, $cur_term->id, array(), $plistIds); if (!array_key_exists($group_name, $new_summary_data_array['all_results_for_body']['serch_terms_ofp'])) { $new_summary_data_array['all_results_for_body']['serch_terms_ofp'][$group_name] = 0; } $new_summary_data_array['all_results_for_body']['serch_terms_ofp'][$group_name] += $ofp_count; $new_summary_data_array['all_results_for_body']['serch_terms_ofp']['summary_ofp_count'] += $ofp_count; } } return $new_summary_data_array; }
private function calculate_findable_score($params) { $this->load->model('ranking_model'); $findable = 0; $all_results_for_body = array(); $data_prepared = $this->ranking_model->getProductsByTerm($params['rank_sites'], $params['rank_date'], $params['rank_terms'], 'rsri.ranking', 'asc', $params['rank_brand']); $summary_data = Ranking_model::get_ofp_total_count($data_prepared, $all_results_for_body); $possible_spot = 0; foreach ($params['rank_sites'] as $site) { foreach ($params['rank_terms'] as $cur_term) { $possible_spot += $this->ranking_model->getOfpCountByTerm($site, $params['rank_date'], $cur_term, array()); } } if (!empty($possible_spot)) { $findable = round($summary_data / $possible_spot * 100); } return $findable; }
public function GetTermRanking_post() { $this->load->model('ranking_model'); $this->load->model('sites_model'); $site_id = $this->input->post('site_id'); $term_id = $this->input->post('term_id'); $brand_id = $this->input->post('brand_id'); $date_of_upload = $this->input->post('date_of_upload'); $brands_group = (int) $this->input->post('group'); $view = $this->input->post('view'); $all_checked_checkboxes = $this->input->post('all_checked_checkboxes'); $all_checked_brand_checkboxes = $this->input->post('all_checked_brand_checkboxes'); $brand_id = Ranking_model::getBrandIdsFromSelectedGroups($brand_id, $all_checked_brand_checkboxes); $all_brands = $this->ranking_model->getBrandByIdsArray($brand_id); /*get sorting params*/ $sort_by = $this->input->post('sort_by'); $sort_dir = $this->input->post('sort_dir'); $brand_ofp_sorting = (int) $this->input->post('brand_ofp_sorting'); $sort_by_arr = explode(' ', $sort_by); $sort_by_num = -1; if (count($sort_by_arr) > 1) { $sort_by_num = (int) $sort_by_arr[1]; } /*check sorting params*/ if ($sort_dir != 'asc') { $sort_dir = 'desc'; } $sort_by = $this->getSortingField($sort_by, $view); if (empty($sort_by)) { $sort_by = 'b.name'; } if ($sort_by == 'rsri.ranking' && is_array($date_of_upload) && ($sort_by_num == -1 || $sort_by_num >= count($date_of_upload))) { $sort_by_num = count($date_of_upload) - 1; } $this->load->model('ranking_model'); $data = array(); //Issue #3042 Associate keywords/groups with selected brand if (!empty($brand_id)) { $this->ranking_model->setAssociatedKeywords($brand_id, $all_checked_checkboxes); } if (!empty($view) && $view == '1s1t') { $total_results = 0; if (is_array($date_of_upload)) { $date_of_upload = $date_of_upload[0]; } $total = $this->ranking_model->getRankingNamesByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, false, $brands_group, $brand_id); $pager = $this->getPager(count($total), 30); $resultsNumberString = $this->getResultsNumbersString($pager['total'], $pager['page']); $products_names = array_slice($total, $pager['offset'], $pager['length']); $names_array = array(); foreach ($products_names as $name) { if (!empty($name->name)) { $names_array[] = $name->name; } } $result = $this->ranking_model->getRankingByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, false, $brands_group, $brand_id, $names_array); foreach ($result as $item) { $item->name = strip_slashes($item->name); //remove slashes from brand name like "That\'s My Ticket" $data[$item->name]['total_results'] = $item->total_results; $total_results = $item->total_results; $data[$item->name]['brand_id'] = $item->brand_id; if (!isset($data[$item->name]['brand_results']) || isset($data[$item->name]['brand_results']) && $item->brand_results > $data[$item->name]['brand_results']) { $data[$item->name]['brand_results'] = $item->brand_results; } if (!isset($data[$item->name]['number_in_results'])) { $data[$item->name]['number_in_results'] = array(); } if (!isset($data[$item->name]['on_first_page']) || !empty($data[$item->name]['on_first_page']) && $item->on_first_page > $data[$item->name]['on_first_page']) { $data[$item->name]['on_first_page'] = $item->on_first_page; } $data[$item->name]['number_in_results'][$item->number_in_results] = array('num' => $item->number_in_results, 'url' => $item->url); } foreach ($data as &$dt) { if (empty($dt['on_first_page'])) { $dt['on_first_page'] = '0/16'; } if (!empty($dt['number_in_results'])) { $dt['number_in_results'] = $this->ranking_model->sortRankingNumbers($dt['number_in_results']); } else { $dt['tmp_number_in_results'] = array(); } } if ($sort_by == 'rsris.on_first_page' && $sort_dir == 'asc') { uasort($data, array($this, 'userSort')); } $this->load->library('RankingTable'); $columns = $this->rankingtable->getColumnSettings($view); $data['table_body'] = $this->rankingtable->getBody($columns, $view, $data); $data['table_header'] = $this->rankingtable->getHeader($columns, $view, array(), $sort_dir, $sort_by); $data['pager'] = $this->load->view('elements/pager_with_indicator_string', array('pager' => $pager, 'indicator_string' => $resultsNumberString), true); $data['brands_group_id'] = $brands_group; $data['total_results'] = $total_results; } elseif (!empty($view) && $view == '1ssv') { foreach ($all_checked_checkboxes as $key => $value) { if (!array_key_exists('id', $value)) { unset($all_checked_checkboxes[$key]); } } $total_results = 0; $tbl_body = ''; if (!is_array($date_of_upload)) { $date_of_upload = array($date_of_upload); } $summary_data = array(); foreach ($all_checked_checkboxes as $key => $value) { $gr_id = $key; if (!array_key_exists($gr_id, $summary_data)) { $summary_data[$gr_id] = array('curr_group_terms_count' => 0, 'curr_group_terms' => array(), 'all_results_for_body' => array('brands' => array(), 'terms' => array(), 'brand_terms' => array())); } $summary_data[$gr_id]['curr_group_terms'] = $this->ranking_model->getTermsIdsByGroupId($key); $current_group_ids = array(); foreach ($summary_data[$gr_id]['curr_group_terms'] as $term) { if (in_array($term->id, $term_id)) { $current_group_ids[] = $term->id; } } foreach ($date_of_upload as $date) { if (!empty($value['id'])) { $all_results_for_body = array(); $data_prepared = $this->ranking_model->getProductsByTerm($site_id, $date, $current_group_ids, 'rsri.ranking', 'asc', $brand_id); $summary_data[$gr_id]['curr_group_terms_count'] += Ranking_model::get_ofp_total_count($data_prepared, $all_results_for_body); if (!empty($all_results_for_body['terms'])) { foreach ($all_results_for_body['terms'] as $key => $arfb) { if (!array_key_exists($key, $summary_data[$gr_id]['all_results_for_body']['terms'])) { $summary_data[$gr_id]['all_results_for_body']['terms'][$key] = 0; } $summary_data[$gr_id]['all_results_for_body']['terms'][$key] += $arfb; } } if (!empty($all_results_for_body['brands'])) { foreach ($all_results_for_body['brands'] as $key => $arfb) { if (!array_key_exists($key, $summary_data[$gr_id]['all_results_for_body']['brands'])) { $summary_data[$gr_id]['all_results_for_body']['brands'][$key] = 0; } $summary_data[$gr_id]['all_results_for_body']['brands'][$key] += $arfb; } } if (!empty($all_results_for_body['brand_terms'])) { foreach ($all_results_for_body['brand_terms'] as $key => $arfb) { if (!array_key_exists($key, $summary_data[$gr_id]['all_results_for_body']['brand_terms'])) { $summary_data[$gr_id]['all_results_for_body']['brand_terms'][$key] = 0; } $summary_data[$gr_id]['all_results_for_body']['brand_terms'][$key] += $arfb; } } } } } //Issue #3732 get average number of page 1 search results $count = count($date_of_upload); if ($count > 1) { foreach ($summary_data as $group_key => &$group_data) { foreach ($group_data as $cur_key => &$cur_data) { if ($cur_key == 'curr_group_terms_count') { $cur_data = round($cur_data / $count); } elseif ($cur_key == 'all_results_for_body') { if (!empty($cur_data['terms'])) { foreach ($cur_data['terms'] as &$term_data) { $term_data = round($term_data / $count); } } if (!empty($cur_data['brands'])) { foreach ($cur_data['brands'] as &$brand_data) { $brand_data = round($brand_data / $count); } } if (!empty($cur_data['brand_terms'])) { foreach ($cur_data['brand_terms'] as &$brand_term_data) { $brand_term_data = round($brand_term_data / $count); } } } } } } $this->load->library('RankingTable'); $columns = $this->rankingtable->getColumnSettings($view); foreach ($summary_data as $key => $sdata) { if (!empty($sdata['curr_group_terms'])) { foreach ($sdata['curr_group_terms'] as $cur_term) { if (in_array($cur_term->id, $term_id)) { $group_name = !empty($cur_term->title) ? $cur_term->title : ''; $data['table_body'] = $this->rankingtable->getBody($columns, $view, array(), '', '', '', '', '', '', '', '', count($brand_id), $sdata['all_results_for_body'], $group_name, $all_brands, $cur_term->id); $tbl_body .= $data['table_body']; $data['table_header'] = $this->rankingtable->getHeader($columns, $view, array(), $sort_dir, $sort_by, '', $all_brands, array()); $data['brands_group_id'] = $brands_group; $data['total_results'] = $total_results; $data['table_body'] = $tbl_body; } } } } } elseif (!empty($view) && $view == '1srv') { if (!is_array($date_of_upload) || count($date_of_upload) == 1) { $data_prepared = $this->ranking_model->getProductsByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, $brand_id); $num_ofp = 0; /*Issue #2548*/ $brands_str = $this->get_brands_ofp_string($data_prepared, $brand_ofp_sorting, $num_ofp); /*End Issue #2548*/ $pager = $this->getPager(count($data_prepared), 30); $products = array_slice($data_prepared, $pager['offset'], $pager['length']); $resultsNumberString = $this->getResultsNumbersString($pager['total'], $pager['page']); $this->load->library('RankingTable'); $columns = $this->rankingtable->getColumnSettings($view); $data['table_body'] = $this->rankingtable->getBody($columns, $view, $products); $data['table_header'] = $this->rankingtable->getHeader($columns, $view, array(), $sort_dir, $sort_by); $data['brands_string'] = $brands_str; $data['pager'] = $this->load->view('elements/pager_with_indicator_string', array('pager' => $pager, 'indicator_string' => $resultsNumberString), true); $data['num_ofp'] = $num_ofp; } else { sort($date_of_upload); $this->load->model('sites_model'); $data_summary = array(); $site = $this->sites_model->get($site_id); /*sorting by ranking*/ $sorted_date_of_upload = array(); $tmp_date_of_upload = $date_of_upload; if ($sort_by_num && !empty($tmp_date_of_upload[$sort_by_num])) { $sorted_date_of_upload[] = $tmp_date_of_upload[$sort_by_num]; array_splice($tmp_date_of_upload, $sort_by_num, 1); } foreach ($tmp_date_of_upload as $date) { $sorted_date_of_upload[] = $date; } //$brand = $this->ranking_model->getBrandById($brand_id); // FIX for #2647 (multiple dates selected comment) //$sorted_date_of_upload = array_reverse($sorted_date_of_upload); foreach ($sorted_date_of_upload as $date) { $products = $this->ranking_model->getProductsByTerm($site_id, $date, $term_id, $sort_by, $sort_dir, $brand_id); foreach ($products as $prod) { if (!array_key_exists($prod->title, $data_summary)) { $data_summary[$prod->title] = array(); $data_summary[$prod->title]['title'] = $prod->title; $data_summary[$prod->title]['url'] = $prod->url; $data_summary[$prod->title]['keyword'] = $prod->keyword; } if (!array_key_exists($date, $data_summary[$prod->title])) { $data_summary[$prod->title][$date] = $prod; } } } $this->load->library('RankingTable'); $columns = $this->rankingtable->getColumnSettings('1srv_md'); $pager = $this->getPager(count($data_summary), 30); $resultsNumberString = $this->getResultsNumbersString($pager['total'], $pager['page']); $products = array_slice($data_summary, $pager['offset'], $pager['length']); $table_body = $this->rankingtable->getBody($columns, '1srv_md', $products, null, null, $date_of_upload, $sort_dir, $sort_by, $sort_by_num); $table_header = $this->rankingtable->getHeader($columns, '1srv_md', $date_of_upload, $sort_dir, $sort_by, $site); $data['table'] = $this->load->view('assess/ranking_table_view_1srv_md', array('products' => $products, 'sort_by' => $sort_by, 'sort_dir' => $sort_dir, 'dates_of_upload' => $date_of_upload, 'sort_by_num' => $sort_by_num, 'table_header' => $table_header, 'table_body' => $table_body, 'pager' => $this->load->view('elements/pager_with_indicator_string', array('pager' => $pager, 'indicator_string' => $resultsNumberString), true)), true); } } $this->response(array('products_table' => $data)); }
protected function getSummaryResultsTable($alert, $dashboard = false) { $site_id = !empty($this->options['rank_site']) ? $this->options['rank_site'] : array(); $brand_id = !empty($this->options['rank_brand']) ? $this->options['rank_brand'] : array(); $term_id = !empty($this->options['rank_term']) ? $this->options['rank_term'] : array(); $productList = !empty($alert->options_array['product_list']) ? $alert->options_array['product_list'] : false; $date_of_upload = is_array($this->getDateNew()) ? $this->getDateNew() : array($this->getDateNew()); // DashBoard if ($dashboard) { $date_of_upload = $this->date_from; } $tbl_body = ''; if (!empty($site_id) && !empty($brand_id) && !empty($term_id)) { $all_brands = $this->getRankingModel()->getBrandByIdsArray($brand_id); $summary_data = array('curr_group_terms_count' => 0, 'curr_group_terms' => array(), 'all_results_for_body' => array('brands' => array(), 'terms' => array(), 'brand_terms' => array())); $summary_data['curr_group_terms'] = $this->getRankingModel()->getKeywordsNamesByIds($term_id); foreach ($site_id as $site) { $all_results_for_body = array(); $data_prepared = $this->getRankingModel()->getProductsByTerm($site, $date_of_upload, $term_id, 'rsri.ranking', 'asc', $brand_id, 0, false, $productList); $summary_data['curr_group_terms_count'] += \Ranking_model::get_ofp_total_count($data_prepared, $all_results_for_body); if (!empty($all_results_for_body['terms'])) { foreach ($all_results_for_body['terms'] as $key => $arfb) { if (!array_key_exists($key, $summary_data['all_results_for_body']['terms'])) { $summary_data['all_results_for_body']['terms'][$key] = 0; } $summary_data['all_results_for_body']['terms'][$key] += $arfb; } } if (!empty($all_results_for_body['brands'])) { foreach ($all_results_for_body['brands'] as $key => $arfb) { if (!array_key_exists($key, $summary_data['all_results_for_body']['brands'])) { $summary_data['all_results_for_body']['brands'][$key] = 0; } $summary_data['all_results_for_body']['brands'][$key] += $arfb; } } if (!empty($all_results_for_body['brand_terms'])) { foreach ($all_results_for_body['brand_terms'] as $key => $arfb) { if (!array_key_exists($key, $summary_data['all_results_for_body']['brand_terms'])) { $summary_data['all_results_for_body']['brand_terms'][$key] = 0; } $summary_data['all_results_for_body']['brand_terms'][$key] += $arfb; } } } //selected brand $new_all_brands = array(); if (!empty($all_brands)) { $selected_key = -1; foreach ($all_brands as $key => $brand) { if (isset($alert->selected_brand) && $alert->selected_brand == $brand->id) { $selected_key = $key; $new_all_brands[] = $brand; break; } } if ($selected_key == -1) { $new_all_brands[] = $all_brands[0]; $selected_key = 0; } unset($all_brands[$selected_key]); } //get compare brand if (!empty($all_brands) && !empty($all_results_for_body['brands'])) { $max_percent_brand = 0; foreach ($all_brands as $key => $brand) { if (!empty($all_results_for_body['brands'][$brand->name])) { if (empty($all_brands[$max_percent_brand]) || empty($all_results_for_body['brands'][$all_brands[$max_percent_brand]->name]) || $all_results_for_body['brands'][$all_brands[$max_percent_brand]->name] < $all_results_for_body['brands'][$brand->name]) { $max_percent_brand = $key; } } } if (isset($all_brands[$max_percent_brand])) { $new_all_brands[] = $all_brands[$max_percent_brand]; } } $this->getCI()->load->library('RankingTable'); // mobile friendly alert templates issue#6157 //$columns = array('Search Terms'=>'true', 'Pg1 Results'=>'true', 'Brand Name'=>'true', 'Other'=>'true'); $columns = array('Search Terms' => 'true'); if (!empty($summary_data['curr_group_terms'])) { $summary_data['all_results_for_body']['search_terms_list'] = $summary_data['curr_group_terms']; // mobile friendly alert templates issue#6157 //$tbl_body .= '<tr>' . $this->getCI()->rankingtable->getHeader($columns, '1ssv', $date_of_upload, '', '', '', $new_all_brands, $summary_data['all_results_for_body'], true) . '</tr>'; $i = 0; $summary_data['all_results_for_body']['serch_terms_ofp'] = array('summary_ofp_count' => 0); foreach ($site_id as $site) { foreach ($summary_data['curr_group_terms'] as $cur_term) { if (in_array($cur_term->id, $term_id)) { $group_name = !empty($cur_term->title) ? $cur_term->title : ''; $ofp_count = $this->getCI()->ranking_model->getOfpCountByTerm($site, $date_of_upload, $cur_term->id, array()); if (!array_key_exists($group_name, $summary_data['all_results_for_body']['serch_terms_ofp'])) { $summary_data['all_results_for_body']['serch_terms_ofp'][$group_name] = 0; } $summary_data['all_results_for_body']['serch_terms_ofp'][$group_name] += $ofp_count; $summary_data['all_results_for_body']['serch_terms_ofp']['summary_ofp_count'] += $ofp_count; } } } $possible_spot = $summary_data['all_results_for_body']['serch_terms_ofp']['summary_ofp_count']; foreach ($new_all_brands as $key => &$value) { if (!empty($summary_data['all_results_for_body']['brands'])) { if (array_key_exists($value->name, $summary_data['all_results_for_body']['brands'])) { $res = round($summary_data['all_results_for_body']['brands'][$value->name] / $possible_spot * 100); } else { $res = 0; } } else { $res = 0; } $value->percent = $res; } // DashBoard if ($dashboard) { return $summary_data; } foreach ($summary_data['curr_group_terms'] as $cur_term) { if (in_array($cur_term->id, $term_id)) { $get_add_row = !$i; ++$i; $group_name = !empty($cur_term->title) ? $cur_term->title : ''; $row = $this->getCI()->rankingtable->getBody($columns, '1ssv', array(), $site_id, '', $date_of_upload, '', '', '', '', '', count($brand_id), $summary_data['all_results_for_body'], $group_name, $new_all_brands, $cur_term->id, true, $get_add_row); $tbl_body .= $row; } } } } return array('table' => $tbl_body, 'brands' => $new_all_brands); }
/** * Return data for ranking charts * @return json * @author Ruslan Ushakov */ public function get_raking_chart_data() { $this->load->model('ranking_model'); $this->load->model('product_model'); $this->load->model('sites_model'); $site_id = $this->input->post('site_id'); $group_id = (int) $this->input->post('group_id'); $term_id = $this->input->post('term_id'); $brand_id = $this->input->post('brand_id'); $brand_id = array_filter($brand_id); if (empty($brand_id)) { $this->load->model('scorecard_model'); $selected_brands = $this->scorecard_model->get_brands_by_search_terms($term_id, $site_id); foreach ($selected_brands as $val) { $brand_id[] = $val->id; } } $all_checked_brand_checkboxes = $this->input->post('all_checked_brand_checkboxes'); $brand_id = Ranking_model::getBrandIdsFromSelectedGroups($brand_id, $all_checked_brand_checkboxes); //if(!empty($brand_id) && is_array($brand_id)) $brand_id = $brand_id[0]; $view = $this->input->post('view'); $date_of_upload = $this->input->post('date_of_upload'); $chart_type = $this->input->post('chart_type'); $selected_products = $this->input->post('selected_products'); $compare_site_id = $this->input->post('compare_site_id'); $compare_date_of_upload = $this->input->post('compare_date_of_upload'); /*get sorting params*/ $sort_by = $this->input->post('sort_by'); $sort_dir = $this->input->post('sort_dir'); $all_checked_checkboxes = $this->input->post('all_checked_checkboxes'); $product_lists = $this->input->post('product_lists'); $term_arr = array(); if ($view == '1s1b' && !$term_id && $chart_type == 'on_first_page_chart') { $term_arr = $this->ranking_model->getSearchTerms($brand_id); } /*check sorting params*/ if ($sort_dir != 'asc') { //$sort_dir = 'desc'; } if ($chart_type == 'on_first_page_chart') { $view = '1s1t'; } $sort_by = $this->getSortingField($sort_by, $view); if (empty($sort_by)) { $sort_by = 'rsri.ranking'; } /*Chart pagination vars*/ $total = 0; $limit = 5; $page = (int) $this->input->post('page'); /*End chart pagination vars*/ $main_search_result = 'ok'; $count = 0; $data = array(); if ($chart_type == 'on_first_page_chart') { if (!$term_id && !empty($term_arr)) { $data['multiple_terms'] = array(); foreach ($term_arr as $term) { $result = $this->ranking_model->getRankingByTerm($site_id, $date_of_upload, $term->search_term_id, $sort_by, $sort_dir, true); $data_prepared = $this->ranking_model->prepareOnFirstPageRanking($result); $data['multiple_terms'][$term->title] = $data_prepared; if (empty($data_prepared)) { $count++; } } if ($count == count($term_arr)) { $main_search_result = '0'; } } elseif ($term_id) { if (is_array($date_of_upload)) { $date_of_upload = $date_of_upload[0]; } if (!empty($group_id)) { $result = $this->ranking_model->getRankingByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, false, $group_id); } else { $result = $this->ranking_model->getRankingByTerm($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, true); } $data = $this->ranking_model->prepareOnFirstPageRanking($result); } else { $data = $this->ranking_model->getOnFirstPageRanking($site_id, $date_of_upload, $term_id, $sort_by, $sort_dir, true); } } elseif ($chart_type == 'product_rankings_chart' && is_array($date_of_upload) && count($date_of_upload) > 1) { foreach ($selected_products as &$prod) { $prod = str_replace('& ', '& ', $prod); } if (empty($selected_products)) { $offset = $page * $limit; $offset_product_names = $this->ranking_model->getChartLimitProductNames($site_id, $date_of_upload, $brand_id, $term_id, $limit, $offset); foreach ($offset_product_names as $name) { $selected_products[] = $name->title; } $total = $this->ranking_model->getChartProductNamesTotal($site_id, $date_of_upload, $brand_id, $term_id); } $res = $this->ranking_model->getRankingInfoForCharts($selected_products, $date_of_upload, $brand_id, $term_id, $site_id); /*sorting received data*/ foreach ($res as $val) { if (!array_key_exists($val->title, $data)) { $data[$val->title] = array(); } $data[$val->title][$val->date_of_upload] = $val->ranking; } /*checking for all dates data was selected*/ /*foreach($data as &$dval){ foreach($date_of_upload as $date){ if(!isset($dval[$date])){ $dval[$date] = 0; } } ksort($dval); }*/ } elseif ($chart_type == 'product_name_view_chart') { $pnv_chart_view = $this->input->post('pnv_chart_view'); if (!empty($date_of_upload) && is_array($date_of_upload)) { $data = array(); if (count($date_of_upload) > 1) { foreach ($date_of_upload as $date) { $res = $this->ranking_model->getQueryProductMatch($site_id, $date, $term_id, $brand_id, $pnv_chart_view); if ($res == '0') { $main_search_result = '0'; } else { $data[$date] = $res; } } } else { $res = $this->ranking_model->getQueryProductMatch($site_id, $date_of_upload, $term_id, $brand_id, $pnv_chart_view); if ($res == '0') { $main_search_result = '0'; } else { $data = $res; } } } else { $res = $this->ranking_model->getQueryProductMatch($site_id, $date_of_upload, $term_id, $brand_id, $pnv_chart_view); if ($res == '0') { $main_search_result = '0'; } else { $data = $res; } } } elseif ($chart_type == 'summary_view_chart') { $brand_res = array(); $brand_res_by_site = array(); $categories = array(); foreach ($all_checked_checkboxes as $key => $value) { if (!array_key_exists('id', $value)) { unset($all_checked_checkboxes[$key]); } } $all_results_for_body = array('brands' => array(), 'terms' => array()); foreach ($all_checked_checkboxes as $key => $value) { $curr_group_terms = $this->ranking_model->getTermsIdsByGroupId($key); $current_group_ids = array(); foreach ($curr_group_terms as $term) { if (in_array($term->id, $term_id)) { $current_group_ids[] = $term->id; } } $gr_id = $key; if (!empty($value['id'])) { $curr_group_terms_count = 0; if (!is_array($site_id)) { $site_id = array($site_id); } foreach ($site_id as $site) { $site_obj = $this->sites_model->getSite($site); if (!empty($site_obj[0])) { $site_obj = $site_obj[0]; } $tmp_all_results_for_body = array(); $data_prepared = $this->ranking_model->getProductsByTerm($site, $date_of_upload, $current_group_ids, 'rsri.ranking', 'asc', $brand_id); $curr_group_terms_count = Ranking_model::get_ofp_total_count($data_prepared, $tmp_all_results_for_body); if (!empty($site_obj['name']) && !array_key_exists($site_obj['name'], $brand_res_by_site)) { $brand_res_by_site[$site_obj['name']] = array(); } if (!empty($tmp_all_results_for_body['brands'])) { foreach ($tmp_all_results_for_body['brands'] as $key => $arfb) { if (!array_key_exists($key, $all_results_for_body['brands'])) { $all_results_for_body['brands'][$key] = 0; } $all_results_for_body['brands'][$key] += $arfb; if (!array_key_exists($key, $brand_res_by_site[$site_obj['name']])) { $brand_res_by_site[$site_obj['name']][$key] = 0; } $brand_res_by_site[$site_obj['name']][$key] += $arfb; } } } } } $column_data = array(); $brands_count_by_site = array(); $brand_res = array(); if (!empty($brand_id)) { foreach ($brand_id as $bid) { $brand = $this->ranking_model->getBrandNameByBrandId($bid); if (!empty($brand->name)) { if (array_key_exists($brand->name, $all_results_for_body['brands'])) { $brand_res[] = array($brand->name, $all_results_for_body['brands'][$brand->name]); unset($all_results_for_body['brands'][$brand->name]); } else { $brand_res[] = array($brand->name, 0); } foreach ($site_id as $site) { $site_obj = $this->sites_model->getSite($site); if (!empty($site_obj[0])) { $site_obj = $site_obj[0]; } if (!empty($site_obj['name']) && !in_array($site_obj['name'], $categories)) { $categories[] = $site_obj['name']; } if (!array_key_exists($brand->name, $column_data)) { $column_data[$brand->name] = array(); } if (!empty($brand_res_by_site[$site_obj['name']][$brand->name])) { $column_data[$brand->name][] = $brand_res_by_site[$site_obj['name']][$brand->name]; } else { $column_data[$brand->name][] = 0; } //count all products on first page by site if (!array_key_exists($site_obj['name'], $brands_count_by_site)) { $brands_count_by_site[$site_obj['name']] = 0; } if (!empty($brand_res_by_site[$site_obj['name']][$brand->name])) { $brands_count_by_site[$site_obj['name']] += $brand_res_by_site[$site_obj['name']][$brand->name]; } } } } if ($this->reportsettings->getSetting('1ssv_sort', 'isEnabledOther')) { //Count other values $sum_other_count = 0; $column_data['Other'] = array(); if (!empty($site_id)) { foreach ($site_id as $siteId) { /** * For now this is the fastest solution to fix swap bug. * In future there should be site validation and common function that will return sites * in normal order. */ $site = $this->sites_model->getSite($siteId); //No comments... Why people use result_array() instead of row_array() ?!!!! $site = array_shift($site); $possible_spot = 0; foreach ($term_id as $cur_term) { $possible_spot += $this->ranking_model->getOfpCountByTerm($site['id'], $date_of_upload, $cur_term); } $brands_count = !empty($brands_count_by_site[$site['name']]) ? $brands_count_by_site[$site['name']] : 0; $other_count = $possible_spot - $brands_count; $column_data['Other'][] = $other_count; $sum_other_count += $other_count; } } $brand_res[] = array('Other', $sum_other_count); } } $data['brand_res'] = $brand_res; $data['categories'] = $categories; $data['column_data'] = $column_data; if (empty($brand_res)) { $main_search_result = '0'; } else { $total = 1; } } elseif ($chart_type == 'pricing_view_chart') { $data = array(); $total = 1; foreach ($date_of_upload as $date) { $data[$date] = array(); $data[$date]['date_str'] = date('F j, Y', strtotime($date)); $categories = array(); $series = array(); $data_prepared = array(); $userData = array('sites' => array()); $currencies = array(); if (!empty($site_id) && count($site_id) > 1) { foreach ($site_id as $site) { $titles_list = $this->ranking_model->getAssociatedProductsByParams($site, $date, $term_id, $brand_id, $product_lists, $sort_by, $sort_dir, $selected_products); $site_obj = $this->sites_model->getSite($site); if (!empty($site_obj[0])) { $site_obj = $site_obj[0]; } if (!empty($site_obj['name']) && !in_array($site_obj['name'], $categories)) { $categories[] = $site_obj['name']; $img = Ranking_model::getSiteLogoByName($site_obj['name']); $site_logo = ''; if (!empty($img)) { $site_logo = base_url('/img/site_logos/' . $img); } $userData['sites'][$site_obj['name']] = $site_logo; foreach ($titles_list as $product) { if (!empty($product->title) && !array_key_exists($product->title, $data_prepared)) { $data_prepared[$product->title] = array(); $data_prepared[$product->title]['name'] = $product->title; $data_prepared[$product->title]['data'] = array(); } if (!array_key_exists($site_obj['name'], $data_prepared[$product->title]['data'][$site_obj['name']])) { $data_prepared[$product->title]['data'][$site_obj['name']] = $product; } $associatedProducts = $this->ranking_model->getAssociatedProducts($product); if (!empty($associatedProducts)) { $data_prepared[$product->title]['data'] = array_merge($data_prepared[$product->title]['data'], $associatedProducts); } } } } } $series_count = 0; foreach ($data_prepared as $dp) { $series[$series_count] = array('name' => $dp['name'], 'data' => array()); foreach ($categories as $key => $category) { if (!empty($dp['data'][$category])) { $series[$series_count]['data'][$key]['y'] = (double) $dp['data'][$category]->price; $series[$series_count]['data'][$key]['image_url'] = $dp['data'][$category]->image_url; $currency_symbol = Ranking_model::getCurrencySymbol($dp['data'][$category]->currency); if (empty($currency_symbol)) { $currency_symbol = '$'; } if (!in_array($currency_symbol, $currencies)) { $currencies[] = $currency_symbol; } $series[$series_count]['data'][$key]['currency_symbol'] = $currency_symbol; } else { $series[$series_count]['data'][$key] = array('y' => 0, 'image_url' => ''); } } ++$series_count; } $userData['currency_str'] = !empty($currencies) ? implode('/', $currencies) : '$'; $userData['counts'] = $series_count * count($site_id); $data[$date]['categories'] = $categories; $data[$date]['series'] = $series; $data[$date]['userData'] = $userData; } } if (empty($data) || $total == 0) { $main_search_result = '0'; } $this->output->set_content_type('application/json')->set_output(json_encode(array('result' => $main_search_result, 'data' => $data, 'date_of_upload' => $date_of_upload, 'total' => $total, 'page' => $page))); }
/** * Return data for PG 1 Share of Shelf View * @param array $filter * @return array * @author Ruslan Ushakov */ protected function _getSummaryTableByFilter($filter) { $data = array(); $total_results = 0; $tbl_body = ''; $this->load->model('ranking_model'); $this->load->model('sites_model'); $this->load->model('user_groups_model'); $this->load->library('RankingTable'); $new_summary_data_array = array('curr_group_terms_count' => 0, 'all_results_for_body' => array('brands' => array(), 'terms' => array(), 'brand_terms' => array(), 'search_terms_list' => array())); foreach ($filter['all_checked_checkboxes'] as $key => $value) { $gr_id = $key; $curr_group_terms = $this->ranking_model->getTermsIdsByGroupId($key); $current_group_ids = array(); foreach ($curr_group_terms as $term) { if (in_array($term->id, $filter['term_id'])) { $current_group_ids[] = $term->id; if (!in_array($term->id, $new_summary_data_array['all_results_for_body']['search_terms_list'])) { $new_summary_data_array['all_results_for_body']['search_terms_list'][] = $term; } } } foreach ($filter['site_id'] as $site) { if (!empty($value['id'])) { $all_results_for_body = array(); $data_prepared = $this->ranking_model->getProductsByTerm($site, $filter['date_of_upload'], $current_group_ids, 'rsri.ranking', 'asc', $filter['brand_id'], 0, false, $filter['product_lists']); $new_summary_data_array['curr_group_terms_count'] += Ranking_model::get_ofp_total_count($data_prepared, $all_results_for_body); if (!empty($all_results_for_body['terms'])) { foreach ($all_results_for_body['terms'] as $key => $arfb) { if (!array_key_exists($key, $new_summary_data_array['all_results_for_body']['terms'])) { $new_summary_data_array['all_results_for_body']['terms'][$key] = 0; } $new_summary_data_array['all_results_for_body']['terms'][$key] += $arfb; } } if (!empty($all_results_for_body['brands'])) { foreach ($all_results_for_body['brands'] as $key => $arfb) { if (!array_key_exists($key, $new_summary_data_array['all_results_for_body']['brands'])) { $new_summary_data_array['all_results_for_body']['brands'][$key] = 0; } $new_summary_data_array['all_results_for_body']['brands'][$key] += $arfb; } } if (!empty($all_results_for_body['brand_terms'])) { foreach ($all_results_for_body['brand_terms'] as $key => $arfb) { if (!array_key_exists($key, $new_summary_data_array['all_results_for_body']['brand_terms'])) { $new_summary_data_array['all_results_for_body']['brand_terms'][$key] = 0; } $new_summary_data_array['all_results_for_body']['brand_terms'][$key] += $arfb; } } } } } $brandResults = $new_summary_data_array['all_results_for_body']['brands']; arsort($brandResults); $sortedBrands = []; foreach ($brandResults as $brand => $val) { foreach ($filter['all_brands1'] as $key => $stdBrand) { if ($stdBrand->name == $brand) { $sortedBrands[] = $stdBrand; unset($filter['all_brands1'][$key]); break; } } } $filter['all_brands1'] = array_merge($sortedBrands, $filter['all_brands1']); $columns = $this->rankingtable->getColumnSettings($filter['view']); if (!empty($new_summary_data_array['all_results_for_body']['search_terms_list'])) { $new_summary_data_array['all_results_for_body']['serch_terms_ofp'] = array('summary_ofp_count' => 0); foreach ($filter['site_id'] as $site) { foreach ($new_summary_data_array['all_results_for_body']['search_terms_list'] as $cur_term) { $group_name = !empty($cur_term->title) ? $cur_term->title : ''; $ofp_count = $this->ranking_model->getOfpCountByTerm($site, $filter['date_of_upload'], $cur_term->id, array(), $filter['product_lists']); if (!array_key_exists($group_name, $new_summary_data_array['all_results_for_body']['serch_terms_ofp'])) { $new_summary_data_array['all_results_for_body']['serch_terms_ofp'][$group_name] = 0; } $new_summary_data_array['all_results_for_body']['serch_terms_ofp'][$group_name] += $ofp_count; $new_summary_data_array['all_results_for_body']['serch_terms_ofp']['summary_ofp_count'] += $ofp_count; } } $rowcount = 0; foreach ($new_summary_data_array['all_results_for_body']['search_terms_list'] as $cur_term) { $get_add_row = !$rowcount; ++$rowcount; $group_name = !empty($cur_term->title) ? $cur_term->title : ''; $data['table_body'] = $this->rankingtable->getBody($columns, $filter['view'], array(), '', '', $filter['date_of_upload'], '', '', '', '', '', count($filter['brand_id']), $new_summary_data_array['all_results_for_body'], $group_name, $filter['all_brands1'], $cur_term->id, false, $get_add_row, $rowcount); $tbl_body .= $data['table_body']; if (empty($data['table_header'])) { $data['table_header'] = $this->rankingtable->getHeader($columns, $filter['view'], $filter['date_of_upload'], $filter['sort_dir'], $filter['sort_by'], '', $filter['all_brands1'], $new_summary_data_array['all_results_for_body']); } $data['table_body'] = $tbl_body; } $data['brands_group_id'] = $filter['brands_group']; $data['total_results'] = $total_results; $data['table'] = '<tr class="dark-row" style="height:40px;">' . $data['table_header'] . ' </tr>' . $data['table_body']; if (count($filter['all_brands1']) > 3) { $data['table'] .= '<tr><td style="border: 0;border-top: 1px solid #ddd;height:30px;"></td><td style="border: 0;border-top: 1px solid #ddd;height:30px;"></td></tr>'; } unset($data['table_header']); unset($data['table_body']); } else { $data['table'] = ''; } return $data; }