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

net_nehmer_static_viewer::get_topic_qb()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.1384

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 20
ccs 9
cts 14
cp 0.6429
crap 6.1384
rs 9.5222
1
<?php
2
/**
3
 * @package net.nehmer.static
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
 * n.n.static site interface class
14
 *
15
 * @package net.nehmer.static
16
 */
17
class net_nehmer_static_viewer extends midcom_baseclasses_components_viewer
18
{
19
    /**
20
     * Initialize the request switch and the content topic.
21
     */
22 81
    public function _on_initialize()
23
    {
24
        // View mode handler, set index viewer according to autoindex setting.
25
        // These, especially the general view handler, must come last, otherwise we'll hide other
26
        // handlers
27 81
        if ($this->_config->get('autoindex')) {
28 1
            $this->_request_switch['autoindex'] = [
29 1
                'handler' => [net_nehmer_static_handler_autoindex::class, 'autoindex'],
30
            ];
31
        } else {
32 80
            $this->_request_switch['index'] = [
33
                'handler' => [net_nehmer_static_handler_view::class, 'view'],
34
            ];
35
        }
36 81
    }
37
38
    /**
39
     * Indexes an article.
40
     *
41
     * @param midcom_db_topic|midcom_core_dbaproxy $topic The topic which we are bound to. If this is not an object, the code
42
     *     tries to load a new topic instance from the database identified by this parameter.
43
     */
44
    public static function index(datamanager $dm, midcom_services_indexer $indexer, $topic)
45
    {
46
        $nav = new midcom_helper_nav();
47
        $node = $nav->get_node($topic->id);
48
49
        $document = $indexer->new_document($dm);
50
        $document->topic_guid = $topic->guid;
51
        $document->topic_url = $node[MIDCOM_NAV_FULLURL];
52
        $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...
53
        $indexer->index($document);
54
    }
55
56
    /**
57
     * Populates the node toolbar depending on the user's rights.
58
     */
59 8
    private function _populate_node_toolbar()
60
    {
61 8
        $buttons = [];
62 8
        $workflow = $this->get_workflow('datamanager');
63 8
        if ($this->_topic->can_do('midgard:create')) {
64 7
            foreach ($this->_request_data['schemadb']->all() as $name => $schema) {
65 7
                $buttons[] = $workflow->get_button("create/{$name}/", [
66 7
                    MIDCOM_TOOLBAR_LABEL => sprintf(
67 7
                        $this->_l10n_midcom->get('create %s'),
68 7
                        $this->_l10n->get($schema->get('description'))
69
                    ),
70 7
                    MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
71 7
                    MIDCOM_TOOLBAR_ACCESSKEY => 'n',
72
                ]);
73
            }
74
        }
75
76 8
        if (   $this->_topic->can_do('midgard:update')
77 8
            && $this->_topic->can_do('midcom:component_config')) {
78 7
            $buttons[] = $workflow->get_button('config/', [
79 7
                MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
80 7
                MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
81 7
                MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
82
            ]);
83
        }
84 8
        $this->_node_toolbar->add_items($buttons);
85 8
    }
86
87
    /**
88
     * The handle callback populates the toolbars.
89
     */
90 8
    public function _on_handle($handler, array $args)
91
    {
92 8
        $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

92
        $this->_request_data['schemadb'] = schemadb::from_path(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
93
94 8
        $this->_populate_node_toolbar();
95 8
    }
96
97 10
    public static function get_topic_qb(int $id, string $sort_property = null) : midcom_core_querybuilder
98
    {
99 10
        $qb = midcom_db_article::new_query_builder();
100 10
        $qb->add_constraint('topic', '=', $id);
101
102 10
        if ($sort_property) {
103 10
            $sort_order = 'ASC';
104 10
            if (str_starts_with($sort_property, 'reverse ')) {
105
                $sort_order = 'DESC';
106
                $sort_property = substr($sort_property, strlen('reverse '));
107
            }
108 10
            if (!str_contains($sort_property, 'metadata.')) {
109
                $ref = midcom_helper_reflector::get('midgard_article');
110
                if (!$ref->property_exists($sort_property)) {
111
                    $sort_property = 'metadata.' . $sort_property;
112
                }
113
            }
114 10
            $qb->add_order($sort_property, $sort_order);
115
        }
116 10
        return $qb;
117
    }
118
}
119