Passed
Push — master ( 366a1b...cc2a52 )
by Andreas
23:47
created

net_nehmer_comments_interface::get_node()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 24
ccs 0
cts 12
cp 0
crap 20
rs 9.8666
1
<?php
2
/**
3
 * @package net.nehmer.comments
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
/**
10
 * Comments component.
11
 *
12
 * This is a component geared for communities offering a way to add comments on arbitrary
13
 * pages. It is primarily geared for dl'ed usage. Its regular welcome URL method only
14
 * shows the configuration interface, commenting the comments topic is prohibited as well.
15
 *
16
 * The component stores the data in its own table, indexed by the object guid they are
17
 * bound to. There is no threading support, comments are ordered by creation date.
18
 *
19
 * Commenting is currently only allowed for registered users for security reasons.
20
 * The user's name and E-Mail will be stored along with the created information in the
21
 * Metadata in case that the user gets deleted.
22
 *
23
 * <b>Install instructions</b>
24
 *
25
 * Just create a topic with this component assigned to it. I recommend dropping it out of
26
 * your navigation, as the component will by dynamically_loaded always, and the topic
27
 * itself is only there for configuration purposes.
28
 *
29
 * In your component (or style), add a DL line like this wherever you want the comment
30
 * feature available:
31
 *
32
 * midcom::get()->dynamic_load('/$path_to_comments_topic/comment/$guid');
33
 *
34
 * $guid is the GUID of the object you're commenting.
35
 *
36
 * @todo Approval
37
 *
38
 * @package net.nehmer.comments
39
 */
40
class net_nehmer_comments_interface extends midcom_baseclasses_components_interface
41
{
42
    /**
43
     * Try to find a comments node (cache results)
44
     */
45
    public static function get_node(midcom_db_topic $topic, $node_id) : ?array
46
    {
47
        if ($node_id) {
48
            try {
49
                $comments_topic = new midcom_db_topic($node_id);
50
            } catch (midcom_error $e) {
51
                return null;
52
            }
53
54
            // We got a topic. Make it a NAP node
55
            $nap = new midcom_helper_nav();
56
            return $nap->get_node($comments_topic->id);
57
        }
58
59
        // No comments topic specified, autoprobe
60
        $node = midcom_helper_misc::find_node_by_component('net.nehmer.comments');
61
62
        // Cache the data
63
        if (midcom::get()->auth->request_sudo($topic->component)) {
64
            $topic->set_parameter($topic->component, 'comments_topic', $node[MIDCOM_NAV_GUID]);
65
            midcom::get()->auth->drop_sudo();
66
        }
67
68
        return $node;
69
    }
70
71
    /**
72
     * The delete handler will drop all entries associated with any deleted object
73
     * so that our DB is clean.
74
     *
75
     * Uses SUDO to ensure privileges.
76
     */
77 184
    public function _on_watched_dba_delete(midcom_core_dbaobject $object)
78
    {
79 184
        $sudo = midcom::get()->auth->request_sudo($this->_component);
80
81 184
        $result = net_nehmer_comments_comment::list_by_objectguid($object->guid);
82
83 184
        foreach ($result as $comment) {
84
            $comment->delete();
85
        }
86
87 184
        if ($sudo) {
88 184
            midcom::get()->auth->drop_sudo();
89
        }
90 184
    }
91
}
92