|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Project: JuKuCMS |
|
22
|
|
|
* License: Apache 2.0 license |
|
23
|
|
|
* User: Justin |
|
24
|
|
|
* Date: 24.08.2018 |
|
25
|
|
|
* Time: 13:44 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class Sidebar { |
|
29
|
|
|
|
|
30
|
|
|
protected $sidebar_id = -1; |
|
31
|
|
|
protected $row = array(); |
|
32
|
|
|
|
|
33
|
|
|
protected $widget_rows = array(); |
|
34
|
|
|
protected $widgets = array(); |
|
35
|
|
|
|
|
36
|
|
|
protected static $is_initialized = false; |
|
37
|
|
|
protected static $all_sidebars = array(); |
|
38
|
|
|
|
|
39
|
|
|
public function __construct() { |
|
40
|
|
|
//load in-memory cache, if neccessary |
|
41
|
|
|
if (!self::isInitialized()) { |
|
42
|
|
|
self::initialize(); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function load (int $sidebar_id) { |
|
47
|
|
|
if (isset(self::$all_sidebars[$sidebar_id])) { |
|
48
|
|
|
$this->row = self::$all_sidebars[$sidebar_id]; |
|
49
|
|
|
} else { |
|
50
|
|
|
throw new IllegalStateException("sidebar with id '" . $sidebar_id . "' doesnt exists."); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->sidebar_id = $sidebar_id; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function loadWidgets () { |
|
57
|
|
|
$this->widgets = array(); |
|
58
|
|
|
|
|
59
|
|
|
//load widgets |
|
60
|
|
|
if (Cache::contains("sidebars", "sidebar_widgets_" . $this->sidebar_id)) { |
|
61
|
|
|
$this->widget_rows = Cache::get("sidebars", "sidebar_widgets_" . $this->sidebar_id); |
|
62
|
|
|
} else { |
|
63
|
|
|
//list rows from database |
|
64
|
|
|
$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebar_widgets` WHERE `sidebar_id` = :sidebar_id ORDER BY `order`; ", array( |
|
65
|
|
|
'sidebar_id' => $this->sidebar_id |
|
66
|
|
|
)); |
|
67
|
|
|
|
|
68
|
|
|
//cache results |
|
69
|
|
|
Cache::put("sidebars", "sidebar_widgets_" . $this->sidebar_id, $rows); |
|
70
|
|
|
|
|
71
|
|
|
$this->widget_rows = $rows; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->widget_rows as $widget_row) { |
|
75
|
|
|
$class_name = $widget_row['class_name']; |
|
76
|
|
|
$widget_instance = new $class_name(); |
|
77
|
|
|
|
|
78
|
|
|
if (!($widget_instance instanceof Widget)) { |
|
79
|
|
|
throw new IllegalStateException("instance has to be an instance of class Widget, this means widget types has to extends class Widget."); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
//cast widget |
|
83
|
|
|
$widget = Widget::castWidget($widget_instance); |
|
84
|
|
|
|
|
85
|
|
|
//load widget and set row, so widget doesn't needs to load data seperate from database |
|
86
|
|
|
$widget->load($widget_row); |
|
87
|
|
|
|
|
88
|
|
|
//add widget to list |
|
89
|
|
|
$this->widgets[] = $widget; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* list widget instances |
|
95
|
|
|
*/ |
|
96
|
|
|
public function listWidgets () : array { |
|
97
|
|
|
return $this->widgets; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function listWidgetTplArray () : array { |
|
101
|
|
|
$array = array(); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($this->listWidgets() as $widget) { |
|
104
|
|
|
$widget = Widget::castWidget($widget); |
|
105
|
|
|
|
|
106
|
|
|
$array[] = array( |
|
107
|
|
|
'id' => $widget->getId(), |
|
108
|
|
|
'title' => $widget->getTitle(), |
|
109
|
|
|
'code' => $widget->getCode(), |
|
110
|
|
|
'css_id' => $widget->getCSSId(), |
|
111
|
|
|
'css_class' => $widget->getCSSClass(), |
|
112
|
|
|
'use_template' => $widget->useTemplate() |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $array; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return int sidebar id |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getSidebarId(): int { |
|
123
|
|
|
return $this->sidebar_id; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getUniqueName () : string { |
|
127
|
|
|
return $this->row['unique_name']; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getTitle () : string { |
|
131
|
|
|
return $this->row['title']; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function isDeletable () : bool { |
|
135
|
|
|
return $this->row['deletable'] == 1; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function getRow () : array { |
|
139
|
|
|
return $this->row; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
protected static function isInitialized(): bool { |
|
146
|
|
|
return self::$is_initialized; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
protected static function initialize () { |
|
150
|
|
|
if (Cache::contains("sidebars", "all_sidebars")) { |
|
151
|
|
|
self::$all_sidebars = Cache::get("sidebars", "all_sidebars"); |
|
152
|
|
|
} else { |
|
153
|
|
|
$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}sidebars`; "); |
|
154
|
|
|
|
|
155
|
|
|
//clear in-memory cache |
|
156
|
|
|
self::$all_sidebars = array(); |
|
157
|
|
|
|
|
158
|
|
|
foreach ($rows as $row) { |
|
159
|
|
|
self::$all_sidebars[$row['sidebar_id']] = $row; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
//cache result |
|
163
|
|
|
Cache::put("sidebars", "all_sidebars"); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
self::$is_initialized = true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public static function create (string $title, string $unique_name, bool $deletable = true) : int { |
|
170
|
|
|
if (empty($unique_name)) { |
|
171
|
|
|
throw new IllegalArgumentException("unique_name cannot be null."); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
Database::getInstance()->execute("INSERT INTO `{praefix}sidebars` ( |
|
175
|
|
|
`sidebar_id`, `unique_name`, `title`, `deletable` |
|
176
|
|
|
) VALUES ( |
|
177
|
|
|
NULL, :unique_name, :title, :deletable |
|
178
|
|
|
) ON DUPLICATE KEY UPDATE `unique_name` = :unique_name, `title` = :title, `deletable` = :deletable; ", array( |
|
179
|
|
|
'unique_name' => $unique_name, |
|
180
|
|
|
'title' => $title, |
|
181
|
|
|
'deletable' => ($deletable ? 1 : 0) |
|
182
|
|
|
)); |
|
183
|
|
|
|
|
184
|
|
|
//clear cache |
|
185
|
|
|
Cache::clear("sidebars"); |
|
186
|
|
|
|
|
187
|
|
|
return Database::getInstance()->lastInsertId(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public static function removeById (int $id) { |
|
191
|
|
|
Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `sidebar_id` = :sidebar_id; ", array( |
|
192
|
|
|
'sidebar_id' => $id |
|
193
|
|
|
)); |
|
194
|
|
|
|
|
195
|
|
|
//clear cache |
|
196
|
|
|
Cache::clear("sidebars"); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public static function removeByUniqueName (string $unique_name) { |
|
200
|
|
|
if (empty($unique_name)) { |
|
201
|
|
|
throw new IllegalArgumentException("unique_name cannot be null."); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
Database::getInstance()->execute("DELETE FROM `{praefix}sidebars` WHERE `unique_name` = :unique_name; ", array( |
|
205
|
|
|
'unique_name' => $unique_name |
|
206
|
|
|
)); |
|
207
|
|
|
|
|
208
|
|
|
//clear cache |
|
209
|
|
|
Cache::clear("sidebars"); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
?> |
|
|
|
|
|
|
215
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.