/** * 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))); }