public function getRankingCity($country, $page) { $var_cache = 'rankingCities_' . $country . '_' . ($page - 1) * 20; if (!Cache::has($var_cache)) { $ranking = FacebookCity::take(20)->skip(($page - 1) * 20)->orderBy('total_user', 'DESC'); if ($country != 'world') { $ranking = $ranking->whereCountryCode(FacebookCountry::whereNameEn($country)->first(['code'])->code); } $ranking = $ranking->get(['id_city', 'name', 'country_code', 'total_user', 'total_female', 'total_male', 'grow_90']); $cities = array(); foreach ($ranking as $key => $value) { $cache = NULL; $cache['position'] = ($page - 1) * 20 + $key + 1; $array_name = explode(',', $value['name']); $cache['name'] = $array_name[0]; $cache['total_user'] = $this->owloo_number_format($value['total_user']); $cache['total_female'] = array('value' => $this->owloo_number_format($value['total_female']), 'percent' => $this->owlooFormatPorcent($value['total_female'], $value['total_user'])); $cache['total_male'] = array('value' => $this->owloo_number_format($value['total_male']), 'percent' => $this->owlooFormatPorcent($value['total_male'], $value['total_user'])); $cache['grow_90'] = $this->formatGrow($value['grow_90'], $value['total_user']); $data_country = FacebookCountry::whereCode($value['country_code'])->first(['name_en']); $cache['country_code'] = strtolower($value['country_code']); $cache['country_url_name'] = $this->convert_string_to_url($data_country['name_en']); if ($country != 'world') { /***** Ages *****/ $cache['ages']['items'] = array(); $ages = FacebookCityAge::whereIdCity($value['id_city'])->orderBy('total_user', 'DESC')->take(5)->get(['name', 'total_user']); foreach ($ages as $age) { $cache['ages']['items'][] = array('name' => $age['name'], 'total_user' => $this->owloo_number_format($age['total_user'])); } //Get max value $cache['ages']['max_user'] = array(); $age_max_total = FacebookCityAge::whereIdCity($value['id_city'])->orderBy('total_user', 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_user']); if (isset($age_max_total['name'])) { $cache['ages']['max_user'] = array('name' => $age_max_total['name'], 'value' => $this->owloo_number_format($age_max_total['total_user']), 'percent' => $this->owlooFormatPorcent($age_max_total['total_user'], $value['total_user'])); } /***** Relationships *****/ $cache['relationships']['items'] = array(); $relationships = FacebookCityRelationship::whereIdCity($value['id_city'])->orderBy('total_user', 'DESC')->take(5)->get(['name', 'total_user']); foreach ($relationships as $relationship) { $cache['relationships']['items'][] = array('name' => $relationship['name'], 'total_user' => $this->owloo_number_format($relationship['total_user'])); } /***** Interests *****/ $cache['interests']['items'] = array(); $interests = FacebookCityInterest::whereIdCity($value['id_city'])->orderBy('total_user', 'DESC')->take(5)->get(['name', 'total_user']); foreach ($interests as $interest) { $cache['interests']['items'][] = array('name' => $interest['name'], 'total_user' => $this->owloo_number_format($interest['total_user'])); } //Get the Trend-up $cache['interests']['trend_up'] = array(); $interest_grow = FacebookCityInterest::whereIdCity($value['id_city'])->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']); if (isset($interest_grow['name']) && $interest_grow['grow_30'] > 0) { $cache['interests']['trend_up'] = array('name' => $interest_grow['name'], 'grow' => $this->formatGrow($interest_grow['grow_30'], $interest_grow['total_user'])); } /***** Comportamientos *****/ $cache['comportamientos']['items'] = array(); $comportamientos = FacebookCityComportamiento::whereIdCity($value['id_city'])->orderBy('total_user', 'DESC')->take(5)->get(['name', 'total_user']); foreach ($comportamientos as $comportamiento) { $cache['comportamientos']['items'][] = array('name' => $comportamiento['name'], 'percent' => $this->owlooFormatPorcent($comportamiento['total_user'], $value['total_user'])); } } $cities[] = $cache; } Cache::put($var_cache, $cities, 1440); } return Cache::get($var_cache); }
public function getRankingCity($country, $page) { $country = strtolower($country); $var_cache = 'rankingCities_' . $country . '_' . ($page - 1) * 20; if (!Cache::has($var_cache)) { //if (true) { $ranking = FacebookCity::take(20)->skip(($page - 1) * 20)->orderBy('total_user', 'DESC'); switch ($country) { case 'world': break; case 'hispanic': $ranking->whereIdiom('es'); break; default: $country_idiom = FacebookCountry::whereIdiom($country)->first(); if ($country_idiom) { $ranking->whereIdiom($country); } else { $country_search = FacebookCountry::whereSlug($country)->first(['code']); if ($country_search) { $ranking->whereCountryCode($country_search->code); } else { return 'Invalid method'; } } } $ranking = $ranking->get(['id_city', 'name', 'country_code', 'total_user', 'total_female', 'total_male', 'grow_90']); $cities = array(); foreach ($ranking as $key => $value) { $cache = NULL; $cache['position'] = ($page - 1) * 20 + $key + 1; $cache['id'] = $value['id_city']; $array_name = explode(',', $value['name']); $cache['name'] = $array_name[0]; $data_country = FacebookCountry::whereCode($value['country_code'])->first(['slug']); $cache['country_code'] = strtolower($value['country_code']); $cache['country_slug'] = $data_country['slug']; //Mes $aux_num = $this->formatGrow($value['grow_90'], $value['total_user']); $cache['second_column'][] = array('value' => $aux_num['value'], 'class' => $aux_num['class']); //Total usuarios $cache['second_column'][] = array('value' => $this->owloo_number_format($value['total_user']), 'class' => ''); $cities[] = $cache; } $second_column = array('País', 'Mes', 'Total usuarios'); $array_result = array('type' => 'fb_country', 'subtype' => 'fb_city', 'main_column' => 'Ciudad', 'second_column' => $second_column, 'large_column' => 4, 'link' => 'facebook-stats/cities', 'items' => $cities); Cache::put($var_cache, $array_result, 1440); } return Cache::get($var_cache); }