1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\XoopsObject;
14: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
15: use Xoops\Core\Kernel\Criteria;
16: use Xoops\Core\Kernel\CriteriaCompo;
17:
18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28: include_once dirname(__DIR__) . '/include/common.php';
29:
30: class PublisherCategory extends XoopsObject
31: {
32: 33: 34: 35:
36: public $publisher = null;
37:
38: 39: 40:
41: public $_categoryPath = false;
42:
43: 44: 45:
46: public function __construct()
47: {
48: $this->publisher = Publisher::getInstance();
49: $this->initVar("categoryid", XOBJ_DTYPE_INT, null, false);
50: $this->initVar("parentid", XOBJ_DTYPE_INT, null, false);
51: $this->initVar("name", XOBJ_DTYPE_TXTBOX, null, true, 100);
52: $this->initVar("description", XOBJ_DTYPE_TXTAREA, null, false, 255);
53: $this->initVar("image", XOBJ_DTYPE_TXTBOX, null, false, 255);
54: $this->initVar("total", XOBJ_DTYPE_INT, 1, false);
55: $this->initVar("weight", XOBJ_DTYPE_INT, 1, false);
56: $this->initVar("created", XOBJ_DTYPE_INT, null, false);
57: $this->initVar("template", XOBJ_DTYPE_TXTBOX, null, false, 255);
58: $this->initVar("header", XOBJ_DTYPE_TXTAREA, null, false);
59: $this->initVar("meta_keywords", XOBJ_DTYPE_TXTAREA, null, false);
60: $this->initVar("meta_description", XOBJ_DTYPE_TXTAREA, null, false);
61: $this->initVar("short_url", XOBJ_DTYPE_TXTBOX, null, false, 255);
62: $this->initVar("moderator", XOBJ_DTYPE_INT, null, false, 0);
63:
64: $this->initVar("itemcount", XOBJ_DTYPE_INT, 0, false);
65: $this->initVar('last_itemid', XOBJ_DTYPE_INT);
66: $this->initVar('last_title_link', XOBJ_DTYPE_TXTBOX);
67: $this->initVar("dohtml", XOBJ_DTYPE_INT, 1, false);
68: }
69:
70: 71: 72:
73: public function notLoaded()
74: {
75: return ($this->getVar('categoryid') == -1);
76: }
77:
78: 79: 80:
81: public function checkPermission()
82: {
83: $xoops = Xoops::getInstance();
84: if ($this->publisher->isUserAdmin()) {
85: return true;
86: }
87: if ($xoops->isUser() && $xoops->user->getVar('uid') == $this->getVar('moderator')) {
88: return true;
89: }
90: return $this->publisher->getPermissionHandler()->isGranted('category_read', $this->getVar('categoryid'));
91: }
92:
93: 94: 95: 96: 97:
98: public function image($format = 's')
99: {
100: if ($this->getVar('image') != '') {
101: return $this->getVar('image', $format);
102: } else {
103: return 'blank.png';
104: }
105: }
106:
107: 108: 109: 110: 111:
112: public function template($format = 'n')
113: {
114: return $this->getVar("template", $format);
115: }
116:
117: 118: 119: 120: 121:
122: public function getCategoryPath($withAllLink = true)
123: {
124: if (!$this->_categoryPath) {
125: if ($withAllLink) {
126: $ret = $this->getCategoryLink();
127: } else {
128: $ret = $this->getVar('name');
129: }
130: $parentid = $this->getVar('parentid');
131: if ($parentid != 0) {
132: $parentObj = $this->publisher->getCategoryHandler()->get($parentid);
133: if ($parentObj->notLoaded()) {
134: exit;
135: }
136: $ret = $parentObj->getCategoryPath($withAllLink) . " > " . $ret;
137: }
138: $this->_categoryPath = $ret;
139: }
140: return $this->_categoryPath;
141: }
142:
143: 144: 145:
146: public function getCategoryPathForMetaTitle()
147: {
148: $ret = '';
149: $parentid = $this->getVar('parentid');
150: if ($parentid != 0) {
151: $parentObj = $this->publisher->getCategoryHandler()->get($parentid);
152: if ($parentObj->notLoaded()) {
153: exit('NOT LOADED');
154: }
155: $ret = $parentObj->getCategoryPath(false);
156: $ret = str_replace(' >', ' -', $ret);
157: }
158: return $ret;
159: }
160:
161: 162: 163:
164: public function getGroups_read()
165: {
166: return $this->publisher->getPermissionHandler()->getGrantedGroupsById('category_read', $this->getVar('categoryid'));
167: }
168:
169: 170: 171:
172: public function getGroups_submit()
173: {
174: return $this->publisher->getPermissionHandler()->getGrantedGroupsById('item_submit', $this->getVar('categoryid'));
175: }
176:
177: 178: 179:
180: public function getGroups_moderation()
181: {
182: return $this->publisher->getPermissionHandler()->getGrantedGroupsById('category_moderation', $this->getVar('categoryid'));
183: }
184:
185: 186: 187:
188: public function getCategoryUrl()
189: {
190: return PublisherUtils::seoGenUrl('category', $this->getVar('categoryid'), $this->getVar('short_url'));
191: }
192:
193: 194: 195: 196: 197:
198: public function getCategoryLink($class = false)
199: {
200: if ($class) {
201: return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->getVar('name') . "</a>";
202: } else {
203: return "<a href='" . $this->getCategoryUrl() . "'>" . $this->getVar('name') . "</a>";
204: }
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function store($sendNotifications = true, $force = true)
214: {
215: $ret = $this->publisher->getCategoryHandler()->insert($this, $force);
216: if ($sendNotifications && $ret && ($this->isNew())) {
217: $this->sendNotifications();
218: }
219: $this->unsetNew();
220: return $ret;
221: }
222:
223: 224: 225:
226: public function sendNotifications()
227: {
228: $xoops = Xoops::getInstance();
229: if ($xoops->isActiveModule('notifications')) {
230: $tags = array();
231: $tags['MODULE_NAME'] = $this->publisher->getModule()->getVar('name');
232: $tags['CATEGORY_NAME'] = $this->getVar('name');
233: $tags['CATEGORY_URL'] = $this->getCategoryUrl();
234: $notification_handler = Notifications::getInstance()->getHandlerNotification();
235: $notification_handler->triggerEvent('global', 0, 'category_created', $tags);
236: }
237: }
238:
239: 240: 241: 242: 243:
244: public function toArray($category = array())
245: {
246: $category['categoryid'] = $this->getVar('categoryid');
247: $category['name'] = $this->getVar('name');
248: $category['categorylink'] = $this->getCategoryLink();
249: $category['categoryurl'] = $this->getCategoryUrl();
250: $category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : '';
251: $category['description'] = $this->getVar('description');
252: $category['header'] = $this->getVar('header');
253: $category['meta_keywords'] = $this->getVar('meta_keywords');
254: $category['meta_description'] = $this->getVar('meta_description');
255: $category['short_url'] = $this->getVar('short_url');
256: if ($this->getVar('last_itemid') > 0) {
257: $category['last_itemid'] = $this->getVar('last_itemid', 'n');
258: $category['last_title_link'] = $this->getVar('last_title_link', 'n');
259: }
260: if ($this->image() !== 'blank.png') {
261: $category['image_path'] = PublisherUtils::getImageDir('category', false) . $this->image();
262: } else {
263: $category['image_path'] = '';
264: }
265: $category['lang_subcategories'] = sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->getVar('name'));
266: return $category;
267: }
268:
269: 270: 271: 272: 273:
274: public function toArrayTable($category = array())
275: {
276: $category['categoryid'] = $this->getVar('categoryid');
277: $category['categorylink'] = $this->getCategoryLink();
278: $category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : '';
279: $category['description'] = $this->getVar('description');
280: if ($this->getVar('last_itemid') > 0) {
281: $category['last_itemid'] = $this->getVar('last_itemid', 'n');
282: $category['last_title_link'] = $this->getVar('last_title_link', 'n');
283: }
284: if ($this->image() !== 'blank.png') {
285: $category['image_path'] = PublisherUtils::getImageDir('category', false) . $this->image();
286: } else {
287: $category['image_path'] = '';
288: }
289: $category['lang_subcategories'] = sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->getVar('name'));
290: return $category;
291: }
292:
293: 294: 295:
296: public function createMetaTags()
297: {
298: $publisher_metagen = new PublisherMetagen($this->getVar('name'), $this->getVar('meta_keywords'), $this->getVar('meta_description'));
299: $publisher_metagen->createMetaTags();
300: }
301: }
302:
303: 304: 305: 306: 307: 308: 309: 310:
311: class PublisherCategoryHandler extends XoopsPersistableObjectHandler
312: {
313: 314: 315: 316:
317: public $publisher = null;
318:
319: 320: 321:
322: public function __construct(Connection $db)
323: {
324: $this->publisher = Publisher::getInstance();
325: parent::__construct($db, "publisher_categories", 'PublisherCategory', "categoryid", "name");
326: }
327:
328: 329: 330: 331: 332: 333:
334: public function get($id = null, $fields = null)
335: {
336: static $cats;
337: if ($fields == null && isset($cats[$id])) {
338: return $cats[$id];
339: }
340: $obj = parent::get($id, $fields);
341: if ($fields == null) {
342: $cats[$id] = $obj;
343: }
344: return $obj;
345: }
346:
347: 348: 349: 350: 351: 352: 353: 354:
355: public function insert(XoopsObject $category, $force = true)
356: {
357:
358: if (!$category->getVar('meta_keywords') || !$category->getVar('meta_description')) {
359: $publisher_metagen = new PublisherMetagen($category->getVar('name'), $category->getVar('meta_keywords'), $category->getVar('description'));
360: if (!$category->getVar('meta_keywords')) {
361: $category->setVar('meta_keywords', $publisher_metagen->_keywords);
362: }
363: if (!$category->getVar('meta_description')) {
364: $category->setVar('meta_description', $publisher_metagen->_description);
365: }
366: }
367:
368: if (!$category->getVar('short_url')) {
369: $category->setVar('short_url', PublisherMetagen::generateSeoTitle($category->getVar('name', 'n'), false));
370: }
371: $ret = parent::insert($category, $force);
372: return $ret;
373: }
374:
375: 376: 377: 378: 379: 380: 381: 382:
383: public function delete(XoopsObject $category, $force = false)
384: {
385: $xoops = Xoops::getInstance();
386:
387: $criteria = new Criteria('categoryid', $category->getVar('categoryid'));
388: $this->publisher->getItemHandler()->deleteAll($criteria, true, true);
389: unset($criteria);
390:
391: $subcats = $this->getCategories(0, 0, $category->getVar('categoryid'));
392: foreach ($subcats as $subcat) {
393: $this->delete($subcat);
394: }
395: if (!parent::delete($category, $force)) {
396: $category->setErrors('An error while deleting.');
397: return false;
398: }
399: $module_id = $this->publisher->getModule()->getVar('mid');
400:
401: $xoops->getHandlerGroupPermission()->deleteByModule($module_id, "category_read", $category->getVar('categoryid'));
402: $xoops->getHandlerGroupPermission()->deleteByModule($module_id, "item_submit", $category->getVar('categoryid'));
403: $xoops->getHandlerGroupPermission()->deleteByModule($module_id, "category_moderation", $category->getVar('categoryid'));
404: return true;
405: }
406:
407: 408: 409: 410: 411: 412: 413: 414: 415: 416:
417: public function &getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $id_as_key = true)
418: {
419: $xoops = Xoops::getInstance();
420: $ret = array();
421: $criteria = new CriteriaCompo();
422: $criteria->setSort($sort);
423: $criteria->setOrder($order);
424: if ($parentid != -1) {
425: $criteria->add(new Criteria('parentid', $parentid));
426: }
427: if (!PublisherUtils::IsUserAdmin()) {
428: $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('category_read');
429: if (count($categoriesGranted) > 0) {
430: $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
431: } else {
432: return $ret;
433: }
434: if ($xoops->isUser()) {
435: $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR');
436: }
437: }
438: $criteria->setStart($start);
439: $criteria->setLimit($limit);
440: $ret = $this->getObjects($criteria, $id_as_key);
441: return $ret;
442: }
443:
444: 445: 446: 447: 448: 449: 450: 451: 452: 453:
454: public function getSubCatArray($category, $level, $cat_array, $cat_result)
455: {
456: global $theresult;
457: $spaces = '';
458: for ($j = 0; $j < $level; ++$j) {
459: $spaces .= '--';
460: }
461: $theresult[$category['categoryid']] = $spaces . $category['name'];
462: if (isset($cat_array[$category['categoryid']])) {
463: $level = $level + 1;
464: foreach ($cat_array[$category['categoryid']] as $cat) {
465: $this->getSubCatArray($cat, $level, $cat_array, $cat_result);
466: }
467: }
468: }
469:
470: public function &getCategoriesForSubmit()
471: {
472: global $theresult;
473: $xoops = Xoops::getInstance();
474: $ret = array();
475: $criteria = new CriteriaCompo();
476: $criteria->setSort('name');
477: $criteria->setOrder('ASC');
478: if (!PublisherUtils::IsUserAdmin()) {
479: $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('item_submit');
480: if (count($categoriesGranted) > 0) {
481: $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
482: } else {
483: return $ret;
484: }
485: if ($xoops->isUser()) {
486: $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR');
487: }
488: }
489: $categories = $this->getAll($criteria, array('categoryid', 'parentid', 'name'), false, false);
490: if (count($categories) == 0) {
491: return $ret;
492: }
493: $cat_array = array();
494: foreach ($categories as $cat) {
495: $cat_array[$cat['parentid']][$cat['categoryid']] = $cat;
496: }
497:
498: if (!isset($cat_array[0])) {
499: return $ret;
500: }
501: $cat_result = array();
502: foreach ($cat_array[0] as $thecat) {
503: $level = 0;
504: $this->getSubCatArray($thecat, $level, $cat_array, $cat_result);
505: }
506: return $theresult;
507: }
508:
509: 510: 511:
512: public function &getCategoriesForSearch()
513: {
514: global $theresult;
515: $xoops = Xoops::getInstance();
516: $ret = array();
517: $criteria = new CriteriaCompo();
518: $criteria->setSort('name');
519: $criteria->setOrder('ASC');
520: if (!PublisherUtils::IsUserAdmin()) {
521: $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('category_read');
522: if (count($categoriesGranted) > 0) {
523: $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
524: } else {
525: return $ret;
526: }
527: if ($xoops->isUser()) {
528: $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR');
529: }
530: }
531: $categories = $this->getAll($criteria, array('categoryid', 'parentid', 'name'), false, false);
532: if (count($categories) == 0) {
533: return $ret;
534: }
535: $cat_array = array();
536: foreach ($categories as $cat) {
537: $cat_array[$cat['parentid']][$cat['categoryid']] = $cat;
538: }
539:
540: if (!isset($cat_array[0])) {
541: return $ret;
542: }
543: $cat_result = array();
544: foreach ($cat_array[0] as $thecat) {
545: $level = 0;
546: $this->getSubCatArray($thecat, $level, $cat_array, $cat_result);
547: }
548: return $theresult;
549: }
550:
551: 552: 553: 554: 555:
556: public function getCategoriesCount($parentid = 0)
557: {
558: $xoops = Xoops::getInstance();
559: if ($parentid == -1) {
560: return $this->getCount();
561: }
562: $criteria = new CriteriaCompo();
563: if (isset($parentid) && ($parentid != -1)) {
564: $criteria->add(new criteria('parentid', $parentid));
565: if (!PublisherUtils::IsUserAdmin()) {
566: $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('category_read');
567: if (count($categoriesGranted) > 0) {
568: $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
569: } else {
570: return 0;
571: }
572: if ($xoops->isUser()) {
573: $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR');
574: }
575: }
576: }
577: return $this->getCount($criteria);
578: }
579:
580: 581: 582: 583: 584: 585: 586:
587: public function &getSubCats($categories)
588: {
589: $xoops = Xoops::getInstance();
590: $criteria = new CriteriaCompo(new Criteria('parentid', "(" . implode(',', array_keys($categories)) . ")", 'IN'));
591: $ret = array();
592: if (!PublisherUtils::IsUserAdmin()) {
593: $categoriesGranted = $this->publisher->getPermissionHandler()->getGrantedItems('category_read');
594: if (count($categoriesGranted) > 0) {
595: $criteria->add(new Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'));
596: } else {
597: return $ret;
598: }
599: if ($xoops->isUser()) {
600: $criteria->add(new Criteria('moderator', $xoops->user->getVar('uid')), 'OR');
601: }
602: }
603: $criteria->setSort('weight');
604: $criteria->setOrder('ASC');
605: $subcats = $this->getObjects($criteria, true);
606:
607: foreach ($subcats as $subcat) {
608: $ret[$subcat->getVar('parentid')][$subcat->getVar('categoryid')] = $subcat;
609: }
610: return $ret;
611: }
612:
613: 614: 615: 616: 617:
618: public function publishedItemsCount($cat_id = 0)
619: {
620: return $this->itemsCount($cat_id, $status = array(_PUBLISHER_STATUS_PUBLISHED));
621: }
622:
623: 624: 625: 626: 627: 628:
629: public function itemsCount($cat_id = 0, $status = '')
630: {
631: return $this->publisher->getItemHandler()->getCountsByCat($cat_id, $status);
632: }
633: }
634: