Passed
Push — master ( 366a1b...cc2a52 )
by Andreas
23:47
created

net_nehmer_blog_handler_index::_seek_comments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 24
ccs 0
cts 12
cp 0
crap 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.blog
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
11
/**
12
 * Blog Index handler page handler
13
 *
14
 * Shows the configured number of postings with their abstracts.
15
 *
16
 * @package net.nehmer.blog
17
 */
18
class net_nehmer_blog_handler_index extends midcom_baseclasses_components_handler
19
{
20
    use net_nehmer_blog_handler;
0 ignored issues
show
introduced by
The trait net_nehmer_blog_handler requires some properties which are not provided by net_nehmer_blog_handler_index: $admin, $name, $auth, $url, $guid, $revised, $metadata, $user
Loading history...
21
22
    /**
23
     * The articles to display
24
     *
25
     * @var Array
26
     */
27
    private $_articles;
28
29
    /**
30
     * Shows the autoindex list. Nothing to do in the handle phase except setting last modified
31
     * dates.
32
     */
33 5
    public function _handler_index(string $handler_id, array $args, array &$data)
34
    {
35 5
        if ($handler_id == 'ajax-latest') {
36 1
            midcom::get()->skip_page_style = true;
37
        }
38
39 5
        $data['datamanager'] = new datamanager($data['schemadb']);
40 5
        $qb = new org_openpsa_qbpager(midcom_db_article::class, 'net_nehmer_blog_index');
41 5
        $data['qb'] = $qb;
42 5
        $this->article_qb_constraints($qb);
0 ignored issues
show
Bug introduced by
$qb of type org_openpsa_qbpager is incompatible with the type midcom_core_querybuilder expected by parameter $qb of net_nehmer_blog_handler_...rticle_qb_constraints(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $this->article_qb_constraints(/** @scrutinizer ignore-type */ $qb);
Loading history...
43
44
        // Set default page title
45 5
        $data['page_title'] = $this->_topic->extra;
46
47
        // Filter by categories
48 5
        if (in_array($handler_id, ['index-category', 'latest-category'])) {
49 2
            $data['category'] = trim(strip_tags($args[0]));
50
51 2
            $this->_process_category_constraint($qb);
52
        }
53
54 5
        $qb->add_order('metadata.published', 'DESC');
55
56 5
        if (in_array($handler_id, ['latest', 'ajax-latest'])) {
57 2
            $qb->results_per_page = $args[0];
58 3
        } elseif ($handler_id == 'latest-category') {
59 1
            $qb->results_per_page = $args[1];
60
        } else {
61 2
            $qb->results_per_page = $this->_config->get('index_entries');
62
        }
63
64 5
        $this->_articles = $qb->execute();
65
66 5
        midcom::get()->metadata->set_request_metadata($this->get_last_modified(), $this->_topic->guid);
67
68 5
        if ($qb->get_current_page() > 1) {
69
            $this->add_breadcrumb(
70
                midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
0 ignored issues
show
Bug introduced by
It seems like midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) can also be of type false; however, parameter $url of midcom_baseclasses_compo...ndler::add_breadcrumb() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX),
Loading history...
71
                sprintf($this->_i18n->get_string('page %s', 'org.openpsa.qbpager'), $qb->get_current_page())
72
            );
73
        }
74 5
    }
75
76 2
    private function _process_category_constraint($qb)
77
    {
78 2
        if (!in_array($this->_request_data['category'], $this->_request_data['categories'])) {
79 2
            if (!$this->_config->get('categories_custom_enable')) {
80
                throw new midcom_error('Custom categories are not allowed');
81
            }
82
            // TODO: Check here if there are actually items in this cat?
83
        }
84
85 2
        $this->apply_category_constraint($qb, $this->_request_data['category']);
86
87
        // Add category to title
88 2
        $this->_request_data['page_title'] = sprintf($this->_l10n->get('%s category %s'), $this->_topic->extra, $this->_request_data['category']);
89 2
        midcom::get()->head->set_pagetitle($this->_request_data['page_title']);
90
91
        // Activate correct leaf
92 2
        if (   $this->_config->get('show_navigation_pseudo_leaves')
93 2
            && in_array($this->_request_data['category'], $this->_request_data['categories'])) {
94
            $this->set_active_leaf($this->_topic->id . '_CAT_' . $this->_request_data['category']);
95
        }
96
97
        // Add RSS feed to headers
98 2
        if ($this->_config->get('rss_enable')) {
99 2
            midcom::get()->head->add_link_head([
100 2
                'rel'   => 'alternate',
101 2
                'type'  => 'application/rss+xml',
102 2
                'title' => $this->_l10n->get('rss 2.0 feed') . ": {$this->_request_data['category']}",
103 2
                'href'  => midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "feeds/category/{$this->_request_data['category']}/",
0 ignored issues
show
Bug introduced by
Are you sure midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                'href'  => /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "feeds/category/{$this->_request_data['category']}/",
Loading history...
104
            ]);
105
        }
106 2
    }
107
108
    /**
109
     * Displays the index page
110
     *
111
     * @param mixed $handler_id The ID of the handler.
112
     * @param array $data The local request data.
113
     */
114 5
    public function _show_index($handler_id, array &$data)
115
    {
116 5
        $data['index_fulltext'] = $this->_config->get('index_fulltext');
117
118 5
        if ($this->_config->get('ajax_comments_enable')) {
119
            if ($node = net_nehmer_comments_interface::get_node($this->_topic, $this->_config->get('comments_topic'))) {
120
                $data['ajax_comments_enable'] = true;
121
                $data['base_ajax_comments_url'] = $node[MIDCOM_NAV_RELATIVEURL] . "comment/";
122
            }
123
        }
124
125 5
        midcom_show_style('index-start');
126
127 5
        $data['comments_enable'] = (bool) $this->_config->get('comments_enable');
128
129 5
        if ($this->_articles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_articles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
130 3
            $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
131 3
            $total_count = count($this->_articles);
132 3
            $data['article_count'] = $total_count;
133 3
            foreach ($this->_articles as $article_counter => $article) {
134
                try {
135 3
                    $data['datamanager']->set_storage($article);
136
                } catch (midcom_error $e) {
137
                    $e->log();
138
                    continue;
139
                }
140
141 3
                $data['article'] = $article;
142 3
                $data['article_counter'] = $article_counter;
143
144 3
                $data['local_view_url'] = $prefix . $this->get_url($article);
0 ignored issues
show
Bug introduced by
Are you sure $prefix of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
                $data['local_view_url'] = /** @scrutinizer ignore-type */ $prefix . $this->get_url($article);
Loading history...
145 3
                $data['view_url'] = $this->get_url($article, true);
146 3
                if (!preg_match('/^http(s):\/\//', $data['view_url'])) {
147 3
                    $data['view_url'] = $prefix . $data['view_url'];
148
                }
149 3
                $data['linked'] = ($article->topic !== $this->_topic->id);
150 3
                if ($data['linked']) {
151
                    $nap = new midcom_helper_nav();
152
                    $data['node'] = $nap->get_node($article->topic);
153
                }
154
155 3
                midcom_show_style('index-item');
156
            }
157
        } else {
158 2
            midcom_show_style('index-empty');
159
        }
160
161 5
        midcom_show_style('index-end');
162 5
    }
163
}
164