Completed
Push — master ( 0021de...b72ee8 )
by Andreas
17:13
created

net_nehmer_static_viewer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 71.7%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 101
ccs 38
cts 53
cp 0.717
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_initialize() 0 12 2
A index() 0 11 1
A _populate_node_toolbar() 0 26 5
A _on_handle() 0 5 1
A get_topic_qb() 0 20 5
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 78
    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 78
        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 77
            $this->_request_switch['index'] = [
33
                'handler' => [net_nehmer_static_handler_view::class, 'view'],
34
            ];
35
        }
36 78
    }
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->read_metadata_from_object($dm->get_storage()->get_value());
53
        $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...
54
        $indexer->index($document);
55
    }
56
57
    /**
58
     * Populates the node toolbar depending on the user's rights.
59
     */
60 8
    private function _populate_node_toolbar()
61
    {
62 8
        $buttons = [];
63 8
        $workflow = $this->get_workflow('datamanager');
64 8
        if ($this->_topic->can_do('midgard:create')) {
65 7
            foreach ($this->_request_data['schemadb']->all() as $name => $schema) {
66 7
                $buttons[] = $workflow->get_button("create/{$name}/", [
67 7
                    MIDCOM_TOOLBAR_LABEL => sprintf(
68 7
                        $this->_l10n_midcom->get('create %s'),
69 7
                        $this->_l10n->get($schema->get('description'))
70
                    ),
71 7
                    MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
72 7
                    MIDCOM_TOOLBAR_ACCESSKEY => 'n',
73
                ]);
74
            }
75
        }
76
77 8
        if (   $this->_topic->can_do('midgard:update')
78 8
            && $this->_topic->can_do('midcom:component_config')) {
79 7
            $buttons[] = $workflow->get_button('config/', [
80 7
                MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
81 7
                MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
82 7
                MIDCOM_TOOLBAR_GLYPHICON => 'wrench',
83
            ]);
84
        }
85 8
        $this->_node_toolbar->add_items($buttons);
86 8
    }
87
88
    /**
89
     * The handle callback populates the toolbars.
90
     */
91 8
    public function _on_handle($handler, array $args)
92
    {
93 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

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