| Total Complexity | 93 |
| Total Lines | 663 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like XoopsNotificationHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XoopsNotificationHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 236 | class XoopsNotificationHandler extends XoopsObjectHandler |
||
| 237 | { |
||
| 238 | /** |
||
| 239 | * Create a {@link XoopsNotification} |
||
| 240 | * |
||
| 241 | * @param bool $isNew Flag the object as "new"? |
||
| 242 | * |
||
| 243 | * @return XoopsNotification |
||
| 244 | */ |
||
| 245 | public function create($isNew = true) |
||
| 246 | { |
||
| 247 | $notification = new XoopsNotification(); |
||
| 248 | if ($isNew) { |
||
| 249 | $notification->setNew(); |
||
| 250 | } |
||
| 251 | |||
| 252 | return $notification; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Retrieve a {@link XoopsNotification} |
||
| 257 | * |
||
| 258 | * @param int $id ID |
||
| 259 | * |
||
| 260 | * @return XoopsNotification|false {@link XoopsNotification}, false on fail |
||
| 261 | **/ |
||
| 262 | public function get($id) |
||
| 263 | { |
||
| 264 | $notification = false; |
||
| 265 | $id = (int) $id; |
||
| 266 | if ($id > 0) { |
||
| 267 | $sql = 'SELECT * FROM ' . $this->db->prefix('xoopsnotifications') . ' WHERE not_id=' . $id; |
||
| 268 | $result = $this->db->query($sql); |
||
|
|
|||
| 269 | if (!$this->db->isResultSet($result)) { |
||
| 270 | return $notification; |
||
| 271 | } |
||
| 272 | $numrows = $this->db->getRowsNum($result); |
||
| 273 | if ($numrows == 1) { |
||
| 274 | $notification = new XoopsNotification(); |
||
| 275 | $notification->assignVars($this->db->fetchArray($result)); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | return $notification; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Write a notification(subscription) to database |
||
| 284 | * |
||
| 285 | * @param XoopsObject|XoopsNotification $notification a XoopsNotification object |
||
| 286 | * |
||
| 287 | * @return bool true on success, otherwise false |
||
| 288 | **/ |
||
| 289 | public function insert(XoopsObject $notification) |
||
| 290 | { |
||
| 291 | $className = 'XoopsNotification'; |
||
| 292 | if (!($notification instanceof $className)) { |
||
| 293 | return false; |
||
| 294 | } |
||
| 295 | if (!$notification->isDirty()) { |
||
| 296 | return true; |
||
| 297 | } |
||
| 298 | if (!$notification->cleanVars()) { |
||
| 299 | return false; |
||
| 300 | } |
||
| 301 | foreach ($notification->cleanVars as $k => $v) { |
||
| 302 | ${$k} = $v; |
||
| 303 | } |
||
| 304 | if ($notification->isNew()) { |
||
| 305 | $not_id = $this->db->genId('xoopsnotifications_not_id_seq'); |
||
| 306 | $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); |
||
| 307 | } else { |
||
| 308 | $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); |
||
| 309 | } |
||
| 310 | if (!$result = $this->db->query($sql)) { |
||
| 311 | return false; |
||
| 312 | } |
||
| 313 | if (empty($not_id)) { |
||
| 314 | $not_id = $this->db->getInsertId(); |
||
| 315 | } |
||
| 316 | $notification->assignVar('not_id', $not_id); |
||
| 317 | |||
| 318 | return true; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Delete a {@link XoopsNotification} from the database |
||
| 323 | * |
||
| 324 | * @param XoopsObject|XoopsNotification $notification a XoopsNotification object |
||
| 325 | * |
||
| 326 | * @return bool true on success, otherwise false |
||
| 327 | **/ |
||
| 328 | public function delete(XoopsObject $notification) |
||
| 329 | { |
||
| 330 | $className = 'XoopsNotification'; |
||
| 331 | if (!($notification instanceof $className)) { |
||
| 332 | return false; |
||
| 333 | } |
||
| 334 | |||
| 335 | $sql = sprintf('DELETE FROM %s WHERE not_id = %u', $this->db->prefix('xoopsnotifications'), $notification->getVar('not_id')); |
||
| 336 | if (!$result = $this->db->query($sql)) { |
||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | return true; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get some {@link XoopsNotification}s |
||
| 345 | * |
||
| 346 | * @param CriteriaElement|CriteriaCompo $criteria |
||
| 347 | * @param bool $id_as_key Use IDs as keys into the array? |
||
| 348 | * |
||
| 349 | * @return array Array of {@link XoopsNotification} objects |
||
| 350 | **/ |
||
| 351 | public function getObjects(?CriteriaElement $criteria = null, $id_as_key = false) |
||
| 352 | { |
||
| 353 | $ret = []; |
||
| 354 | $limit = $start = 0; |
||
| 355 | $sql = 'SELECT * FROM ' . $this->db->prefix('xoopsnotifications'); |
||
| 356 | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) { |
||
| 357 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 358 | $sort = ($criteria->getSort() != '') ? $criteria->getSort() : 'not_id'; |
||
| 359 | $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder(); |
||
| 360 | $limit = $criteria->getLimit(); |
||
| 361 | $start = $criteria->getStart(); |
||
| 362 | } |
||
| 363 | $result = $this->db->query($sql, $limit, $start); |
||
| 364 | if (!$this->db->isResultSet($result)) { |
||
| 365 | return $ret; |
||
| 366 | } |
||
| 367 | /** @var array $myrow */ |
||
| 368 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 369 | $notification = new XoopsNotification(); |
||
| 370 | $notification->assignVars($myrow); |
||
| 371 | if (!$id_as_key) { |
||
| 372 | $ret[] = $notification; |
||
| 373 | } else { |
||
| 374 | $ret[$myrow['not_id']] = $notification; |
||
| 375 | } |
||
| 376 | unset($notification); |
||
| 377 | } |
||
| 378 | |||
| 379 | return $ret; |
||
| 380 | } |
||
| 381 | |||
| 382 | // TODO: Need this?? |
||
| 383 | /** |
||
| 384 | * Count Notifications |
||
| 385 | * |
||
| 386 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} |
||
| 387 | * |
||
| 388 | * @return int Count |
||
| 389 | **/ |
||
| 390 | public function getCount(?CriteriaElement $criteria = null) |
||
| 391 | { |
||
| 392 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xoopsnotifications'); |
||
| 393 | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) { |
||
| 394 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 395 | } |
||
| 396 | $result = $this->db->query($sql); |
||
| 397 | if (!$this->db->isResultSet($result)) { |
||
| 398 | return 0; |
||
| 399 | } |
||
| 400 | [$count] = $this->db->fetchRow($result); |
||
| 401 | |||
| 402 | return (int) $count; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Delete multiple notifications |
||
| 407 | * |
||
| 408 | * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | **/ |
||
| 412 | public function deleteAll(?CriteriaElement $criteria = null) |
||
| 413 | { |
||
| 414 | $sql = 'DELETE FROM ' . $this->db->prefix('xoopsnotifications'); |
||
| 415 | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) { |
||
| 416 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 417 | } |
||
| 418 | if (!$result = $this->db->query($sql)) { |
||
| 419 | return false; |
||
| 420 | } |
||
| 421 | |||
| 422 | return true; |
||
| 423 | } |
||
| 424 | |||
| 425 | // TODO: rename this... |
||
| 426 | // Also, should we have get by module, get by category, etc...?? |
||
| 427 | /** |
||
| 428 | * @param $module_id |
||
| 429 | * @param $category |
||
| 430 | * @param $item_id |
||
| 431 | * @param $event |
||
| 432 | * @param $user_id |
||
| 433 | * |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function &getNotification($module_id, $category, $item_id, $event, $user_id) |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Determine if a user is subscribed to a particular event in |
||
| 455 | * a particular module. |
||
| 456 | * |
||
| 457 | * @param string $category Category of notification event |
||
| 458 | * @param int $item_id Item ID of notification event |
||
| 459 | * @param string $event Event |
||
| 460 | * @param int $module_id ID of module (default current module) |
||
| 461 | * @param int $user_id ID of user (default current user) |
||
| 462 | * return int 0 if not subscribe; non-zero if subscribed |
||
| 463 | * |
||
| 464 | * @return int |
||
| 465 | */ |
||
| 466 | public function isSubscribed($category, $item_id, $event, $module_id, $user_id) |
||
| 467 | { |
||
| 468 | $criteria = new CriteriaCompo(); |
||
| 469 | $criteria->add(new Criteria('not_modid', (int) $module_id)); |
||
| 470 | $criteria->add(new Criteria('not_category', $this->db->escape($category))); |
||
| 471 | $criteria->add(new Criteria('not_itemid', (int) $item_id)); |
||
| 472 | $criteria->add(new Criteria('not_event', $this->db->escape($event))); |
||
| 473 | $criteria->add(new Criteria('not_uid', (int) $user_id)); |
||
| 474 | |||
| 475 | return $this->getCount($criteria); |
||
| 476 | } |
||
| 477 | |||
| 478 | // TODO: how about a function to subscribe a whole group of users??? |
||
| 479 | // e.g. if we want to add all moderators to be notified of subscription |
||
| 480 | // of new threads... |
||
| 481 | /** |
||
| 482 | * Subscribe for notification for an event(s) |
||
| 483 | * |
||
| 484 | * @param string $category category of notification |
||
| 485 | * @param int $item_id ID of the item |
||
| 486 | * @param mixed $events event string or array of events |
||
| 487 | * @param int $mode force a particular notification mode |
||
| 488 | * (e.g. once_only) (default to current user preference) |
||
| 489 | * @param int $module_id ID of the module (default to current module) |
||
| 490 | * @param int $user_id ID of the user (default to current user) |
||
| 491 | * * |
||
| 492 | * |
||
| 493 | * @return bool |
||
| 494 | */ |
||
| 495 | public function subscribe($category, $item_id, $events, $mode = null, $module_id = null, $user_id = null) |
||
| 496 | { |
||
| 497 | if (!isset($user_id)) { |
||
| 498 | global $xoopsUser; |
||
| 499 | if (empty($xoopsUser)) { |
||
| 500 | return false; // anonymous cannot subscribe |
||
| 501 | } else { |
||
| 502 | $user_id = $xoopsUser->getVar('uid'); |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | if (!isset($module_id)) { |
||
| 507 | global $xoopsModule; |
||
| 508 | $module_id = $xoopsModule->getVar('mid'); |
||
| 509 | } |
||
| 510 | |||
| 511 | if (!isset($mode)) { |
||
| 512 | $user = new XoopsUser($user_id); |
||
| 513 | $mode = $user->getVar('notify_mode'); |
||
| 514 | } |
||
| 515 | |||
| 516 | if (!is_array($events)) { |
||
| 517 | $events = [$events]; |
||
| 518 | } |
||
| 519 | foreach ($events as $event) { |
||
| 520 | /** @var XoopsNotification $notification */ |
||
| 521 | if ($notification = $this->getNotification($module_id, $category, $item_id, $event, $user_id)) { |
||
| 522 | if ($notification->getVar('not_mode') != $mode) { |
||
| 523 | $this->updateByField($notification, 'not_mode', $mode); |
||
| 524 | } |
||
| 525 | } else { |
||
| 526 | $notification = $this->create(); |
||
| 527 | $notification->setVar('not_modid', $module_id); |
||
| 528 | $notification->setVar('not_category', $category); |
||
| 529 | $notification->setVar('not_itemid', $item_id); |
||
| 530 | $notification->setVar('not_uid', $user_id); |
||
| 531 | $notification->setVar('not_event', $event); |
||
| 532 | $notification->setVar('not_mode', $mode); |
||
| 533 | $this->insert($notification); |
||
| 534 | } |
||
| 535 | } |
||
| 536 | return true; |
||
| 537 | } |
||
| 538 | |||
| 539 | // TODO: this will be to provide a list of everything a particular |
||
| 540 | // user has subscribed to... e.g. for on the 'Profile' page, similar |
||
| 541 | // to how we see the various posts etc. that the user has made. |
||
| 542 | // We may also want to have a function where we can specify module id |
||
| 543 | /** |
||
| 544 | * Get a list of notifications by user ID |
||
| 545 | * |
||
| 546 | * @param int $user_id ID of the user |
||
| 547 | * |
||
| 548 | * @return array Array of {@link XoopsNotification} objects |
||
| 549 | **/ |
||
| 550 | public function getByUser($user_id) |
||
| 551 | { |
||
| 552 | $criteria = new Criteria('not_uid', $user_id); |
||
| 553 | |||
| 554 | return $this->getObjects($criteria, true); |
||
| 555 | } |
||
| 556 | |||
| 557 | // TODO: rename this?? |
||
| 558 | /** |
||
| 559 | * Get a list of notification events for the current item/mod/user |
||
| 560 | * |
||
| 561 | * @param $category |
||
| 562 | * @param $item_id |
||
| 563 | * @param $module_id |
||
| 564 | * @param $user_id |
||
| 565 | * @return array |
||
| 566 | */ |
||
| 567 | public function getSubscribedEvents($category, $item_id, $module_id, $user_id) |
||
| 583 | } |
||
| 584 | |||
| 585 | // TODO: is this a useful function?? (Copied from comment_handler) |
||
| 586 | /** |
||
| 587 | * Retrieve items by their ID |
||
| 588 | * |
||
| 589 | * @param int $module_id Module ID |
||
| 590 | * @param int $item_id Item ID |
||
| 591 | * @param string $order Sort order |
||
| 592 | * @param int $mode not_mode see include/notification_constants.php |
||
| 593 | * |
||
| 594 | * @param null $status |
||
| 595 | * |
||
| 596 | * @return array Array of {@link XoopsNotification} objects |
||
| 597 | */ |
||
| 598 | public function getByItemId($module_id, $item_id, $order = null, $mode = null) |
||
| 599 | { |
||
| 600 | $criteria = new CriteriaCompo(new Criteria('not_modid', (int) $module_id)); |
||
| 601 | $criteria->add(new Criteria('not_itemid', (int) $item_id)); |
||
| 602 | if (isset($mode)) { |
||
| 603 | $criteria->add(new Criteria('not_mode', (int) $mode)); |
||
| 604 | } |
||
| 605 | if (isset($order)) { |
||
| 606 | $criteria->setOrder($order); |
||
| 607 | } |
||
| 608 | |||
| 609 | return $this->getObjects($criteria); |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Send notifications to users |
||
| 614 | * |
||
| 615 | * @param string $category notification category |
||
| 616 | * @param int $item_id ID of the item |
||
| 617 | * @param array $events trigger events |
||
| 618 | * @param array $extra_tags array of substitutions for template to be |
||
| 619 | * merged with the one from function. |
||
| 620 | * @param array $user_list only notify the selected users |
||
| 621 | * @param int $module_id ID of the module |
||
| 622 | * @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. |
||
| 623 | * @internal param string $event notification event |
||
| 624 | */ |
||
| 625 | // TODO:(?) - pass in an event LIST. This will help to avoid |
||
| 626 | // problem of sending people multiple emails for similar events. |
||
| 627 | // BUT, then we need an array of mail templates, etc... Unless |
||
| 628 | // mail templates can include logic in the future, then we can |
||
| 629 | // tailor the mail so it makes sense for any of the possible |
||
| 630 | // (or combination of) events. |
||
| 631 | public function triggerEvents($category, $item_id, $events, $extra_tags = [], $user_list = [], $module_id = null, $omit_user_id = null) |
||
| 632 | { |
||
| 633 | if (!is_array($events)) { |
||
| 634 | $events = [$events]; |
||
| 635 | } |
||
| 636 | foreach ($events as $event) { |
||
| 637 | $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id); |
||
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Trigger a single notification event |
||
| 643 | * |
||
| 644 | * @param string $category |
||
| 645 | * @param int $item_id |
||
| 646 | * @param string $event |
||
| 647 | * @param array $extra_tags |
||
| 648 | * @param array $user_list |
||
| 649 | * @param int $module_id |
||
| 650 | * @param int $omit_user_id |
||
| 651 | * @return mixed |
||
| 652 | */ |
||
| 653 | public function triggerEvent($category, $item_id, $event, $extra_tags = [], $user_list = [], $module_id = null, $omit_user_id = null) |
||
| 654 | { |
||
| 655 | if (!isset($module_id)) { |
||
| 656 | global $xoopsModule; |
||
| 657 | $module = $xoopsModule; |
||
| 658 | $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0; |
||
| 659 | } else { |
||
| 660 | /** @var XoopsModuleHandler $module_handler */ |
||
| 661 | $module_handler = xoops_getHandler('module'); |
||
| 662 | $module = $module_handler->get($module_id); |
||
| 663 | } |
||
| 664 | |||
| 665 | // Check if event is enabled |
||
| 666 | /** @var XoopsConfigHandler $config_handler */ |
||
| 667 | $config_handler = xoops_getHandler('config'); |
||
| 668 | $mod_config = $config_handler->getConfigsByCat(0, $module->getVar('mid')); |
||
| 669 | if (empty($mod_config['notification_enabled'])) { |
||
| 670 | return false; |
||
| 671 | } |
||
| 672 | $category_info = & notificationCategoryInfo($category, $module_id); |
||
| 673 | $event_info = & notificationEventInfo($category, $event, $module_id); |
||
| 674 | if (!in_array(notificationGenerateConfig($category_info, $event_info, 'option_name'), $mod_config['notification_events']) && empty($event_info['invisible'])) { |
||
| 675 | return false; |
||
| 676 | } |
||
| 677 | |||
| 678 | if (!isset($omit_user_id)) { |
||
| 679 | global $xoopsUser; |
||
| 680 | $omit_user_id = 0; |
||
| 681 | if (!empty($xoopsUser)) { |
||
| 682 | $omit_user_id = $xoopsUser->getVar('uid'); |
||
| 683 | } |
||
| 684 | } |
||
| 685 | $criteria = new CriteriaCompo(); |
||
| 686 | $criteria->add(new Criteria('not_modid', (int) $module_id)); |
||
| 687 | $criteria->add(new Criteria('not_category', $this->db->escape($category))); |
||
| 688 | $criteria->add(new Criteria('not_itemid', (int) $item_id)); |
||
| 689 | $criteria->add(new Criteria('not_event', $this->db->escape($event))); |
||
| 690 | $mode_criteria = new CriteriaCompo(); |
||
| 691 | $mode_criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDALWAYS), 'OR'); |
||
| 692 | $mode_criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE), 'OR'); |
||
| 693 | $mode_criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT), 'OR'); |
||
| 694 | $criteria->add($mode_criteria); |
||
| 695 | if (!empty($user_list)) { |
||
| 696 | $user_criteria = new CriteriaCompo(); |
||
| 697 | foreach ($user_list as $user) { |
||
| 698 | $user_criteria->add(new Criteria('not_uid', (int) $user), 'OR'); |
||
| 699 | } |
||
| 700 | $criteria->add($user_criteria); |
||
| 701 | } |
||
| 702 | $notifications = $this->getObjects($criteria); |
||
| 703 | if (empty($notifications)) { |
||
| 704 | return null; |
||
| 705 | } |
||
| 706 | |||
| 707 | // Add some tag substitutions here |
||
| 708 | |||
| 709 | $not_config = $module->getInfo('notification'); |
||
| 710 | $tags = []; |
||
| 711 | if (!empty($not_config)) { |
||
| 712 | if (!empty($not_config['tags_file'])) { |
||
| 713 | $tags_file = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname') . '/' . $not_config['tags_file']); |
||
| 714 | if (file_exists($tags_file)) { |
||
| 715 | include_once $tags_file; |
||
| 716 | if (!empty($not_config['tags_func'])) { |
||
| 717 | $tags_func = $not_config['tags_func']; |
||
| 718 | if (function_exists($tags_func)) { |
||
| 719 | $tags = $tags_func($category, (int) $item_id, $event); |
||
| 720 | } |
||
| 721 | } |
||
| 722 | } |
||
| 723 | } |
||
| 724 | // RMV-NEW |
||
| 725 | if (!empty($not_config['lookup_file'])) { |
||
| 726 | $lookup_file = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname') . '/' . $not_config['lookup_file']); |
||
| 727 | if (file_exists($lookup_file)) { |
||
| 728 | include_once $lookup_file; |
||
| 729 | if (!empty($not_config['lookup_func'])) { |
||
| 730 | $lookup_func = $not_config['lookup_func']; |
||
| 731 | if (function_exists($lookup_func)) { |
||
| 732 | $item_info = $lookup_func($category, (int) $item_id); |
||
| 733 | } |
||
| 734 | } |
||
| 735 | } |
||
| 736 | } |
||
| 737 | } |
||
| 738 | |||
| 739 | $tags['X_ITEM_NAME'] = !empty($item_info['name']) ? $item_info['name'] : '[' . _NOT_ITEMNAMENOTAVAILABLE . ']'; |
||
| 740 | $tags['X_ITEM_URL'] = !empty($item_info['url']) ? $item_info['url'] : '[' . _NOT_ITEMURLNOTAVAILABLE . ']'; |
||
| 741 | $tags['X_ITEM_TYPE'] = !empty($category_info['item_name']) ? $category_info['title'] : '[' . _NOT_ITEMTYPENOTAVAILABLE . ']'; |
||
| 742 | $tags['X_MODULE'] = $module->getVar('name'); |
||
| 743 | $tags['X_MODULE_URL'] = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/'; |
||
| 744 | $tags['X_NOTIFY_CATEGORY'] = $category; |
||
| 745 | $tags['X_NOTIFY_EVENT'] = $event; |
||
| 746 | $tags['X_UNSUBSCRIBE_URL'] = XOOPS_URL . '/notifications.php'; |
||
| 747 | |||
| 748 | $template_dir = $event_info['mail_template_dir']; |
||
| 749 | $template = $event_info['mail_template'] . '.tpl'; |
||
| 750 | $subject = $event_info['mail_subject']; |
||
| 751 | |||
| 752 | // Pre-merge any extra tags if they are common to all notifications |
||
| 753 | if (!empty($extra_tags)) { |
||
| 754 | $tags = array_merge($tags, $extra_tags); |
||
| 755 | } |
||
| 756 | |||
| 757 | foreach ($notifications as $notification) { |
||
| 758 | if (empty($omit_user_id) || $notification->getVar('not_uid') != $omit_user_id) { |
||
| 759 | |||
| 760 | // Add user-specific tags |
||
| 761 | //$tags['X_UNSUBSCRIBE_URL'] = 'TODO'; |
||
| 762 | // TODO: don't show unsubscribe link if it is 'one-time' ?? |
||
| 763 | |||
| 764 | // Notify the user with the merged tags |
||
| 765 | $notification->notifyUser($template_dir, $template, $subject, $tags); |
||
| 766 | } |
||
| 767 | } |
||
| 768 | |||
| 769 | return null; |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Delete all notifications for one user |
||
| 774 | * |
||
| 775 | * @param int $user_id ID of the user |
||
| 776 | * @return bool |
||
| 777 | **/ |
||
| 778 | public function unsubscribeByUser($user_id) |
||
| 783 | } |
||
| 784 | |||
| 785 | // TODO: allow these to use current module, etc... |
||
| 786 | /** |
||
| 787 | * Unsubscribe notifications for an event(s). |
||
| 788 | * |
||
| 789 | * @param string $category category of the events |
||
| 790 | * @param int $item_id ID of the item |
||
| 791 | * @param mixed $events event string or array of events |
||
| 792 | * @param int $module_id ID of the module (default current module) |
||
| 793 | * @param int $user_id UID of the user (default current user) |
||
| 794 | * |
||
| 795 | * @return bool |
||
| 796 | **/ |
||
| 797 | public function unsubscribe($category, $item_id, $events, $module_id = null, $user_id = null) |
||
| 798 | { |
||
| 799 | if (!isset($user_id)) { |
||
| 800 | global $xoopsUser; |
||
| 801 | if (empty($xoopsUser)) { |
||
| 802 | return false; // anonymous cannot subscribe |
||
| 803 | } else { |
||
| 804 | $user_id = $xoopsUser->getVar('uid'); |
||
| 805 | } |
||
| 806 | } |
||
| 807 | if (!isset($module_id)) { |
||
| 808 | global $xoopsModule; |
||
| 809 | $module_id = $xoopsModule->getVar('mid'); |
||
| 810 | } |
||
| 811 | $criteria = new CriteriaCompo(); |
||
| 812 | $criteria->add(new Criteria('not_modid', (int) $module_id)); |
||
| 813 | $criteria->add(new Criteria('not_category', $this->db->escape($category))); |
||
| 814 | $criteria->add(new Criteria('not_itemid', (int) $item_id)); |
||
| 815 | $criteria->add(new Criteria('not_uid', (int) $user_id)); |
||
| 816 | if (!is_array($events)) { |
||
| 817 | $events = [$events]; |
||
| 818 | } |
||
| 819 | $event_criteria = new CriteriaCompo(); |
||
| 820 | foreach ($events as $event) { |
||
| 821 | $event_criteria->add(new Criteria('not_event', $this->db->escape($event)), 'OR'); |
||
| 822 | } |
||
| 823 | $criteria->add($event_criteria); |
||
| 824 | |||
| 825 | return $this->deleteAll($criteria); |
||
| 826 | } |
||
| 827 | |||
| 828 | // TODO: When 'update' a module, may need to switch around some |
||
| 829 | // notification classes/IDs... or delete the ones that no longer |
||
| 830 | // exist. |
||
| 831 | /** |
||
| 832 | * Delete all notifications for a particular module |
||
| 833 | * |
||
| 834 | * @param int $module_id ID of the module |
||
| 835 | * @return bool |
||
| 836 | **/ |
||
| 837 | public function unsubscribeByModule($module_id) |
||
| 838 | { |
||
| 839 | $criteria = new Criteria('not_modid', (int) $module_id); |
||
| 840 | |||
| 841 | return $this->deleteAll($criteria); |
||
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Delete all subscriptions for a particular item. |
||
| 846 | * |
||
| 847 | * @param int $module_id ID of the module to which item belongs |
||
| 848 | * @param string $category Notification category of the item |
||
| 849 | * @param int $item_id ID of the item |
||
| 850 | * |
||
| 851 | * @return bool |
||
| 852 | **/ |
||
| 853 | public function unsubscribeByItem($module_id, $category, $item_id) |
||
| 854 | { |
||
| 855 | $criteria = new CriteriaCompo(); |
||
| 856 | $criteria->add(new Criteria('not_modid', (int) $module_id)); |
||
| 857 | $criteria->add(new Criteria('not_category', $this->db->escape($category))); |
||
| 858 | $criteria->add(new Criteria('not_itemid', (int) $item_id)); |
||
| 859 | |||
| 860 | return $this->deleteAll($criteria); |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Perform notification maintenance activites at login time. |
||
| 865 | * In particular, any notifications for the newly logged-in |
||
| 866 | * user with mode XOOPS_NOTIFICATION_MODE_WAITFORLOGIN are |
||
| 867 | * switched to mode XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT. |
||
| 868 | * |
||
| 869 | * @param int $user_id ID of the user being logged in |
||
| 870 | **/ |
||
| 871 | public function doLoginMaintenance($user_id) |
||
| 872 | { |
||
| 873 | $criteria = new CriteriaCompo(); |
||
| 874 | $criteria->add(new Criteria('not_uid', (int) $user_id)); |
||
| 875 | $criteria->add(new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN)); |
||
| 876 | |||
| 877 | $notifications = $this->getObjects($criteria, true); |
||
| 878 | foreach ($notifications as $n) { |
||
| 879 | $n->setVar('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT); |
||
| 880 | $this->insert($n); |
||
| 881 | } |
||
| 882 | } |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Update |
||
| 886 | * |
||
| 887 | * @param XoopsNotification $notification {@link XoopsNotification} object |
||
| 888 | * @param string $field_name Name of the field |
||
| 889 | * @param mixed $field_value Value to write |
||
| 890 | * |
||
| 891 | * @return bool |
||
| 892 | **/ |
||
| 893 | public function updateByField(XoopsNotification $notification, $field_name, $field_value) |
||
| 899 | } |
||
| 900 | } |
||
| 901 |