function sp_postlistview_query($where, $order, $count, $view, $type)
    {
        global $spGlobals, $spThisUser;
        # If no WHERE clause then return empty
        if (empty($where)) {
            return;
        }
        # build list of forums user can view
        $fids = sp_user_visible_forums($view);
        if (!empty($fids)) {
            $fids = implode(',', $fids);
            $where .= ' AND ' . SFPOSTS . ".forum_id IN ({$fids})";
        }
        # Check order
        if (empty($order)) {
            $order = SFPOSTS . '.post_id DESC';
        }
        $spdb = new spdbComplex();
        $spdb->table = SFPOSTS;
        $spdb->fields = SFPOSTS . '.post_id, post_content, ' . spdb_zone_datetime('post_date') . ', ' . SFPOSTS . '.topic_id, ' . SFPOSTS . '.forum_id,
								  ' . SFPOSTS . '.user_id, guest_name, post_status, post_index, forum_name, forum_slug, forum_disabled, ' . SFFORUMS . '.group_id, group_name,
								  topic_name, topic_slug, ' . SFTOPICS . '.post_count, topic_opened, display_name';
        $spdb->join = array(SFFORUMS . ' ON ' . SFFORUMS . '.forum_id = ' . SFPOSTS . '.forum_id', SFGROUPS . ' ON ' . SFGROUPS . '.group_id = ' . SFFORUMS . '.group_id', SFTOPICS . ' ON ' . SFTOPICS . '.topic_id = ' . SFPOSTS . '.topic_id');
        $spdb->left_join = array(SFMEMBERS . ' ON ' . SFMEMBERS . '.user_id = ' . SFPOSTS . '.user_id');
        $spdb->where = $where;
        $spdb->orderby = $order;
        if ($count) {
            $spdb->limits = $count;
        }
        $spdb = apply_filters('sph_post_list_query', $spdb, $this, $type);
        if (!empty($spThisUser->inspect['q_spPostListView'])) {
            $spdb->inspect = 'spPostListView';
            $spdb->show = true;
        }
        $records = $spdb->select();
        # Now check authorisations and clean up the object
        $list = array();
        # Some values we need
        # How many topics to a page?
        $ppaged = $spGlobals['display']['posts']['perpage'];
        if (empty($ppaged) || $ppaged == 0) {
            $ppaged = 20;
        }
        # establish topic sort order
        $porder = 'ASC';
        # default
        if ($spGlobals['display']['posts']['sortdesc']) {
            $porder = 'DESC';
        }
        # global override
        if ($records) {
            $listPos = 1;
            foreach ($records as $r) {
                if (sp_can_view($r->forum_id, 'forum-title')) {
                    if ($r->post_status == 0 || sp_get_auth('moderate_posts', $r->forum_id)) {
                        $p = $r->post_id;
                        $list[$p] = $r;
                        # Now apply any necessary filters and data changes
                        $list[$p]->post_content = sp_filter_content_display($r->post_content);
                        $list[$p]->post_content_raw = $r->post_content;
                        $list[$p]->forum_name = sp_filter_title_display($r->forum_name);
                        $list[$p]->forum_disabled = $r->forum_disabled;
                        $list[$p]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                        $list[$p]->topic_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 1, 0);
                        $list[$p]->topic_name = sp_filter_title_display($r->topic_name);
                        $list[$p]->topic_opened = $r->topic_opened;
                        $list[$p]->group_name = sp_filter_title_display($r->group_name);
                        if (sp_can_view($r->forum_id, 'post-content', $spThisUser->ID, $r->user_id, $r->topic_id, $r->post_id)) {
                            $list[$p]->post_tip = $r->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($r->post_content, $r->post_status);
                        } else {
                            $list[$p]->post_tip = '';
                        }
                        # Ensure display name is populated
                        if (empty($r->display_name)) {
                            $list[$p]->display_name = $list[$p]->guest_name;
                        }
                        $list[$p]->display_name = sp_filter_name_display($list[$p]->display_name);
                        # determine the page for the post permalink
                        if ($porder == 'ASC') {
                            $page = $r->post_index / $ppaged;
                            if (!is_int($page)) {
                                $page = intval($page + 1);
                            }
                        } else {
                            $page = $r->post_count - $r->post_index;
                            $page = $page / $ppaged;
                            $page = intval($page + 1);
                        }
                        $list[$p]->post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, $page, $r->post_id, $r->post_index);
                        $list[$p]->list_position = $listPos;
                        $list[$p] = apply_filters('sph_post_list_record', $list[$p], $r, $type);
                    }
                }
                $listPos++;
            }
        }
        return $list;
    }
 function sp_listview_populate_topicids($topicIds, $forumIds, $count)
 {
     global $spGlobals, $spThisUser;
     if (empty($topicIds)) {
         $topicIds = array();
     }
     if (!empty($forumIds)) {
         if (!is_array($forumIds)) {
             $forumIds = explode(',', $forumIds);
         }
     } else {
         $forumIds = sp_user_visible_forums();
     }
     # if NO forums ids then we should go no further
     if (empty($forumIds)) {
         return $topicIds;
     }
     # get the auths ID for moderating posts
     $mod = $spGlobals['auths_map']['moderate_posts'];
     # so let's trawl the new topics/post cache
     if (isset($spGlobals['topic_cache']['new']) && !empty($spGlobals['topic_cache']['new'])) {
         foreach ($spGlobals['topic_cache']['new'] as $t) {
             # 1 - check if topic ID already in list
             if (in_array($t[LISTTOPIC], $topicIds)) {
                 continue;
             }
             # 2 - check if topic n forum that can be viewed
             if (!in_array($t[LISTFORUM], $forumIds)) {
                 continue;
             }
             # 3 - if post in moderation can user moderate
             if ($t[LISTSTATUS] == true && $spThisUser->auths[$t[LISTFORUM]][$mod] == false) {
                 continue;
             }
             # 4 - so - we can add topic ID to list
             $topicIds[] = $t[LISTTOPIC];
             # 5 - and if we have enough then break the loop
             if (count($topicIds) == $count) {
                 break;
             }
         }
     }
     # Only process if there are topics defined
     if (empty($topicIds)) {
         return '';
     }
     return $topicIds;
 }
 function sp_searchview_control($count)
 {
     global $spVars;
     $searchType = $spVars['searchtype'];
     $searchInclude = $spVars['searchinclude'];
     # (LIMIT) how many topics per page?
     if (!$count) {
         $count = 30;
     }
     $this->searchShow = $count;
     if ($spVars['searchpage'] == 1) {
         $startlimit = 0;
     } else {
         $startlimit = ($spVars['searchpage'] - 1) * $count;
     }
     # For this page?
     $this->limit = $startlimit . ', ' . $count;
     # (WHERE) All or specific forum?
     if ($spVars['forumslug'] == 'all') {
         # create forumIds list and where clause
         $forumIds = sp_user_visible_forums('post-content');
         if (empty($forumIds)) {
             return;
         }
         $this->forumWhere = 'forum_id IN (' . implode(',', $forumIds) . ') ';
     } else {
         # check we can see this forum and create where clause
         if (!sp_get_auth('view_forum', $spVars['forumid'])) {
             return;
         }
         $this->forumWhere = 'forum_id=' . $spVars['forumid'];
     }
     if (empty($spVars['searchvalue'])) {
         return '';
     }
     if ($searchType == 4 || $searchType == 5) {
         $this->searchTermRaw = sp_get_member_item((int) $spVars['searchvalue'], 'display_name');
     } else {
         $this->searchTermRaw = $spVars['searchvalue'];
     }
     $this->searchTerm = $this->sp_construct_search_term($spVars['searchvalue'], $searchType, $searchInclude);
     # if search type is 1,2 or 3 (i.e., normal data searches) and we are looking for page 1 then we need to run
     # the query. Note - if posts and titles then we need to run it twice!
     # If we are not loading page 1 however then we can grab the results from the cache.
     # For all other searchtypes - just rin the standard routine
     if ($searchType > 3) {
         $r = $this->sp_searchview_query($searchType, $searchInclude);
         return $r;
     }
     if ($spVars['searchpage'] == 1 && $spVars['newsearch'] == true) {
         $r = $this->sp_searchview_query($searchType, $searchInclude);
         # Remove dupes and re-sort
         if ($r) {
             $r = array_unique($r);
             rsort($r, SORT_NUMERIC);
             # Now hive off into a transient
             $d = array();
             $d['url'] = $this->searchPermalink;
             $d['page'] = $spVars['searchpage'];
             $t = array();
             $t[0] = $d;
             $t[1] = $r;
             sp_add_cache('search', $t);
         }
     } else {
         # Get the data from the cache if not page 1 for first time
         $r = sp_get_cache('search');
         if ($r) {
             $d = $r[0];
             $r = $r[1];
             $d['url'] = $this->searchPermalink;
             $d['page'] = $spVars['searchpage'];
             $t = array();
             $t[0] = $d;
             $t[1] = $r;
             # update the transient with the new url
             sp_add_cache('search', $t);
         }
     }
     # Now work out which part of the $r array to return
     if ($r) {
         $spVars['searchresults'] = count($r);
         $this->searchCount = $spVars['searchresults'];
         $this->searchInclude = $searchInclude;
         $this->searchType = $searchType;
         return array_slice($r, $startlimit, $count);
     }
 }