public function getRankingPage($idiom, $category, $page = 1)
 {
     $where = ucfirst($idiom);
     $var_cache = 'rankingLikes' . $where . 'PageCat' . $category . '_' . ($page - 1) * 20;
     if (!Cache::has($var_cache)) {
         if ($where != 'World' && $where != 'Hispanic') {
             $ranking = FacebookPageLocalFans::take(20)->skip(($page - 1) * 20)->orderBy('likes_local_fans', 'DESC')->orderBy('likes', 'DESC');
         } else {
             $ranking = FacebookPage::take(20)->skip(($page - 1) * 20)->orderBy('likes', 'DESC')->orderBy('talking_about', 'DESC');
         }
         switch ($where) {
             case 'World':
                 $ranking->whereParent(0)->orWhereRaw('id_page = parent');
                 break;
             case 'Hispanic':
                 $ranking->whereIdiom('es')->whereParent(0)->orWhereRaw('id_page = parent');
                 break;
             default:
                 if (FacebookCountry::whereCode(strtolower($where))->first()) {
                     $ranking->where('country_code', strtoupper($where));
                 } else {
                     return 'Invalid method';
                 }
         }
         if ($category != 'all') {
             $ranking->where('category_id', $category);
         }
         if ($where != 'World' && $where != 'Hispanic') {
             $ranking = $ranking->get(['fb_id', 'username', 'name', 'is_verified', 'likes', 'likes_grow_7', 'talking_about', 'likes_local_fans']);
         } else {
             $ranking = $ranking->get(['fb_id', 'username', 'name', 'is_verified', 'likes', 'likes_grow_7', 'talking_about', 'country_code', 'first_country_code']);
         }
         $pages = array();
         foreach ($ranking as $key => $value) {
             $cache = NULL;
             $cache['position'] = ($page - 1) * 20 + $key + 1;
             $cache['username'] = strtolower($value['username']);
             $cache['name'] = $value['name'];
             $cache['picture'] = 'http://graph.facebook.com/' . $value['fb_id'] . '/picture?height=50&type=normal&width=50';
             $cache['is_verified'] = $value['is_verified'];
             $cache['likes'] = $this->owloo_number_format($value['likes']);
             $cache['grow_7'] = !empty($value['likes_grow_7']) ? $value['likes_grow_7'] : 0;
             $cache['pta'] = 0;
             if ($value['likes'] > 0) {
                 $cache['pta'] = $this->owlooFormatPorcent($value['talking_about'], $value['likes']);
             }
             if ($where != 'World' && $where != 'Hispanic') {
                 $cache['likes_local_fans'] = $this->owloo_number_format($value['likes_local_fans']);
             } else {
                 $cache['country'] = strtolower(!empty($value['country_code']) ? $value['country_code'] : $value['first_country_code']);
             }
             $pages[] = $cache;
         }
         Cache::put($var_cache, $pages, 1440);
     }
     return Cache::get($var_cache);
 }
 public function getRankingPage($idiom, $category, $page = 1)
 {
     $where = strtolower($idiom);
     $var_cache = 'rankingLikes' . ucfirst($where) . 'PageCat' . ucfirst($category) . '_' . ($page - 1) * 20;
     if (!Cache::has($var_cache)) {
         //if(true){
         $is_ranking_by_country = false;
         $country_idiom = FacebookCountry::whereIdiom($where)->first();
         if ($where != 'world' && $where != 'hispanic' && !$country_idiom) {
             $ranking = FacebookPageLocalFans::take(20)->skip(($page - 1) * 20)->orderBy('likes_local_fans', 'DESC')->orderBy('likes', 'DESC');
             $is_ranking_by_country = true;
         } else {
             $ranking = FacebookPage::take(20)->skip(($page - 1) * 20)->orderBy('likes', 'DESC')->orderBy('talking_about', 'DESC');
         }
         $is_have_category = false;
         $id_category = NULL;
         if ($category != 'all') {
             $category_search = FacebookCategory::whereCategory($category)->first();
             if ($category_search) {
                 $id_category = $category_search->id_category;
                 $is_have_category = true;
             } else {
                 return 'Invalid method';
             }
         }
         switch ($where) {
             case 'world':
                 if ($is_have_category) {
                     $ranking->whereCategoryId($id_category)->where(function ($query) {
                         $query->whereParent(0)->orWhereRaw('id_page = parent');
                     });
                 } else {
                     $ranking->whereParent(0)->orWhereRaw('id_page = parent');
                 }
                 break;
             case 'hispanic':
                 if ($is_have_category) {
                     $ranking->whereIdiom('es')->whereCategoryId($id_category)->where(function ($query) {
                         $query->whereParent(0)->orWhereRaw('id_page = parent');
                     });
                 } else {
                     $ranking->whereIdiom('es')->where(function ($query) {
                         $query->whereParent(0)->orWhereRaw('id_page = parent');
                     });
                 }
                 break;
             default:
                 $country = FacebookCountry::whereSlug($where)->first();
                 if ($country) {
                     if ($is_have_category) {
                         $ranking->where('country_code', strtoupper($country->code))->whereCategoryId($id_category)->where(function ($query) {
                             $query->whereParent(0)->orWhereRaw('id_page = parent');
                         });
                     } else {
                         $ranking->where('country_code', strtoupper($country->code))->where(function ($query) {
                             $query->whereParent(0)->orWhereRaw('id_page = parent');
                         });
                     }
                 } elseif ($country_idiom) {
                     if ($is_have_category) {
                         $ranking->whereIdiom($where)->whereCategoryId($id_category)->where(function ($query) {
                             $query->whereParent(0)->orWhereRaw('id_page = parent');
                         });
                     } else {
                         $ranking->whereIdiom($where)->where(function ($query) {
                             $query->whereParent(0)->orWhereRaw('id_page = parent');
                         });
                     }
                 } else {
                     return 'Invalid method';
                 }
         }
         if ($is_ranking_by_country) {
             $ranking = $ranking->get(['category_id', 'fb_id', 'username', 'name', 'is_verified', 'likes', 'likes_grow_7', 'talking_about', 'likes_local_fans']);
         } else {
             $ranking = $ranking->get(['category_id', 'fb_id', 'username', 'name', 'is_verified', 'likes', 'likes_grow_7', 'talking_about', 'country_code', 'country_slug', 'first_country_code', 'first_country_slug']);
         }
         $pages = array();
         foreach ($ranking as $key => $value) {
             $cache = NULL;
             $cache['category_id'] = $value['category_id'];
             $cache['position'] = ($page - 1) * 20 + $key + 1;
             $cache['username'] = strtolower($value['username']);
             $cache['name'] = $value['name'];
             $cache['picture'] = 'http://graph.facebook.com/' . $value['fb_id'] . '/picture?height=50&type=normal&width=50';
             $cache['is_verified'] = $value['is_verified'];
             if (!$is_ranking_by_country) {
                 $cache['country']['code'] = strtolower(!empty($value['country_code']) ? $value['country_code'] : $value['first_country_code']);
                 $cache['country']['slug'] = !empty($value['country_slug']) ? $value['country_slug'] : $value['first_country_slug'];
             }
             $pta = 0;
             if ($value['likes'] > 0) {
                 $pta = $this->owlooFormatPorcent($value['talking_about'], $value['likes']);
             }
             //PTA
             $cache['second_column'][] = array('value' => $pta . '%', 'class' => '');
             //Semana
             $aux_num = $this->formatGrow($value['likes_grow_7'], $value['likes']);
             $cache['second_column'][] = array('value' => $aux_num['value'], 'class' => $aux_num['class']);
             //Fans totales
             $cache['second_column'][] = array('value' => $this->owloo_number_format($value['likes']), 'class' => '');
             if ($is_ranking_by_country) {
                 //Fans locales
                 $cache['second_column'][] = array('value' => $this->owloo_number_format($value['likes_local_fans']), 'class' => '');
             }
             $pages[] = $cache;
         }
         $second_column = array();
         if ($is_ranking_by_country) {
             $second_column = array('PTA', 'Semana', 'Fans totales', 'Fans locales');
         } else {
             $second_column = array('País', 'PTA', 'Semana', 'Fans totales');
         }
         $array_result = array('type' => 'fb_page', 'subtype' => $is_ranking_by_country ? 'fb_country_page' : 'fb_all_pages', 'main_column' => 'Página', 'second_column' => $second_column, 'large_column' => 3, 'link' => 'facebook-analytics/pages', 'items' => $pages);
         Cache::put($var_cache, $array_result, 1440);
     }
     return Cache::get($var_cache);
 }