function sp_groupview_query($groupids = '', $idOrder = false)
    {
        global $spThisUser;
        # can we get the results from the cache?
        $records = array();
        if (empty($spThisUser->inspect['q_spGroupView'])) {
            $records = sp_get_cache('group');
        }
        if (!$records) {
            $WHERE = '';
            if (!empty($groupids)) {
                $gcount = count($groupids);
                $done = 0;
                foreach ($groupids as $id) {
                    $WHERE .= '(' . SFGROUPS . ".group_id={$id})";
                    $done++;
                    if ($done < $gcount) {
                        $WHERE .= ' OR ';
                    }
                }
            }
            $this->groupViewStatus = empty($groupids) ? 'no data' : 'no access';
            # retrieve group and forum records
            $spdb = new spdbComplex();
            $spdb->table = SFGROUPS;
            $spdb->fields = SFGROUPS . '.group_id, group_name, group_desc, group_rss, group_icon, group_message,
								forum_id, forum_name, forum_slug, forum_desc, forum_status, forum_disabled, forum_icon, forum_icon_new, forum_icon_locked, forum_rss_private,
								post_id, post_id_held, topic_count, post_count, post_count_held, parent, children';
            $spdb->join = array(SFFORUMS . ' ON ' . SFGROUPS . '.group_id = ' . SFFORUMS . '.group_id');
            $spdb->where = $WHERE;
            $spdb->orderby = 'group_seq, forum_seq';
            $spdb = apply_filters('sph_groupview_query', $spdb, $this);
            if (!empty($spThisUser->inspect['q_spGroupView'])) {
                $spdb->inspect = 'spGroupView';
                $spdb->show = true;
                $spThisUser->inspect['q_spGroupView'] = false;
            }
            $records = $spdb->select();
            if ($records) {
                sp_add_cache('group', $records);
            }
        }
        $g = '';
        if ($records) {
            # Set status initially to 'no access' in case current user can view no forums
            $this->groupViewStatus = 'no access';
            $gidx = 0;
            $fidx = 0;
            $sidx = 0;
            $cparent = 0;
            $subPostId = 0;
            # define array to collect data
            $p = array();
            $g = array();
            foreach ($records as $r) {
                $groupid = $r->group_id;
                $forumid = $r->forum_id;
                if (sp_can_view($forumid, 'forum-title')) {
                    if ($gidx == 0 || $g[$gidx]->group_id != $groupid) {
                        # reset status to 'data'
                        $this->groupViewStatus = 'data';
                        $gidx = $groupid;
                        $fidx = 0;
                        $g[$gidx] = new stdClass();
                        $g[$gidx]->group_id = $r->group_id;
                        $g[$gidx]->group_name = sp_filter_title_display($r->group_name);
                        $g[$gidx]->group_desc = sp_filter_title_display($r->group_desc);
                        $g[$gidx]->group_rss = esc_url($r->group_rss);
                        $g[$gidx]->group_icon = sanitize_file_name($r->group_icon);
                        $g[$gidx]->group_message = sp_filter_text_display($r->group_message);
                        $g[$gidx]->group_rss_active = 0;
                        $g[$gidx] = apply_filters('sph_groupview_group_records', $g[$gidx], $r);
                    }
                    if (isset($r->forum_id)) {
                        # Is this a subform?
                        if ($r->parent != 0) {
                            $sidx = $r->forum_id;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx] = new stdClass();
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_id = $r->forum_id;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_name = sp_filter_title_display($r->forum_name);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_slug = $r->forum_slug;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_icon = sanitize_file_name($r->forum_icon);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_icon_new = sanitize_file_name($r->forum_icon_new);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_icon_locked = sanitize_file_name($r->forum_icon_locked);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_disabled = $r->forum_disabled;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->topic_count = $r->topic_count;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_count = $r->post_count;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->parent = $r->parent;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->children = $r->children;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id = $r->post_id;
                            $g[$gidx]->forums[$cparent]->subforums[$sidx]->unread = 0;
                            # check if we can look at posts in moderation - if not swap for 'held' values
                            if (!sp_get_auth('moderate_posts', $r->forum_id)) {
                                $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_count = $r->post_count_held;
                                $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id = $r->post_id_held;
                            }
                            # See if any forums are in the current users newpost list
                            if ($spThisUser->member && isset($spThisUser->newposts['forums'])) {
                                $c = 0;
                                if ($spThisUser->newposts['forums']) {
                                    foreach ($spThisUser->newposts['forums'] as $fnp) {
                                        if ($fnp == $sidx) {
                                            $c++;
                                        }
                                    }
                                }
                                # set the subforum unread count
                                $g[$gidx]->forums[$cparent]->subforums[$sidx]->unread = $c;
                            }
                            # Update top parent counts with subforum counts
                            $g[$gidx]->forums[$cparent]->topic_count_sub += $g[$gidx]->forums[$cparent]->subforums[$sidx]->topic_count;
                            $g[$gidx]->forums[$cparent]->post_count_sub += $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_count;
                            # and what about the most recent post? Is this in a subforum?
                            if ($g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id > $g[$gidx]->forums[$cparent]->post_id && $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id > $subPostId) {
                                # store the alternative forum id in case we need to display the topic data for this one if inc. subs
                                $g[$gidx]->forums[$cparent]->forum_id_sub = $r->forum_id;
                                # add the last post in subforum to the list for stats retrieval
                                $subPostId = $g[$gidx]->forums[$cparent]->subforums[$sidx]->post_id;
                                $p[$r->forum_id] = $subPostId;
                            }
                        } else {
                            # it's a top level forum
                            $subPostId = 0;
                            $fidx = $forumid;
                            $g[$gidx]->forums[$fidx] = new stdClass();
                            $g[$gidx]->forums[$fidx]->forum_id = $r->forum_id;
                            $g[$gidx]->forums[$fidx]->forum_id_sub = 0;
                            $g[$gidx]->forums[$fidx]->forum_name = sp_filter_title_display($r->forum_name);
                            $g[$gidx]->forums[$fidx]->forum_slug = $r->forum_slug;
                            $g[$gidx]->forums[$fidx]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                            $g[$gidx]->forums[$fidx]->forum_desc = sp_filter_title_display($r->forum_desc);
                            $g[$gidx]->forums[$fidx]->forum_status = $r->forum_status;
                            $g[$gidx]->forums[$fidx]->forum_disabled = $r->forum_disabled;
                            $g[$gidx]->forums[$fidx]->forum_icon = sanitize_file_name($r->forum_icon);
                            $g[$gidx]->forums[$fidx]->forum_icon_new = sanitize_file_name($r->forum_icon_new);
                            $g[$gidx]->forums[$fidx]->forum_icon_locked = sanitize_file_name($r->forum_icon_locked);
                            $g[$gidx]->forums[$fidx]->forum_rss_private = $r->forum_rss_private;
                            $g[$gidx]->forums[$fidx]->post_id = $r->post_id;
                            $g[$gidx]->forums[$fidx]->topic_count = $r->topic_count;
                            $g[$gidx]->forums[$fidx]->topic_count_sub = $r->topic_count;
                            $g[$gidx]->forums[$fidx]->post_count = $r->post_count;
                            $g[$gidx]->forums[$fidx]->post_count_sub = $r->post_count;
                            $g[$gidx]->forums[$fidx]->parent = $r->parent;
                            $g[$gidx]->forums[$fidx]->children = $r->children;
                            $g[$gidx]->forums[$fidx]->unread = 0;
                            if (empty($g[$gidx]->forums[$fidx]->post_id)) {
                                $g[$gidx]->forums[$fidx]->post_id = 0;
                            }
                            # Can the user create new topics or should we lock the forum?
                            $g[$gidx]->forums[$fidx]->start_topics = sp_get_auth('start_topics', $r->forum_id);
                            # check if we can look at posts in moderation - if not swap for 'held' values
                            if (!sp_get_auth('moderate_posts', $r->forum_id)) {
                                $g[$gidx]->forums[$fidx]->post_id = $r->post_id_held;
                                $g[$gidx]->forums[$fidx]->post_count = $r->post_count_held;
                                $g[$gidx]->forums[$fidx]->post_count_sub = $r->post_count_held;
                                $thisPostid = $r->post_id_held;
                            } else {
                                $thisPostid = $r->post_id;
                            }
                            # See if any forums are in the current users newpost list
                            if ($spThisUser->member && isset($spThisUser->newposts['forums'])) {
                                $c = 0;
                                if ($spThisUser->newposts['forums']) {
                                    foreach ($spThisUser->newposts['forums'] as $fnp) {
                                        if ($fnp == $fidx) {
                                            $c++;
                                        }
                                    }
                                }
                                $g[$gidx]->forums[$fidx]->unread = $c;
                            }
                            if (empty($r->children)) {
                                $cparent = 0;
                            } else {
                                $cparent = $fidx;
                                $sidx = 0;
                            }
                            # Build post id array for collecting stats at the end
                            if (!empty($thisPostid)) {
                                $p[$fidx] = $thisPostid;
                            }
                            $g[$gidx]->forums[$fidx] = apply_filters('sph_groupview_forum_records', $g[$gidx]->forums[$fidx], $r);
                        }
                        # Build special Group level flag on whether to show group RSS button or not (based on any forum in group having RSS access
                        if (sp_get_auth('view_forum', $r->forum_id) && !$r->forum_rss_private) {
                            $g[$gidx]->group_rss_active = 1;
                        }
                    }
                }
            }
        }
        if ($this->includeStats == true) {
            # Go grab the forum stats and data
            if (!empty($p)) {
                $stats = $this->sp_groupview_stats_query($p);
                if ($stats) {
                    foreach ($g as $gr) {
                        foreach ($gr->forums as $f) {
                            if (!empty($stats[$f->forum_id])) {
                                $s = $stats[$f->forum_id];
                                $f->topic_id = $s->topic_id;
                                $f->topic_name = sp_filter_title_display($s->topic_name);
                                $f->topic_slug = $s->topic_slug;
                                $f->post_id = $s->post_id;
                                $f->post_permalink = sp_build_url($f->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                                $f->post_date = $s->post_date;
                                $f->post_status = $s->post_status;
                                $f->post_index = $s->post_index;
                                # see if we can display the tooltip
                                if (sp_can_view($f->forum_id, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                                    $f->post_tip = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                                } else {
                                    $f->post_tip = '';
                                }
                                $f->user_id = $s->user_id;
                                $f->display_name = sp_filter_name_display($s->display_name);
                                $f->guest_name = sp_filter_name_display($s->guest_name);
                            }
                            # do we need to record a possible subforum substitute topic?
                            $fsub = $f->forum_id_sub;
                            if ($fsub != 0 && !empty($stats[$fsub])) {
                                $s = $stats[$fsub];
                                $f->topic_id_sub = $s->topic_id;
                                $f->topic_name_sub = sp_filter_title_display($s->topic_name);
                                $f->topic_slug_sub = $s->topic_slug;
                                $f->post_id_sub = $s->post_id;
                                $f->post_permalink_sub = sp_build_url($f->subforums[$fsub]->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                                $f->post_date_sub = $s->post_date;
                                $f->post_status_sub = $s->post_status;
                                $f->post_index_sub = $s->post_index;
                                # see if we can display the tooltip
                                if (sp_can_view($fsub, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                                    $f->post_tip_sub = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                                } else {
                                    $f->post_tip_sub = '';
                                }
                                $f->user_id_sub = $s->user_id;
                                $f->display_name_sub = sp_filter_name_display($s->display_name);
                                $f->guest_name_sub = sp_filter_name_display($s->guest_name);
                            }
                            $f = apply_filters('sph_groupview_stats_records', $f, $s);
                        }
                    }
                    unset($stats);
                }
            }
        }
        # Do we need to re-order IDs based on passed in IDs
        if ($groupids && $idOrder) {
            $n = array();
            foreach ($groupids as $gid) {
                if (array_key_exists($gid, $g)) {
                    $n[$gid] = $g[$gid];
                }
            }
            $g = $n;
            unset($n);
        }
        return $g;
    }
    function sp_listview_query($topicIds, $count, $group, $forumIds, $firstPost, $popup)
    {
        global $spThisUser, $spGlobals;
        # If no topic ids and no count then nothjing to do - return empty
        if (empty($topicIds) && $count == 0) {
            return;
        }
        # set popup flag for new posts
        $this->popup = $popup;
        # Do we have enough topic ids to satisfy count?
        if (empty($topicIds) || $count != 0 && count($topicIds) < $count) {
            $topicIds = $this->sp_listview_populate_topicids($topicIds, $forumIds, $count);
        }
        # Do we havwe too many topic ids?
        if ($topicIds && ($count != 0 && count($topicIds) > $count)) {
            $topicIds = array_slice($topicIds, 0, $count, true);
        }
        if (empty($topicIds)) {
            return;
        }
        # Construct the main WHERE clause and then main query
        $where = SFTOPICS . '.topic_id IN (' . implode(',', $topicIds) . ')';
        if ($group) {
            $orderby = 'group_seq, forum_seq, ' . SFTOPICS . '.post_id DESC';
        } else {
            $orderby = SFTOPICS . '.post_id DESC';
        }
        $spdb = new spdbComplex();
        $spdb->table = SFTOPICS;
        $spdb->fields = SFTOPICS . '.forum_id, forum_name, forum_slug, forum_disabled, ' . SFTOPICS . '.topic_id, topic_name, topic_slug, topic_icon, topic_icon_new, ' . SFTOPICS . '.post_count,
								' . SFTOPICS . '.post_id, post_status, post_index, ' . spdb_zone_datetime('post_date') . ',
								guest_name, ' . SFPOSTS . '.user_id, post_content, display_name';
        $spdb->join = array(SFFORUMS . ' ON ' . SFFORUMS . '.forum_id = ' . SFTOPICS . '.forum_id', SFGROUPS . ' ON ' . SFGROUPS . '.group_id = ' . SFFORUMS . '.group_id', SFPOSTS . ' ON ' . SFPOSTS . '.post_id = ' . SFTOPICS . '.post_id');
        $spdb->left_join = array(SFMEMBERS . ' ON ' . SFMEMBERS . '.user_id = ' . SFPOSTS . '.user_id');
        $spdb->where = $where;
        $spdb->orderby = $orderby;
        $spdb = apply_filters('sph_topic_list_query', $spdb, $this);
        $records = $spdb->select();
        # add filters where required plus extra data
        # And the new array
        $list = array();
        if ($records) {
            # check if all forum ids are the same
            $x = current($records);
            $f = $x->forum_id;
            $single = 1;
            foreach ($records as $r) {
                if ($r->forum_id != $f) {
                    $single = 0;
                }
            }
            reset($records);
            $new = '';
            $first = '';
            # Now we can grab the supplementary post records where there may be new posts...
            if ($spThisUser->member) {
                $new = $this->sp_listview_populate_newposts($topicIds);
            }
            # go and grab the first post info if desired
            if ($firstPost) {
                $first = $this->sp_listview_populate_firstposts($topicIds);
            }
            # 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
            $order = 'ASC';
            # default
            if ($spGlobals['display']['posts']['sortdesc']) {
                $order = 'DESC';
            }
            # global override
            $listPos = 1;
            foreach ($records as $r) {
                $show = true;
                # can the user see this forum?
                if (!sp_can_view($r->forum_id, 'topic-title')) {
                    $show = false;
                }
                # if in moderattion can this user approve posts?
                if ($r->post_status != 0 && !sp_get_auth('moderate_posts', $r->forum_id)) {
                    $show = false;
                }
                if ($show) {
                    $t = $r->topic_id;
                    $list[$t] = new stdClass();
                    $list[$t]->forum_id = $r->forum_id;
                    $list[$t]->forum_name = sp_filter_title_display($r->forum_name);
                    $list[$t]->forum_disabled = $r->forum_disabled;
                    $list[$t]->forum_permalink = sp_build_url($r->forum_slug, '', 1, 0);
                    $list[$t]->topic_id = $r->topic_id;
                    $list[$t]->topic_name = sp_filter_title_display($r->topic_name);
                    $list[$t]->topic_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 1, 0);
                    $list[$t]->topic_icon = sanitize_file_name($r->topic_icon);
                    $list[$t]->topic_icon_new = sanitize_file_name($r->topic_icon_new);
                    $list[$t]->post_count = $r->post_count;
                    $list[$t]->post_id = $r->post_id;
                    $list[$t]->post_status = $r->post_status;
                    $list[$t]->post_date = $r->post_date;
                    $list[$t]->user_id = $r->user_id;
                    $list[$t]->guest_name = sp_filter_name_display($r->guest_name);
                    $list[$t]->display_name = sp_filter_name_display($r->display_name);
                    if (sp_can_view($r->forum_id, 'post-content', $spThisUser->ID, $r->user_id)) {
                        $list[$t]->post_tip = $r->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($r->post_content, $r->post_status);
                    } else {
                        $list[$t]->post_tip = '';
                    }
                    $list[$t]->list_position = $listPos;
                    if (empty($r->display_name)) {
                        $list[$t]->display_name = $list[$t]->guest_name;
                    }
                    # Lastly determine the page for the post permalink
                    if ($order == '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);
                    }
                    $r->page = $page;
                    $list[$t]->post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, $r->page, $r->post_id, $r->post_index);
                    $list[$t]->single_forum = $single;
                    # add in any new post details if they exist
                    if (!empty($new) && array_key_exists($t, $new)) {
                        $list[$t]->new_post_count = $new[$t]->new_post_count;
                        $list[$t]->new_post_post_id = $new[$t]->new_post_post_id;
                        $list[$t]->new_post_post_index = $new[$t]->new_post_post_index;
                        $list[$t]->new_post_post_date = $new[$t]->new_post_post_date;
                        $list[$t]->new_post_user_id = $new[$t]->new_post_user_id;
                        $list[$t]->new_post_display_name = $new[$t]->new_post_display_name;
                        $list[$t]->new_post_guest_name = $new[$t]->new_post_guest_name;
                        $list[$t]->new_post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 0, $new[$t]->new_post_post_id, $new[$t]->new_post_post_index);
                        if (empty($new[$t]->new_post_display_name)) {
                            $list[$t]->new_post_display_name = $new[$t]->new_post_guest_name;
                        }
                    }
                    # add the first post info if desired
                    if ($firstPost) {
                        $list[$t]->first_post_permalink = sp_build_url($r->forum_slug, $r->topic_slug, 0, $first[$t]->post_id, 1);
                        $list[$t]->first_post_date = $first[$t]->post_date;
                        $list[$t]->first_user_id = $first[$t]->user_id;
                        $list[$t]->first_guest_name = sp_filter_name_display($first[$t]->guest_name);
                        $list[$t]->first_display_name = sp_filter_name_display($first[$t]->display_name);
                        if (sp_can_view($r->forum_id, 'post-content', $spThisUser->ID, $first[$t]->user_id)) {
                            $list[$t]->first_post_tip = $first[$t]->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($first[$t]->post_content, $first[$t]->post_status);
                        } else {
                            $list[$t]->first_post_tip = '';
                        }
                        if (empty($list[$t]->first_display_name)) {
                            $list[$t]->first_display_name = $list[$t]->first_guest_name;
                        }
                    }
                    $list[$t] = apply_filters('sph_topic_list_record', $list[$t], $r);
                    $listPos++;
                }
            }
            unset($records);
            unset($new);
            unset($first);
        }
        return $list;
    }
