|
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 extends midcom_baseclasses_components_purecode |
|
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 |
|
parent::__construct(); |
|
73
|
9 |
|
$this->_object_class = $classname; |
|
74
|
9 |
|
$this->_parent_field = $parent_field; |
|
75
|
|
|
|
|
76
|
9 |
|
self::add_head_elements(); |
|
77
|
9 |
|
} |
|
78
|
|
|
|
|
79
|
9 |
|
public function render(array $items = []) |
|
80
|
|
|
{ |
|
81
|
9 |
|
if (empty($items)) { |
|
82
|
9 |
|
$items = $this->_list_items($this->root_node); |
|
83
|
9 |
|
if (empty($items)) { |
|
84
|
2 |
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
7 |
|
$identifier = 't_' . md5('org_openpsa_widgets_treemenu_' . $this->_object_class); |
|
89
|
|
|
|
|
90
|
7 |
|
echo '<div class="openpsa-fancytree-container" id="' . $identifier . "\">\n"; |
|
91
|
7 |
|
$this->_render_items($items); |
|
92
|
7 |
|
echo "\n</div>\n"; |
|
93
|
|
|
|
|
94
|
|
|
echo <<<JSINIT |
|
95
|
|
|
<script type="text/javascript"> |
|
96
|
7 |
|
org_openpsa_tree.setup("{$identifier}"); |
|
97
|
|
|
</script> |
|
98
|
|
|
JSINIT; |
|
99
|
7 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Internal helper for loading the items recursively |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $id The parent object ID |
|
105
|
|
|
*/ |
|
106
|
9 |
|
private function _list_items($id) : array |
|
107
|
|
|
{ |
|
108
|
9 |
|
$data = []; |
|
109
|
|
|
|
|
110
|
9 |
|
$value_properties = ['id']; |
|
111
|
9 |
|
$mc = midcom::get()->dbfactory->new_collector($this->_object_class, $this->_parent_field, (int) $id); |
|
112
|
9 |
|
foreach ($this->constraints as [$field, $operator, $value]) { |
|
113
|
2 |
|
$mc->add_constraint($field, $operator, $value); |
|
114
|
|
|
} |
|
115
|
9 |
|
foreach ($this->title_fields as $field) { |
|
116
|
9 |
|
$value_properties[] = $field; |
|
117
|
9 |
|
$mc->add_order($field); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
9 |
|
foreach ($mc->get_rows($value_properties) as $guid => $values) { |
|
121
|
7 |
|
$entry = ['guid' => $guid]; |
|
122
|
|
|
|
|
123
|
7 |
|
foreach ($this->title_fields as $field) { |
|
124
|
7 |
|
if (!empty($values[$field])) { |
|
125
|
7 |
|
$entry['title'] = $values[$field]; |
|
126
|
7 |
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
7 |
|
if (empty($entry['title'])) { |
|
130
|
3 |
|
$entry['title'] = $this->_l10n->get('unknown'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
7 |
|
$entry['children'] = $this->_list_items($values['id']); |
|
134
|
7 |
|
$data[] = $entry; |
|
135
|
|
|
} |
|
136
|
9 |
|
return $data; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
7 |
|
private function _render_items(array $items) |
|
140
|
|
|
{ |
|
141
|
7 |
|
if (empty($items)) { |
|
142
|
|
|
return; |
|
143
|
|
|
} |
|
144
|
7 |
|
$prefix = midcom::get()->get_host_prefix(); |
|
145
|
7 |
|
echo "<ul>\n"; |
|
146
|
7 |
|
foreach ($items as $item) { |
|
147
|
7 |
|
if (is_callable($this->link_callback)) { |
|
148
|
7 |
|
$url = call_user_func($this->link_callback, $item['guid']); |
|
149
|
|
|
} else { |
|
150
|
|
|
$url = $prefix . 'midcom-permalink-' . $item['guid']; |
|
151
|
|
|
} |
|
152
|
7 |
|
echo '<li id="g_' . $item['guid'] . '"><a href="' . $url . '">' . $item['title'] . "</a>\n"; |
|
153
|
7 |
|
if (!empty($item['children'])) { |
|
154
|
|
|
$this->_render_items($item['children']); |
|
155
|
|
|
} |
|
156
|
7 |
|
echo "</li>\n"; |
|
157
|
|
|
} |
|
158
|
7 |
|
echo "</ul>\n"; |
|
159
|
7 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Add necessary head elements |
|
163
|
|
|
*/ |
|
164
|
18 |
|
public static function add_head_elements() |
|
165
|
|
|
{ |
|
166
|
18 |
|
if (self::$_head_elements_added) { |
|
167
|
18 |
|
return; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
$head = midcom::get()->head; |
|
171
|
1 |
|
$head->enable_jquery_ui(); |
|
172
|
1 |
|
$head->add_jsfile(MIDCOM_STATIC_URL . '/jQuery/fancytree-2.35.0/jquery.fancytree-all.min.js'); |
|
173
|
1 |
|
$head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css"); |
|
174
|
1 |
|
$head->add_stylesheet(MIDCOM_STATIC_URL . "/jQuery/fancytree-2.35.0/skin-awesome/ui.fancytree.min.css"); |
|
175
|
1 |
|
$head->add_stylesheet(MIDCOM_STATIC_URL . "/org.openpsa.widgets/fancytree.custom.css"); |
|
176
|
1 |
|
$head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.widgets/fancytree.custom.js'); |
|
177
|
1 |
|
self::$_head_elements_added = true; |
|
178
|
1 |
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|