XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
notification.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
22 // RMV-NOTIFY
23 include_once $GLOBALS['xoops']->path('include/notification_constants.php');
24 include_once $GLOBALS['xoops']->path('include/notification_functions.php');
25 
36 {
40  function XoopsNotification()
41  {
42  $this->XoopsObject();
43  $this->initVar('not_id', XOBJ_DTYPE_INT, NULL, false);
44  $this->initVar('not_modid', XOBJ_DTYPE_INT, NULL, false);
45  $this->initVar('not_category', XOBJ_DTYPE_TXTBOX, null, false, 30);
46  $this->initVar('not_itemid', XOBJ_DTYPE_INT, 0, false);
47  $this->initVar('not_event', XOBJ_DTYPE_TXTBOX, null, false, 30);
48  $this->initVar('not_uid', XOBJ_DTYPE_INT, 0, true);
49  $this->initVar('not_mode', XOBJ_DTYPE_INT, 0, false);
50  }
51 
52  // FIXME:???
53  // To send email to multiple users simultaneously, we would need to move
54  // the notify functionality to the handler class. BUT, some of the tags
55  // are user-dependent, so every email msg will be unique. (Unless maybe use
56  // smarty for email templates in the future.) Also we would have to keep
57  // track if each user wanted email or PM.
58 
59 
63  function id($format = 'N')
64  {
65  return $this->getVar('not_id', $format);
66  }
67 
71  function not_id($format = '')
72  {
73  return $this->getVar('not_id', $format);
74  }
75 
79  function not_modid($format = '')
80  {
81  return $this->getVar('not_modid', $format);
82  }
83 
87  function not_category($format = '')
88  {
89  return $this->getVar('not_category', $format);
90  }
91 
95  function not_itemid($format = '')
96  {
97  return $this->getVar('not_itemid', $format);
98  }
99 
103  function not_event($format = '')
104  {
105  return $this->getVar('not_event', $format);
106  }
107 
111  function not_uid($format = '')
112  {
113  return $this->getVar('not_uid', $format);
114  }
115 
119  function not_mode($format = '')
120  {
121  return $this->getVar('not_mode', $format);
122  }
123 
134  function notifyUser($template_dir, $template, $subject, $tags)
135  {
136  // Check the user's notification preference.
137  $member_handler =& xoops_gethandler('member');
138  $user =& $member_handler->getUser($this->getVar('not_uid'));
139  if (!is_object($user)) {
140  return true;
141  }
142  $method = $user->getVar('notify_method');
143 
144  $xoopsMailer =& xoops_getMailer();
145  include_once $GLOBALS['xoops']->path('include/notification_constants.php');
146  switch ($method) {
148  $xoopsMailer->usePM();
149  $config_handler =& xoops_gethandler('config');
150  $xoopsMailerConfig = $config_handler->getConfigsByCat(XOOPS_CONF_MAILER);
151  $xoopsMailer->setFromUser($member_handler->getUser($xoopsMailerConfig['fromuid']));
152  foreach ($tags as $k => $v) {
153  $xoopsMailer->assign($k, $v);
154  }
155  break;
157  $xoopsMailer->useMail();
158  foreach ($tags as $k => $v) {
159  $xoopsMailer->assign($k, preg_replace("/&amp;/i", '&', $v));
160  }
161  break;
162  default:
163  return true; // report error in user's profile??
164  break;
165  }
166 
167  // Set up the mailer
168  $xoopsMailer->setTemplateDir($template_dir);
169  $xoopsMailer->setTemplate($template);
170  $xoopsMailer->setToUsers($user);
171  //global $xoopsConfig;
172  //$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
173  //$xoopsMailer->setFromName($xoopsConfig['sitename']);
174  $xoopsMailer->setSubject($subject);
175  $success = $xoopsMailer->send();
176 
177  // If send-once-then-delete, delete notification
178  // If send-once-then-wait, disable notification
179  include_once $GLOBALS['xoops']->path('include/notification_constants.php');
180  $notification_handler =& xoops_gethandler('notification');
181 
182  if ($this->getVar('not_mode') == XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE) {
183  $notification_handler->delete($this);
184  return $success;
185  }
186 
187  if ($this->getVar('not_mode') == XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT) {
188  $this->setVar('not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN);
189  $notification_handler->insert($this);
190  }
191  return $success;
192 
193  }
194 
195 }
196 
211 {
219  function &create($isNew = true)
220  {
221  $notification = new XoopsNotification();
222  if ($isNew) {
223  $notification->setNew();
224  }
225  return $notification;
226  }
227 
235  function &get($id)
236  {
237  $notification = false;
238  $id = intval($id);
239  if ($id > 0) {
240  $sql = 'SELECT * FROM ' . $this->db->prefix('xoopsnotifications') . ' WHERE not_id=' . $id;
241  if (!$result = $this->db->query($sql)) {
242  return $notification;
243  }
244  $numrows = $this->db->getRowsNum($result);
245  if ($numrows == 1) {
246  $notification = new XoopsNotification();
247  $notification->assignVars($this->db->fetchArray($result));
248  }
249  }
250  return $notification;
251  }
252 
260  function insert(&$notification)
261  {
265  if (!is_a($notification, 'xoopsnotification')) {
266  return false;
267  }
268  if (!$notification->isDirty()) {
269  return true;
270  }
271  if (!$notification->cleanVars()) {
272  return false;
273  }
274  foreach ($notification->cleanVars as $k => $v) {
275  ${$k} = $v;
276  }
277  if ($notification->isNew()) {
278  $not_id = $this->db->genId('xoopsnotifications_not_id_seq');
279  $sql = sprintf("INSERT INTO %s (not_id, not_modid, not_itemid, not_category, not_uid, not_event, not_mode) VALUES (%u, %u, %u, %s, %u, %s, %u)", $this->db->prefix('xoopsnotifications'), $not_id, $not_modid, $not_itemid, $this->db->quoteString($not_category), $not_uid, $this->db->quoteString($not_event), $not_mode);
280  } else {
281  $sql = sprintf("UPDATE %s SET not_modid = %u, not_itemid = %u, not_category = %s, not_uid = %u, not_event = %s, not_mode = %u WHERE not_id = %u", $this->db->prefix('xoopsnotifications'), $not_modid, $not_itemid, $this->db->quoteString($not_category), $not_uid, $this->db->quoteString($not_event), $not_mode, $not_id);
282  }
283  if (!$result = $this->db->query($sql)) {
284  return false;
285  }
286  if (empty($not_id)) {
287  $not_id = $this->db->getInsertId();
288  }
289  $notification->assignVar('not_id', $not_id);
290  return true;
291  }
292 
300  function delete(&$notification)
301  {
305  if (!is_a($notification, 'xoopsnotification')) {
306  return false;
307  }
308 
309  $sql = sprintf("DELETE FROM %s WHERE not_id = %u", $this->db->prefix('xoopsnotifications'), $notification->getVar('not_id'));
310  if (!$result = $this->db->query($sql)) {
311  return false;
312  }
313  return true;
314  }
315 
324  function getObjects($criteria = null, $id_as_key = false)
325  {
326  $ret = array();
327  $limit = $start = 0;
328  $sql = 'SELECT * FROM ' . $this->db->prefix('xoopsnotifications');
329  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
330  $sql .= ' ' . $criteria->renderWhere();
331  $sort = ($criteria->getSort() != '') ? $criteria->getSort() : 'not_id';
332  $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
333  $limit = $criteria->getLimit();
334  $start = $criteria->getStart();
335  }
336  $result = $this->db->query($sql, $limit, $start);
337  if (!$result) {
338  return $ret;
339  }
340  while ($myrow = $this->db->fetchArray($result)) {
341  $notification = new XoopsNotification();
342  $notification->assignVars($myrow);
343  if (!$id_as_key) {
344  $ret[] =& $notification;
345  } else {
346  $ret[$myrow['not_id']] =& $notification;
347  }
348  unset($notification);
349  }
350  return $ret;
351  }
352 
353  // TODO: Need this??
361  function getCount($criteria = null)
362  {
363  $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xoopsnotifications');
364  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
365  $sql .= ' ' . $criteria->renderWhere();
366  }
367  if (!$result =& $this->db->query($sql)) {
368  return 0;
369  }
370  list ($count) = $this->db->fetchRow($result);
371  return $count;
372  }
373 
381  function deleteAll($criteria = null)
382  {
383  $sql = 'DELETE FROM ' . $this->db->prefix('xoopsnotifications');
384  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
385  $sql .= ' ' . $criteria->renderWhere();
386  }
387  if (!$result = $this->db->query($sql)) {
388  return false;
389  }
390  return true;
391  }
392 
393  // TODO: rename this...
394  // Also, should we have get by module, get by category, etc...??
395  function &getNotification($module_id, $category, $item_id, $event, $user_id)
396  {
397  $criteria = new CriteriaCompo();
398  $criteria->add(new Criteria('not_modid', intval($module_id)));
399  $criteria->add(new Criteria('not_category', mysql_real_escape_string($category)));
400  $criteria->add(new Criteria('not_itemid', intval($item_id)));
401  $criteria->add(new Criteria('not_event', mysql_real_escape_string($event)));
402  $criteria->add(new Criteria('not_uid', intval($user_id)));
403  $objects = $this->getObjects($criteria);
404  if (count($objects) == 1) {
405  return $objects[0];
406  }
407  $inst = false;
408  return $inst;
409  }
410 
422  function isSubscribed($category, $item_id, $event, $module_id, $user_id)
423  {
424  $criteria = new CriteriaCompo();
425  $criteria->add(new Criteria('not_modid', intval($module_id)));
426  $criteria->add(new Criteria('not_category', mysql_real_escape_string($category)));
427  $criteria->add(new Criteria('not_itemid', intval($item_id)));
428  $criteria->add(new Criteria('not_event', mysql_real_escape_string($event)));
429  $criteria->add(new Criteria('not_uid', intval($user_id)));
430  return $this->getCount($criteria);
431 
432  }
433 
434  // TODO: how about a function to subscribe a whole group of users???
435  // e.g. if we want to add all moderators to be notified of subscription
436  // of new threads...
448  function subscribe($category, $item_id, $events, $mode = null, $module_id = null, $user_id = null)
449  {
450  if (!isset($user_id)) {
451  global $xoopsUser;
452  if (empty($xoopsUser)) {
453  return false; // anonymous cannot subscribe
454  } else {
455  $user_id = $xoopsUser->getVar('uid');
456  }
457  }
458 
459  if (!isset($module_id)) {
460  global $xoopsModule;
461  $module_id = $xoopsModule->getVar('mid');
462  }
463 
464  if (!isset($mode)) {
465  $user = new XoopsUser($user_id);
466  $mode = $user->getVar('notify_mode');
467  }
468 
469  if (!is_array($events)) {
470  $events = array($events);
471  }
472  foreach ($events as $event) {
473  if ($notification =& $this->getNotification($module_id, $category, $item_id, $event, $user_id)) {
474  if ($notification->getVar('not_mode') != $mode) {
475  $this->updateByField($notification, 'not_mode', $mode);
476  }
477  } else {
478  $notification =& $this->create();
479  $notification->setVar('not_modid', $module_id);
480  $notification->setVar('not_category', $category);
481  $notification->setVar('not_itemid', $item_id);
482  $notification->setVar('not_uid', $user_id);
483  $notification->setVar('not_event', $event);
484  $notification->setVar('not_mode', $mode);
485  $this->insert($notification);
486  }
487  }
488  }
489 
490  // TODO: this will be to provide a list of everything a particular
491  // user has subscribed to... e.g. for on the 'Profile' page, similar
492  // to how we see the various posts etc. that the user has made.
493  // We may also want to have a function where we can specify module id
501  function getByUser($user_id)
502  {
503  $criteria = new Criteria('not_uid', $user_id);
504  return $this->getObjects($criteria, true);
505  }
506 
507  // TODO: rename this??
512  function getSubscribedEvents($category, $item_id, $module_id, $user_id)
513  {
514  $criteria = new CriteriaCompo();
515  $criteria->add(new Criteria('not_modid', intval($module_id)));
516  $criteria->add(new Criteria('not_category', mysql_real_escape_string($category)));
517  if ($item_id) {
518  $criteria->add(new Criteria('not_itemid', intval($item_id)));
519  }
520  $criteria->add(new Criteria('not_uid', intval($user_id)));
521  $results = $this->getObjects($criteria, true);
522  $ret = array();
523  foreach (array_keys($results) as $i) {
524  $ret[] = $results[$i]->getVar('not_event');
525  }
526  return $ret;
527  }
528 
529  // TODO: is this a useful function?? (Copied from comment_handler)
539  function getByItemId($module_id, $item_id, $order = null, $status = null)
540  {
541  $criteria = new CriteriaCompo(new Criteria('com_modid', intval($module_id)));
542  $criteria->add(new Criteria('com_itemid', intval($item_id)));
543  if (isset($status)) {
544  $criteria->add(new Criteria('com_status', intval($status)));
545  }
546  if (isset($order)) {
547  $criteria->setOrder($order);
548  }
549  return $this->getObjects($criteria);
550  }
551 
564  // TODO:(?) - pass in an event LIST. This will help to avoid
565  // problem of sending people multiple emails for similar events.
566  // BUT, then we need an array of mail templates, etc... Unless
567  // mail templates can include logic in the future, then we can
568  // tailor the mail so it makes sense for any of the possible
569  // (or combination of) events.
570  function triggerEvents($category, $item_id, $events, $extra_tags = array(), $user_list = array(), $module_id = null, $omit_user_id = null)
571  {
572  if (!is_array($events)) {
573  $events = array($events);
574  }
575  foreach ($events as $event) {
576  $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
577  }
578  }
579 
592  function triggerEvent($category, $item_id, $event, $extra_tags = array(), $user_list = array(), $module_id = null, $omit_user_id = null)
593  {
594 
595  if (!isset($module_id)) {
596  global $xoopsModule;
598  $module_id =! empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
599  } else {
600  $module_handler =& xoops_gethandler('module');
602  }
603 
604  // Check if event is enabled
605  $config_handler =& xoops_gethandler('config');
606  $mod_config = $config_handler->getConfigsByCat(0, $module->getVar('mid'));
607  if (empty($mod_config['notification_enabled'])) {
608  return false;
609  }
610  $category_info = notificationCategoryInfo($category, $module_id);
611  $event_info = notificationEventInfo($category, $event, $module_id);
612  if (!in_array(notificationGenerateConfig($category_info, $event_info, 'option_name'), $mod_config['notification_events']) && empty($event_info['invisible'])) {
613  return false;
614  }
615 
616  if (!isset($omit_user_id)) {
617  global $xoopsUser;
618  if (!empty($xoopsUser)) {
619  $omit_user_id = $xoopsUser->getVar('uid');
620  } else {
621  $omit_user_id = 0;
622  }
623  }
624  $criteria = new CriteriaCompo();
625  $criteria->add(new Criteria('not_modid', intval($module_id)));
626  $criteria->add(new Criteria('not_category', mysql_real_escape_string($category)));
627  $criteria->add(new Criteria('not_itemid', intval($item_id)));
628  $criteria->add(new Criteria('not_event', mysql_real_escape_string($event)));
629  $mode_criteria = new CriteriaCompo();
630  $mode_criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDALWAYS), 'OR');
631  $mode_criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE), 'OR');
632  $mode_criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT), 'OR');
633  $criteria->add($mode_criteria);
634  if (!empty($user_list)) {
635  $user_criteria = new CriteriaCompo();
636  foreach ($user_list as $user) {
637  $user_criteria->add(new Criteria('not_uid', intval($user)), 'OR');
638  }
639  $criteria->add($user_criteria);
640  }
641  $notifications = $this->getObjects($criteria);
642  if (empty($notifications)) {
643  return;
644  }
645 
646  // Add some tag substitutions here
647 
648  $not_config = $module->getInfo('notification');
649  $tags = array();
650  if (!empty($not_config)) {
651  if (!empty($not_config['tags_file'])) {
652  $tags_file = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname') . '/' . $not_config['tags_file']);
653  if (file_exists($tags_file)) {
654  include_once $tags_file;
655  if (!empty($not_config['tags_func'])) {
656  $tags_func = $not_config['tags_func'];
657  if (function_exists($tags_func)) {
658  $tags = $tags_func($category, intval($item_id), $event);
659  }
660  }
661  }
662  }
663  // RMV-NEW
664  if (! empty($not_config['lookup_file'])) {
665  $lookup_file = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname') . '/' . $not_config['lookup_file']);
666  if (file_exists($lookup_file)) {
667  include_once $lookup_file;
668  if (!empty($not_config['lookup_func'])) {
669  $lookup_func = $not_config['lookup_func'];
670  if (function_exists($lookup_func)) {
671  $item_info = $lookup_func($category, intval($item_id));
672  }
673  }
674  }
675  }
676  }
677  $tags['X_ITEM_NAME'] = ! empty($item_info['name']) ? $item_info['name'] : '[' . _NOT_ITEMNAMENOTAVAILABLE . ']';
678  $tags['X_ITEM_URL'] = ! empty($item_info['url']) ? $item_info['url'] : '[' . _NOT_ITEMURLNOTAVAILABLE . ']';
679  $tags['X_ITEM_TYPE'] = ! empty($category_info['item_name']) ? $category_info['title'] : '[' . _NOT_ITEMTYPENOTAVAILABLE . ']';
680  $tags['X_MODULE'] = $module->getVar('name');
681  $tags['X_MODULE_URL'] = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/';
682  $tags['X_NOTIFY_CATEGORY'] = $category;
683  $tags['X_NOTIFY_EVENT'] = $event;
684 
685  $template_dir = $event_info['mail_template_dir'];
686  $template = $event_info['mail_template'] . '.tpl';
687  $subject = $event_info['mail_subject'];
688 
689  foreach ($notifications as $notification) {
690  if (empty($omit_user_id) || $notification->getVar('not_uid') != $omit_user_id) {
691  // user-specific tags
692  //$tags['X_UNSUBSCRIBE_URL'] = 'TODO';
693  // TODO: don't show unsubscribe link if it is 'one-time' ??
694  $tags['X_UNSUBSCRIBE_URL'] = XOOPS_URL . '/notifications.php';
695  $tags = array_merge($tags, $extra_tags);
696  $notification->notifyUser($template_dir, $template, $subject, $tags);
697  }
698  }
699  }
700 
708  {
709  $criteria = new Criteria('not_uid', intval($user_id));
710  return $this->deleteAll($criteria);
711  }
712 
713  // TODO: allow these to use current module, etc...
725  function unsubscribe($category, $item_id, $events, $module_id = null, $user_id = null)
726  {
727  if (!isset($user_id)) {
728  global $xoopsUser;
729  if (empty($xoopsUser)) {
730  return false; // anonymous cannot subscribe
731  } else {
732  $user_id = $xoopsUser->getVar('uid');
733  }
734  }
735  if (!isset($module_id)) {
736  global $xoopsModule;
737  $module_id = $xoopsModule->getVar('mid');
738  }
739  $criteria = new CriteriaCompo();
740  $criteria->add(new Criteria('not_modid', intval($module_id)));
741  $criteria->add(new Criteria('not_category', mysql_real_escape_string($category)));
742  $criteria->add(new Criteria('not_itemid', intval($item_id)));
743  $criteria->add(new Criteria('not_uid', intval($user_id)));
744  if (!is_array($events)) {
745  $events = array($events);
746  }
747  $event_criteria = new CriteriaCompo();
748  foreach ($events as $event) {
749  $event_criteria->add(new Criteria('not_event', mysql_real_escape_string($event)), 'OR');
750  }
751  $criteria->add($event_criteria);
752  return $this->deleteAll($criteria);
753  }
754 
755  // TODO: When 'update' a module, may need to switch around some
756  // notification classes/IDs... or delete the ones that no longer
757  // exist.
765  {
766  $criteria = new Criteria('not_modid', intval($module_id));
767  return $this->deleteAll($criteria);
768  }
769 
779  function unsubscribeByItem($module_id, $category, $item_id)
780  {
781  $criteria = new CriteriaCompo();
782  $criteria->add(new Criteria('not_modid', intval($module_id)));
783  $criteria->add(new Criteria('not_category', mysql_real_escape_string($category)));
784  $criteria->add(new Criteria('not_itemid', intval($item_id)));
785  return $this->deleteAll($criteria);
786  }
787 
797  {
798  $criteria = new CriteriaCompo();
799  $criteria->add(new Criteria('not_uid', intval($user_id)));
801 
802  $notifications = $this->getObjects($criteria, true);
803  foreach ($notifications as $n) {
804  $n->setVar('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT);
805  $this->insert($n);
806  }
807  }
808 
818  function updateByField(&$notification, $field_name, $field_value)
819  {
820  $notification->unsetNew();
821  $notification->setVar($field_name, $field_value);
822  return $this->insert($notification);
823  }
824 
825 }
826 ?>