Example #3
0
 function sp_forumview_build_subforums($forumid, $f, $fidx, $subs)
 {
     global $spThisUser;
     ksort($subs);
     foreach ($subs as $sub) {
         if (sp_can_view($sub->forum_id, 'topic-title')) {
             $f[$fidx]->subforums[$sub->forum_id] = new stdClass();
             $f[$fidx]->subforums[$sub->forum_id]->top_parent = $fidx;
             $f[$fidx]->subforums[$sub->forum_id]->top_sub_parent = $sub->topSubParent;
             $f[$fidx]->subforums[$sub->forum_id]->forum_id = $sub->forum_id;
             $f[$fidx]->subforums[$sub->forum_id]->forum_id_sub = 0;
             $f[$fidx]->subforums[$sub->forum_id]->forum_name = sp_filter_title_display($sub->forum_name);
             $f[$fidx]->subforums[$sub->forum_id]->forum_permalink = sp_build_url($sub->forum_slug, '', 1, 0);
             $f[$fidx]->subforums[$sub->forum_id]->forum_slug = $sub->forum_slug;
             $f[$fidx]->subforums[$sub->forum_id]->forum_desc = sp_filter_title_display($sub->forum_desc);
             $f[$fidx]->subforums[$sub->forum_id]->forum_status = $sub->forum_status;
             $f[$fidx]->subforums[$sub->forum_id]->forum_disabled = $sub->forum_disabled;
             $f[$fidx]->subforums[$sub->forum_id]->forum_icon = sanitize_file_name($sub->forum_icon);
             $f[$fidx]->subforums[$sub->forum_id]->forum_icon_new = sanitize_file_name($sub->forum_icon_new);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon = sanitize_file_name($sub->topic_icon);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon_new = sanitize_file_name($sub->topic_icon_new);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon_locked = sanitize_file_name($sub->topic_icon_locked);
             $f[$fidx]->subforums[$sub->forum_id]->topic_icon_pinned = sanitize_file_name($sub->topic_icon_pinned);
             $f[$fidx]->subforums[$sub->forum_id]->forum_rss_private = $sub->forum_rss_private;
             $f[$fidx]->subforums[$sub->forum_id]->post_id = $sub->post_id;
             $f[$fidx]->subforums[$sub->forum_id]->post_id_held = $sub->post_id_held;
             $f[$fidx]->subforums[$sub->forum_id]->topic_count = $sub->topic_count;
             $f[$fidx]->subforums[$sub->forum_id]->topic_count_sub = $sub->topic_count;
             $f[$fidx]->subforums[$sub->forum_id]->post_count = $sub->post_count;
             $f[$fidx]->subforums[$sub->forum_id]->post_count_sub = $sub->post_count;
             $f[$fidx]->subforums[$sub->forum_id]->post_count_held = $sub->post_count_held;
             $f[$fidx]->subforums[$sub->forum_id]->parent = $sub->parent;
             $f[$fidx]->subforums[$sub->forum_id]->children = $sub->children;
             $f[$fidx]->subforums[$sub->forum_id]->unread = 0;
             # Can the user create new topics or should we lock the forum?
             $f[$fidx]->subforums[$sub->forum_id]->start_topics = sp_get_auth('start_topics', $sub->forum_id);
             # See if any forums are in the current users newpost list
             if ($spThisUser->member) {
                 $c = 0;
                 if ($spThisUser->newposts && $spThisUser->newposts['forums']) {
                     foreach ($spThisUser->newposts['forums'] as $fnp) {
                         if ($fnp == $sub->forum_id) {
                             $c++;
                         }
                     }
                 }
                 $f[$fidx]->subforums[$sub->forum_id]->unread = $c;
             }
             # check if we can look at posts in moderation - if not swap for 'held' values
             if (!sp_get_auth('moderate_posts', $sub->forum_id)) {
                 $f[$fidx]->subforums[$sub->forum_id]->post_id = $sub->post_id_held;
                 $f[$fidx]->subforums[$sub->forum_id]->post_count = $sub->post_count_held;
                 $f[$fidx]->subforums[$sub->forum_id]->post_count_sub = $sub->post_count_held;
                 $thisPostid = $sub->post_id_held;
             } else {
                 $thisPostid = $sub->post_id;
             }
             # Build post id array for collecting stats at the end
             if (!empty($thisPostid)) {
                 $p[$sub->forum_id] = $thisPostid;
             }
             # if this subforum has a parent that is differemt to the main forum being dislayed in the view
             # then it has to be a nested subforum so do we need to merge the numbers?
             if ($sub->parent != $forumid) {
                 $f[$fidx]->subforums[$sub->parent]->topic_count_sub += $f[$fidx]->subforums[$sub->forum_id]->topic_count;
                 $f[$fidx]->subforums[$sub->parent]->post_count_sub += $f[$fidx]->subforums[$sub->forum_id]->post_count;
                 # and what about the most recent post? Is this in a nested subforum?
                 if ($f[$fidx]->subforums[$sub->forum_id]->post_id > $f[$fidx]->subforums[$sub->parent]->post_id) {
                     # store the alternative forum id in case we need to display the topic data for this one if inc. subs
                     $f[$fidx]->subforums[$sub->parent]->forum_id_sub = $sub->forum_id;
                 }
             }
         }
     }
     # Go grab the sub forum stats and data
     if (!empty($p)) {
         $stats = $this->sp_subforumview_stats_query($p);
         if ($stats) {
             $s = '';
             foreach ($subs as $sub) {
                 if (!empty($stats[$sub->forum_id])) {
                     $s = $stats[$sub->forum_id];
                     $f[$fidx]->subforums[$sub->forum_id]->topic_id = $s->topic_id;
                     $f[$fidx]->subforums[$sub->forum_id]->topic_name = sp_filter_title_display($s->topic_name);
                     $f[$fidx]->subforums[$sub->forum_id]->topic_slug = $s->topic_slug;
                     $f[$fidx]->subforums[$sub->forum_id]->post_id = $s->post_id;
                     $f[$fidx]->subforums[$sub->forum_id]->post_permalink = sp_build_url($f[$fidx]->subforums[$sub->forum_id]->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                     $f[$fidx]->subforums[$sub->forum_id]->post_date = $s->post_date;
                     $f[$fidx]->subforums[$sub->forum_id]->post_status = $s->post_status;
                     $f[$fidx]->subforums[$sub->forum_id]->post_index = $s->post_index;
                     # see if we can display the tooltip
                     if (sp_can_view($sub->forum_id, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                     } else {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip = '';
                     }
                     $f[$fidx]->subforums[$sub->forum_id]->user_id = $s->user_id;
                     $f[$fidx]->subforums[$sub->forum_id]->display_name = sp_filter_name_display($s->display_name);
                     $f[$fidx]->subforums[$sub->forum_id]->guest_name = sp_filter_name_display($s->guest_name);
                 }
                 # do we need to record a possible subforum substitute topic?
                 $fsub = isset($f[$fidx]->subforums[$sub->forum_id]->forum_id_sub) ? $f[$fidx]->subforums[$sub->forum_id]->forum_id_sub : 0;
                 if ($fsub != 0 && !empty($stats[$fsub])) {
                     $s = $stats[$fsub];
                     $f[$fidx]->subforums[$sub->forum_id]->topic_id_sub = $s->topic_id;
                     $f[$fidx]->subforums[$sub->forum_id]->topic_name_sub = sp_filter_title_display($s->topic_name);
                     $f[$fidx]->subforums[$sub->forum_id]->topic_slug_sub = $s->topic_slug;
                     $f[$fidx]->subforums[$sub->forum_id]->post_id_sub = $s->post_id;
                     $f[$fidx]->subforums[$sub->forum_id]->post_permalink_sub = sp_build_url($f[$fidx]->subforums[$fsub]->forum_slug, $s->topic_slug, 0, $s->post_id, $s->post_index);
                     $f[$fidx]->subforums[$sub->forum_id]->post_date_sub = $s->post_date;
                     $f[$fidx]->subforums[$sub->forum_id]->post_status_sub = $s->post_status;
                     $f[$fidx]->subforums[$sub->forum_id]->post_index_sub = $s->post_index;
                     # see if we can display the tooltip
                     if (sp_can_view($fsub, 'post-content', $spThisUser->ID, $s->user_id, $s->topic_id, $s->post_id)) {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip_sub = $s->post_status ? sp_text('Post awaiting moderation') : sp_filter_tooltip_display($s->post_content, $s->post_status);
                     } else {
                         $f[$fidx]->subforums[$sub->forum_id]->post_tip_sub = '';
                     }
                     $f[$fidx]->subforums[$sub->forum_id]->user_id_sub = $s->user_id;
                     $f[$fidx]->subforums[$sub->forum_id]->display_name_sub = sp_filter_name_display($s->display_name);
                     $f[$fidx]->subforums[$sub->forum_id]->guest_name_sub = sp_filter_name_display($s->guest_name);
                 }
                 # allow plugins to add more data to combined subforum/post data structure
                 $f[$fidx]->subforums[$sub->forum_id] = apply_filters('sph_forumview_subforum_records', $f[$fidx]->subforums[$sub->forum_id], $s);
             }
         }
         unset($subs);
         unset($stats);
     }
     return $f;
 }
    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;
    }