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