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