1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\Criteria;
14: use Xoops\Core\Kernel\CriteriaCompo;
15: use Xoops\Core\Kernel\CriteriaElement;
16: use Xoops\Core\Kernel\XoopsObject;
17: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
18: use Xoops\Core\Kernel\Handlers\XoopsUser;
19:
20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: class NotificationsNotification extends XoopsObject
34: {
35: 36: 37:
38: public function __construct()
39: {
40: $this->initVar('id', XOBJ_DTYPE_INT, null, false);
41: $this->initVar('modid', XOBJ_DTYPE_INT, null, false);
42: $this->initVar('category', XOBJ_DTYPE_TXTBOX, null, false, 30);
43: $this->initVar('itemid', XOBJ_DTYPE_INT, 0, false);
44: $this->initVar('event', XOBJ_DTYPE_TXTBOX, null, false, 30);
45: $this->initVar('uid', XOBJ_DTYPE_INT, 0, true);
46: $this->initVar('mode', XOBJ_DTYPE_INT, 0, false);
47: }
48:
49:
50:
51:
52:
53:
54:
55:
56: 57: 58: 59: 60: 61: 62: 63: 64: 65:
66: public function notifyUser($template_dir, $template, $subject, $tags)
67: {
68:
69: $xoops = xoops::getInstance();
70: $helper = Notifications::getInstance();
71:
72:
73: $member_handler = $xoops->getHandlerMember();
74: $user = $member_handler->getUser($this->getVar('uid'));
75: if (!is_object($user)) {
76: return true;
77: }
78:
79: $method = $user->getVar('notify_method');
80:
81: $xoopsMailer = $xoops->getMailer();
82: switch ($method) {
83: case NOTIFICATIONS_METHOD_PM:
84: $xoopsMailer->usePM();
85:
86: $xoopsMailer->setFromUser($member_handler->getUser($xoops->getConfig('fromuid')));
87: foreach ($tags as $k => $v) {
88: $xoopsMailer->assign($k, $v);
89: }
90: break;
91: case NOTIFICATIONS_METHOD_EMAIL:
92: $xoopsMailer->useMail();
93: foreach ($tags as $k => $v) {
94: $xoopsMailer->assign($k, preg_replace("/&/i", '&', $v));
95: }
96: break;
97: default:
98: return true;
99: break;
100: }
101:
102:
103: $xoopsMailer->setTemplateDir($template_dir);
104: $xoopsMailer->setTemplate($template);
105: $xoopsMailer->setToUsers($user);
106:
107:
108:
109: $xoopsMailer->setSubject($subject);
110: $success = $xoopsMailer->send();
111:
112:
113:
114: $notification_handler = $helper->getHandlerNotification();
115:
116: if ($this->getVar('mode') == NOTIFICATIONS_MODE_SENDONCETHENDELETE) {
117: $notification_handler->delete($this);
118: return $success;
119: }
120:
121: if ($this->getVar('mode') == NOTIFICATIONS_MODE_SENDONCETHENWAIT) {
122: $this->setVar('mode', NOTIFICATIONS_MODE_WAITFORLOGIN);
123: $notification_handler->insert($this);
124: }
125: return $success;
126: }
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136: 137:
138: class NotificationsNotificationHandler extends XoopsPersistableObjectHandler
139: {
140: 141: 142: 143: 144:
145: public function __construct(Connection $db = null)
146: {
147: parent::__construct($db, 'notifications', 'NotificationsNotification', 'id', 'itemid');
148: }
149:
150: 151: 152: 153: 154: 155: 156: 157:
158: public function getObjectsArray(CriteriaElement $criteria = null, $id_as_key = false)
159: {
160: $qb = $this->db2->createXoopsQueryBuilder()
161: ->select('*')
162: ->from($this->table, null);
163: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
164: $criteria->renderQb($qb);
165: }
166: $result = $qb->execute();
167: $ret = array();
168: if (!$result) {
169: return $ret;
170: }
171: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
172: $notification = new NotificationsNotification();
173: $notification->assignVars($myrow);
174: if (!$id_as_key) {
175: $ret[] = $notification;
176: } else {
177: $ret[$myrow['id']] = $notification;
178: }
179: unset($notification);
180: }
181: return $ret;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197:
198: public function getNotification($module_id, $category, $item_id, $event, $user_id)
199: {
200: $criteria = new CriteriaCompo();
201: $criteria->add(new Criteria('modid', (int)($module_id)));
202: $criteria->add(new Criteria('category', $category));
203: $criteria->add(new Criteria('itemid', (int)($item_id)));
204: $criteria->add(new Criteria('event', $event));
205: $criteria->add(new Criteria('uid', (int)($user_id)));
206: $objects = $this->getObjectsArray($criteria);
207: if (count($objects) == 1) {
208: return $objects[0];
209: }
210: $inst = false;
211: return $inst;
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225:
226: public function isSubscribed($category, $item_id, $event, $module_id, $user_id)
227: {
228: $criteria = new CriteriaCompo();
229: $criteria->add(new Criteria('modid', (int)($module_id)));
230: $criteria->add(new Criteria('category', $category));
231: $criteria->add(new Criteria('itemid', (int)($item_id)));
232: $criteria->add(new Criteria('event', $event));
233: $criteria->add(new Criteria('uid', (int)($user_id)));
234: return $this->getCount($criteria);
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253:
254: public function subscribe($category, $item_id, $events, $mode = null, $module_id = null, $user_id = null)
255: {
256: $xoops = Xoops::getInstance();
257: if (!isset($user_id)) {
258: if (!$xoops->isUser()) {
259: return false;
260: } else {
261: $user_id = $xoops->user->getVar('uid');
262: }
263: }
264:
265: if (!isset($module_id)) {
266: $module_id = $xoops->module->getVar('mid');
267: }
268:
269:
270: if (!isset($mode)) {
271: $user = new XoopsUser($user_id);
272: $mode = $user->getVar('notify_mode');
273: }
274:
275: if (!is_array($events)) {
276: $events = array($events);
277: }
278: foreach ($events as $event) {
279: if ($notification = $this->getNotification($module_id, $category, $item_id, $event, $user_id)) {
280: if ($notification->getVar('mode') != $mode) {
281: $this->updateByField($notification, 'mode', $mode);
282: }
283: } else {
284: $notification = $this->create();
285: $notification->setVar('modid', $module_id);
286: $notification->setVar('category', $category);
287: $notification->setVar('itemid', $item_id);
288: $notification->setVar('uid', $user_id);
289: $notification->setVar('event', $event);
290: $notification->setVar('mode', $mode);
291: $this->insert($notification);
292: }
293: }
294: return true;
295: }
296:
297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308:
309: public function getByUser($user_id)
310: {
311: $criteria = new Criteria('uid', $user_id);
312: return $this->getObjectsArray($criteria, true);
313: }
314:
315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326:
327: public function getSubscribedEvents($category, $item_id, $module_id, $user_id)
328: {
329: $criteria = new CriteriaCompo();
330: $criteria->add(new Criteria('modid', (int)($module_id)));
331: $criteria->add(new Criteria('category', $category));
332: if ($item_id) {
333: $criteria->add(new Criteria('itemid', (int)($item_id)));
334: }
335: $criteria->add(new Criteria('uid', (int)($user_id)));
336: $results = $this->getObjectsArray($criteria, true);
337: $ret = array();
338:
339: foreach ($results as $result) {
340: $ret[] = $result->getVar('event');
341: }
342: return $ret;
343: }
344:
345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356:
357: public function getByItemId($module_id, $item_id, $order = null, $status = null)
358: {
359: $criteria = new CriteriaCompo(new Criteria('modid', (int)($module_id)));
360: $criteria->add(new Criteria('itemid', (int)($item_id)));
361: if (isset($status)) {
362: $criteria->add(new Criteria('status', (int)($status)));
363: }
364: if (isset($order)) {
365: $criteria->setOrder($order);
366: }
367: return $this->getObjectsArray($criteria);
368: }
369:
370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391:
392: public function triggerEvents(
393: $category,
394: $item_id,
395: $events,
396: $extra_tags = array(),
397: $user_list = array(),
398: $module_id = null,
399: $omit_user_id = null
400: ) {
401: if (!is_array($events)) {
402: $events = array($events);
403: }
404: foreach ($events as $event) {
405: $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
406: }
407: }
408:
409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421:
422: public function triggerEvent(
423: $category,
424: $item_id,
425: $event,
426: $extra_tags = array(),
427: $user_list = array(),
428: $module_id = null,
429: $omit_user_id = null
430: ) {
431: $xoops = xoops::getInstance();
432: $helper = Notifications::getInstance();
433:
434: if (!isset($module_id)) {
435: $module = $xoops->module;
436: $module_id = $xoops->isModule() ? $xoops->module->getVar('mid') : 0;
437: } else {
438: $module = $xoops->getHandlerModule()->get($module_id);
439: }
440:
441:
442: $mod_config = $xoops->getHandlerConfig()->getConfigsByModule($module->getVar('mid'));
443: if (empty($mod_config['notifications_enabled'])) {
444: return false;
445: }
446: $category_info = $helper->getCategory($category, $module->getVar('dirname'));
447: $event_info = $helper->getEvent($category, $event, $module->getVar('dirname'));
448: if (!in_array(
449: $helper->generateConfig($category_info, $event_info, 'option_name'),
450: $mod_config['notification_events']
451: ) && empty($event_info['invisible'])) {
452: return false;
453: }
454:
455: if (!isset($omit_user_id)) {
456: if ($xoops->isUser()) {
457: $omit_user_id = $xoops->user->getVar('uid');
458: } else {
459: $omit_user_id = 0;
460: }
461: }
462: $criteria = new CriteriaCompo();
463: $criteria->add(new Criteria('modid', (int)($module_id)));
464: $criteria->add(new Criteria('category', $category));
465: $criteria->add(new Criteria('itemid', (int)($item_id)));
466: $criteria->add(new Criteria('event', $event));
467: $mode_criteria = new CriteriaCompo();
468: $mode_criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_SENDALWAYS), 'OR');
469: $mode_criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_SENDONCETHENDELETE), 'OR');
470: $mode_criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_SENDONCETHENWAIT), 'OR');
471: $criteria->add($mode_criteria);
472: if (!empty($user_list)) {
473: $user_criteria = new CriteriaCompo();
474: foreach ($user_list as $user) {
475: $user_criteria->add(new Criteria('uid', (int)($user)), 'OR');
476: }
477: $criteria->add($user_criteria);
478: }
479: $notifications = $this->getObjectsArray($criteria);
480: if (empty($notifications)) {
481: return false;
482: }
483:
484: $item_info = $helper->getEvent($category, $item_id, $module->getVar('dirname'));
485:
486:
487: $tags = $helper->getTags($category, $item_id, $event, $module->getVar('dirname'));
488:
489: $tags['X_ITEM_NAME']
490: = !empty($item_info['name']) ? $item_info['name'] : '[' . _MD_NOTIFICATIONS_ITEMNAMENOTAVAILABLE . ']';
491: $tags['X_ITEM_URL']
492: = !empty($item_info['url']) ? $item_info['url'] : '[' . _MD_NOTIFICATIONS_ITEMURLNOTAVAILABLE . ']';
493: $tags['X_ITEM_TYPE']
494: = !empty($category_info['item_name']) ? $category_info['title'] : '['
495: . _MD_NOTIFICATIONS_ITEMTYPENOTAVAILABLE . ']';
496: $tags['X_MODULE'] = $module->getVar('name');
497: $tags['X_MODULE_URL'] = \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/';
498: $tags['X_NOTIFY_CATEGORY'] = $category;
499: $tags['X_NOTIFY_EVENT'] = $event;
500:
501: $template_dir = $event_info['mail_template_dir'];
502: $template = $event_info['mail_template'] . '.tpl';
503: $subject = $event_info['mail_subject'];
504:
505: foreach ($notifications as $notification) {
506:
507: if (empty($omit_user_id) || $notification->getVar('uid') != $omit_user_id) {
508:
509:
510:
511: $tags['X_UNSUBSCRIBE_URL'] = $helper->url('index.php');
512: $tags = array_merge($tags, $extra_tags);
513: $notification->notifyUser($template_dir, $template, $subject, $tags);
514: }
515: }
516: return true;
517: }
518:
519: 520: 521: 522: 523: 524: 525:
526: public function unsubscribeByUser($user_id)
527: {
528: $criteria = new Criteria('uid', (int)($user_id));
529: return $this->deleteAll($criteria);
530: }
531:
532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544:
545: public function unsubscribe($category, $item_id, $events, $module_id = null, $user_id = null)
546: {
547: $xoops = Xoops::getInstance();
548: if (!isset($user_id)) {
549: if (!$xoops->isUser()) {
550: return false;
551: } else {
552: $user_id = $xoops->user->getVar('uid');
553: }
554: }
555: if (!isset($module_id)) {
556: $module_id = $xoops->module->getVar('mid');
557: }
558: $criteria = new CriteriaCompo();
559: $criteria->add(new Criteria('modid', (int)($module_id)));
560: $criteria->add(new Criteria('category', $category));
561: $criteria->add(new Criteria('itemid', (int)($item_id)));
562: $criteria->add(new Criteria('uid', (int)($user_id)));
563: if (!is_array($events)) {
564: $events = array($events);
565: }
566: $event_criteria = new CriteriaCompo();
567: foreach ($events as $event) {
568: $event_criteria->add(new Criteria('event', $event), 'OR');
569: }
570: $criteria->add($event_criteria);
571: return $this->deleteAll($criteria);
572: }
573:
574: 575: 576: 577: 578: 579: 580: 581: 582:
583: public function unsubscribeByModule($module_id)
584: {
585: $criteria = new Criteria('modid', (int)($module_id));
586: return $this->deleteAll($criteria);
587: }
588:
589: 590: 591: 592: 593: 594: 595: 596: 597:
598: public function unsubscribeByItem($module_id, $category, $item_id)
599: {
600: $criteria = new CriteriaCompo();
601: $criteria->add(new Criteria('modid', (int)($module_id)));
602: $criteria->add(new Criteria('category', $category));
603: $criteria->add(new Criteria('itemid', (int)($item_id)));
604: return $this->deleteAll($criteria);
605: }
606:
607: 608: 609: 610: 611: 612: 613: 614: 615: 616:
617: public function doLoginMaintenance($user_id)
618: {
619: $criteria = new CriteriaCompo();
620: $criteria->add(new Criteria('uid', (int)($user_id)));
621: $criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_WAITFORLOGIN));
622:
623: $notifications = $this->getObjectsArray($criteria, true);
624: foreach ($notifications as $n) {
625:
626: $n->setVar('mode', NOTIFICATIONS_MODE_SENDONCETHENWAIT);
627: $this->insert($n);
628: }
629: }
630:
631: 632: 633: 634: 635: 636: 637: 638: 639:
640: public function updateByField(NotificationsNotification $notification, $field_name, $field_value)
641: {
642: $notification->unsetNew();
643: $notification->setVar($field_name, $field_value);
644: return $this->insert($notification);
645: }
646: }
647: