Passed
Push — master ( 8997f4...d76e03 )
by Andreas
73:31 queued 35:13
created

org_openpsa_calendar_interface   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 111
ccs 40
cts 60
cp 0.6667
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create_root_event() 0 26 3
A resolve_object_link() 0 6 2
A get_create_button() 0 10 2
A find_root_event() 0 27 4
A _on_reindex() 0 12 1
A get_viewer_attributes() 0 7 2
1
<?php
2
/**
3
 * @package org.openpsa.calendar
4
 * @author Nemein Oy http://www.nemein.com/
5
 * @copyright Nemein Oy http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
11
/**
12
 * OpenPSA group calendar
13
 *
14
 * @package org.openpsa.calendar
15
 */
16
class org_openpsa_calendar_interface extends midcom_baseclasses_components_interface
17
implements midcom_services_permalinks_resolver
18
{
19 1
    public static function create_root_event() : org_openpsa_calendar_event_dba
20
    {
21 1
        midcom::get()->auth->request_sudo('org.openpsa.calendar');
22 1
        $event = new org_openpsa_calendar_event_dba();
23 1
        $event->up = 0;
24 1
        $event->title = '__org_openpsa_calendar';
25
        //Fill in dummy dates to get around date range error
26 1
        $event->start = time();
27 1
        $event->end = time() + 3600;
28
29 1
        $ret = $event->create();
30 1
        midcom::get()->auth->drop_sudo();
31 1
        if (!$ret) {
32
            debug_add('Failed to create OpenPSA root event, reason ' . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR);
33
            throw new midcom_error('Failed to create the root event');
34
        }
35
36 1
        $siteconfig = org_openpsa_core_siteconfig::get_instance();
37 1
        $topic_guid = $siteconfig->get_node_guid('org.openpsa.calendar');
38
39 1
        if ($topic_guid) {
40 1
            $topic = new midcom_db_topic($topic_guid);
41 1
            $topic->set_parameter('org.openpsa.calendar', 'calendar_root_event', $event->guid);
42
        }
43
44 1
        return $event;
45
    }
46
47 14
    public static function find_root_event() : org_openpsa_calendar_event_dba
48
    {
49 14
        $config = midcom_baseclasses_components_configuration::get('org.openpsa.calendar', 'config');
50 14
        $root_guid = $config->get('calendar_root_event');
51
52 14
        if (mgd_is_guid($root_guid)) {
53 3
            return org_openpsa_calendar_event_dba::get_cached($root_guid);
54
        }
55
        // Check for calendar event tree.
56 14
        $qb = org_openpsa_calendar_event_dba::new_query_builder();
57 14
        $qb->add_constraint('title', '=', '__org_openpsa_calendar');
58 14
        $qb->add_constraint('up', '=', 0);
59 14
        if ($ret = $qb->execute()) {
60 13
            $root_event = $ret[0];
61
        } else {
62 1
            debug_add("OpenPSA Calendar root event could not be found", MIDCOM_LOG_ERROR);
63
            //Attempt to auto-initialize
64 1
            $root_event = self::create_root_event();
65
        }
66
67 14
        $siteconfig = org_openpsa_core_siteconfig::get_instance();
68 14
        if ($topic_guid = $siteconfig->get_node_guid('org.openpsa.calendar')) {
69 14
            $topic = new midcom_db_topic($topic_guid);
70 14
            $topic->set_parameter('org.openpsa.calendar', 'calendar_root_event', $root_event->guid);
71 14
            $config->set('calendar_root_event', $root_event->guid);
72
        }
73 14
        return $root_event;
74
    }
75
76
    /**
77
     * Prepare the indexer client
78
     */
79
    public function _on_reindex($topic, midcom_helper_configuration $config, midcom_services_indexer &$indexer)
80
    {
81
        $root_event = self::find_root_event();
82
83
        $qb = org_openpsa_calendar_event_dba::new_query_builder();
84
        $qb->add_constraint('up', '=', $root_event->id);
85
        $dm = datamanager::from_schemadb($config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('schemadb') can also be of type false; however, parameter $path of midcom\datamanager\datamanager::from_schemadb() 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

85
        $dm = datamanager::from_schemadb(/** @scrutinizer ignore-type */ $config->get('schemadb'));
Loading history...
86
87
        $indexer = new org_openpsa_calendar_midcom_indexer($topic, $indexer);
88
        $indexer->add_query('events', $qb, $dm);
89
90
        return $indexer;
91
    }
92
93
    /**
94
     * Returns button config for opening the new event popup
95
     */
96 2
    public static function get_create_button($node, string $url) : array
97
    {
98 2
        if (empty($node[MIDCOM_NAV_FULLURL])) {
99
            throw new midcom_error('given node is not valid');
100
        }
101
102 2
        $workflow = new midcom\workflow\datamanager;
103 2
        return $workflow->get_button($node[MIDCOM_NAV_ABSOLUTEURL] . "event/new/" . $url, [
104 2
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('create event', 'org.openpsa.calendar'),
105 2
            MIDCOM_TOOLBAR_GLYPHICON => 'calendar-plus-o',
106
        ]);
107
    }
108
109
    /**
110
     * Returns attribute string for opening the event popup
111
     */
112
    public static function get_viewer_attributes(string $guid, array $node) : string
113
    {
114
        if (empty($node[MIDCOM_NAV_FULLURL])) {
115
            throw new midcom_error('given node is not valid');
116
        }
117
        $workflow = new midcom\workflow\viewer;
118
        return $workflow->render_attributes() . ' href="' . $node[MIDCOM_NAV_FULLURL] . 'event/' . $guid . '/"';
119
    }
120
121
    public function resolve_object_link(midcom_db_topic $topic, midcom_core_dbaobject $object) : ?string
122
    {
123
        if ($object instanceof org_openpsa_calendar_event_dba) {
124
            return "event/{$object->guid}/";
125
        }
126
        return null;
127
    }
128
}
129