1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// |
3
|
|
|
// ------------------------------------------------------------------------ // |
4
|
|
|
// XOOPS - PHP Content Management System // |
5
|
|
|
// Copyright (c) 2000-2016 XOOPS.org // |
6
|
|
|
// <http://xoops.org/> // |
7
|
|
|
// ------------------------------------------------------------------------ // |
8
|
|
|
// This program is free software; you can redistribute it and/or modify // |
9
|
|
|
// it under the terms of the GNU General Public License as published by // |
10
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
11
|
|
|
// (at your option) any later version. // |
12
|
|
|
|
13
|
|
|
// You may not change or alter any portion of this comment or credits // |
14
|
|
|
// of supporting developers from this source code or any supporting // |
15
|
|
|
// source code which is considered copyrighted (c) material of the // |
16
|
|
|
// original comment or credit authors. // |
17
|
|
|
// This program is distributed in the hope that it will be useful, // |
18
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20
|
|
|
// GNU General Public License for more details. // |
21
|
|
|
|
22
|
|
|
// You should have received a copy of the GNU General Public License // |
23
|
|
|
// along with this program; if not, write to the Free Software // |
24
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
25
|
|
|
// ------------------------------------------------------------------------ // |
26
|
|
|
// URL: http://xoops.org/ // |
27
|
|
|
// Project: XOOPS Project // |
28
|
|
|
// -------------------------------------------------------------------------// |
29
|
|
|
if (!class_exists('smartpartner_PersistableObjectHandler')) { |
30
|
|
|
include_once XOOPS_ROOT_PATH . '/modules/smartpartner/class/object.php'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class SmartpartnerCategory |
35
|
|
|
*/ |
36
|
|
|
class SmartpartnerCategory extends XoopsObject |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* SmartpartnerCategory constructor. |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$this->initVar('categoryid', XOBJ_DTYPE_INT, null, false); |
44
|
|
|
$this->initVar('parentid', XOBJ_DTYPE_INT, null, false); |
45
|
|
|
$this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100); |
46
|
|
|
$this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255); |
47
|
|
|
$this->initVar('image', XOBJ_DTYPE_TXTBOX, null, false, 255); |
48
|
|
|
$this->initVar('total', XOBJ_DTYPE_INT, 1, false); |
49
|
|
|
$this->initVar('weight', XOBJ_DTYPE_INT, 1, false); |
50
|
|
|
$this->initVar('created', XOBJ_DTYPE_INT, null, false); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function categoryid() |
57
|
|
|
{ |
58
|
|
|
return $this->getVar('categoryid'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function parentid() |
65
|
|
|
{ |
66
|
|
|
return $this->getVar('parentid'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $format |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function name($format = 'S') |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$ret = $this->getVar('name', $format); |
76
|
|
|
if (($format === 's') || ($format === 'S') || ($format === 'show')) { |
77
|
|
|
$myts = MyTextSanitizer::getInstance(); |
78
|
|
|
$ret = $myts->displayTarea($ret); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $ret; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $format |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function description($format = 'S') |
89
|
|
|
{ |
90
|
|
|
return $this->getVar('description', $format); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $format |
95
|
|
|
* @return mixed|string |
96
|
|
|
*/ |
97
|
|
|
public function image($format = 'S') |
98
|
|
|
{ |
99
|
|
|
if ($this->getVar('image') != '') { |
100
|
|
|
return $this->getVar('image', $format); |
101
|
|
|
} else { |
102
|
|
|
return 'blank.png'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param bool $falseIfNoImage |
108
|
|
|
* @return bool|mixed|string |
109
|
|
|
*/ |
110
|
|
|
public function getImageUrl($falseIfNoImage = false) |
111
|
|
|
{ |
112
|
|
View Code Duplication |
if (($this->getVar('image') !== '') && ($this->getVar('image') !== 'blank.png') && ($this->getVar('image') !== '-1')) { |
|
|
|
|
113
|
|
|
return smartpartner_getImageDir('category', false) . $this->image(); |
114
|
|
|
} elseif ($falseIfNoImage) { |
115
|
|
|
return false; |
116
|
|
|
} elseif (!$this->getVar('image_url')) { |
117
|
|
|
return smartpartner_getImageDir('category', false) . 'blank.png'; |
118
|
|
|
} else { |
119
|
|
|
return $this->getVar('image_url'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function weight() |
127
|
|
|
{ |
128
|
|
|
return $this->getVar('weight'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function notLoaded() |
135
|
|
|
{ |
136
|
|
|
return ($this->getVar('categoryid') == -1); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param bool $withAllLink |
141
|
|
|
* @return mixed|string |
142
|
|
|
*/ |
143
|
|
|
public function getCategoryPath($withAllLink = true) |
144
|
|
|
{ |
145
|
|
|
$filename = 'category.php'; |
|
|
|
|
146
|
|
|
if ($withAllLink) { |
147
|
|
|
$ret = $this->getCategoryLink(); |
148
|
|
|
} else { |
149
|
|
|
$ret = $this->name(); |
150
|
|
|
} |
151
|
|
|
$parentid = $this->parentid(); |
152
|
|
|
global $smartPartnerCategoryHandler; |
|
|
|
|
153
|
|
|
if ($parentid != 0) { |
154
|
|
|
$parentObj = $smartPartnerCategoryHandler->get($parentid); |
155
|
|
|
if ($parentObj->notLoaded()) { |
156
|
|
|
exit; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
$parentid = $parentObj->parentid(); |
|
|
|
|
159
|
|
|
$ret = $parentObj->getCategoryPath($withAllLink) . ' > ' . $ret; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $ret; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string |
|
|
|
|
167
|
|
|
*/ |
168
|
|
|
public function getCategoryUrl() |
169
|
|
|
{ |
170
|
|
|
return smartpartner_seo_genUrl('category', $this->categoryid(), $this->name()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param bool $class |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getCategoryLink($class = false) |
178
|
|
|
{ |
179
|
|
|
if ($class) { |
180
|
|
|
return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>'; |
181
|
|
|
} else { |
182
|
|
|
return "<a href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>'; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param bool $sendNotifications |
188
|
|
|
* @param bool $force |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public function store($sendNotifications = true, $force = true) |
192
|
|
|
{ |
193
|
|
|
global $smartPartnerCategoryHandler; |
|
|
|
|
194
|
|
|
$ret = $smartPartnerCategoryHandler->insert($this, $force); |
195
|
|
|
if ($sendNotifications && $ret && $this->isNew()) { |
196
|
|
|
$this->sendNotifications(); |
197
|
|
|
} |
198
|
|
|
$this->unsetNew(); |
199
|
|
|
|
200
|
|
|
return $ret; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function sendNotifications() |
204
|
|
|
{ |
205
|
|
|
$hModule = xoops_getHandler('module'); |
206
|
|
|
$smartModule =& $hModule->getByDirname('smartpartner'); |
207
|
|
|
$module_id = $smartModule->getVar('mid'); |
|
|
|
|
208
|
|
|
|
209
|
|
|
$myts = MyTextSanitizer::getInstance(); |
210
|
|
|
$notificationHandler = xoops_getHandler('notification'); |
|
|
|
|
211
|
|
|
|
212
|
|
|
$tags = array(); |
213
|
|
|
$tags['MODULE_NAME'] = $myts->displayTarea($smartModule->getVar('name')); |
214
|
|
|
$tags['CATEGORY_NAME'] = $this->name(); |
215
|
|
|
$tags['CATEGORY_URL'] = $this->getCategoryUrl(); |
216
|
|
|
|
217
|
|
|
$notificationHandler = xoops_getHandler('notification'); |
218
|
|
|
$notificationHandler->triggerEvent('global_item', 0, 'category_created', $tags); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param array $category |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
public function toArray($category = array()) |
226
|
|
|
{ |
227
|
|
|
$category['categoryid'] = $this->categoryid(); |
228
|
|
|
$category['name'] = $this->name(); |
229
|
|
|
$category['categorylink'] = $this->getCategoryLink(); |
230
|
|
|
$category['total'] = $this->getVar('itemcount'); |
231
|
|
|
$category['description'] = $this->description(); |
232
|
|
|
|
233
|
|
|
if ($this->image() !== 'blank.png') { |
234
|
|
|
$category['image_path'] = smartpartner_getImageDir('category', false) . $this->image(); |
235
|
|
|
} else { |
236
|
|
|
$category['image_path'] = ''; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $category; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Class SmartpartnerCategoryHandler |
245
|
|
|
*/ |
246
|
|
|
class SmartpartnerCategoryHandler extends smartpartner_PersistableObjectHandler |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
/** |
249
|
|
|
* SmartpartnerCategoryHandler constructor. |
250
|
|
|
* @param object|XoopsDatabase $db |
251
|
|
|
*/ |
252
|
|
|
public function __construct(XoopsDatabase $db) |
253
|
|
|
{ |
254
|
|
|
parent::__construct($db, 'smartpartner_categories', 'SmartpartnerCategory', 'categoryid', 'name'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param XoopsObject $category |
259
|
|
|
* @param bool $force |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
|
|
public function delete(XoopsObject $category, $force = false) |
263
|
|
|
{ |
264
|
|
|
/*if (parent::delete($object, $force)) { |
|
|
|
|
265
|
|
|
global $xoopsModule; |
266
|
|
|
|
267
|
|
|
// TODO: Delete partners in this category |
268
|
|
|
return true; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return false;*/ |
272
|
|
|
|
273
|
|
|
if (strtolower(get_class($category)) !== 'smartpartnercategory') { |
274
|
|
|
return false; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// Deleting the partners |
278
|
|
|
global $smartPartnerPartnerHandler; |
|
|
|
|
279
|
|
|
if (!isset($smartPartnerPartnerHandler)) { |
280
|
|
|
$smartPartnerPartnerHandler = smartpartner_gethandler('partner'); |
281
|
|
|
} |
282
|
|
|
$criteria = new Criteria('category', $category->categoryid()); |
283
|
|
|
$partners = $smartPartnerPartnerHandler->getObjects($criteria); |
284
|
|
|
if ($partners) { |
285
|
|
|
foreach ($partners as $partner) { |
286
|
|
|
$smartPartnerPartnerHandler->delete($partner); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// Deleteing the sub categories |
291
|
|
|
$subcats = $this->getCategories(0, 0, $category->categoryid()); |
292
|
|
|
foreach ($subcats as $subcat) { |
293
|
|
|
$this->delete($subcat); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$sql = sprintf('DELETE FROM %s WHERE categoryid = %u ', $this->db->prefix('smartpartner_categories'), $category->getVar('categoryid')); |
297
|
|
|
|
298
|
|
View Code Duplication |
if (false != $force) { |
|
|
|
|
299
|
|
|
$result = $this->db->queryF($sql); |
300
|
|
|
} else { |
301
|
|
|
$result = $this->db->query($sql); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (!$result) { |
305
|
|
|
return false; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return true; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param int $limit |
313
|
|
|
* @param int $start |
314
|
|
|
* @param int $parentid |
315
|
|
|
* @param string $sort |
316
|
|
|
* @param string $order |
317
|
|
|
* @param bool $id_as_key |
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
|
|
public function getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $id_as_key = true) |
321
|
|
|
{ |
322
|
|
|
$criteria = new CriteriaCompo(); |
323
|
|
|
|
324
|
|
|
$criteria->setSort($sort); |
325
|
|
|
$criteria->setOrder($order); |
326
|
|
|
|
327
|
|
|
if ($parentid != -1) { |
328
|
|
|
$criteria->add(new Criteria('parentid', $parentid)); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$criteria->setStart($start); |
332
|
|
|
$criteria->setLimit($limit); |
333
|
|
|
$ret = $this->getObjects($criteria, $id_as_key); |
334
|
|
|
|
335
|
|
|
return $ret; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param int $parentid |
340
|
|
|
* @return int |
341
|
|
|
*/ |
342
|
|
|
public function getCategoriesCount($parentid = 0) |
343
|
|
|
{ |
344
|
|
|
if ($parentid == -1) { |
345
|
|
|
return $this->getCount(); |
346
|
|
|
} |
347
|
|
|
$criteria = new CriteriaCompo(); |
348
|
|
|
if (isset($parentid) && ($parentid != -1)) { |
349
|
|
|
$criteria->add(new criteria('parentid', $parentid)); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return $this->getCount($criteria); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.