[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/notification/ -> manager.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\notification;
  15  
  16  use Symfony\Component\DependencyInjection\ContainerInterface;
  17  
  18  /**
  19  * Notifications service class
  20  */
  21  class manager
  22  {
  23      /** @var array */
  24      protected $notification_types;
  25  
  26      /** @var array */
  27      protected $subscription_types;
  28  
  29      /** @var method\method_interface[] */
  30      protected $notification_methods;
  31  
  32      /** @var ContainerInterface */
  33      protected $phpbb_container;
  34  
  35      /** @var \phpbb\user_loader */
  36      protected $user_loader;
  37  
  38      /** @var \phpbb\event\dispatcher_interface */
  39      protected $phpbb_dispatcher;
  40  
  41      /** @var \phpbb\db\driver\driver_interface */
  42      protected $db;
  43  
  44      /** @var \phpbb\cache\service */
  45      protected $cache;
  46  
  47      /** @var \phpbb\language\language */
  48      protected $language;
  49  
  50      /** @var \phpbb\user */
  51      protected $user;
  52  
  53      /** @var string */
  54      protected $notification_types_table;
  55  
  56      /** @var string */
  57      protected $user_notifications_table;
  58  
  59      /**
  60      * Notification Constructor
  61      *
  62      * @param array $notification_types
  63      * @param array $notification_methods
  64      * @param ContainerInterface $phpbb_container
  65      * @param \phpbb\user_loader $user_loader
  66      * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher
  67      * @param \phpbb\db\driver\driver_interface $db
  68      * @param \phpbb\cache\service $cache
  69      * @param \phpbb\language\language $language
  70      * @param \phpbb\user $user
  71      * @param string $notification_types_table
  72      * @param string $user_notifications_table
  73      *
  74      * @return \phpbb\notification\manager
  75      */
  76  	public function __construct($notification_types, $notification_methods, ContainerInterface $phpbb_container, \phpbb\user_loader $user_loader, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, \phpbb\language\language $language, \phpbb\user $user, $notification_types_table, $user_notifications_table)
  77      {
  78          $this->notification_types = $notification_types;
  79          $this->notification_methods = $notification_methods;
  80          $this->phpbb_container = $phpbb_container;
  81  
  82          $this->user_loader = $user_loader;
  83          $this->phpbb_dispatcher = $phpbb_dispatcher;
  84          $this->db = $db;
  85          $this->cache = $cache;
  86          $this->language = $language;
  87          $this->user = $user;
  88  
  89          $this->notification_types_table = $notification_types_table;
  90          $this->user_notifications_table = $user_notifications_table;
  91      }
  92  
  93      /**
  94      * Load the user's notifications for a given method
  95      *
  96      * @param string $method_name
  97      * @param array $options Optional options to control what notifications are loaded
  98      *                notification_id        Notification id to load (or array of notification ids)
  99      *                user_id                User id to load notifications for (Default: $user->data['user_id'])
 100      *                order_by            Order by (Default: notification_time)
 101      *                order_dir            Order direction (Default: DESC)
 102      *                 limit                Number of notifications to load (Default: 5)
 103      *                 start                Notifications offset (Default: 0)
 104      *                 all_unread            Load all unread notifications? If set to true, count_unread is set to true (Default: false)
 105      *                 count_unread        Count all unread notifications? (Default: false)
 106      *                 count_total            Count all notifications? (Default: false)
 107      * @return array Array of information based on the request with keys:
 108      *    'notifications'        array of notification type objects
 109      *    'unread_count'        number of unread notifications the user has if count_unread is true in the options
 110      *    'total_count'        number of notifications the user has if count_total is true in the options
 111      * @throws \phpbb\notification\exception when the method doesn't refer to a class extending \phpbb\notification\method\method_interface
 112      */
 113  	public function load_notifications($method_name, array $options = array())
 114      {
 115          $method = $this->get_method_class($method_name);
 116  
 117          if (! $method instanceof \phpbb\notification\method\method_interface)
 118          {
 119              throw new \phpbb\notification\exception($this->language->lang('NOTIFICATION_METHOD_INVALID', $method_name));
 120          }
 121          else if ($method->is_available())
 122          {
 123              return $method->load_notifications($options);
 124          }
 125          else
 126          {
 127              return array(
 128                  'notifications'        => array(),
 129                  'unread_count'        => 0,
 130                  'total_count'        => 0,
 131              );
 132          }
 133      }
 134  
 135      /**
 136       * Mark notifications read or unread for all available methods
 137       *
 138       * @param bool|string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types). False to mark read for all item types
 139       * @param bool|int|array $item_id Item id or array of item ids. False to mark read for all item ids
 140       * @param bool|int|array $user_id User id or array of user ids. False to mark read for all user ids
 141       * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False)
 142       *
 143       * @deprecated since 3.2
 144       */
 145  	public function mark_notifications_read($notification_type_name, $item_id, $user_id, $time = false)
 146      {
 147          $this->mark_notifications($notification_type_name, $item_id, $user_id, $time);
 148      }
 149  
 150      /**
 151      * Mark notifications read or unread for all available methods
 152      *
 153      * @param bool|string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types). False to mark read for all item types
 154      * @param bool|int|array $item_id Item id or array of item ids. False to mark read for all item ids
 155      * @param bool|int|array $user_id User id or array of user ids. False to mark read for all user ids
 156      * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False)
 157      * @param bool $mark_read Define if the notification as to be set to True or False. (Default: True)
 158      */
 159  	public function mark_notifications($notification_type_name, $item_id, $user_id, $time = false, $mark_read = true)
 160      {
 161          if (is_array($notification_type_name))
 162          {
 163              $notification_type_id = $this->get_notification_type_ids($notification_type_name);
 164          }
 165          else if ($notification_type_name !== false)
 166          {
 167              $notification_type_id = $this->get_notification_type_id($notification_type_name);
 168          }
 169          else
 170          {
 171              $notification_type_id = false;
 172          }
 173  
 174          /** @var method\method_interface $method */
 175          foreach ($this->get_available_subscription_methods() as $method)
 176          {
 177              $method->mark_notifications($notification_type_id, $item_id, $user_id, $time, $mark_read);
 178          }
 179      }
 180  
 181      /**
 182       * Mark notifications read or unread from a parent identifier for all available methods
 183       *
 184       * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types)
 185       * @param bool|int|array $item_parent_id Item parent id or array of item parent ids. False to mark read for all item parent ids
 186       * @param bool|int|array $user_id User id or array of user ids. False to mark read for all user ids
 187       * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False)
 188       *
 189       * @deprecated since 3.2
 190       */
 191  	public function mark_notifications_read_by_parent($notification_type_name, $item_parent_id, $user_id, $time = false)
 192      {
 193          $this->mark_notifications_by_parent($notification_type_name, $item_parent_id, $user_id, $time);
 194      }
 195  
 196      /**
 197      * Mark notifications read or unread from a parent identifier for all available methods
 198      *
 199      * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types)
 200      * @param bool|int|array $item_parent_id Item parent id or array of item parent ids. False to mark read for all item parent ids
 201      * @param bool|int|array $user_id User id or array of user ids. False to mark read for all user ids
 202      * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False)
 203      * @param bool $mark_read Define if the notification as to be set to True or False. (Default: True)
 204      */
 205  	public function mark_notifications_by_parent($notification_type_name, $item_parent_id, $user_id, $time = false, $mark_read = true)
 206      {
 207          if (is_array($notification_type_name))
 208          {
 209              $notification_type_id = $this->get_notification_type_ids($notification_type_name);
 210          }
 211          else
 212          {
 213              $notification_type_id = $this->get_notification_type_id($notification_type_name);
 214          }
 215  
 216          /** @var method\method_interface $method */
 217          foreach ($this->get_available_subscription_methods() as $method)
 218          {
 219              $method->mark_notifications_by_parent($notification_type_id, $item_parent_id, $user_id, $time, $mark_read);
 220          }
 221      }
 222  
 223      /**
 224      * Mark notifications read or unread for a given method
 225      *
 226      * @param string $method_name
 227      * @param int|array $notification_id Notification id or array of notification ids.
 228      * @param bool|int $time Time at which to mark all notifications prior to as read. False to mark all as read. (Default: False)
 229      * @param bool $mark_read Define if the notification as to be set to True or False. (Default: True)
 230      */
 231  	public function mark_notifications_by_id($method_name, $notification_id, $time = false, $mark_read = true)
 232      {
 233          $method = $this->get_method_class($method_name);
 234  
 235          if ($method instanceof \phpbb\notification\method\method_interface && $method->is_available())
 236          {
 237              $method->mark_notifications_by_id($notification_id, $time, $mark_read);
 238          }
 239      }
 240  
 241      /**
 242      * Add a notification
 243      *
 244      * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types)
 245      *            Note: If you send an array of types, any user who could receive multiple notifications from this single item will only receive
 246      *             a single notification. If they MUST receive multiple notifications, call this function multiple times instead of sending an array
 247      * @param array $data Data specific for this type that will be inserted
 248      * @param array $options Optional options to control what notifications are loaded
 249      *             ignore_users    array of data to specify which users should not receive certain types of notifications
 250      * @return array Information about what users were notified and how they were notified
 251      */
 252  	public function add_notifications($notification_type_name, $data, array $options = array())
 253      {
 254          $options = array_merge(array(
 255              'ignore_users'        => array(),
 256          ), $options);
 257  
 258          if (is_array($notification_type_name))
 259          {
 260              $notified_users = array();
 261              $temp_options = $options;
 262  
 263              foreach ($notification_type_name as $type)
 264              {
 265                  $temp_options['ignore_users'] = $options['ignore_users'] + $notified_users;
 266                  $notified_users += $this->add_notifications($type, $data, $temp_options);
 267              }
 268  
 269              return $notified_users;
 270          }
 271  
 272          // find out which users want to receive this type of notification
 273          $notify_users = $this->get_item_type_class($notification_type_name)->find_users_for_notification($data, $options);
 274  
 275          /**
 276          * Allow filtering the notify_users array for a notification that is about to be sent.
 277          * Here, $notify_users is already filtered by f_read and the ignored list included in the options variable
 278          *
 279          * @event core.notification_manager_add_notifications
 280          * @var    string    notification_type_name        The forum id from where the topic belongs
 281          * @var    array     data                        Data specific for the notification_type_name used will be inserted
 282          * @var    array     notify_users                The array of userid that are going to be notified for this notification. Set to array() to cancel.
 283          * @var    array     options                        The options that were used when this method was called (read only)
 284          *
 285          * @since 3.1.3-RC1
 286          */
 287          $vars = array(
 288              'notification_type_name',
 289              'data',
 290              'notify_users',
 291              'options',
 292          );
 293          extract($this->phpbb_dispatcher->trigger_event('core.notification_manager_add_notifications', compact($vars)));
 294  
 295          $this->add_notifications_for_users($notification_type_name, $data, $notify_users);
 296  
 297          return $notify_users;
 298      }
 299  
 300      /**
 301      * Add a notification for specific users
 302      *
 303      * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types)
 304      * @param array $data Data specific for this type that will be inserted
 305      * @param array $notify_users User list to notify
 306      */
 307  	public function add_notifications_for_users($notification_type_name, $data, $notify_users)
 308      {
 309          if (is_array($notification_type_name))
 310          {
 311              foreach ($notification_type_name as $type)
 312              {
 313                  $this->add_notifications_for_users($type, $data, $notify_users);
 314              }
 315  
 316              return;
 317          }
 318  
 319          $notification_type_id = $this->get_notification_type_id($notification_type_name);
 320  
 321          $item_id = $this->get_item_type_class($notification_type_name)->get_item_id($data);
 322  
 323          $user_ids = array();
 324          $notification_methods = array();
 325  
 326          // Never send notifications to the anonymous user!
 327          unset($notify_users[ANONYMOUS]);
 328  
 329          // Make sure not to send new notifications to users who've already been notified about this item
 330          // This may happen when an item was added, but now new users are able to see the item
 331          // We remove each user which was already notified by at least one method.
 332          /** @var method\method_interface $method */
 333          foreach ($this->get_subscription_methods_instances() as $method)
 334          {
 335              $notified_users = $method->get_notified_users($notification_type_id, array('item_id' => $item_id));
 336              foreach ($notified_users as $user => $notifications)
 337              {
 338                  unset($notify_users[$user]);
 339              }
 340          }
 341  
 342          if (!count($notify_users))
 343          {
 344              return;
 345          }
 346  
 347          // Allow notifications to perform actions before creating the insert array (such as run a query to cache some data needed for all notifications)
 348          $notification = $this->get_item_type_class($notification_type_name);
 349          $pre_create_data = $notification->pre_create_insert_array($data, $notify_users);
 350          unset($notification);
 351  
 352          // Go through each user so we can insert a row in the DB and then notify them by their desired means
 353          foreach ($notify_users as $user => $methods)
 354          {
 355              $notification = $this->get_item_type_class($notification_type_name);
 356  
 357              $notification->user_id = (int) $user;
 358  
 359              // Generate the insert_array
 360              $notification->create_insert_array($data, $pre_create_data);
 361  
 362              // Users are needed to send notifications
 363              $user_ids = array_merge($user_ids, $notification->users_to_query());
 364  
 365              foreach ($methods as $method)
 366              {
 367                  // setup the notification methods and add the notification to the queue
 368                  if (!isset($notification_methods[$method]))
 369                  {
 370                      $notification_methods[$method] = $this->get_method_class($method);
 371                  }
 372  
 373                  $notification_methods[$method]->add_to_queue($notification);
 374              }
 375          }
 376  
 377          // We need to load all of the users to send notifications
 378          $this->user_loader->load_users($user_ids);
 379  
 380          // run the queue for each method to send notifications
 381          foreach ($notification_methods as $method)
 382          {
 383              $method->notify();
 384          }
 385      }
 386  
 387      /**
 388      * Update notification
 389      *
 390      * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types)
 391      * @param array $data Data specific for this type that will be updated
 392      * @param array $options
 393      */
 394  	public function update_notifications($notification_type_name, array $data, array $options = array())
 395      {
 396          if (is_array($notification_type_name))
 397          {
 398              foreach ($notification_type_name as $type)
 399              {
 400                  $this->update_notifications($type, $data);
 401              }
 402  
 403              return;
 404          }
 405  
 406          $this->update_notification($this->get_item_type_class($notification_type_name), $data, $options);
 407      }
 408  
 409      /**
 410      * Update a notification
 411      *
 412      * @param \phpbb\notification\type\type_interface $notification The notification
 413      * @param array $data Data specific for this type that will be updated
 414      * @param array $options
 415      */
 416  	public function update_notification(\phpbb\notification\type\type_interface $notification, array $data, array $options = array())
 417      {
 418          if (empty($options))
 419          {
 420              $options['item_id'] = $notification->get_item_id($data);
 421          }
 422  
 423          /** @var method\method_interface $method */
 424          foreach ($this->get_available_subscription_methods() as $method)
 425          {
 426              $method->update_notification($notification, $data, $options);
 427          }
 428      }
 429  
 430      /**
 431      * Delete a notification
 432      *
 433      * @param string|array $notification_type_name Type identifier or array of item types (only acceptable if the $item_id is identical for the specified types)
 434      * @param int|array $item_id Identifier within the type (or array of ids)
 435      * @param mixed $parent_id Parent identifier within the type (or array of ids), used in combination with item_id if specified (Default: false; not checked)
 436      * @param mixed $user_id User id (Default: false; not checked)
 437      */
 438  	public function delete_notifications($notification_type_name, $item_id, $parent_id = false, $user_id = false)
 439      {
 440          if (is_array($notification_type_name))
 441          {
 442              foreach ($notification_type_name as $type)
 443              {
 444                  $this->delete_notifications($type, $item_id, $parent_id, $user_id);
 445              }
 446  
 447              return;
 448          }
 449  
 450          $notification_type_id = $this->get_notification_type_id($notification_type_name);
 451  
 452          /** @var method\method_interface $method */
 453          foreach ($this->get_available_subscription_methods() as $method)
 454          {
 455              $method->delete_notifications($notification_type_id, $item_id, $parent_id, $user_id);
 456          }
 457      }
 458  
 459      /**
 460      * Get all of the subscription types
 461      *
 462      * @return array Array of item types
 463      */
 464  	public function get_subscription_types()
 465      {
 466          if ($this->subscription_types === null)
 467          {
 468              $this->subscription_types = array();
 469  
 470              foreach ($this->notification_types as $type_name => $data)
 471              {
 472                  /** @var type\base $type */
 473                  $type = $this->get_item_type_class($type_name);
 474  
 475                  if ($type instanceof \phpbb\notification\type\type_interface && $type->is_available())
 476                  {
 477                      $options = array_merge(array(
 478                          'type'    => $type,
 479                          'id'    => $type->get_type(),
 480                          'lang'    => 'NOTIFICATION_TYPE_' . strtoupper($type->get_type()),
 481                          'group'    => 'NOTIFICATION_GROUP_MISCELLANEOUS',
 482                      ), (($type::$notification_option !== false) ? $type::$notification_option : array()));
 483  
 484                      $this->subscription_types[$options['group']][$options['id']] = $options;
 485                  }
 486              }
 487  
 488              // Move Miscellaneous to the very last section
 489              if (isset($this->subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']))
 490              {
 491                  $miscellaneous = $this->subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS'];
 492                  unset($this->subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS']);
 493                  $this->subscription_types['NOTIFICATION_GROUP_MISCELLANEOUS'] = $miscellaneous;
 494              }
 495          }
 496  
 497          return $this->subscription_types;
 498      }
 499  
 500      /**
 501      * Get all of the subscription methods
 502      *
 503      * @return array Array of methods
 504      */
 505  	public function get_subscription_methods()
 506      {
 507          $subscription_methods = array();
 508  
 509          /** @var method\method_interface $method */
 510          foreach ($this->get_available_subscription_methods() as $method_name => $method)
 511          {
 512              $subscription_methods[$method_name] = array(
 513                  'method'    => $method,
 514                  'id'        => $method->get_type(),
 515                  'lang'        => str_replace('.', '_', strtoupper($method->get_type())),
 516              );
 517          }
 518  
 519          return $subscription_methods;
 520      }
 521  
 522      /**
 523      * Get all of the subscription methods
 524      *
 525      * @return array Array of method's instances
 526      */
 527  	private function get_subscription_methods_instances()
 528      {
 529          $subscription_methods = array();
 530  
 531          foreach ($this->notification_methods as $method_name => $data)
 532          {
 533              $method = $this->get_method_class($method_name);
 534  
 535              if ($method instanceof \phpbb\notification\method\method_interface)
 536              {
 537                  $subscription_methods[$method_name] = $method;
 538              }
 539          }
 540  
 541          return $subscription_methods;
 542      }
 543  
 544      /**
 545      * Get all of the available subscription methods
 546      *
 547      * @return array Array of method's instances
 548      */
 549  	private function get_available_subscription_methods()
 550      {
 551          $subscription_methods = array();
 552  
 553          /** @var method\method_interface $method */
 554          foreach ($this->get_subscription_methods_instances() as $method_name => $method)
 555          {
 556              if ($method->is_available())
 557              {
 558                  $subscription_methods[$method_name] = $method;
 559              }
 560          }
 561  
 562          return $subscription_methods;
 563      }
 564  
 565  
 566      /**
 567      * Get user's notification data
 568      *
 569      * @param int $user_id The user_id of the user to get the notifications for
 570      *
 571      * @return array User's notification
 572      */
 573  	protected function get_user_notifications($user_id)
 574      {
 575          $sql = 'SELECT method, notify, item_type
 576                  FROM ' . $this->user_notifications_table . '
 577                  WHERE user_id = ' . (int) $user_id . '
 578                      AND item_id = 0';
 579  
 580          $result = $this->db->sql_query($sql);
 581          $user_notifications = array();
 582  
 583          while ($row = $this->db->sql_fetchrow($result))
 584          {
 585              $user_notifications[$row['item_type']][] = $row;
 586          }
 587  
 588          $this->db->sql_freeresult($result);
 589  
 590          return $user_notifications;
 591      }
 592  
 593      /**
 594      * Get global subscriptions (item_id = 0)
 595      *
 596      * @param bool|int $user_id The user_id to add the subscription for (bool false for current user)
 597      *
 598      * @return array Subscriptions
 599      */
 600  	public function get_global_subscriptions($user_id = false)
 601      {
 602          $user_id = $user_id ?: $this->user->data['user_id'];
 603  
 604          $subscriptions = array();
 605          $default_methods = $this->get_default_methods();
 606  
 607          $user_notifications = $this->get_user_notifications($user_id);
 608  
 609          foreach ($this->get_subscription_types() as $types)
 610          {
 611              foreach ($types as $id => $type)
 612              {
 613                  $type_subscriptions = $default_methods;
 614                  if (!empty($user_notifications[$id]))
 615                  {
 616                      foreach ($user_notifications[$id] as $user_notification)
 617                      {
 618                          $key = array_search($user_notification['method'], $type_subscriptions, true);
 619                          if (!$user_notification['notify'])
 620                          {
 621                              if ($key !== false)
 622                              {
 623                                  unset($type_subscriptions[$key]);
 624                              }
 625  
 626                              continue;
 627                          }
 628                          else if ($key === false)
 629                          {
 630                              $type_subscriptions[] = $user_notification['method'];
 631                          }
 632                      }
 633                  }
 634  
 635                  if (!empty($type_subscriptions))
 636                  {
 637                      $subscriptions[$id] = $type_subscriptions;
 638                  }
 639              }
 640          }
 641  
 642          return $subscriptions;
 643      }
 644  
 645      /**
 646      * Add a subscription
 647      *
 648      * @param string $item_type Type identifier of the subscription
 649      * @param int $item_id The id of the item
 650      * @param string $method The method of the notification e.g. 'board', 'email', or 'jabber'
 651      *                       (if null a subscription will be added for all the defaults methods)
 652      * @param bool|int $user_id The user_id to add the subscription for (bool false for current user)
 653      */
 654  	public function add_subscription($item_type, $item_id = 0, $method = null, $user_id = false)
 655      {
 656          if ($method === null)
 657          {
 658              foreach ($this->get_default_methods() as $method_name)
 659              {
 660                  $this->add_subscription($item_type, $item_id, $method_name, $user_id);
 661              }
 662  
 663              return;
 664          }
 665  
 666          $user_id = ($user_id === false) ? $this->user->data['user_id'] : $user_id;
 667  
 668          $sql = 'SELECT notify
 669              FROM ' . $this->user_notifications_table . "
 670              WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
 671                  AND item_id = " . (int) $item_id . '
 672                  AND user_id = ' .(int) $user_id . "
 673                  AND method = '" . $this->db->sql_escape($method) . "'";
 674          $this->db->sql_query($sql);
 675          $current = $this->db->sql_fetchfield('notify');
 676          $this->db->sql_freeresult();
 677  
 678          if ($current === false)
 679          {
 680              $sql = 'INSERT INTO ' . $this->user_notifications_table . ' ' .
 681                  $this->db->sql_build_array('INSERT', array(
 682                      'item_type'        => $item_type,
 683                      'item_id'        => (int) $item_id,
 684                      'user_id'        => (int) $user_id,
 685                      'method'        => $method,
 686                      'notify'        => 1,
 687                  ));
 688              $this->db->sql_query($sql);
 689          }
 690          else if (!$current)
 691          {
 692              $sql = 'UPDATE ' . $this->user_notifications_table . "
 693                  SET notify = 1
 694                  WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
 695                      AND item_id = " . (int) $item_id . '
 696                      AND user_id = ' .(int) $user_id . "
 697                      AND method = '" . $this->db->sql_escape($method) . "'";
 698              $this->db->sql_query($sql);
 699          }
 700      }
 701  
 702      /**
 703      * Delete a subscription
 704      *
 705      * @param string $item_type Type identifier of the subscription
 706      * @param int $item_id The id of the item
 707      * @param string $method The method of the notification e.g. 'board', 'email', or 'jabber'
 708      * @param bool|int $user_id The user_id to add the subscription for (bool false for current user)
 709      */
 710  	public function delete_subscription($item_type, $item_id = 0, $method = null, $user_id = false)
 711      {
 712          if ($method === null)
 713          {
 714              foreach ($this->get_default_methods() as $method_name)
 715              {
 716                  $this->delete_subscription($item_type, $item_id, $method_name, $user_id);
 717              }
 718  
 719              return;
 720          }
 721  
 722          $user_id = $user_id ?: $this->user->data['user_id'];
 723  
 724          $sql = 'UPDATE ' . $this->user_notifications_table . "
 725              SET notify = 0
 726              WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
 727                  AND item_id = " . (int) $item_id . '
 728                  AND user_id = ' .(int) $user_id . "
 729                  AND method = '" . $this->db->sql_escape($method) . "'";
 730          $this->db->sql_query($sql);
 731  
 732          if (!$this->db->sql_affectedrows())
 733          {
 734              $sql = 'INSERT INTO ' . $this->user_notifications_table . ' ' .
 735                  $this->db->sql_build_array('INSERT', array(
 736                      'item_type'        => $item_type,
 737                      'item_id'        => (int) $item_id,
 738                      'user_id'        => (int) $user_id,
 739                      'method'        => $method,
 740                      'notify'        => 0,
 741                  ));
 742              $this->db->sql_query($sql);
 743          }
 744      }
 745  
 746      /**
 747      * Disable all notifications of a certain type
 748      *
 749      * This should be called when an extension which has notification types
 750      * is disabled so that all those notifications are hidden and do not
 751      * cause errors
 752      *
 753      * @param string $notification_type_name Type identifier of the subscription
 754      */
 755  	public function disable_notifications($notification_type_name)
 756      {
 757          $sql = 'UPDATE ' . $this->notification_types_table . "
 758              SET notification_type_enabled = 0
 759              WHERE notification_type_name = '" . $this->db->sql_escape($notification_type_name) . "'";
 760          $this->db->sql_query($sql);
 761      }
 762  
 763      /**
 764      * Purge all notifications of a certain type
 765      *
 766      * This should be called when an extension which has notification types
 767      * is purged so that all those notifications are removed
 768      *
 769      * @param string $notification_type_name Type identifier of the subscription
 770      */
 771  	public function purge_notifications($notification_type_name)
 772      {
 773          // If a notification is never used, its type will not be added to the database
 774          // nor its id cached. If this method is called by an extension during the
 775          // purge step, and that extension never used its notifications,
 776          // get_notification_type_id() will throw an exception. However,
 777          // because no notification type was added to the database,
 778          // there is nothing to delete, so we can silently drop the exception.
 779          try
 780          {
 781              $notification_type_id = $this->get_notification_type_id($notification_type_name);
 782  
 783              /** @var method\method_interface $method */
 784              foreach ($this->get_available_subscription_methods() as $method)
 785              {
 786                  $method->purge_notifications($notification_type_id);
 787              }
 788  
 789          }
 790          catch (\phpbb\notification\exception $e)
 791          {
 792              // Continue
 793          }
 794      }
 795  
 796      /**
 797      * Enable all notifications of a certain type
 798      *
 799      * This should be called when an extension which has notification types
 800      * that was disabled is re-enabled so that all those notifications that
 801      * were hidden are shown again
 802      *
 803      * @param string $notification_type_name Type identifier of the subscription
 804      */
 805  	public function enable_notifications($notification_type_name)
 806      {
 807          $sql = 'UPDATE ' . $this->notification_types_table . "
 808              SET notification_type_enabled = 1
 809              WHERE notification_type_name = '" . $this->db->sql_escape($notification_type_name) . "'";
 810          $this->db->sql_query($sql);
 811      }
 812  
 813      /**
 814      * Delete all notifications older than a certain time
 815      *
 816      * @param int $timestamp Unix timestamp to delete all notifications that were created before
 817      * @param bool $only_read True (default) to only prune read notifications
 818      */
 819  	public function prune_notifications($timestamp, $only_read = true)
 820      {
 821          /** @var method\method_interface $method */
 822          foreach ($this->get_available_subscription_methods() as $method)
 823          {
 824              $method->prune_notifications($timestamp, $only_read);
 825          }
 826      }
 827  
 828      /**
 829       * Helper to get the list of methods enabled by default
 830       *
 831       * @return method\method_interface[]
 832       */
 833  	public function get_default_methods()
 834      {
 835          $default_methods = array();
 836  
 837          foreach ($this->notification_methods as $method)
 838          {
 839              if ($method->is_enabled_by_default() && $method->is_available())
 840              {
 841                  $default_methods[] = $method->get_type();
 842              }
 843          }
 844  
 845          return $default_methods;
 846      }
 847  
 848      /**
 849       * Helper to get the notifications item type class and set it up
 850       *
 851       * @param string $notification_type_name
 852       * @param array  $data
 853       * @return type\type_interface
 854       */
 855  	public function get_item_type_class($notification_type_name, $data = array())
 856      {
 857          $item = $this->load_object($notification_type_name);
 858  
 859          $item->set_initial_data($data);
 860  
 861          return $item;
 862      }
 863  
 864      /**
 865       * Helper to get the notifications method class and set it up
 866       *
 867       * @param string $method_name
 868       * @return method\method_interface
 869       */
 870  	public function get_method_class($method_name)
 871      {
 872          return $this->load_object($method_name);
 873      }
 874  
 875      /**
 876       * Helper to load objects (notification types/methods)
 877       *
 878       * @param string $object_name
 879       * @return method\method_interface|type\type_interface
 880       */
 881  	protected function load_object($object_name)
 882      {
 883          $object = $this->phpbb_container->get($object_name);
 884  
 885          if (method_exists($object, 'set_notification_manager'))
 886          {
 887              $object->set_notification_manager($this);
 888          }
 889  
 890          return $object;
 891      }
 892  
 893      /**
 894      * Get the notification type id from the name
 895      *
 896      * @param string $notification_type_name The name
 897      * @return int the notification_type_id
 898      * @throws \phpbb\notification\exception
 899      */
 900  	public function get_notification_type_id($notification_type_name)
 901      {
 902          $sql = 'SELECT notification_type_id, notification_type_name
 903              FROM ' . $this->notification_types_table;
 904          $result = $this->db->sql_query($sql, 604800); // cache for one week
 905          while ($row = $this->db->sql_fetchrow($result))
 906          {
 907              $notification_type_ids[$row['notification_type_name']] = (int) $row['notification_type_id'];
 908          }
 909          $this->db->sql_freeresult($result);
 910  
 911          if (!isset($notification_type_ids[$notification_type_name]))
 912          {
 913              if (!isset($this->notification_types[$notification_type_name]) && !isset($this->notification_types['notification.type.' . $notification_type_name]))
 914              {
 915                  throw new \phpbb\notification\exception('NOTIFICATION_TYPE_NOT_EXIST', array($notification_type_name));
 916              }
 917  
 918              $sql = 'INSERT INTO ' . $this->notification_types_table . ' ' . $this->db->sql_build_array('INSERT', array(
 919                  'notification_type_name'        => $notification_type_name,
 920                  'notification_type_enabled'        => 1,
 921              ));
 922              $this->db->sql_query($sql);
 923  
 924              // expose new notification type ID for this request
 925              $notification_type_ids[$notification_type_name] = (int) $this->db->sql_nextid();
 926  
 927              // destroy cache, we have a new addition which we have to to load next time
 928              $this->cache->destroy('sql', $this->notification_types_table);
 929          }
 930  
 931          return $notification_type_ids[$notification_type_name];
 932      }
 933  
 934      /**
 935      * Get notification type ids (as an array)
 936      *
 937      * @param string|array $notification_type_names Notification type names
 938      * @return array Array of integers
 939      */
 940  	public function get_notification_type_ids($notification_type_names)
 941      {
 942          if (!is_array($notification_type_names))
 943          {
 944              $notification_type_names = array($notification_type_names);
 945          }
 946  
 947          $notification_type_ids = array();
 948  
 949          foreach ($notification_type_names as $name)
 950          {
 951              $notification_type_ids[$name] = $this->get_notification_type_id($name);
 952          }
 953  
 954          return $notification_type_ids;
 955      }
 956  
 957      /**
 958      * Find the users which are already notified
 959      *
 960      * @param bool|string|array $notification_type_name Type identifier or array of item types (only acceptable if the $data is identical for the specified types). False to retrieve all item types
 961      * @param array $options
 962      * @return array The list of the notified users
 963      */
 964  	public function get_notified_users($notification_type_name, array $options)
 965      {
 966          $notification_type_id = $this->get_notification_type_id($notification_type_name);
 967  
 968          $notified_users = array();
 969  
 970          /** @var method\method_interface $method */
 971          foreach ($this->get_available_subscription_methods() as $method)
 972          {
 973              $notified_users = $notified_users + $method->get_notified_users($notification_type_id, $options);
 974          }
 975  
 976          return $notified_users;
 977      }
 978  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1