XOOPS  2.6.0
notification.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
18 
33 {
37  public function __construct()
38  {
39  $this->initVar('id', XOBJ_DTYPE_INT, null, false);
40  $this->initVar('modid', XOBJ_DTYPE_INT, null, false);
41  $this->initVar('category', XOBJ_DTYPE_TXTBOX, null, false, 30);
42  $this->initVar('itemid', XOBJ_DTYPE_INT, 0, false);
43  $this->initVar('event', XOBJ_DTYPE_TXTBOX, null, false, 30);
44  $this->initVar('uid', XOBJ_DTYPE_INT, 0, true);
45  $this->initVar('mode', XOBJ_DTYPE_INT, 0, false);
46  }
47 
48  // FIXME:???
49  // To send email to multiple users simultaneously, we would need to move
50  // the notify functionality to the handler class. BUT, some of the tags
51  // are user-dependent, so every email msg will be unique. (Unless maybe use
52  // smarty for email templates in the future.) Also we would have to keep
53  // track if each user wanted email or PM.
54 
65  public function notifyUser($template_dir, $template, $subject, $tags)
66  {
67  //todo fix this for mail module
68  $xoops = xoops::getInstance();
70 
71  // Check the user's notification preference.
72  $member_handler = $xoops->getHandlerMember();
73  $user = $member_handler->getUser($this->getVar('uid'));
74  if (!is_object($user)) {
75  return true;
76  }
77  //todo, remove this from user profile
78  $method = $user->getVar('notify_method');
79 
80  $xoopsMailer = $xoops->getMailer();
81  switch ($method) {
82  case NOTIFICATIONS_METHOD_PM:
83  $xoopsMailer->usePM();
84  //todo, is this config in core or in mail module?
85  $xoopsMailer->setFromUser($member_handler->getUser($xoops->getConfig('fromuid')));
86  foreach ($tags as $k => $v) {
87  $xoopsMailer->assign($k, $v);
88  }
89  break;
90  case NOTIFICATIONS_METHOD_EMAIL:
91  $xoopsMailer->useMail();
92  foreach ($tags as $k => $v) {
93  $xoopsMailer->assign($k, preg_replace("/&amp;/i", '&', $v));
94  }
95  break;
96  default:
97  return true; // report error in user's profile??
98  break;
99  }
100 
101  // Set up the mailer
102  $xoopsMailer->setTemplateDir($template_dir);
103  $xoopsMailer->setTemplate($template);
104  $xoopsMailer->setToUsers($user);
105  //global $xoopsConfig;
106  //$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
107  //$xoopsMailer->setFromName($xoopsConfig['sitename']);
108  $xoopsMailer->setSubject($subject);
109  $success = $xoopsMailer->send();
110 
111  // If send-once-then-delete, delete notification
112  // If send-once-then-wait, disable notification
113  $notification_handler = $helper->getHandlerNotification();
114 
115  if ($this->getVar('mode') == NOTIFICATIONS_MODE_SENDONCETHENDELETE) {
116  $notification_handler->delete($this);
117  return $success;
118  }
119 
120  if ($this->getVar('mode') == NOTIFICATIONS_MODE_SENDONCETHENWAIT) {
121  $this->setVar('mode', NOTIFICATIONS_MODE_WAITFORLOGIN);
122  $notification_handler->insert($this);
123  }
124  return $success;
125  }
126 }
127 
138 {
144  public function __construct(Connection $db = null)
145  {
146  parent::__construct($db, 'notifications', 'NotificationsNotification', 'id', 'itemid');
147  }
148 
157  public function getObjectsArray(CriteriaElement $criteria = null, $id_as_key = false)
158  {
159  $qb = $this->db2->createXoopsQueryBuilder()
160  ->select('*')
161  ->from($this->table, null);
162  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
163  $criteria->renderQb($qb);
164  }
165  $result = $qb->execute();
166  $ret = array();
167  if (!$result) {
168  return $ret;
169  }
170  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
171  $notification = new NotificationsNotification();
172  $notification->assignVars($myrow);
173  if (!$id_as_key) {
174  $ret[] = $notification;
175  } else {
176  $ret[$myrow['id']] = $notification;
177  }
178  unset($notification);
179  }
180  return $ret;
181  }
182 
197  public function getNotification($module_id, $category, $item_id, $event, $user_id)
198  {
199  $criteria = new CriteriaCompo();
200  $criteria->add(new Criteria('modid', intval($module_id)));
201  $criteria->add(new Criteria('category', $category));
202  $criteria->add(new Criteria('itemid', intval($item_id)));
203  $criteria->add(new Criteria('event', $event));
204  $criteria->add(new Criteria('uid', intval($user_id)));
205  $objects = $this->getObjectsArray($criteria);
206  if (count($objects) == 1) {
207  return $objects[0];
208  }
209  $inst = false;
210  return $inst;
211  }
212 
225  public function isSubscribed($category, $item_id, $event, $module_id, $user_id)
226  {
227  $criteria = new CriteriaCompo();
228  $criteria->add(new Criteria('modid', intval($module_id)));
229  $criteria->add(new Criteria('category', $category));
230  $criteria->add(new Criteria('itemid', intval($item_id)));
231  $criteria->add(new Criteria('event', $event));
232  $criteria->add(new Criteria('uid', intval($user_id)));
233  return $this->getCount($criteria);
234  }
235 
253  public function subscribe($category, $item_id, $events, $mode = null, $module_id = null, $user_id = null)
254  {
256  if (!isset($user_id)) {
257  if (!$xoops->isUser()) {
258  return false; // anonymous cannot subscribe
259  } else {
260  $user_id = $xoops->user->getVar('uid');
261  }
262  }
263 
264  if (!isset($module_id)) {
265  $module_id = $xoops->module->getVar('mid');
266  }
267 
268  //todo, remove this from user profile
269  if (!isset($mode)) {
270  $user = new XoopsUser($user_id);
271  $mode = $user->getVar('notify_mode');
272  }
273 
274  if (!is_array($events)) {
275  $events = array($events);
276  }
277  foreach ($events as $event) {
278  if ($notification = $this->getNotification($module_id, $category, $item_id, $event, $user_id)) {
279  if ($notification->getVar('mode') != $mode) {
280  $this->updateByField($notification, 'mode', $mode);
281  }
282  } else {
283  $notification = $this->create();
284  $notification->setVar('modid', $module_id);
285  $notification->setVar('category', $category);
286  $notification->setVar('itemid', $item_id);
287  $notification->setVar('uid', $user_id);
288  $notification->setVar('event', $event);
289  $notification->setVar('mode', $mode);
290  $this->insert($notification);
291  }
292  }
293  return true;
294  }
295 
308  public function getByUser($user_id)
309  {
310  $criteria = new Criteria('uid', $user_id);
311  return $this->getObjectsArray($criteria, true);
312  }
313 
326  public function getSubscribedEvents($category, $item_id, $module_id, $user_id)
327  {
328  $criteria = new CriteriaCompo();
329  $criteria->add(new Criteria('modid', intval($module_id)));
330  $criteria->add(new Criteria('category', $category));
331  if ($item_id) {
332  $criteria->add(new Criteria('itemid', intval($item_id)));
333  }
334  $criteria->add(new Criteria('uid', intval($user_id)));
335  $results = $this->getObjectsArray($criteria, true);
336  $ret = array();
337  /* @var NotificationsNotification $result*/
338  foreach ($results as $result) {
339  $ret[] = $result->getVar('event');
340  }
341  return $ret;
342  }
343 
356  public function getByItemId($module_id, $item_id, $order = null, $status = null)
357  {
358  $criteria = new CriteriaCompo(new Criteria('modid', intval($module_id)));
359  $criteria->add(new Criteria('itemid', intval($item_id)));
360  if (isset($status)) {
361  $criteria->add(new Criteria('status', intval($status)));
362  }
363  if (isset($order)) {
364  $criteria->setOrder($order);
365  }
366  return $this->getObjectsArray($criteria);
367  }
368 
391  public function triggerEvents(
392  $category,
393  $item_id,
394  $events,
395  $extra_tags = array(),
396  $user_list = array(),
397  $module_id = null,
398  $omit_user_id = null
399  ) {
400  if (!is_array($events)) {
401  $events = array($events);
402  }
403  foreach ($events as $event) {
404  $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
405  }
406  }
407 
421  public function triggerEvent(
422  $category,
423  $item_id,
424  $event,
425  $extra_tags = array(),
426  $user_list = array(),
427  $module_id = null,
428  $omit_user_id = null
429  ) {
430  $xoops = xoops::getInstance();
432 
433  if (!isset($module_id)) {
434  $module = $xoops->module;
435  $module_id = $xoops->isModule() ? $xoops->module->getVar('mid') : 0;
436  } else {
437  $module = $xoops->getHandlerModule()->get($module_id);
438  }
439 
440  // Check if event is enabled
441  $mod_config = $xoops->getHandlerConfig()->getConfigsByModule($module->getVar('mid'));
442  if (empty($mod_config['notifications_enabled'])) {
443  return false;
444  }
445  $category_info = $helper->getCategory($category, $module->getVar('dirname'));
446  $event_info = $helper->getEvent($category, $event, $module->getVar('dirname'));
447  if (!in_array(
448  $helper->generateConfig($category_info, $event_info, 'option_name'),
449  $mod_config['notification_events']
450  ) && empty($event_info['invisible'])) {
451  return false;
452  }
453 
454  if (!isset($omit_user_id)) {
455  if ($xoops->isUser()) {
456  $omit_user_id = $xoops->user->getVar('uid');
457  } else {
458  $omit_user_id = 0;
459  }
460  }
461  $criteria = new CriteriaCompo();
462  $criteria->add(new Criteria('modid', intval($module_id)));
463  $criteria->add(new Criteria('category', $category));
464  $criteria->add(new Criteria('itemid', intval($item_id)));
465  $criteria->add(new Criteria('event', $event));
466  $mode_criteria = new CriteriaCompo();
467  $mode_criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_SENDALWAYS), 'OR');
468  $mode_criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_SENDONCETHENDELETE), 'OR');
469  $mode_criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_SENDONCETHENWAIT), 'OR');
470  $criteria->add($mode_criteria);
471  if (!empty($user_list)) {
472  $user_criteria = new CriteriaCompo();
473  foreach ($user_list as $user) {
474  $user_criteria->add(new Criteria('uid', intval($user)), 'OR');
475  }
476  $criteria->add($user_criteria);
477  }
478  $notifications = $this->getObjectsArray($criteria);
479  if (empty($notifications)) {
480  return false;
481  }
482 
483  $item_info = $helper->getEvent($category, $item_id, $module->getVar('dirname'));
484 
485  // Add some tag substitutions here
486  $tags = $helper->getTags($category, $item_id, $event, $module->getVar('dirname'));
487 
488  $tags['X_ITEM_NAME']
489  = !empty($item_info['name']) ? $item_info['name'] : '[' . _MD_NOTIFICATIONS_ITEMNAMENOTAVAILABLE . ']';
490  $tags['X_ITEM_URL']
491  = !empty($item_info['url']) ? $item_info['url'] : '[' . _MD_NOTIFICATIONS_ITEMURLNOTAVAILABLE . ']';
492  $tags['X_ITEM_TYPE']
493  = !empty($category_info['item_name']) ? $category_info['title'] : '['
495  $tags['X_MODULE'] = $module->getVar('name');
496  $tags['X_MODULE_URL'] = \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/';
497  $tags['X_NOTIFY_CATEGORY'] = $category;
498  $tags['X_NOTIFY_EVENT'] = $event;
499 
500  $template_dir = $event_info['mail_template_dir'];
501  $template = $event_info['mail_template'] . '.tpl';
502  $subject = $event_info['mail_subject'];
503 
504  foreach ($notifications as $notification) {
505  /* @var $notification NotificationsNotification */
506  if (empty($omit_user_id) || $notification->getVar('uid') != $omit_user_id) {
507  // user-specific tags
508  //$tags['X_UNSUBSCRIBE_URL'] = 'TODO';
509  // TODO: don't show unsubscribe link if it is 'one-time' ??
510  $tags['X_UNSUBSCRIBE_URL'] = $helper->url('index.php');
511  $tags = array_merge($tags, $extra_tags);
512  $notification->notifyUser($template_dir, $template, $subject, $tags);
513  }
514  }
515  return true;
516  }
517 
525  public function unsubscribeByUser($user_id)
526  {
527  $criteria = new Criteria('uid', intval($user_id));
528  return $this->deleteAll($criteria);
529  }
530 
544  public function unsubscribe($category, $item_id, $events, $module_id = null, $user_id = null)
545  {
547  if (!isset($user_id)) {
548  if (!$xoops->isUser()) {
549  return false; // anonymous cannot subscribe
550  } else {
551  $user_id = $xoops->user->getVar('uid');
552  }
553  }
554  if (!isset($module_id)) {
555  $module_id = $xoops->module->getVar('mid');
556  }
557  $criteria = new CriteriaCompo();
558  $criteria->add(new Criteria('modid', intval($module_id)));
559  $criteria->add(new Criteria('category', $category));
560  $criteria->add(new Criteria('itemid', intval($item_id)));
561  $criteria->add(new Criteria('uid', intval($user_id)));
562  if (!is_array($events)) {
563  $events = array($events);
564  }
565  $event_criteria = new CriteriaCompo();
566  foreach ($events as $event) {
567  $event_criteria->add(new Criteria('event', $event), 'OR');
568  }
569  $criteria->add($event_criteria);
570  return $this->deleteAll($criteria);
571  }
572 
583  {
584  $criteria = new Criteria('modid', intval($module_id));
585  return $this->deleteAll($criteria);
586  }
587 
597  public function unsubscribeByItem($module_id, $category, $item_id)
598  {
599  $criteria = new CriteriaCompo();
600  $criteria->add(new Criteria('modid', intval($module_id)));
601  $criteria->add(new Criteria('category', $category));
602  $criteria->add(new Criteria('itemid', intval($item_id)));
603  return $this->deleteAll($criteria);
604  }
605 
616  public function doLoginMaintenance($user_id)
617  {
618  $criteria = new CriteriaCompo();
619  $criteria->add(new Criteria('uid', intval($user_id)));
620  $criteria->add(new Criteria('mode', NOTIFICATIONS_MODE_WAITFORLOGIN));
621 
622  $notifications = $this->getObjectsArray($criteria, true);
623  foreach ($notifications as $n) {
624  /* @var $n NotificationsNotification */
625  $n->setVar('mode', NOTIFICATIONS_MODE_SENDONCETHENWAIT);
626  $this->insert($n);
627  }
628  }
629 
639  public function updateByField(NotificationsNotification $notification, $field_name, $field_value)
640  {
641  $notification->unsetNew();
642  $notification->setVar($field_name, $field_value);
643  return $this->insert($notification);
644  }
645 }
isSubscribed($category, $item_id, $event, $module_id, $user_id)
const _MD_NOTIFICATIONS_ITEMURLNOTAVAILABLE
Definition: main.php:36
const _MD_NOTIFICATIONS_ITEMNAMENOTAVAILABLE
Definition: main.php:34
if($uname== ''||$pass== '') $member_handler
Definition: checklogin.php:44
__construct(Connection $db=null)
updateByField(NotificationsNotification $notification, $field_name, $field_value)
static getInstance()
Definition: Xoops.php:160
triggerEvent($category, $item_id, $event, $extra_tags=array(), $user_list=array(), $module_id=null, $omit_user_id=null)
$module_id
Definition: update.php:56
$notification_handler
Definition: update.php:64
static getInstance()
Definition: helper.php:47
$result
Definition: pda.php:33
$user
Definition: checklogin.php:47
const _MD_NOTIFICATIONS_ITEMTYPENOTAVAILABLE
Definition: main.php:35
subscribe($category, $item_id, $events, $mode=null, $module_id=null, $user_id=null)
getNotification($module_id, $category, $item_id, $event, $user_id)
notifyUser($template_dir, $template, $subject, $tags)
getObjectsArray(CriteriaElement $criteria=null, $id_as_key=false)
getVar($key, $format= 's')
$xoops
Definition: admin.php:25
$status
static get($name)
$module
Definition: main.php:52
deleteAll(CriteriaElement $criteria, $force=true, $asObject=false)
$helper
$criteria
triggerEvents($category, $item_id, $events, $extra_tags=array(), $user_list=array(), $module_id=null, $omit_user_id=null)
setVar($key, $value, $not_gpc=false)
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options= '')
unsubscribeByItem($module_id, $category, $item_id)
$user_id
Definition: update.php:57
getSubscribedEvents($category, $item_id, $module_id, $user_id)
unsubscribe($category, $item_id, $events, $module_id=null, $user_id=null)
getByItemId($module_id, $item_id, $order=null, $status=null)