Habari\Theme::act_display PHP Method

act_display() public method

This is the default, generic function to grab posts. To "filter" the posts retrieved, simply pass any filters to the handler_vars variables associated with the post retrieval. For instance, to filter by tag, ensure that handler_vars['tag'] contains the tag to filter by. Simple as that.
public act_display ( $paramarray = ['user_filters' => []] )
    public function act_display($paramarray = array('user_filters' => array()))
    {
        Utils::check_request_method(array('GET', 'HEAD', 'POST'));
        // Get any full-query parameters
        $possible = array('user_filters', 'fallback', 'posts', 'post', 'content_type');
        foreach ($possible as $varname) {
            if (isset($paramarray[$varname])) {
                ${$varname} = $paramarray[$varname];
            }
        }
        /**
         * Since handler_vars no longer contains $_GET and $_POST, we have broken out our valid filters into
         * an array based upon where we should expect them to be. We then only merge those specific pieces together.
         *
         * These are ordered so that handler vars gets overwritten by POST, which gets overwritten by GET, should the
         * same key exist multiple places. This seemed logical to me at the time, but needs further thought.
         */
        $where_filters = array();
        $where_filters_hv = Controller::get_handler_vars()->filter_keys($this->valid_filters['handler_vars']);
        $where_filters_post = $_POST->filter_keys($this->valid_filters['POST']);
        $where_filters_get = $_GET->filter_keys($this->valid_filters['GET']);
        $where_filters = $where_filters_hv->merge($where_filters_post, $where_filters_get);
        $where_filters['vocabulary'] = array();
        if (array_key_exists('tag', $where_filters)) {
            $tags = Tags::parse_url_tags($where_filters['tag']);
            $not_tag = $tags['exclude_tag'];
            $all_tag = $tags['include_tag'];
            if (count($not_tag) > 0) {
                $where_filters['vocabulary'] = array_merge($where_filters['vocabulary'], array(Tags::vocabulary()->name . ':not:term' => $not_tag));
            }
            if (count($all_tag) > 0) {
                $where_filters['vocabulary'] = array_merge($where_filters['vocabulary'], array(Tags::vocabulary()->name . ':all:term' => $all_tag));
            }
            $where_filters['tag_slug'] = Utils::slugify($where_filters['tag']);
            unset($where_filters['tag']);
        }
        if (!isset($_GET['preview'])) {
            $where_filters['status'] = Post::status('published');
        }
        if (!isset($posts)) {
            $user_filters = Plugins::filter('template_user_filters', $user_filters);
            // Work around the tags parameters to Posts::get() being subsumed by the vocabulary parameter
            if (isset($user_filters['not:tag'])) {
                $user_filters['vocabulary'] = array(Tags::vocabulary()->name . ':not:term' => $user_filters['not:tag']);
                unset($user_filters['not:tag']);
            }
            if (isset($user_filters['tag'])) {
                $user_filters['vocabulary'] = array(Tags::vocabulary()->name . ':term_display' => $user_filters['tag']);
                unset($user_filters['tag']);
            }
            $where_filters = $where_filters->merge($user_filters);
            $where_filters = Plugins::filter('template_where_filters', $where_filters);
            $posts = Posts::get($where_filters);
        }
        $this->assign('posts', $posts);
        if ($posts !== false && count($posts) > 0) {
            if (count($posts) == 1) {
                $post = $posts instanceof Post ? $posts : reset($posts);
                Stack::add('body_class', Post::type_name($post->content_type) . '-' . $post->id);
                Stack::add('body_class', 'single');
            } else {
                $post = reset($posts);
                Stack::add('body_class', 'multiple');
            }
            $this->assign('post', $post);
            $type = Post::type_name($post->content_type);
        } elseif ($posts === false || isset($where_filters['page']) && $where_filters['page'] > 1 && count($posts) == 0) {
            if ($this->template_exists('404')) {
                $fallback = array('404');
                // Replace template variables with the 404 rewrite rule
                $this->request->{URL::get_matched_rule()->name} = false;
                $this->request->{URL::set_404()->name} = true;
                $this->matched_rule = URL::get_matched_rule();
                // 404 status header sent in act_display_404, but we're past
                // that, so send it now.
                header('HTTP/1.1 404 Not Found', true, 404);
            } else {
                $this->display('header');
                echo '<h2>';
                _e("Whoops! 404. The page you were trying to access is not really there. Please try again.");
                echo '</h2>';
                header('HTTP/1.1 404 Not Found', true, 404);
                $this->display('footer');
                die;
            }
        }
        $extract = $where_filters->filter_keys('page', 'type', 'id', 'slug', 'posttag', 'year', 'month', 'day', 'tag', 'tag_slug');
        foreach ($extract as $key => $value) {
            ${$key} = $value;
        }
        $this->assign('page', isset($page) ? $page : 1);
        if (!isset($fallback)) {
            // Default fallbacks based on the number of posts
            $fallback = array('{$type}.{$id}', '{$type}.{$slug}', '{$type}.tag.{$posttag}');
            if (count($posts) > 1) {
                $fallback[] = '{$type}.multiple';
                $fallback[] = 'multiple';
            } else {
                $fallback[] = '{$type}.single';
                $fallback[] = 'single';
            }
        }
        $searches = array('{$id}', '{$slug}', '{$year}', '{$month}', '{$day}', '{$type}', '{$tag}');
        $replacements = array(isset($post) && $post instanceof Post ? $post->id : '-', isset($post) && $post instanceof Post ? $post->slug : '-', isset($year) ? $year : '-', isset($month) ? $month : '-', isset($day) ? $day : '-', isset($type) ? $type : '-', isset($tag_slug) ? $tag_slug : '-');
        $fallback[] = 'home';
        $fallback = Plugins::filter('template_fallback', $fallback, $posts, isset($post) ? $post : null);
        $fallback = array_values(array_unique(MultiByte::str_replace($searches, $replacements, $fallback)));
        for ($z = 0; $z < count($fallback); $z++) {
            if (MultiByte::strpos($fallback[$z], '{$posttag}') !== false && isset($post) && $post instanceof Post) {
                $replacements = array();
                if ($alltags = $post->tags) {
                    foreach ($alltags as $current_tag) {
                        $replacements[] = MultiByte::str_replace('{$posttag}', $current_tag->term, $fallback[$z]);
                    }
                    array_splice($fallback, $z, 1, $replacements);
                } else {
                    break;
                }
            }
        }
        return $this->display_fallback($fallback);
    }