public function getCountry($country_name)
 {
     $var_cache = 'facebookCountry' . str_replace('-', '_', $country_name);
     //if (!Cache::has($var_cache)) {
     if (true) {
         $data = FacebookCountry::whereSlug($country_name)->first();
         if ($data) {
             $country['id_country'] = $data['id_country'];
             $country['code'] = strtolower($data['code']);
             $country['name'] = $data['name'];
             $country['abbreviation'] = $data['abbreviation'];
             $country['slug'] = $data['slug'];
             $country['total_user'] = $this->owloo_number_format($data['total_user']);
             $country['total_user_str'] = $this->owloo_number_format_str($data['total_user']);
             $country['total_female'] = array('value' => $this->owloo_number_format($data['total_female']), 'value_str' => $this->owloo_number_format_str($data['total_female']), 'percent' => $this->owlooFormatPorcent($data['total_female'], $data['total_user']));
             $country['total_male'] = array('value' => $this->owloo_number_format($data['total_male']), 'value_str' => $this->owloo_number_format_str($data['total_male']), 'percent' => $this->owlooFormatPorcent($data['total_male'], $data['total_user']));
             $country['audience_history'] = $this->owloo_chart_data_format($data['audience_history']);
             $country['audience_grow']['grow_90'] = $this->formatGrow($data['audience_grow_90'], $data['total_user']);
             $country['audience_grow']['grow_180'] = $this->formatGrow($data['audience_grow_180'], $data['total_user']);
             $country['audience_grow']['grow_270'] = $this->formatGrow($data['audience_grow_270'], $data['total_user']);
             $country['audience_grow']['grow_360'] = $this->formatGrow($data['audience_grow_360'], $data['total_user']);
             $country['audience_down_360'] = $this->formatGrow($data['audience_down_360'], $data['total_user']);
             $country['general_ranking'] = $data['general_ranking'];
             $country['trends'] = array();
             /***** Cities *****/
             $country['cities']['supports'] = $data['supports_city'] ? true : false;
             $country['cities']['items'] = array();
             $cities = FacebookCity::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['id_city', 'name', 'total_user']);
             foreach ($cities as $city) {
                 $country['cities']['items'][] = array('id' => $city['id_city'], 'name' => $city['name'], 'total_user' => $this->owloo_number_format($city['total_user']), 'percent' => $this->owlooFormatPorcent($city['total_user'], $data['total_user']));
             }
             /***** Regions *****/
             $country['regions']['supports'] = $data['supports_region'] ? true : false;
             $country['regions']['items'] = array();
             $regions = FacebookRegion::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['id_region', 'name', 'total_user']);
             foreach ($regions as $region) {
                 $country['regions']['items'][] = array('id' => $region['id_region'], 'name' => $region['name'], 'total_user' => $this->owloo_number_format($region['total_user']), 'percent' => $this->owlooFormatPorcent($region['total_user'], $data['total_user']));
             }
             /***** Ages *****/
             $country['ages']['has_more'] = true;
             $country['ages']['items'] = array();
             $ages = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             foreach ($ages as $age) {
                 $country['ages']['items'][] = array('name' => $age['name'], 'total_user' => $this->owloo_number_format($age['total_user']), 'percent' => $this->owlooFormatPorcent($age['total_user'], $data['total_user']), 'more' => array('female' => $this->owloo_number_format($age['total_female']), 'male' => $this->owloo_number_format($age['total_male']), 'grow_30' => $this->formatGrow($age['grow_30'], $age['total_user'])));
             }
             //Get max values
             foreach (array('user') as $value) {
                 $country['ages']['max_' . $value] = array();
                 $age_max_total = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_' . $value, 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_' . $value]);
                 if (isset($age_max_total['name'])) {
                     $country['ages']['max_' . $value] = array('name' => $age_max_total['name'], 'value' => $this->owloo_number_format($age_max_total['total_' . $value]), 'percent' => $this->owlooFormatPorcent($age_max_total['total_' . $value], $data['total_user']));
                 }
             }
             //Get Trend-up
             $trend_up = array();
             $age_grow = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_user', 'grow_30']);
             if (isset($age_grow['name']) && $age_grow['grow_30'] > 0) {
                 $trend_up = array('name' => $age_grow['name'], 'grow' => $this->formatGrow($age_grow['grow_30'], $age_grow['total_user']));
             }
             $country['trends']['age'] = $trend_up;
             /***** Languages *****/
             $country['languages']['has_more'] = false;
             $country['languages']['items'] = array();
             $languages = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'grow_30']);
             foreach ($languages as $language) {
                 $country['languages']['items'][] = array('name' => $language['name'], 'total_user' => $this->owloo_number_format($language['total_user']), 'percent' => $this->owlooFormatPorcent($language['total_user'], $data['total_user']));
             }
             //Get max values
             foreach (array('user') as $value) {
                 $country['languages']['max_' . $value] = array();
                 $language_max_total = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('total_' . $value, 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_' . $value]);
                 if (isset($language_max_total['name'])) {
                     $country['languages']['max_' . $value] = array('name' => $language_max_total['name'], 'value' => $this->owloo_number_format($language_max_total['total_' . $value]), 'percent' => $this->owlooFormatPorcent($language_max_total['total_' . $value], $data['total_user']));
                 }
             }
             //Get the Trend-up
             $trend_up = array();
             $language_grow = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($language_grow['name']) && $language_grow['grow_30'] > 0) {
                 $trend_up = array('name' => $language_grow['name'], 'grow' => $this->formatGrow($language_grow['grow_30'], $language_grow['total_user']));
             }
             $country['trends']['language'] = $trend_up;
             /***** Gender trend *****/
             $trend_up = array();
             if ($data['audience_female_grow_30'] > 0 || $data['audience_male_grow_30'] > 0) {
                 if ($data['audience_male_grow_30'] > $data['audience_female_grow_30']) {
                     $trend_up = array('name' => 'Hombres', 'grow' => $this->formatGrow($data['audience_male_grow_30'], $data['total_male']));
                 } else {
                     $trend_up = array('name' => 'Mujeres', 'grow' => $this->formatGrow($data['audience_female_grow_30'], $data['total_female']));
                 }
             }
             $country['trends']['gender'] = $trend_up;
             /***** Relationships *****/
             $country['relationships']['has_more'] = true;
             $country['relationships']['items'] = array();
             $relationships = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_female', 'total_male', 'total_user_grow_30']);
             foreach ($relationships as $relationship) {
                 $country['relationships']['items'][] = array('name' => $relationship['name'], 'total_user' => $this->owloo_number_format($relationship['total_user']), 'percent' => $this->owlooFormatPorcent($relationship['total_user'], $data['total_user']), 'more' => array('female' => $this->owloo_number_format($relationship['total_female']), 'male' => $this->owloo_number_format($relationship['total_male']), 'grow_30' => $this->formatGrow($relationship['total_user_grow_30'], $relationship['total_user'])));
             }
             //Get Trend-up
             foreach (array('user', 'female', 'male') as $value) {
                 $trend_relationships[$value] = array();
                 $relationship_grow = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_' . $value . '_grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_' . $value, 'total_' . $value . '_grow_30']);
                 if (isset($relationship_grow['name']) && $relationship_grow['total_' . $value . '_grow_30'] > 0) {
                     $trend_relationships[$value] = array('name' => $relationship_grow['name'], 'grow' => $this->formatGrow($relationship_grow['total_' . $value . '_grow_30'], $relationship_grow['total_' . $value]));
                 }
             }
             $country['trends']['female_relationship'] = $trend_relationships['female'];
             $country['trends']['male_relationship'] = $trend_relationships['male'];
             /***** Interests *****/
             $country['interests']['items'] = array();
             $interests = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id', 'name', 'total_user', 'grow_7']);
             foreach ($interests as $interest) {
                 $country['interests']['items'][] = array('id' => $interest['id'], 'name' => $interest['name'], 'grow_7' => $this->formatGrow($interest['grow_7'], $interest['total_user']), 'total_user' => $this->owloo_number_format($interest['total_user']), 'percent' => $this->owlooFormatPorcent($interest['total_user'], $data['total_user']));
             }
             //Get the Trend-up
             $country['interests']['trend_up'] = array();
             foreach (array(1, 3, 7, 15, 30) as $days) {
                 $country['interests']['trend_up']['grow_' . $days] = array();
                 $interest_grow = FacebookCountryInterest::whereCountryCode($country['code'])->orderBy('grow_' . $days, 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_' . $days]);
                 $time = NULL;
                 switch ($days) {
                     case 1:
                         $time = '24 hours';
                         break;
                     case 3:
                         $time = '72 hours';
                         break;
                     default:
                         $time = $days . ' days';
                         break;
                 }
                 if (isset($interest_grow['name']) && $interest_grow['grow_' . $days] > 0) {
                     $country['interests']['trend_up']['grow_' . $days] = array('name' => $interest_grow['name'], 'grow' => $this->formatGrow($interest_grow['grow_' . $days], $interest_grow['total_user']), 'time' => $time);
                 } else {
                     $country['interests']['trend_up']['grow_' . $days] = array('name' => '', 'grow' => '', 'time' => $time);
                 }
             }
             /***** Comportamientos *****/
             $country['comportamientos']['items'] = array();
             $items_has_more = array();
             $comportamientos = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel(2)->whereMobileDevice(0)->orderBy('total_user', 'DESC')->get(['id', 'name', 'total_user', 'grow_7']);
             foreach ($comportamientos as $comportamiento) {
                 if ($comportamiento['total_user'] != '') {
                     $country['comportamientos']['items'][] = array('has_more' => false, 'id' => $comportamiento['id'], 'name' => $comportamiento['name'], 'grow_7' => $this->formatGrow($comportamiento['grow_7'], $comportamiento['total_user']), 'total_user' => $this->owloo_number_format($comportamiento['total_user']), 'percent' => $this->owlooFormatPorcent($comportamiento['total_user'], $data['total_user']));
                 } else {
                     $items_has_more[] = array('has_more' => true, 'id' => $comportamiento['id_comportamiento'], 'name' => $comportamiento['name']);
                 }
             }
             foreach ($items_has_more as $more) {
                 $country['comportamientos']['items'][] = $more;
             }
             //Get the Trend-up
             $country['comportamientos']['trend_up'] = array();
             foreach (array(1, 3, 7, 15, 30) as $days) {
                 $country['comportamientos']['trend_up']['grow_' . $days] = array();
                 $comportamiento_grow = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereMobileDevice(0)->orderBy('grow_' . $days, 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_' . $days]);
                 $time = NULL;
                 switch ($days) {
                     case 1:
                         $time = '24 hours';
                         break;
                     case 3:
                         $time = '72 hours';
                         break;
                     default:
                         $time = $days . ' days';
                         break;
                 }
                 if (isset($comportamiento_grow['name']) && $comportamiento_grow['grow_' . $days] > 0) {
                     $country['comportamientos']['trend_up']['grow_' . $days] = array('name' => $comportamiento_grow['name'], 'grow' => $this->formatGrow($comportamiento_grow['grow_' . $days], $comportamiento_grow['total_user']), 'time' => $time);
                 } else {
                     $country['comportamientos']['trend_up']['grow_' . $days] = array('name' => '', 'grow' => '', 'time' => $time);
                 }
             }
             /***** Mobile device *****/
             //Top values
             $country['mobile_devices']['top'] = array();
             $top_elements = array(array('id' => '92', 'name' => 'Android'), array('id' => '94', 'name' => 'IOS'), array('id' => '93', 'name' => 'Windows Phones'), array('id' => '96', 'name' => 'Todos los dispositivos móviles'), array('id' => '100', 'name' => 'Propietarios de tabletas'));
             foreach ($top_elements as $top_comportamiento) {
                 $mobile_device = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereIdComportamiento($top_comportamiento['id'])->get(['total_user'])->first();
                 if ($mobile_device) {
                     $country['mobile_devices']['top'][] = array('name' => $top_comportamiento['name'], 'total_user' => $this->owloo_number_format($mobile_device['total_user']));
                 }
             }
             //Table
             $country['mobile_devices']['brands'] = array();
             foreach (array('android', 'ios', 'others') as $operating_system) {
                 $mobile_devices = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel($operating_system == 'ios' ? 4 : 3)->whereMobileOs($operating_system)->orderBy('total_user', 'DESC')->get(['id', 'id_comportamiento', 'name', 'total_user', 'grow_7']);
                 foreach ($mobile_devices as $mobile_device) {
                     $country['mobile_devices']['brands'][$operating_system][] = array('has_more' => $operating_system != 'ios' && $this->mobile_device_has_more_device($mobile_device['id_comportamiento']) ? true : false, 'id' => $mobile_device['id'], 'name' => $mobile_device['name'], 'grow_7' => $this->formatGrow($mobile_device['grow_7'], $mobile_device['total_user']), 'total_user' => $this->owloo_number_format($mobile_device['total_user']), 'percent' => $this->owlooFormatPorcent($mobile_device['total_user'], $data['total_user']));
                 }
             }
             //Trend ups
             $country['mobile_devices']['trend_up'] = array();
             foreach (array('android', 'ios', 'others') as $operating_system) {
                 foreach (array(1, 3, 7, 15, 30) as $days) {
                     $country['mobile_devices']['trend_up'][$operating_system]['grow_' . $days] = array();
                     $comportamiento_grow = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereMobileOs($operating_system)->orderBy('grow_' . $days, 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_' . $days]);
                     $time = NULL;
                     switch ($days) {
                         case 1:
                             $time = '24 hours';
                             break;
                         case 3:
                             $time = '72 hours';
                             break;
                         default:
                             $time = $days . ' days';
                             break;
                     }
                     if (isset($comportamiento_grow['name']) && $comportamiento_grow['grow_' . $days] > 0) {
                         $country['mobile_devices']['trend_up'][$operating_system]['grow_' . $days] = array('name' => $comportamiento_grow['name'], 'grow' => $this->formatGrow($comportamiento_grow['grow_' . $days], $comportamiento_grow['total_user']), 'time' => $time);
                     } else {
                         $country['mobile_devices']['trend_up'][$operating_system]['grow_' . $days] = array('name' => '', 'grow' => '', 'time' => $time);
                     }
                 }
             }
             //Android vs. IOS
             //Top values
             $country['mobile_devices']['vs'] = array();
             $vs_elements = array(array('id' => '92', 'name' => 'Android'), array('id' => '94', 'name' => 'IOS'));
             foreach ($vs_elements as $vs_comportamiento) {
                 $mobile_device = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereIdComportamiento($vs_comportamiento['id'])->get(['chart_history'])->first();
                 if ($mobile_device) {
                     $country['mobile_devices']['vs'][strtolower($vs_comportamiento['name'])] = array('name' => $vs_comportamiento['name'], 'history' => $this->owloo_chart_data_format($mobile_device['chart_history']));
                 }
             }
             Cache::put($var_cache, $country, 1440);
         } else {
             return 'Invalid method';
         }
     }
     return Cache::get($var_cache);
 }
 public function getCountry($code)
 {
     $var_cache = 'facebookCountry' . $code;
     if (!Cache::has($var_cache)) {
         $data = FacebookCountry::whereCode($code)->first();
         if ($data) {
             $country['id_country'] = $data['id_country'];
             $country['code'] = strtolower($data['code']);
             $country['name'] = $data['name'];
             $country['abbreviation'] = $data['abbreviation'];
             $country['link'] = $this->convert_string_to_url($data['name_en']);
             $country['total_user'] = $this->owloo_number_format($data['total_user']);
             $country['total_female'] = array('value' => $this->owloo_number_format($data['total_female']), 'percent' => $this->owlooFormatPorcent($data['total_female'], $data['total_user']));
             $country['total_male'] = array('value' => $this->owloo_number_format($data['total_male']), 'percent' => $this->owlooFormatPorcent($data['total_male'], $data['total_user']));
             $country['audience_history'] = json_decode($data['audience_history'], true);
             $country['audience_grow']['grow_1'] = $this->formatGrow($data['audience_grow_1'], $data['total_user']);
             $country['audience_grow']['grow_7'] = $this->formatGrow($data['audience_grow_7'], $data['total_user']);
             $country['audience_grow']['grow_30'] = $this->formatGrow($data['audience_grow_30'], $data['total_user']);
             $country['audience_grow']['grow_60'] = $this->formatGrow($data['audience_grow_60'], $data['total_user']);
             $country['audience_grow']['grow_90'] = $this->formatGrow($data['audience_grow_90'], $data['total_user']);
             $country['audience_grow']['grow_180'] = $this->formatGrow($data['audience_grow_180'], $data['total_user']);
             $country['general_ranking'] = $data['general_ranking'];
             /***** Cities *****/
             $country['cities']['supports'] = $data['supports_city'];
             $country['cities']['items'] = array();
             $cities = FacebookCity::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['name', 'total_user']);
             foreach ($cities as $city) {
                 $country['cities']['items'][] = array('name' => $city['name'], 'total_user' => $this->owloo_number_format($city['total_user']), 'percent' => $this->owlooFormatPorcent($city['total_user'], $data['total_user']));
             }
             /***** Regions *****/
             $country['regions']['supports'] = $data['supports_region'];
             $country['regions']['items'] = array();
             $regions = FacebookRegion::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['name', 'total_user']);
             foreach ($regions as $region) {
                 $country['regions']['items'][] = array('name' => $region['name'], 'total_user' => $this->owloo_number_format($region['total_user']), 'percent' => $this->owlooFormatPorcent($region['total_user'], $data['total_user']));
             }
             /***** Ages *****/
             $country['ages']['items'] = array();
             $ages = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             foreach ($ages as $age) {
                 $country['ages']['items'][] = array('name' => $age['name'], 'total_user' => $this->owloo_number_format($age['total_user']), 'percent' => $this->owlooFormatPorcent($age['total_user'], $data['total_user']));
             }
             //Get max values
             foreach (array('user', 'female', 'male') as $value) {
                 $country['ages']['max_' . $value] = array();
                 $age_max_total = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_' . $value, 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_' . $value]);
                 if (isset($age_max_total['name'])) {
                     $country['ages']['max_' . $value] = array('name' => $age_max_total['name'], 'value' => $this->owloo_number_format($age_max_total['total_' . $value]), 'percent' => $this->owlooFormatPorcent($age_max_total['total_' . $value], $data['total_user']));
                 }
             }
             //Get Trend-up
             $country['ages']['trend_up'] = array();
             $age_grow = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_user', 'grow_30']);
             if (isset($age_grow['name']) && $age_grow['grow_30'] > 0) {
                 $country['ages']['trend_up'] = array('name' => $age_grow['name'], 'grow' => $this->formatGrow($age_grow['grow_30'], $age_grow['total_user']));
             }
             /***** Languages *****/
             $country['languages']['items'] = array();
             $languages = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'grow_30']);
             foreach ($languages as $language) {
                 $country['languages']['items'][] = array('name' => $language['name'], 'total_user' => $this->owloo_number_format($language['total_user']), 'percent' => $this->owlooFormatPorcent($language['total_user'], $data['total_user']));
             }
             //Get the Trend-up
             $country['languages']['trend_up'] = array();
             $language_grow = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($language_grow['name']) && $language_grow['grow_30'] > 0) {
                 $country['languages']['trend_up'] = array('name' => $language_grow['name'], 'grow' => $this->formatGrow($language_grow['grow_30'], $language_grow['total_user']));
             }
             /***** Relationships *****/
             $country['relationships']['items'] = array();
             $relationships = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_user_grow_30', 'total_female_grow_30', 'total_male_grow_30']);
             foreach ($relationships as $relationship) {
                 $country['relationships']['items'][] = array('name' => $relationship['name'], 'total_user' => $this->owloo_number_format($relationship['total_user']), 'percent' => $this->owlooFormatPorcent($relationship['total_user'], $data['total_user']));
             }
             //Get Trend-up
             foreach (array('user', 'female', 'male') as $value) {
                 $country['relationships']['trend_up_' . $value] = array();
                 $relationship_grow = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_' . $value . '_grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_' . $value, 'total_' . $value . '_grow_30']);
                 if (isset($relationship_grow['name']) && $relationship_grow['total_' . $value . '_grow_30'] > 0) {
                     $country['relationships']['trend_up_' . $value] = array('name' => $relationship_grow['name'], 'grow' => $this->formatGrow($relationship_grow['total_' . $value . '_grow_30'], $relationship_grow['total_' . $value]));
                 }
             }
             /***** Interests *****/
             $country['interests']['items'] = array();
             $interests = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             $count_nivel_1 = 0;
             foreach ($interests as $interest) {
                 $country['interests']['items'][$count_nivel_1] = array('name' => $interest['name'], 'total_user' => $this->owloo_number_format($interest['total_user']), 'female_percent' => $this->owlooFormatPorcent($interest['total_female'], $interest['total_user']), 'male_percent' => $this->owlooFormatPorcent($interest['total_male'], $interest['total_user']), 'percent' => $this->owlooFormatPorcent($interest['total_user'], $data['total_user']), 'items' => array());
                 $interests_childrens_1 = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivelSuperior($interest['id_interest'])->orderBy('total_user', 'DESC')->take(5)->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                 $count_nivel_2 = 0;
                 foreach ($interests_childrens_1 as $children_1) {
                     $country['interests']['items'][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => $this->owloo_number_format($children_1['total_user']), 'percent' => $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']), 'items' => array());
                     $interests_childrens_2 = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivelSuperior($children_1['id_interest'])->orderBy('total_user', 'DESC')->take(5)->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     foreach ($interests_childrens_2 as $children_2) {
                         $country['interests']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][] = array('name' => $children_2['name'], 'total_user' => $this->owloo_number_format($children_2['total_user']), 'percent' => $this->owlooFormatPorcent($children_2['total_user'], $data['total_user']));
                     }
                     $count_nivel_2++;
                 }
                 $count_nivel_1++;
             }
             //Get the Trend-up
             $country['interests']['trend_up'] = array();
             $interest_grow = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($interest_grow['name']) && $interest_grow['grow_30'] > 0) {
                 $country['interests']['trend_up'] = array('name' => $interest_grow['name'], 'grow' => $this->formatGrow($interest_grow['grow_30'], $interest_grow['total_user']));
             }
             /***** Comportamientos *****/
             $country['comportamientos']['items'] = array();
             $comportamientos = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             $count_nivel_1 = 0;
             foreach ($comportamientos as $comportamiento) {
                 $country['comportamientos']['items'][$count_nivel_1] = array('name' => $comportamiento['name'], 'total_user' => !empty($comportamiento['total_user']) ? $this->owloo_number_format($comportamiento['total_user']) : NULL, 'female_percent' => !empty($comportamiento['total_user']) ? $this->owlooFormatPorcent($comportamiento['total_female'], $comportamiento['total_user']) : NULL, 'male_percent' => !empty($comportamiento['total_user']) ? $this->owlooFormatPorcent($comportamiento['total_male'], $comportamiento['total_user']) : NULL, 'percent' => !empty($comportamiento['total_user']) ? $this->owlooFormatPorcent($comportamiento['total_user'], $data['total_user']) : NULL, 'items' => array());
                 $comportamientos_childrens_1 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($comportamiento['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                 $count_nivel_2 = 0;
                 foreach ($comportamientos_childrens_1 as $children_1) {
                     $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => !empty($children_1['total_user']) ? $this->owloo_number_format($children_1['total_user']) : NULL, 'percent' => !empty($children_1['total_user']) ? $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']) : NULL, 'items' => array());
                     $comportamientos_childrens_2 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($children_1['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     $count_nivel_3 = 0;
                     foreach ($comportamientos_childrens_2 as $children_2) {
                         $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][$count_nivel_3] = array('name' => $children_2['name'], 'total_user' => !empty($children_2['total_user']) ? $this->owloo_number_format($children_2['total_user']) : NULL, 'percent' => !empty($children_2['total_user']) ? $this->owlooFormatPorcent($children_2['total_user'], $data['total_user']) : NULL);
                         $comportamientos_childrens_3 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($children_2['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                         foreach ($comportamientos_childrens_3 as $children_3) {
                             $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][$count_nivel_3]['items'][] = array('name' => $children_3['name'], 'total_user' => !empty($children_2['total_user']) ? $this->owloo_number_format($children_2['total_user']) : NULL, 'percent' => $this->owlooFormatPorcent($children_3['total_user'], $data['total_user']));
                         }
                         $count_nivel_3++;
                     }
                     $count_nivel_2++;
                 }
                 $count_nivel_1++;
             }
             /***** Mobile device *****/
             /*
                41 - Samsung
                25 - Apple
                39 - Google
                62 - Otros dispositivos Android
                63 - Blackberry
                69 - HTC
                71 - LG
                76 - Sony
                77 - Motorola
                78 - Nokia
                90 - Huawei
                92 - Todos los dispositivos Android
                93 - Windows Phones
                94 - Todos los dispositivos iOS
                95 - Teléfonos convencionales
                96 - Todos los dispositivos móviles
                97 - Smartphones y tablets
                99 - Smartphone Owners
                100 - Tablet Owners
             */
             $mobile_ids = array('Android' => array(41, 90, 39, 71, 69, 76, 77, 62), 'iOS' => array(25), 'Otros' => array(93, 78, 63));
             foreach ($mobile_ids as $key => $value) {
                 $count_nivel_1 = 0;
                 foreach ($value as $item) {
                     $mobile_devices = FacebookCountryComportamiento::whereIdComportamiento($item)->whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     foreach ($mobile_devices as $mobile_device) {
                         $country['mobile_devices'][$key][$count_nivel_1] = array('name' => $mobile_device['name'], 'total_user' => !empty($mobile_device['total_user']) ? $this->owloo_number_format($mobile_device['total_user']) : NULL, 'female_percent' => !empty($mobile_device['total_user']) ? $this->owlooFormatPorcent($mobile_device['total_female'], $mobile_device['total_user']) : NULL, 'male_percent' => !empty($mobile_device['total_user']) ? $this->owlooFormatPorcent($mobile_device['total_male'], $mobile_device['total_user']) : NULL, 'percent' => !empty($mobile_device['total_user']) ? $this->owlooFormatPorcent($mobile_device['total_user'], $data['total_user']) : NULL, 'items' => array());
                         $mobile_devices_childrens_1 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($mobile_device['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                         $count_nivel_2 = 0;
                         foreach ($mobile_devices_childrens_1 as $children_1) {
                             $country['mobile_devices'][$key][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => $this->owloo_number_format($children_1['total_user']), 'percent' => $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']), 'items' => array());
                             $count_nivel_2++;
                         }
                         $count_nivel_1++;
                     }
                 }
             }
             Cache::put($var_cache, $country, 1440);
         } else {
             return 'Invalid method';
         }
     }
     return Cache::get($var_cache);
 }
 public function getCountry($code)
 {
     $var_cache = 'facebookCountry' . $code;
     if (!Cache::has($var_cache)) {
         $data = FacebookCountry::whereCode($code)->first();
         if ($data) {
             $country['id_country'] = $data['id_country'];
             $country['code'] = strtolower($data['code']);
             $country['name'] = $data['name'];
             $country['abbreviation'] = $data['abbreviation'];
             $country['link'] = $this->convert_string_to_url($data['name_en']);
             $country['total_user'] = $this->owloo_number_format($data['total_user']);
             $country['total_female'] = array('value' => $this->owloo_number_format($data['total_female']), 'percent' => $this->owlooFormatPorcent($data['total_female'], $data['total_user']));
             $country['total_male'] = array('value' => $this->owloo_number_format($data['total_male']), 'percent' => $this->owlooFormatPorcent($data['total_male'], $data['total_user']));
             $country['audience_history'] = json_decode($data['audience_history'], true);
             $country['audience_grow']['grow_1'] = $this->formatGrow($data['audience_grow_1'], $data['total_user']);
             $country['audience_grow']['grow_7'] = $this->formatGrow($data['audience_grow_7'], $data['total_user']);
             $country['audience_grow']['grow_30'] = $this->formatGrow($data['audience_grow_30'], $data['total_user']);
             $country['audience_grow']['grow_60'] = $this->formatGrow($data['audience_grow_60'], $data['total_user']);
             $country['audience_grow']['grow_90'] = $this->formatGrow($data['audience_grow_90'], $data['total_user']);
             $country['audience_grow']['grow_180'] = $this->formatGrow($data['audience_grow_180'], $data['total_user']);
             $country['general_ranking'] = $data['general_ranking'];
             /***** Cities *****/
             $country['cities']['supports'] = $data['supports_city'];
             $country['cities']['items'] = array();
             $cities = FacebookCity::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['name', 'total_user']);
             foreach ($cities as $city) {
                 $country['cities']['items'][] = array('name' => $city['name'], 'total_user' => $this->owloo_number_format($city['total_user']), 'percent' => $this->owlooFormatPorcent($city['total_user'], $data['total_user']));
             }
             /***** Regions *****/
             $country['regions']['supports'] = $data['supports_region'];
             $country['regions']['items'] = array();
             $regions = FacebookRegion::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['name', 'total_user']);
             foreach ($regions as $region) {
                 $country['regions']['items'][] = array('name' => $region['name'], 'total_user' => $this->owloo_number_format($region['total_user']), 'percent' => $this->owlooFormatPorcent($region['total_user'], $data['total_user']));
             }
             /***** Ages *****/
             $country['ages']['items'] = array();
             $ages = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             foreach ($ages as $age) {
                 $country['ages']['items'][] = array('name' => $age['name'], 'total_user' => $this->owloo_number_format($age['total_user']), 'percent' => $this->owlooFormatPorcent($age['total_user'], $data['total_user']));
             }
             //Get max values
             foreach (array('user', 'female', 'male') as $value) {
                 $country['ages']['max_' . $value] = array();
                 $age_max_total = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_' . $value, 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_' . $value]);
                 if (isset($age_max_total['name'])) {
                     $country['ages']['max_' . $value] = array('name' => $age_max_total['name'], 'value' => $this->owloo_number_format($age_max_total['total_' . $value]), 'percent' => $this->owlooFormatPorcent($age_max_total['total_' . $value], $data['total_user']));
                 }
             }
             //Get Trend-up
             $country['ages']['trend_up'] = array();
             $age_grow = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_user', 'grow_30']);
             if (isset($age_grow['name']) && $age_grow['grow_30'] > 0) {
                 $country['ages']['trend_up'] = array('name' => $age_grow['name'], 'grow' => $this->formatGrow($age_grow['grow_30'], $age_grow['total_user']));
             }
             /***** Languages *****/
             $country['languages']['items'] = array();
             $languages = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'grow_30']);
             foreach ($languages as $language) {
                 $country['languages']['items'][] = array('name' => $language['name'], 'total_user' => $this->owloo_number_format($language['total_user']), 'percent' => $this->owlooFormatPorcent($language['total_user'], $data['total_user']));
             }
             //Get the Trend-up
             $country['languages']['trend_up'] = array();
             $language_grow = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($language_grow['name']) && $language_grow['grow_30'] > 0) {
                 $country['languages']['trend_up'] = array('name' => $language_grow['name'], 'grow' => $this->formatGrow($language_grow['grow_30'], $language_grow['total_user']));
             }
             /***** Relationships *****/
             $country['relationships']['items'] = array();
             $relationships = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_user_grow_30', 'total_female_grow_30', 'total_male_grow_30']);
             foreach ($relationships as $relationship) {
                 $country['relationships']['items'][] = array('name' => $relationship['name'], 'total_user' => $this->owloo_number_format($relationship['total_user']), 'percent' => $this->owlooFormatPorcent($relationship['total_user'], $data['total_user']));
             }
             //Get Trend-up
             foreach (array('user', 'female', 'male') as $value) {
                 $country['relationships']['trend_up_' . $value] = array();
                 $relationship_grow = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_' . $value . '_grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_' . $value, 'total_' . $value . '_grow_30']);
                 if (isset($relationship_grow['name']) && $relationship_grow['total_' . $value . '_grow_30'] > 0) {
                     $country['relationships']['trend_up_' . $value] = array('name' => $relationship_grow['name'], 'grow' => $this->formatGrow($relationship_grow['total_' . $value . '_grow_30'], $relationship_grow['total_' . $value]));
                 }
             }
             /***** Interests *****/
             $country['interests']['items'] = array();
             $interests = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             $count_nivel_1 = 0;
             foreach ($interests as $interest) {
                 $country['interests']['items'][$count_nivel_1] = array('name' => $interest['name'], 'total_user' => $this->owloo_number_format($interest['total_user']), 'percent' => $this->owlooFormatPorcent($interest['total_user'], $data['total_user']), 'items' => array());
                 $interests_childrens_1 = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivelSuperior($interest['id_interest'])->orderBy('total_user', 'DESC')->take(5)->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                 $count_nivel_2 = 0;
                 foreach ($interests_childrens_1 as $children_1) {
                     $country['interests']['items'][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => $this->owloo_number_format($children_1['total_user']), 'percent' => $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']), 'items' => array());
                     $interests_childrens_2 = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivelSuperior($children_1['id_interest'])->orderBy('total_user', 'DESC')->take(5)->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     foreach ($interests_childrens_2 as $children_2) {
                         $country['interests']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][] = array('name' => $children_2['name'], 'total_user' => $this->owloo_number_format($children_2['total_user']), 'percent' => $this->owlooFormatPorcent($children_2['total_user'], $data['total_user']));
                     }
                     $count_nivel_2++;
                 }
                 $count_nivel_1++;
             }
             //Get the Trend-up
             $country['interests']['trend_up'] = array();
             $interest_grow = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($interest_grow['name']) && $interest_grow['grow_30'] > 0) {
                 $country['interests']['trend_up'] = array('name' => $interest_grow['name'], 'grow' => $this->formatGrow($interest_grow['grow_30'], $interest_grow['total_user']));
             }
             /***** Comportamientos *****/
             $country['comportamientos']['items'] = array();
             $comportamientos = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             $count_nivel_1 = 0;
             foreach ($comportamientos as $comportamiento) {
                 $country['comportamientos']['items'][$count_nivel_1] = array('name' => $comportamiento['name'], 'total_user' => $this->owloo_number_format($comportamiento['total_user']), 'percent' => $this->owlooFormatPorcent($comportamiento['total_user'], $data['total_user']), 'items' => array());
                 $comportamientos_childrens_1 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($comportamiento['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                 $count_nivel_2 = 0;
                 foreach ($comportamientos_childrens_1 as $children_1) {
                     $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => $this->owloo_number_format($children_1['total_user']), 'percent' => $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']), 'items' => array());
                     $comportamientos_childrens_2 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($children_1['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     $count_nivel_3 = 0;
                     foreach ($comportamientos_childrens_2 as $children_2) {
                         $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][$count_nivel_3] = array('name' => $children_2['name'], 'total_user' => $this->owloo_number_format($children_2['total_user']), 'percent' => $this->owlooFormatPorcent($children_2['total_user'], $data['total_user']));
                         $comportamientos_childrens_3 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($children_2['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                         foreach ($comportamientos_childrens_3 as $children_3) {
                             $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][$count_nivel_3]['items'][] = array('name' => $children_3['name'], 'total_user' => $this->owloo_number_format($children_3['total_user']), 'percent' => $this->owlooFormatPorcent($children_3['total_user'], $data['total_user']));
                         }
                         $count_nivel_3++;
                     }
                     $count_nivel_2++;
                 }
                 $count_nivel_1++;
             }
             //Get the Trend-up
             $country['comportamientos']['trend_up'] = array();
             $comportamiento_grow = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel(1)->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($comportamiento_grow['name']) && $comportamiento_grow['grow_30'] > 0) {
                 $country['comportamientos']['trend_up'] = array('name' => $comportamiento_grow['name'], 'grow' => $this->formatGrow($comportamiento_grow['grow_30'], $comportamiento_grow['total_user']));
             }
             print_r($country);
             die;
             Cache::put($var_cache, $country, 1440);
         } else {
             return 'Invalid method';
         }
     }
     return Cache::get($var_cache);
 }