Passed
Push — master ( de7a06...bc2447 )
by Andreas
24:19
created

net_nehmer_blog_viewer::_on_handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
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
use midcom\datamanager\schemadb;
11
12
/**
13
 * Blog site interface class
14
 *
15
 * @package net.nehmer.blog
16
 */
17
class net_nehmer_blog_viewer extends midcom_baseclasses_components_viewer
18
{
19
    /**
20
     * Initialize the request switch and the content topic.
21
     */
22 22
    public function _on_initialize()
23
    {
24 22
        if ($this->_config->get('view_in_url')) {
25
            $this->_request_switch['view-raw'] = [
26
                'handler' => [net_nehmer_blog_handler_view::class, 'view'],
27
                'fixed_args' => ['view', 'raw'],
28
                'variable_args' => 1,
29
            ];
30
            $this->_request_switch['view'] = [
31
                'handler' => [net_nehmer_blog_handler_view::class, 'view'],
32
                'fixed_args' => 'view',
33
                'variable_args' => 1,
34
            ];
35
        }
36
37 22
        if ($this->_config->get('rss_subscription_enable')) {
38 5
            net_nemein_rss_manage::register_plugin($this);
39
        }
40 22
    }
41
42
    /**
43
     * Adds the RSS Feed LINK head elements.
44
     */
45 16
    private function _add_link_head()
46
    {
47 16
        if ($this->_config->get('rss_enable')) {
48 16
            midcom::get()->head->add_link_head([
49 16
                'rel'   => 'alternate',
50 16
                'type'  => 'application/rss+xml',
51 16
                'title' => $this->_l10n->get('rss 2.0 feed'),
52 16
                'href'  => midcom::get()->get_host_name() . midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . 'rss.xml',
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

52
                'href'  => midcom::get()->get_host_name() . /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . 'rss.xml',
Loading history...
53
            ]);
54 16
            midcom::get()->head->add_link_head([
55 16
                'rel'   => 'alternate',
56 16
                'type'  => 'application/atom+xml',
57 16
                'title' => $this->_l10n->get('atom feed'),
58 16
                'href'  => midcom::get()->get_host_name() . midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . 'atom.xml',
59
            ]);
60
        }
61 16
    }
62
63
    /**
64
     * Populates the node toolbar depending on the user's rights.
65
     */
66 16
    private function _populate_node_toolbar()
67
    {
68 16
        $buttons = [];
69 16
        $workflow = $this->get_workflow('datamanager');
70 16
        if ($this->_topic->can_do('midgard:create')) {
71 14
            foreach ($this->_request_data['schemadb']->all() as $name => $schema) {
72 14
                $buttons[] = $workflow->get_button("create/{$name}/", [
73 14
                    MIDCOM_TOOLBAR_LABEL => sprintf(
74 14
                        $this->_l10n_midcom->get('create %s'),
75 14
                        $this->_l10n->get($schema->get('description'))
76
                    ),
77 14
                    MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
78 14
                    MIDCOM_TOOLBAR_ACCESSKEY => 'n',
79
                ]);
80
            }
81
        }
82
83 16
        if ($this->_config->get('rss_subscription_enable')) {
84
            net_nemein_rss_manage::add_toolbar_buttons($this->_node_toolbar, $this->_topic->can_do('midgard:create'));
85
        }
86
87 16
        if (   $this->_topic->can_do('midgard:update')
88 16
            && $this->_topic->can_do('midcom:component_config')) {
89 14
            $buttons[] = $workflow->get_button('config/', [
90 14
                MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
91 14
                MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
92 14
                MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
93
            ]);
94
        }
95 16
        $this->_node_toolbar->add_items($buttons);
96 16
    }
97
98 16
    public function _on_handle($handler, array $args)
99
    {
100 16
        $this->_request_data['schemadb'] = schemadb::from_path($this->_config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb') can also be of type false; however, parameter $path of midcom\datamanager\schemadb::from_path() 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

100
        $this->_request_data['schemadb'] = schemadb::from_path(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
101 16
        $this->_add_categories();
102
103 16
        $this->_add_link_head();
104 16
        $this->_populate_node_toolbar();
105 16
    }
106
107
    /**
108
     * Populate the categories configured for the topic into the schemas
109
     */
110 16
    private function _add_categories()
111
    {
112 16
        $this->_request_data['categories'] = [];
113 16
        if ($this->_config->get('categories') != '') {
114
            $this->_request_data['categories'] = explode(',', $this->_config->get('categories'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('categories') can also be of type false; however, parameter $string of explode() 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

114
            $this->_request_data['categories'] = explode(',', /** @scrutinizer ignore-type */ $this->_config->get('categories'));
Loading history...
115
116
            foreach ($this->_request_data['schemadb']->all() as $schema) {
117
                if (   $schema->has_field('categories')
118
                    && $schema->get_field('categories')['type'] == 'select') {
119
                    // TODO: Merge schema local and component config categories?
120
                    $options = array_combine($this->_request_data['categories'], $this->_request_data['categories']);
121
                    $schema->get_field('categories')['type_config']['options'] = $options;
122
                }
123
            }
124
        }
125 16
    }
126
127
    /**
128
     * Indexes an article.
129
     *
130
     * @param midcom_db_topic|midcom_core_dbaproxy $topic The topic which we are bound to. If this is not an object, the code
131
     *     tries to load a new topic instance from the database identified by this parameter.
132
     */
133 1
    public static function index(datamanager $dm, midcom_services_indexer $indexer, $topic)
134
    {
135 1
        $config = new midcom_helper_configuration($topic, 'net.nehmer.blog');
136
137 1
        if ($config->get('disable_indexing')) {
138
            return;
139
        }
140
141 1
        $nav = new midcom_helper_nav();
142 1
        $node = $nav->get_node($topic->id);
143
144 1
        $document = $indexer->new_document($dm);
145 1
        $document->topic_guid = $topic->guid;
146 1
        $document->component = $topic->component;
0 ignored issues
show
Bug Best Practice introduced by
The property component does not exist on midcom_core_dbaproxy. Since you implemented __get, consider adding a @property annotation.
Loading history...
147 1
        $document->topic_url = $node[MIDCOM_NAV_FULLURL];
148 1
        $indexer->index($document);
149 1
    }
150
}
151