home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / notification.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  30.0 KB  |  821 lines

  1. <?php
  2. // $Id: notification.php 1102 2007-10-19 02:55:52Z dugris $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.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. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.xoops.org/ http://jp.xoops.org/  http://www.myweb.ne.jp/  //
  29. // Project: The XOOPS Project (http://www.xoops.org/)                        //
  30. // ------------------------------------------------------------------------- //
  31.  
  32. if (!defined('XOOPS_ROOT_PATH')) {
  33.     exit();
  34. }
  35. // RMV-NOTIFY
  36. include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
  37. include_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
  38.  
  39. /**
  40.  *
  41.  *
  42.  * @package     kernel
  43.  * @subpackage  notification
  44.  *
  45.  * @author        Michael van Dam    <mvandam@caltech.edu>
  46.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  47.  */
  48.  
  49. /**
  50.  * A Notification
  51.  *
  52.  * @package     kernel
  53.  * @subpackage  notification
  54.  *
  55.  * @author        Michael van Dam    <mvandam@caltech.edu>
  56.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  57.  */
  58. class XoopsNotification extends XoopsObject
  59. {
  60.  
  61.     /**
  62.      * Constructor
  63.      **/
  64.     function XoopsNotification()
  65.     {
  66.         $this->XoopsObject();
  67.         $this->initVar('not_id', XOBJ_DTYPE_INT, NULL, false);
  68.         $this->initVar('not_modid', XOBJ_DTYPE_INT, NULL, false);
  69.         $this->initVar('not_category', XOBJ_DTYPE_TXTBOX, null, false, 30);
  70.         $this->initVar('not_itemid', XOBJ_DTYPE_INT, 0, false);
  71.         $this->initVar('not_event', XOBJ_DTYPE_TXTBOX, null, false, 30);
  72.         $this->initVar('not_uid', XOBJ_DTYPE_INT, 0, true);
  73.         $this->initVar('not_mode', XOBJ_DTYPE_INT, 0, false);
  74.     }
  75.  
  76. // FIXME:???
  77. // To send email to multiple users simultaneously, we would need to move
  78. // the notify functionality to the handler class.  BUT, some of the tags
  79. // are user-dependent, so every email msg will be unique.  (Unless maybe use
  80. // smarty for email templates in the future.)  Also we would have to keep
  81. // track if each user wanted email or PM.
  82.  
  83.     /**
  84.      * Send a notification message to the user
  85.      *
  86.      * @param  string  $template_dir  Template directory
  87.      * @param  string  $template      Template name
  88.      * @param  string  $subject       Subject line for notification message
  89.      * @param  array   $tags Array of substitutions for template variables
  90.      *
  91.      * @return  bool    true if success, false if error
  92.      **/
  93.     function notifyUser($template_dir, $template, $subject, $tags)
  94.     {
  95.         // Check the user's notification preference.
  96.  
  97.         $member_handler =& xoops_gethandler('member');
  98.         $user =& $member_handler->getUser($this->getVar('not_uid'));
  99.         if (!is_object($user)) {
  100.             return true;
  101.         }
  102.         $method = $user->getVar('notify_method');
  103.  
  104.         $xoopsMailer =& getMailer();
  105.         include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
  106.         switch($method) {
  107.         case XOOPS_NOTIFICATION_METHOD_PM:
  108.             $xoopsMailer->usePM();
  109.             $config_handler =& xoops_gethandler('config');
  110.             $xoopsMailerConfig =& $config_handler->getConfigsByCat(XOOPS_CONF_MAILER);
  111.             $xoopsMailer->setFromUser($member_handler->getUser($xoopsMailerConfig['fromuid']));
  112.             foreach ($tags as $k=>$v) {
  113.                 $xoopsMailer->assign($k, $v);
  114.             }
  115.             break;
  116.         case XOOPS_NOTIFICATION_METHOD_EMAIL:
  117.             $xoopsMailer->useMail();
  118.             foreach ($tags as $k=>$v) {
  119.                 $xoopsMailer->assign($k, preg_replace("/&/i", '&', $v));
  120.             }
  121.             break;
  122.         default:
  123.             return true; // report error in user's profile??
  124.             break;
  125.         }
  126.  
  127.         // Set up the mailer
  128.         $xoopsMailer->setTemplateDir($template_dir);
  129.         $xoopsMailer->setTemplate($template);
  130.         $xoopsMailer->setToUsers($user);
  131.         //global $xoopsConfig;
  132.         //$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
  133.         //$xoopsMailer->setFromName($xoopsConfig['sitename']);
  134.         $xoopsMailer->setSubject($subject);
  135.         $success = $xoopsMailer->send();
  136.  
  137.         // If send-once-then-delete, delete notification
  138.         // If send-once-then-wait, disable notification
  139.  
  140.         include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
  141.         $notification_handler =& xoops_gethandler('notification');
  142.  
  143.         if ($this->getVar('not_mode') == XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE) {
  144.             $notification_handler->delete($this);
  145.             return $success;
  146.         }
  147.  
  148.         if ($this->getVar('not_mode') == XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT) {
  149.             $this->setVar('not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN);
  150.             $notification_handler->insert($this);
  151.         }
  152.         return $success;
  153.  
  154.     }
  155.  
  156. }
  157.  
  158. /**
  159.  * XOOPS notification handler class.
  160.  *
  161.  * This class is responsible for providing data access mechanisms to the data source
  162.  * of XOOPS notification class objects.
  163.  *
  164.  *
  165.  * @package     kernel
  166.  * @subpackage  notification
  167.  *
  168.  * @author        Michael van Dam <mvandam@caltech.edu>
  169.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  170.  */
  171. class XoopsNotificationHandler extends XoopsObjectHandler
  172. {
  173.  
  174.     /**
  175.      * Create a {@link XoopsNotification}
  176.      *
  177.      * @param    bool    $isNew  Flag the object as "new"?
  178.      *
  179.      * @return    object
  180.      */
  181.     function &create($isNew = true)
  182.     {
  183.         $notification = new XoopsNotification();
  184.         if ($isNew) {
  185.             $notification->setNew();
  186.         }
  187.         return $notification;
  188.     }
  189.  
  190.  
  191.     /**
  192.      * Retrieve a {@link XoopsNotification}
  193.      *
  194.      * @param   int $id ID
  195.      *
  196.      * @return  object  {@link XoopsNotification}, FALSE on fail
  197.      **/
  198.     function &get($id)
  199.     {
  200.         $notification = false;
  201.         $id = intval($id);
  202.         if ($id > 0) {
  203.             $sql = 'SELECT * FROM '.$this->db->prefix('xoopsnotifications').' WHERE not_id='.$id;
  204.             if (!$result = $this->db->query($sql)) {
  205.                 return $notification;
  206.             }
  207.             $numrows = $this->db->getRowsNum($result);
  208.             if ($numrows == 1) {
  209.                 $notification = new XoopsNotification();
  210.                 $notification->assignVars($this->db->fetchArray($result));
  211.             }
  212.         }
  213.         return $notification;
  214.     }
  215.  
  216.  
  217.     /**
  218.      * Write a notification(subscription) to database
  219.      *
  220.      * @param   object  &$notification
  221.      *
  222.      * @return  bool
  223.      **/
  224.     function insert(&$notification)
  225.     {
  226.         /**
  227.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  228.         */
  229.         if (!is_a($notification, 'xoopsnotification')) {
  230.             return false;
  231.         }
  232.         if (!$notification->isDirty()) {
  233.             return true;
  234.         }
  235.         if (!$notification->cleanVars()) {
  236.             return false;
  237.         }
  238.         foreach ($notification->cleanVars as $k => $v) {
  239.             ${$k} = $v;
  240.         }
  241.         if ($notification->isNew()) {
  242.             $not_id = $this->db->genId('xoopsnotifications_not_id_seq');
  243.         $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);
  244.         } else {
  245.         $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);
  246.         }
  247.         if (!$result = $this->db->query($sql)) {
  248.             return false;
  249.         }
  250.         if (empty($not_id)) {
  251.             $not_id = $this->db->getInsertId();
  252.         }
  253.         $notification->assignVar('not_id', $not_id);
  254.         return true;
  255.     }
  256.  
  257.     /**
  258.      * Delete a {@link XoopsNotification} from the database
  259.      *
  260.      * @param   object  &$notification
  261.      *
  262.      * @return  bool
  263.      **/
  264.     function delete(&$notification)
  265.     {
  266.         /**
  267.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  268.         */
  269.         if (!is_a($notification, 'xoopsnotification')) {
  270.             return false;
  271.         }
  272.  
  273.         $sql = sprintf("DELETE FROM %s WHERE not_id = %u", $this->db->prefix('xoopsnotifications'), $notification->getVar('not_id'));
  274.         if (!$result = $this->db->query($sql)) {
  275.             return false;
  276.         }
  277.         return true;
  278.     }
  279.  
  280.     /**
  281.      * Get some {@link XoopsNotification}s
  282.      *
  283.      * @param   object  $criteria
  284.      * @param   bool    $id_as_key  Use IDs as keys into the array?
  285.      *
  286.      * @return  array   Array of {@link XoopsNotification} objects
  287.      **/
  288.     function getObjects($criteria = null, $id_as_key = false)
  289.     {
  290.         $ret = array();
  291.         $limit = $start = 0;
  292.         $sql = 'SELECT * FROM '.$this->db->prefix('xoopsnotifications');
  293.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  294.             $sql .= ' '.$criteria->renderWhere();
  295.             $sort = ($criteria->getSort() != '') ? $criteria->getSort() : 'not_id';
  296.             $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
  297.             $limit = $criteria->getLimit();
  298.             $start = $criteria->getStart();
  299.         }
  300.         $result = $this->db->query($sql, $limit, $start);
  301.         if (!$result) {
  302.             return $ret;
  303.         }
  304.         while ($myrow = $this->db->fetchArray($result)) {
  305.             $notification = new XoopsNotification();
  306.             $notification->assignVars($myrow);
  307.             if (!$id_as_key) {
  308.                 $ret[] =& $notification;
  309.             } else {
  310.                 $ret[$myrow['not_id']] =& $notification;
  311.             }
  312.             unset($notification);
  313.         }
  314.         return $ret;
  315.     }
  316.  
  317. // TODO: Need this??
  318.     /**
  319.      * Count Notifications
  320.      *
  321.      * @param   object  $criteria   {@link CriteriaElement}
  322.      *
  323.      * @return  int     Count
  324.      **/
  325.     function getCount($criteria = null)
  326.     {
  327.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('xoopsnotifications');
  328.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  329.             $sql .= ' '.$criteria->renderWhere();
  330.         }
  331.         if (!$result =& $this->db->query($sql)) {
  332.             return 0;
  333.         }
  334.         list($count) = $this->db->fetchRow($result);
  335.         return $count;
  336.     }
  337.  
  338.     /**
  339.      * Delete multiple notifications
  340.      *
  341.      * @param   object  $criteria   {@link CriteriaElement}
  342.      *
  343.      * @return  bool
  344.      **/
  345.     function deleteAll($criteria = null)
  346.     {
  347.         $sql = 'DELETE FROM '.$this->db->prefix('xoopsnotifications');
  348.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  349.             $sql .= ' '.$criteria->renderWhere();
  350.         }
  351.         if (!$result = $this->db->query($sql)) {
  352.             return false;
  353.         }
  354.         return true;
  355.     }
  356.  
  357. // Need this??
  358.     /**
  359.      * Change a value in multiple notifications
  360.      *
  361.      * @param   string  $fieldname  Name of the field
  362.      * @param   string  $fieldvalue Value to write
  363.      * @param   object  $criteria   {@link CriteriaElement}
  364.      *
  365.      * @return  bool
  366.      **/
  367. /*
  368.     function updateAll($fieldname, $fieldvalue, $criteria = null)
  369.     {
  370.         $set_clause = is_numeric($fieldvalue) ? $filedname.' = '.$fieldvalue : $filedname." = '".$fieldvalue."'";
  371.         $sql = 'UPDATE '.$this->db->prefix('xoopsnotifications').' SET '.$set_clause;
  372.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  373.             $sql .= ' '.$criteria->renderWhere();
  374.         }
  375.         if (!$result = $this->db->query($sql)) {
  376.             return false;
  377.         }
  378.         return true;
  379.     }
  380. */
  381.  
  382.     // TODO: rename this...
  383.     // Also, should we have get by module, get by category, etc...??
  384.  
  385.     function &getNotification ($module_id, $category, $item_id, $event, $user_id)
  386.     {
  387.         $criteria = new CriteriaCompo();
  388.         $criteria->add(new Criteria('not_modid', intval($module_id)));
  389.         $criteria->add(new Criteria('not_category', $category));
  390.         $criteria->add(new Criteria('not_itemid', intval($item_id)));
  391.         $criteria->add(new Criteria('not_event', $event));
  392.         $criteria->add(new Criteria('not_uid', intval($user_id)));
  393.         $objects = $this->getObjects($criteria);
  394.         if (count($objects) == 1) {
  395.             return $objects[0];
  396.         }
  397.         $inst = false;
  398.         return $inst;
  399.     }
  400.  
  401.     /**
  402.      * Determine if a user is subscribed to a particular event in
  403.      * a particular module.
  404.      *
  405.      * @param  string  $category  Category of notification event
  406.      * @param  int     $item_id   Item ID of notification event
  407.      * @param  string  $event     Event
  408.      * @param  int     $module_id ID of module (default current module)
  409.      * @param  int     $user_id   ID of user (default current user)
  410.      * return int  0 if not subscribe; non-zero if subscribed
  411.      */
  412.  
  413.     function isSubscribed ($category, $item_id, $event, $module_id, $user_id)
  414.     {
  415.         $criteria = new CriteriaCompo();
  416.         $criteria->add(new Criteria('not_modid', intval($module_id)));
  417.         $criteria->add(new Criteria('not_category', $category));
  418.         $criteria->add(new Criteria('not_itemid', intval($item_id)));
  419.         $criteria->add(new Criteria('not_event', $event));
  420.         $criteria->add(new Criteria('not_uid', intval($user_id)));
  421.         return $this->getCount($criteria);
  422.  
  423.     }
  424.  
  425.  
  426.     // TODO: how about a function to subscribe a whole group of users???
  427.     // e.g. if we want to add all moderators to be notified of subscription
  428.     // of new threads...
  429.  
  430.     /**
  431.      * Subscribe for notification for an event(s)
  432.      *
  433.      * @param  string $category    category of notification
  434.      * @param  int    $item_id     ID of the item
  435.      * @param  mixed  $events      event string or array of events
  436.      * @param  int    $mode        force a particular notification mode
  437.      *                             (e.g. once_only) (default to current user preference)
  438.      * @param  int    $module_id   ID of the module (default to current module)
  439.      * @param  int    $user_id     ID of the user (default to current user)
  440.      **/
  441.     function subscribe ($category, $item_id, $events, $mode=null, $module_id=null, $user_id=null)
  442.     {
  443.         if (!isset($user_id)) {
  444.             global $xoopsUser;
  445.             if (empty($xoopsUser)) {
  446.                 return false;  // anonymous cannot subscribe
  447.             } else {
  448.                 $user_id = $xoopsUser->getVar('uid');
  449.             }
  450.         }
  451.  
  452.         if (!isset($module_id)) {
  453.             global $xoopsModule;
  454.             $module_id = $xoopsModule->getVar('mid');
  455.         }
  456.  
  457.         if (!isset($mode)) {
  458.             $user = new XoopsUser($user_id);
  459.             $mode = $user->getVar('notify_mode');
  460.         }
  461.  
  462.         if (!is_array($events)) $events = array($events);
  463.         foreach ($events as $event) {
  464.             if ($notification =& $this->getNotification($module_id, $category, $item_id, $event, $user_id)) {
  465.                 if ($notification->getVar('not_mode') != $mode) {
  466.                     $this->updateByField($notification, 'not_mode', $mode);
  467.                 }
  468.             } else {
  469.                 $notification =& $this->create();
  470.                 $notification->setVar('not_modid', $module_id);
  471.                 $notification->setVar('not_category', $category);
  472.                 $notification->setVar('not_itemid', $item_id);
  473.                 $notification->setVar('not_uid', $user_id);
  474.                 $notification->setVar('not_event', $event);
  475.                 $notification->setVar('not_mode', $mode);
  476.                 $this->insert($notification);
  477.             }
  478.         }
  479.     }
  480.  
  481.  
  482. // TODO: this will be to provide a list of everything a particular
  483. // user has subscribed to... e.g. for on the 'Profile' page, similar
  484. // to how we see the various posts etc. that the user has made.
  485. // We may also want to have a function where we can specify module id
  486.     /**
  487.      * Get a list of notifications by user ID
  488.      *
  489.      * @param  int  $user_id  ID of the user
  490.      *
  491.      * @return array  Array of {@link XoopsNotification} objects
  492.      **/
  493.     function getByUser ($user_id)
  494.     {
  495.         $criteria = new Criteria ('not_uid', $user_id);
  496.         return $this->getObjects($criteria, true);
  497.     }
  498.  
  499.     // TODO: rename this??
  500.     /**
  501.      * Get a list of notification events for the current item/mod/user
  502.      *
  503.      **/
  504.     function getSubscribedEvents ($category, $item_id, $module_id, $user_id)
  505.     {
  506.         $criteria = new CriteriaCompo();
  507.         $criteria->add (new Criteria('not_modid', $module_id));
  508.         $criteria->add (new Criteria('not_category', $category));
  509.         if ($item_id) {
  510.             $criteria->add (new Criteria('not_itemid', $item_id));
  511.         }
  512.         $criteria->add (new Criteria('not_uid', $user_id));
  513.         $results = $this->getObjects($criteria, true);
  514.         $ret = array();
  515.         foreach (array_keys($results) as $i) {
  516.             $ret[] = $results[$i]->getVar('not_event');
  517.         }
  518.         return $ret;
  519.     }
  520.  
  521. // TODO: is this a useful function?? (Copied from comment_handler)
  522.     /**
  523.      * Retrieve items by their ID
  524.      *
  525.      * @param   int     $module_id  Module ID
  526.      * @param   int     $item_id    Item ID
  527.      * @param   string  $order      Sort order
  528.      *
  529.      * @return  array   Array of {@link XoopsNotification} objects
  530.      **/
  531.     function getByItemId($module_id, $item_id, $order = null, $status = null)
  532.     {
  533.         $criteria = new CriteriaCompo(new Criteria('com_modid', intval($module_id)));
  534.         $criteria->add(new Criteria('com_itemid', intval($item_id)));
  535.         if (isset($status)) {
  536.             $criteria->add(new Criteria('com_status', intval($status)));
  537.         }
  538.         if (isset($order)) {
  539.             $criteria->setOrder($order);
  540.         }
  541.         return $this->getObjects($criteria);
  542.     }
  543.  
  544.  
  545.     /**
  546.      * Send notifications to users
  547.      *
  548.      * @param  string $category   notification category
  549.      * @param  int $item_id    ID of the item
  550.      * @param  string  $event  notification event
  551.      * @param  array  $extra_tags array of substitutions for template to be
  552.      *                             merged with the one from function..
  553.      * @param  array  $user_list  only notify the selected users
  554.      * @param  int $module_id  ID of the module
  555.      * @param  int $omit_user_id    ID of the user to omit from notifications. (default to current user).  set to 0 for all users to receive notification.
  556.      **/
  557.     // TODO:(?) - pass in an event LIST.  This will help to avoid
  558.     // problem of sending people multiple emails for similar events.
  559.     // BUT, then we need an array of mail templates, etc...  Unless
  560.     // mail templates can include logic in the future, then we can
  561.     // tailor the mail so it makes sense for any of the possible
  562.     // (or combination of) events.
  563.  
  564.     function triggerEvents ($category, $item_id, $events, $extra_tags=array(), $user_list=array(), $module_id=null, $omit_user_id=null)
  565.     {
  566.         if (!is_array($events)) {
  567.             $events = array($events);
  568.         }
  569.         foreach ($events as $event) {
  570.             $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
  571.         }
  572.     }
  573.  
  574.     function triggerEvent ($category, $item_id, $event, $extra_tags=array(), $user_list=array(), $module_id=null, $omit_user_id=null)
  575.     {
  576.  
  577.         if (!isset($module_id)) {
  578.             global $xoopsModule;
  579.             $module =& $xoopsModule;
  580.             $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
  581.         } else {
  582.             $module_handler =& xoops_gethandler('module');
  583.             $module =& $module_handler->get($module_id);
  584.         }
  585.  
  586.         // Check if event is enabled
  587.         $config_handler =& xoops_gethandler('config');
  588.         $mod_config =& $config_handler->getConfigsByCat(0,$module->getVar('mid'));
  589.         if (empty($mod_config['notification_enabled'])) {
  590.             return false;
  591.         }
  592.         $category_info =& notificationCategoryInfo ($category, $module_id);
  593.         $event_info =& notificationEventInfo ($category, $event, $module_id);
  594.         if (!in_array(notificationGenerateConfig($category_info,$event_info,'option_name'),$mod_config['notification_events']) && empty($event_info['invisible'])) {
  595.             return false;
  596.         }
  597.  
  598.         if (!isset($omit_user_id)) {
  599.             global $xoopsUser;
  600.             if (!empty($xoopsUser)) {
  601.                 $omit_user_id = $xoopsUser->getVar('uid');
  602.             } else {
  603.                 $omit_user_id = 0;
  604.             }
  605.         }
  606.         $criteria = new CriteriaCompo();
  607.         $criteria->add(new Criteria('not_modid', intval($module_id)));
  608.         $criteria->add(new Criteria('not_category', $category));
  609.         $criteria->add(new Criteria('not_itemid', intval($item_id)));
  610.         $criteria->add(new Criteria('not_event', $event));
  611.         $mode_criteria = new CriteriaCompo();
  612.         $mode_criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDALWAYS), 'OR');
  613.         $mode_criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE), 'OR');
  614.         $mode_criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT), 'OR');
  615.         $criteria->add($mode_criteria);
  616.         if (!empty($user_list)) {
  617.             $user_criteria = new CriteriaCompo();
  618.             foreach ($user_list as $user) {
  619.                 $user_criteria->add (new Criteria('not_uid', $user), 'OR');
  620.             }
  621.             $criteria->add($user_criteria);
  622.         }
  623.         $notifications =& $this->getObjects($criteria);
  624.         if (empty($notifications)) {
  625.             return;
  626.         }
  627.  
  628.         // Add some tag substitutions here
  629.  
  630.         $not_config = $module->getInfo('notification');
  631.         $tags = array();
  632.         if (!empty($not_config)) {
  633.             if (!empty($not_config['tags_file'])) {
  634.                 $tags_file = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/' . $not_config['tags_file'];
  635.                 if (file_exists($tags_file)) {
  636.                     include_once $tags_file;
  637.                     if (!empty($not_config['tags_func'])) {
  638.                         $tags_func = $not_config['tags_func'];
  639.                         if (function_exists($tags_func)) {
  640.                             $tags = $tags_func($category, intval($item_id), $event);
  641.                         }
  642.                     }
  643.                 }
  644.             }
  645.             // RMV-NEW
  646.             if (!empty($not_config['lookup_file'])) {
  647.                 $lookup_file = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/' . $not_config['lookup_file'];
  648.                 if (file_exists($lookup_file)) {
  649.                     include_once $lookup_file;
  650.                     if (!empty($not_config['lookup_func'])) {
  651.                         $lookup_func = $not_config['lookup_func'];
  652.                         if (function_exists($lookup_func)) {
  653.                             $item_info = $lookup_func($category, intval($item_id));
  654.                         }
  655.                     }
  656.                 }
  657.             }
  658.         }
  659.         $tags['X_ITEM_NAME'] = !empty($item_info['name']) ? $item_info['name'] : '[' . _NOT_ITEMNAMENOTAVAILABLE . ']';
  660.         $tags['X_ITEM_URL']  = !empty($item_info['url']) ? $item_info['url'] : '[' . _NOT_ITEMURLNOTAVAILABLE . ']';
  661.         $tags['X_ITEM_TYPE'] = !empty($category_info['item_name']) ? $category_info['title'] : '[' . _NOT_ITEMTYPENOTAVAILABLE . ']';
  662.         $tags['X_MODULE'] = $module->getVar('name');
  663.         $tags['X_MODULE_URL'] = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/';
  664.         $tags['X_NOTIFY_CATEGORY'] = $category;
  665.         $tags['X_NOTIFY_EVENT'] = $event;
  666.  
  667.         $template_dir = $event_info['mail_template_dir'];
  668.         $template = $event_info['mail_template'] . '.tpl';
  669.         $subject = $event_info['mail_subject'];
  670.  
  671.         foreach ($notifications as $notification) {
  672.             if (empty($omit_user_id) || $notification->getVar('not_uid') != $omit_user_id) {
  673.                 // user-specific tags
  674.                 //$tags['X_UNSUBSCRIBE_URL'] = 'TODO';
  675.                 // TODO: don't show unsubscribe link if it is 'one-time' ??
  676.                 $tags['X_UNSUBSCRIBE_URL'] = XOOPS_URL . '/notifications.php';
  677.                 $tags = array_merge ($tags, $extra_tags);
  678.  
  679.                 $notification->notifyUser($template_dir, $template, $subject, $tags);
  680.             }
  681.         }
  682.     }
  683.  
  684.  
  685.     /**
  686.      * Delete all notifications for one user
  687.      *
  688.      * @param   int $user_id  ID of the user
  689.      * @return  bool
  690.      **/
  691.     function unsubscribeByUser ($user_id)
  692.     {
  693.         $criteria = new Criteria('not_uid', intval($user_id));
  694.         return $this->deleteAll($criteria);
  695.     }
  696.  
  697.  
  698. // TODO: allow these to use current module, etc...
  699.  
  700.     /**
  701.      * Unsubscribe notifications for an event(s).
  702.      *
  703.      * @param  string  $category    category of the events
  704.      * @param  int     $item_id     ID of the item
  705.      * @param  mixed   $events      event string or array of events
  706.      * @param  int     $module_id   ID of the module (default current module)
  707.      * @param  int     $user_id     UID of the user (default current user)
  708.      *
  709.      * @return bool
  710.      **/
  711.  
  712.     function unsubscribe ($category, $item_id, $events, $module_id=null, $user_id=null)
  713.     {
  714.         if (!isset($user_id)) {
  715.             global $xoopsUser;
  716.             if (empty($xoopsUser)) {
  717.                 return false;  // anonymous cannot subscribe
  718.             } else {
  719.                 $user_id = $xoopsUser->getVar('uid');
  720.             }
  721.         }
  722.  
  723.         if (!isset($module_id)) {
  724.             global $xoopsModule;
  725.             $module_id = $xoopsModule->getVar('mid');
  726.         }
  727.  
  728.         $criteria = new CriteriaCompo();
  729.         $criteria->add (new Criteria('not_modid', intval($module_id)));
  730.         $criteria->add (new Criteria('not_category', $category));
  731.         $criteria->add (new Criteria('not_itemid', intval($item_id)));
  732.         $criteria->add (new Criteria('not_uid', intval($user_id)));
  733.         if (!is_array($events)) {
  734.             $events = array($events);
  735.         }
  736.         $event_criteria = new CriteriaCompo();
  737.         foreach ($events as $event) {
  738.             $event_criteria->add (new Criteria('not_event', $event), 'OR');
  739.         }
  740.         $criteria->add($event_criteria);
  741.         return $this->deleteAll($criteria);
  742.     }
  743.  
  744.  
  745.     // TODO: When 'update' a module, may need to switch around some
  746.     //  notification classes/IDs...  or delete the ones that no longer
  747.     //  exist.
  748.  
  749.     /**
  750.      * Delete all notifications for a particular module
  751.      *
  752.      * @param   int $module_id  ID of the module
  753.      * @return  bool
  754.      **/
  755.     function unsubscribeByModule ($module_id)
  756.     {
  757.         $criteria = new Criteria('not_modid', intval($module_id));
  758.         return $this->deleteAll($criteria);
  759.     }
  760.  
  761.  
  762.     /**
  763.      * Delete all subscriptions for a particular item.
  764.      *
  765.      * @param  int    $module_id  ID of the module to which item belongs
  766.      * @param  string $category   Notification category of the item
  767.      * @param  int    $item_id    ID of the item
  768.      *
  769.      * @return bool
  770.      **/
  771.     function unsubscribeByItem ($module_id, $category, $item_id)
  772.     {
  773.         $criteria = new CriteriaCompo();
  774.         $criteria->add (new Criteria('not_modid', intval($module_id)));
  775.         $criteria->add (new Criteria('not_category', $category));
  776.         $criteria->add (new Criteria('not_itemid', intval($item_id)));
  777.         return $this->deleteAll($criteria);
  778.     }
  779.  
  780.  
  781.     /**
  782.      * Perform notification maintenance activites at login time.
  783.      * In particular, any notifications for the newly logged-in
  784.      * user with mode XOOPS_NOTIFICATION_MODE_WAITFORLOGIN are
  785.      * switched to mode XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT.
  786.      *
  787.      * @param  int  $user_id  ID of the user being logged in
  788.      **/
  789.     function doLoginMaintenance ($user_id)
  790.     {
  791.         $criteria = new CriteriaCompo();
  792.         $criteria->add (new Criteria('not_uid', intval($user_id)));
  793.         $criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN));
  794.  
  795.         $notifications = $this->getObjects($criteria, true);
  796.         foreach ($notifications as $n) {
  797.             $n->setVar('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT);
  798.             $this->insert($n);
  799.         }
  800.     }
  801.  
  802.  
  803.     /**
  804.      * Update
  805.      *
  806.      * @param   object  &$notification  {@link XoopsNotification} object
  807.      * @param   string  $field_name     Name of the field
  808.      * @param   mixed   $field_value    Value to write
  809.      *
  810.      * @return  bool
  811.      **/
  812.     function updateByField(&$notification, $field_name, $field_value)
  813.     {
  814.         $notification->unsetNew();
  815.         $notification->setVar($field_name, $field_value);
  816.         return $this->insert($notification);
  817.     }
  818.  
  819.  
  820. }
  821. ?>