/**
  * Get widget posts
  *
  * @param $type either 'top' or 'new', matching the post_status in the db.
  * return array $posts
  */
 public function getPostsWidget($h, $type, $custom = true, $limit = 10)
 {
     $h->vars['limit'] = $limit;
     $posts = '';
     // include sb_base_functions class:
     require_once PLUGINS . 'sb_base/libs/SbBaseFunctions.php';
     $sbfuncs = new SbBaseFunctions();
     if (!$custom) {
         // Show latest on front page, top stories on latest page, or both otherwise
         if ($type == 'new' && $h->pageName != 'latest') {
             $posts = $sbfuncs->prepareList($h, 'new');
         } elseif ($type == 'top' && $h->pageName != 'popular') {
             $posts = $sbfuncs->prepareList($h, 'top');
         } elseif ($type == 'upcoming' && $h->pageName != 'upcoming') {
             $posts = $sbfuncs->prepareList($h, 'upcoming');
         }
     } else {
         // Return posts regardless of what page we're viewing
         if ($type == 'new') {
             $posts = $sbfuncs->prepareList($h, 'new');
             // get latest stories
         } elseif ($type == 'top') {
             $posts = $sbfuncs->prepareList($h, 'top');
             // get top stories
         } elseif ($type == 'upcoming') {
             $posts = $sbfuncs->prepareList($h, 'upcoming');
             // get upcoming stories
         }
     }
     if ($type == 'all') {
         $posts = $sbfuncs->prepareList($h, 'all');
         // get all stories
     } elseif ($type == 'top-24-hours') {
         $posts = $sbfuncs->prepareList($h, 'top-24-hours');
         // get top stories from last 24 hours
     } elseif ($type == 'top-48-hours') {
         $posts = $sbfuncs->prepareList($h, 'top-48-hours');
         // get top stories from last 48 hours
     } elseif ($type == 'top-7-days') {
         $posts = $sbfuncs->prepareList($h, 'top-7-days');
         // get top stories from last 7 days
     } elseif ($type == 'top-30-days') {
         $posts = $sbfuncs->prepareList($h, 'top-30-days');
         // get top stories from last 30 days
     } elseif ($type == 'top-365-days') {
         $posts = $sbfuncs->prepareList($h, 'top-365-days');
         // get top stories from last 365 days
     } elseif ($type == 'top-all-time') {
         $posts = $sbfuncs->prepareList($h, 'top-all-time');
         // get top stories from all time
     }
     if ($posts) {
         return $posts;
     } else {
         return false;
     }
 }
 /**
  * Determine the pageType
  */
 public function theme_index_top($h)
 {
     // check if we're using the sort/filter links
     if ($h->cage->get->keyExists('sort')) {
         $h->pageName = 'sort';
     }
     $h->pluginHook('sb_base_pre_rss_forward');
     // check if this is an RSS link forwarding to the source
     if ($h->cage->get->keyExists('forward')) {
         $post_id = $h->cage->get->testInt('forward');
         if ($post_id) {
             $post = $h->getPost($post_id);
         }
         if (isset($post->post_orig_url)) {
             header("Location:" . urldecode($post->post_orig_url));
             exit;
         }
     }
     // include sb_base_functions class:
     require_once PLUGINS . 'sb_base/libs/SbBaseFunctions.php';
     $sb_funcs = new SbBaseFunctions();
     $h->pluginHook('sb_base_pre_set_home');
     // Allow SB Base to set the homepage to "popular" unless already set.
     if (!$h->home) {
         $h->setHome('popular', 'popular');
         // and force page name to popular, too, if not already set.
     }
     switch ($h->pageName) {
         case 'rss':
             $sb_funcs->postRssFeedQuery($h);
             $sb_funcs->feed_results = $sb_funcs->getPosts($h, $sb_funcs->feed_array);
             $sb_funcs->doPostRssFeed($h, $sb_funcs->feed_results);
             exit;
             break;
         case 'popular':
             $h->pageType = 'list';
             $h->pageTitle = $h->home == 'popular' ? $h->lang["sb_base_site_name"] : $h->lang["sb_base_top"];
             break;
         case 'latest':
             $h->pageType = 'list';
             $h->pageTitle = $h->lang["sb_base_latest"];
             break;
         case 'upcoming':
             $h->pageType = 'list';
             $h->pageTitle = $h->lang["sb_base_upcoming"];
             break;
         case 'all':
             $h->pageType = 'list';
             $h->pageTitle = $h->lang["sb_base_all"];
             break;
         case 'sort':
             $sort = $h->cage->get->testPage('sort');
             if ($sort) {
                 $h->pageType = 'list';
                 $sort_lang = 'sb_base_' . str_replace('-', '_', $sort);
                 $h->pageTitle = $h->lang[$sort_lang];
             }
             break;
         default:
             // no default or we'd mess up anything set by other plugins
     }
     $h->pluginHook('sb_base_theme_index_top');
     // case for paginated pages, but no pagename
     if ((!$h->pageName || $h->pageName == 'popular') && $h->cage->get->keyExists('pg')) {
         if (!$h->home) {
             $h->setHome('popular');
         }
         // query vars previously prevented getPageName returning a name
         $h->pageName = 'popular';
         $h->pageType = 'list';
         $h->pageTitle = $h->lang["sb_base_top"];
     }
     // no need to continue for other types of homepage
     if ($h->pageName == $h->home && $h->home != 'popular') {
         return false;
     }
     // stop here if not a list or the pageType has been set elsewhere:
     if (!empty($h->pageType) && $h->pageType != 'list' && $h->pageType != 'post') {
         return false;
     }
     // get settings
     $h->vars['sb_base_settings'] = $h->getSerializedSettings('sb_base');
     $posts_per_page = $h->vars['sb_base_settings']['posts_per_page'];
     // if a list, get the posts:
     switch ($h->pageType) {
         case 'list':
             $post_count = $sb_funcs->prepareList($h, '', 'count');
             // get the number of posts
             $post_query = $sb_funcs->prepareList($h, '', 'query');
             // and the SQL query used
             $h->vars['pagedResults'] = $h->pagination($post_query, $post_count, $posts_per_page, 'posts');
             break;
         case 'post':
             // if a post is already set (e.g. from the sb_categories plugin), we don't want to
             // do the default stuff below. We do, however, need the "target", "editorial" stuff after it, though...
             break;
         default:
             // Probably a post, let's check:
             if (is_numeric($h->pageName)) {
                 // Page name is a number so it must be a post with non-friendly urls
                 $exists = $h->readPost($h->pageName);
                 // read current post
                 if (!$exists) {
                     $h->pageTitle = $h->lang['main_theme_page_not_found'];
                     return false;
                 }
                 $h->pageTitle = $h->post->title;
                 $h->pageType = 'post';
             } elseif ($post_id = $h->isPostUrl($h->pageName)) {
                 // Page name belongs to a story
                 $h->readPost($post_id);
                 // read current post
                 $h->pageTitle = $h->post->title;
                 $h->pageType = 'post';
             }
     }
     // close switch
     // user defined settings:
     if (!$h->currentUser->settings) {
         // logged out users get the default settings:
         $h->currentUser->settings = $h->getDefaultSettings('site');
     }
     // open links in a new tab?
     if ($h->currentUser->settings['new_tab']) {
         $h->vars['target'] = 'target="_blank"';
     } else {
         $h->vars['target'] = '';
     }
     // open link to the source or the site post?
     if ($h->currentUser->settings['link_action']) {
         $h->vars['link_action'] = 'source';
     } else {
         $h->vars['link_action'] = '';
     }
     // editorial (story with an internal link)
     if (strstr($h->post->origUrl, BASEURL)) {
         $h->vars['editorial'] = true;
     } else {
         $h->vars['editorial'] = false;
     }
     // get settings from Submit
     if (!isset($h->vars['submit_settings'])) {
         $h->vars['submit_settings'] = $h->getSerializedSettings('submit');
     }
 }