1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class to handle the modx-categories |
5
|
|
|
*/ |
6
|
|
|
class Categories |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var DBAPI |
10
|
|
|
*/ |
11
|
|
|
public $db; |
|
|
|
|
12
|
|
|
public $db_tbl = array(); |
13
|
|
|
public $elements = array('templates', 'tmplvars', 'htmlsnippets', 'snippets', 'plugins', 'modules'); |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
global $modx; |
18
|
|
|
|
19
|
|
|
$this->db = &$modx->db; |
20
|
|
|
$this->db_tbl['categories'] = $modx->getFullTableName('categories'); |
21
|
|
|
|
22
|
|
|
foreach ($this->elements as $element) { |
23
|
|
|
$this->db_tbl[$element] = $modx->getFullTableName('site_' . $element); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get all categories |
30
|
|
|
* @return array $categories / array contains all categories |
31
|
|
|
*/ |
32
|
|
|
public function getCategories() |
33
|
|
|
{ |
34
|
|
|
$categories = $this->db->makeArray( |
35
|
|
|
$this->db->select( |
36
|
|
|
'*', |
37
|
|
|
$this->db_tbl['categories'], |
38
|
|
|
'1', |
39
|
|
|
'`rank`,`category`' |
40
|
|
|
) |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
return empty($categories) ? array() : $categories; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $search |
48
|
|
|
* @param string $where |
49
|
|
|
* @return array|bool|object|stdClass |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function getCategory($search, $where = 'category') |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$category = $this->db->getRow( |
54
|
|
|
$this->db->select( |
55
|
|
|
'*', |
56
|
|
|
$this->db_tbl['categories'], |
57
|
|
|
"`" . $where . "` = '" . $this->db->escape($search) . "'" |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
return $category; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $value |
66
|
|
|
* @param string $search |
67
|
|
|
* @param string $where |
68
|
|
|
* @return bool|int|string |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function getCategoryValue($value, $search, $where = 'category') |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$_value = $this->db->getValue( |
73
|
|
|
$this->db->select( |
74
|
|
|
'`' . $value . '`', |
75
|
|
|
$this->db_tbl['categories'], |
76
|
|
|
"`" . $where . "` = '" . $this->db->escape($search) . "'" |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return $_value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $category_id |
85
|
|
|
* @param string $element |
86
|
|
|
* @return array|bool |
|
|
|
|
87
|
|
|
*/ |
88
|
|
|
public function getAssignedElements($category_id, $element) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if (in_array($element, $this->elements, true)) { |
91
|
|
|
$elements = $this->db->makeArray( |
92
|
|
|
$this->db->select( |
93
|
|
|
'*', |
94
|
|
|
$this->db_tbl[$element], |
95
|
|
|
"`category` = '" . (int)$category_id . "'" |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
// correct the name of templates |
100
|
|
|
if ($element === 'templates') { |
101
|
|
|
$_elements_count = count($elements); |
102
|
|
|
for ($i = 0; $i < $_elements_count; $i++) { |
103
|
|
|
$elements[$i]['name'] = $elements[$i]['templatename']; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $elements; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $category_id |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getAllAssignedElements($category_id) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$elements = array(); |
120
|
|
|
foreach ($this->elements as $element) { |
121
|
|
|
$elements[$element] = $this->getAssignedElements($category_id, $element); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $elements; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $category_id |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function deleteCategory($category_id) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$_update = array('category' => 0); |
134
|
|
|
foreach ($this->elements as $element) { |
135
|
|
|
$this->db->update( |
136
|
|
|
$_update, |
137
|
|
|
$this->db_tbl[$element], |
138
|
|
|
"`category` = '" . (int)$category_id . "'" |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->db->delete( |
143
|
|
|
$this->db_tbl['categories'], |
144
|
|
|
"`id` = '" . (int)$category_id . "'" |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
return $this->db->getAffectedRows() === 1; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $category_id |
152
|
|
|
* @param array $data |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function updateCategory($category_id, $data = array()) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
if (empty($data) || empty($category_id)) { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$_update = array( |
162
|
|
|
'category' => $this->db->escape($data['category']), |
163
|
|
|
'rank' => (int)$data['rank'] |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$this->db->update( |
167
|
|
|
$_update, |
168
|
|
|
$this->db_tbl['categories'], |
169
|
|
|
"`id` = '" . (int)$category_id . "'" |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
if ($this->db->getAffectedRows() === 1) { |
|
|
|
|
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $category_name |
181
|
|
|
* @param int $category_rank |
182
|
|
|
* @return bool|int|mixed |
|
|
|
|
183
|
|
|
*/ |
184
|
|
|
public function addCategory($category_name, $category_rank) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
if ($this->isCategoryExists($category_name)) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$_insert = array( |
191
|
|
|
'category' => $this->db->escape($category_name), |
192
|
|
|
'rank' => (int)$category_rank |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
$this->db->insert( |
196
|
|
|
$_insert, |
197
|
|
|
$this->db_tbl['categories'] |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
if ($this->db->getAffectedRows() === 1) { |
201
|
|
|
return $this->db->getInsertId(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $category_name |
209
|
|
|
* @return bool|int|string |
210
|
|
|
*/ |
211
|
|
|
public function isCategoryExists($category_name) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$category = $this->db->escape($category_name); |
214
|
|
|
|
215
|
|
|
$category_id = $this->db->getValue( |
216
|
|
|
$this->db->select( |
217
|
|
|
'`id`', |
218
|
|
|
$this->db_tbl['categories'], |
219
|
|
|
"`category` = '" . $category . "'" |
220
|
|
|
) |
221
|
|
|
); |
222
|
|
|
|
223
|
|
|
if ($this->db->getAffectedRows() === 1) { |
224
|
|
|
return $category_id; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.