Passed
Push — master ( 05472f...c5d927 )
by Andreas
18:18
created

org_openpsa_widgets_tree   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 95.16%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
c 2
b 0
f 0
dl 0
loc 163
ccs 59
cts 62
cp 0.9516
rs 10
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 18 3
A _render_items() 0 20 5
A __construct() 0 6 1
B _list_items() 0 31 7
A add_head_elements() 0 14 2
1
<?php
2
/**
3
 * @package org.openpsa.widgets
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * fancytree-based tree widget
11
 *
12
 * @package org.openpsa.widgets
13
 */
14
class org_openpsa_widgets_tree
15
{
16
    /**
17
     * The tree's root node, if any
18
     *
19
     * @var int
20
     */
21
    public $root_node = 0;
22
23
    /**
24
     * The object fields to use for link titles
25
     *
26
     * @var array
27
     */
28
    public $title_fields = ['title'];
29
30
    /**
31
     * Callback for rendering object links. It receives the GUID as parameter
32
     *
33
     * @var callable
34
     */
35
    public $link_callback;
36
37
    /**
38
     * The constraints for the children MC
39
     *
40
     * @var array
41
     */
42
    public $constraints = [];
43
44
    /**
45
     * The object's class name
46
     *
47
     * @var string
48
     */
49
    private $_object_class;
50
51
    /**
52
     * The object's parent field
53
     *
54
     * @var string
55
     */
56
    private $_parent_field;
57
58
    /**
59
     * Flag that tracks if JS/CSS files have already been added
60
     *
61
     * @var boolean
62
     */
63
    private static $_head_elements_added = false;
64
65
    /**
66
     * Constructor, adds head elements and initializes some variables
67
     *
68
     * @param string $parent_field Where to look for children
69
     */
70 9
    public function __construct(string $classname, string $parent_field)
71
    {
72 9
        $this->_object_class = $classname;
73 9
        $this->_parent_field = $parent_field;
74
75 9
        self::add_head_elements();
76 9
    }
77
78 9
    public function render(array $items = [])
79
    {
80 9
        if (empty($items)) {
81 9
            $items = $this->_list_items($this->root_node);
82 9
            if (empty($items)) {
83 2
                return;
84
            }
85
        }
86
87 7
        $identifier = 't_' . md5('org_openpsa_widgets_treemenu_' . $this->_object_class);
88
89 7
        echo '<div class="openpsa-fancytree-container" id="' . $identifier . "\">\n";
90 7
        $this->_render_items($items);
91 7
        echo "\n</div>\n";
92
93
        echo <<<JSINIT
94
<script type="text/javascript">
95 7
    org_openpsa_tree.setup("{$identifier}");
96
</script>
97
JSINIT;
98 7
    }
99
100
    /**
101
     * Internal helper for loading the items recursively
102
     *
103
     * @param int $id The parent object ID
104
     */
105 9
    private function _list_items($id) : array
106
    {
107 9
        $data = [];
108
109 9
        $value_properties = ['id'];
110 9
        $mc = midcom::get()->dbfactory->new_collector($this->_object_class, $this->_parent_field, (int) $id);
111 9
        foreach ($this->constraints as [$field, $operator, $value]) {
112 2
            $mc->add_constraint($field, $operator, $value);
113
        }
114 9
        foreach ($this->title_fields as $field) {
115 9
            $value_properties[] = $field;
116 9
            $mc->add_order($field);
117
        }
118
119 9
        foreach ($mc->get_rows($value_properties) as $guid => $values) {
120 7
            $entry = ['guid' => $guid];
121
122 7
            foreach ($this->title_fields as $field) {
123 7
                if (!empty($values[$field])) {
124 7
                    $entry['title'] = $values[$field];
125 7
                    break;
126
                }
127
            }
128 7
            if (empty($entry['title'])) {
129 3
                $entry['title'] = $entry['guid'];
130
            }
131
132 7
            $entry['children'] = $this->_list_items($values['id']);
133 7
            $data[] = $entry;
134
        }
135 9
        return $data;
136
    }
137
138 7
    private function _render_items(array $items)
139
    {
140 7
        if (empty($items)) {
141
            return;
142
        }
143 7
        $prefix = midcom::get()->get_host_prefix();
144 7
        echo "<ul>\n";
145 7
        foreach ($items as $item) {
146 7
            if (is_callable($this->link_callback)) {
147 7
                $url = call_user_func($this->link_callback, $item['guid']);
148
            } else {
149
                $url = $prefix . 'midcom-permalink-' . $item['guid'];
150
            }
151 7
            echo '<li id="g_' . $item['guid'] . '"><a href="' . $url . '">' . $item['title'] . "</a>\n";
152 7
            if (!empty($item['children'])) {
153
                $this->_render_items($item['children']);
154
            }
155 7
            echo "</li>\n";
156
        }
157 7
        echo "</ul>\n";
158 7
    }
159
160
    /**
161
     * Add necessary head elements
162
     */
163 18
    public static function add_head_elements()
164
    {
165 18
        if (self::$_head_elements_added) {
166 18
            return;
167
        }
168
169 1
        $head = midcom::get()->head;
170 1
        $head->enable_jquery_ui();
171 1
        $head->add_jsfile(MIDCOM_STATIC_URL . '/jQuery/fancytree-2.35.0/jquery.fancytree-all.min.js');
172 1
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
173 1
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/jQuery/fancytree-2.35.0/skin-awesome/ui.fancytree.min.css");
174 1
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/org.openpsa.widgets/fancytree.custom.css");
175 1
        $head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.widgets/fancytree.custom.js');
176 1
        self::$_head_elements_added = true;
177 1
    }
178
}
179