Passed
Push — master ( 335385...121bf2 )
by Andreas
18:20
created

net_nehmer_blog_handler::get_url()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.3949

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 3
nop 2
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
crap 6.3949
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.blog
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Handler addons
11
 *
12
 * @package net.nehmer.blog
13
 */
14
trait net_nehmer_blog_handler
15
{
16 6
    public function get_url(midcom_db_article $article, bool $allow_external = false) : string
17
    {
18 6
        if (   $allow_external
19 6
            && $this->_config->get('link_to_external_url')
20 6
            && !empty($article->url)) {
21
            return $article->url;
22
        }
23
24 6
        $view_url = $article->name ?: $article->guid;
25
26 6
        if ($this->_config->get('view_in_url')) {
27
            $view_url = 'view/' . $view_url;
28
        }
29 6
        return $view_url . '/';
30
    }
31
32
    /**
33
     * Simple helper, gets the last modified timestamp of the topic combination
34
     * specified.
35
     */
36 9
    public function get_last_modified() : int
37
    {
38
        // Get last modified timestamp
39 9
        $qb = midcom_db_article::new_query_builder();
40 9
        $this->article_qb_constraints($qb);
41 9
        $qb->add_order('metadata.revised', 'DESC');
42 9
        $qb->set_limit(1);
43
44 9
        $articles = $qb->execute();
45
46 9
        if (array_key_exists(0, $articles)) {
47 9
            return max($this->_topic->metadata->revised, $articles[0]->metadata->revised);
48
        }
49
        return $this->_topic->metadata->revised;
50
    }
51
52
    /**
53
     * Sets the constraints for QB for articles
54
     *
55
     * @param midcom_core_querybuilder $qb The QB object
56
     */
57 10
    public function article_qb_constraints($qb)
58
    {
59 10
        $topic_guids = [$this->_topic->guid];
60
61
        // Resolve any other topics we may need
62 10
        if ($list_from_folders = $this->_config->get('list_from_folders')) {
63
            // We have specific folders to list from, therefore list from them and current node
64
            $guids = explode('|', $list_from_folders);
65
            $topic_guids = array_merge($topic_guids, array_filter($guids, 'mgd_is_guid'));
66
        }
67
68 10
        $qb->add_constraint('topic.guid', 'IN', $topic_guids);
69
70 10
        if (   count($topic_guids) > 1
71 10
            && $list_from_folders_categories = $this->_config->get('list_from_folders_categories')) {
72
            $list_from_folders_categories = explode(',', $list_from_folders_categories);
73
74
            $qb->begin_group('OR');
75
            $qb->add_constraint('topic.guid', '=', $topic_guids[0]);
76
            $qb->begin_group('OR');
77
            foreach ($list_from_folders_categories as $category) {
78
                $this->apply_category_constraint($qb, $category);
79
            }
80
            $qb->end_group();
81
            $qb->end_group();
82
        }
83
84 10
        net_nehmer_blog_navigation::add_scheduling_constraints($qb, $this->_config);
85 10
        $qb->add_constraint('up', '=', 0);
86 10
    }
87
88 3
    public function apply_category_constraint($qb, string $category)
89
    {
90 3
        if ($category = trim($category)) {
91 3
            $qb->begin_group('OR');
92 3
            $qb->add_constraint('extra1', 'LIKE', "%|{$category}|%");
93 3
            $qb->add_constraint('extra1', '=', $category);
94 3
            $qb->end_group();
95
        }
96
    }
97
}