[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/notification/type/ -> base.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\type;
  15  
  16  /**
  17  * Base notifications class
  18  */
  19  abstract class base implements \phpbb\notification\type\type_interface
  20  {
  21      /** @var \phpbb\notification\manager */
  22      protected $notification_manager;
  23  
  24      /** @var \phpbb\user_loader */
  25      protected $user_loader;
  26  
  27      /** @var \phpbb\db\driver\driver_interface */
  28      protected $db;
  29  
  30      /** @var \phpbb\cache\driver\driver_interface */
  31      protected $cache;
  32  
  33      /** @var \phpbb\template\template */
  34      protected $template;
  35  
  36      /** @var \phpbb\user */
  37      protected $user;
  38  
  39      /** @var \phpbb\auth\auth */
  40      protected $auth;
  41  
  42      /** @var \phpbb\config\config */
  43      protected $config;
  44  
  45      /** @var string */
  46      protected $phpbb_root_path;
  47  
  48      /** @var string */
  49      protected $php_ext;
  50  
  51      /** @var string */
  52      protected $notification_types_table;
  53  
  54      /** @var string */
  55      protected $notifications_table;
  56  
  57      /** @var string */
  58      protected $user_notifications_table;
  59  
  60      /**
  61      * Notification option data (for outputting to the user)
  62      *
  63      * @var bool|array False if the service should use its default data
  64      *                     Array of data (including keys 'id', 'lang', and 'group')
  65      */
  66      public static $notification_option = false;
  67  
  68      /**
  69      * The notification_type_id, set upon creation of the class
  70      * This is the notification_type_id from the notification_types table
  71      *
  72      * @var int
  73      */
  74      protected $notification_type_id;
  75  
  76      /**
  77      * Indentification data
  78      * notification_type_id    - ID of the item type (auto generated, from notification types table)
  79      * item_id                - ID of the item (e.g. post_id, msg_id)
  80      * item_parent_id        - Parent item id (ex: for topic => forum_id, for post => topic_id, etc)
  81      * user_id
  82      * notification_read
  83      * notification_time
  84      * notification_data (special serialized field that each notification type can use to store stuff)
  85      *
  86      * @var array $data Notification row from the database
  87      *         This must be private, all interaction should use __get(), __set(), get_data(), set_data()
  88      */
  89      private $data = array();
  90  
  91      /**
  92      * Notification Type Base Constructor
  93      *
  94      * @param \phpbb\user_loader $user_loader
  95      * @param \phpbb\db\driver\driver_interface $db
  96      * @param \phpbb\cache\driver\driver_interface $cache
  97      * @param \phpbb\user $user
  98      * @param \phpbb\auth\auth $auth
  99      * @param \phpbb\config\config $config
 100      * @param string $phpbb_root_path
 101      * @param string $php_ext
 102      * @param string $notification_types_table
 103      * @param string $notifications_table
 104      * @param string $user_notifications_table
 105      * @return \phpbb\notification\type\base
 106      */
 107  	public function __construct(\phpbb\user_loader $user_loader, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, $user, \phpbb\auth\auth $auth, \phpbb\config\config $config, $phpbb_root_path, $php_ext, $notification_types_table, $notifications_table, $user_notifications_table)
 108      {
 109          $this->user_loader = $user_loader;
 110          $this->db = $db;
 111          $this->cache = $cache;
 112          $this->user = $user;
 113          $this->auth = $auth;
 114          $this->config = $config;
 115  
 116          $this->phpbb_root_path = $phpbb_root_path;
 117          $this->php_ext = $php_ext;
 118  
 119          $this->notification_types_table = $notification_types_table;
 120          $this->notifications_table = $notifications_table;
 121          $this->user_notifications_table = $user_notifications_table;
 122      }
 123  
 124      /**
 125      * Set notification manager (required)
 126      *
 127      * @param \phpbb\notification\manager $notification_manager
 128      */
 129  	public function set_notification_manager(\phpbb\notification\manager $notification_manager)
 130      {
 131          $this->notification_manager = $notification_manager;
 132  
 133          $this->notification_type_id = $this->notification_manager->get_notification_type_id($this->get_type());
 134      }
 135  
 136      /**
 137      * Set initial data from the database
 138      *
 139      * @param array $data Row directly from the database
 140      */
 141  	public function set_initial_data($data = array())
 142      {
 143          // The row from the database (unless this is a new notification we're going to add)
 144          $this->data = $data;
 145          $this->data['notification_data'] = (isset($this->data['notification_data'])) ? unserialize($this->data['notification_data']) : array();
 146      }
 147  
 148      /**
 149      * Magic method to get data from this notification
 150      *
 151      * @param mixed $name
 152      * @return mixed
 153      */
 154  	public function __get($name)
 155      {
 156          return (!isset($this->data[$name])) ? null : $this->data[$name];
 157      }
 158  
 159  
 160      /**
 161      * Magic method to set data on this notification
 162      *
 163      * @param mixed $name
 164      * @param mixed $value
 165      *
 166      * @return null
 167      */
 168  	public function __set($name, $value)
 169      {
 170          $this->data[$name] = $value;
 171      }
 172  
 173  
 174      /**
 175      * Magic method to get a string of this notification
 176      *
 177      * Primarily for testing
 178      *
 179      * @return mixed
 180      */
 181  	public function __toString()
 182      {
 183          return (!empty($this->data)) ? var_export($this->data, true) : $this->get_type();
 184      }
 185  
 186      /**
 187      * Get special data (only important for the classes that extend this)
 188      *
 189      * @param string $name Name of the variable to get
 190      * @return mixed
 191      */
 192  	protected function get_data($name)
 193      {
 194          return ($name === false) ? $this->data['notification_data'] : ((isset($this->data['notification_data'][$name])) ? $this->data['notification_data'][$name] : null);
 195      }
 196  
 197      /**
 198      * Set special data (only important for the classes that extend this)
 199      *
 200      * @param string $name Name of the variable to set
 201      * @param mixed $value Value to set to the variable
 202      * @return mixed
 203      */
 204  	protected function set_data($name, $value)
 205      {
 206          $this->data['notification_data'][$name] = $value;
 207      }
 208  
 209      /**
 210      * Function for preparing the data for insertion in an SQL query
 211      * (The service handles insertion)
 212      *
 213      * @param array $type_data Data unique to this notification type
 214      * @param array $pre_create_data Data from pre_create_insert_array()
 215      * @return array Array of data ready to be inserted into the database
 216      */
 217  	public function create_insert_array($type_data, $pre_create_data = array())
 218      {
 219          // Defaults
 220          $this->data = array_merge(array(
 221              'item_id'                => static::get_item_id($type_data),
 222              'notification_type_id'    => $this->notification_type_id,
 223              'item_parent_id'        => static::get_item_parent_id($type_data),
 224  
 225              'notification_time'        => time(),
 226              'notification_read'        => false,
 227  
 228              'notification_data'                    => array(),
 229          ), $this->data);
 230  
 231          $data = $this->data;
 232  
 233          $data['notification_data'] = serialize($data['notification_data']);
 234  
 235          return $data;
 236      }
 237  
 238      /**
 239      * Function for preparing the data for update in an SQL query
 240      * (The service handles insertion)
 241      *
 242      * @param array $type_data Data unique to this notification type
 243      * @return array Array of data ready to be updated in the database
 244      */
 245  	public function create_update_array($type_data)
 246      {
 247          $data = $this->create_insert_array($type_data);
 248  
 249          // Unset data unique to each row
 250          unset(
 251              $data['notification_time'], // Also unsetting time, since it always tries to change the time to current (if you actually need to change the time, over-ride this function)
 252              $data['notification_id'],
 253              $data['notification_read'],
 254              $data['user_id']
 255          );
 256  
 257          return $data;
 258      }
 259  
 260      /**
 261      * Mark this item read
 262      *
 263      * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
 264      * @return string|null If $return is False, nothing will be returned, else the sql code to update this item
 265      */
 266  	public function mark_read($return = false)
 267      {
 268          return $this->mark(false, $return);
 269      }
 270  
 271      /**
 272      * Mark this item unread
 273      *
 274      * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
 275      * @return string|null If $return is False, nothing will be returned, else the sql code to update this item
 276      */
 277  	public function mark_unread($return = false)
 278      {
 279          return $this->mark(true, $return);
 280      }
 281  
 282      /**
 283      * {inheritDoc}
 284      */
 285  	public function get_redirect_url()
 286      {
 287          return $this->get_url();
 288      }
 289  
 290      /**
 291      * Prepare to output the notification to the template
 292      *
 293      * @return array Template variables
 294      */
 295  	public function prepare_for_display()
 296      {
 297          $mark_hash = generate_link_hash('mark_notification_read');
 298  
 299          if ($this->get_url())
 300          {
 301              $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&amp;hash=' . $mark_hash);
 302          }
 303          else
 304          {
 305              $redirect = (($this->user->page['page_dir']) ? $this->user->page['page_dir'] . '/' : '') . $this->user->page['page_name'] . (($this->user->page['query_string']) ? '?' . $this->user->page['query_string'] : '');
 306  
 307              $u_mark_read = append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'mark_notification=' . $this->notification_id . '&amp;hash=' . $mark_hash . '&amp;redirect=' . urlencode($redirect));
 308          }
 309  
 310          return array(
 311              'NOTIFICATION_ID'    => $this->notification_id,
 312              'STYLING'            => $this->get_style_class(),
 313              'AVATAR'            => $this->get_avatar(),
 314              'FORMATTED_TITLE'    => $this->get_title(),
 315              'REFERENCE'            => $this->get_reference(),
 316              'FORUM'                => $this->get_forum(),
 317              'REASON'            => $this->get_reason(),
 318              'URL'                => $this->get_url(),
 319              'TIME'                   => $this->user->format_date($this->notification_time),
 320              'UNREAD'            => !$this->notification_read,
 321              'U_MARK_READ'        => (!$this->notification_read) ? $u_mark_read : '',
 322          );
 323      }
 324  
 325      /**
 326      * -------------- Fall back functions -------------------
 327      */
 328  
 329      /**
 330      * URL to unsubscribe to this notification (fall back)
 331      *
 332      * @param string|bool $method Method name to unsubscribe from (email|jabber|etc), False to unsubscribe from all notifications for this item
 333      */
 334  	public function get_unsubscribe_url($method = false)
 335      {
 336          return false;
 337      }
 338  
 339      /**
 340      * Get the CSS style class of the notification (fall back)
 341      *
 342      * @return string
 343      */
 344  	public function get_style_class()
 345      {
 346          return '';
 347      }
 348  
 349      /**
 350      * Get the user's avatar (fall back)
 351      *
 352      * @return string
 353      */
 354  	public function get_avatar()
 355      {
 356          return '';
 357      }
 358  
 359      /**
 360      * Get the reference of the notifcation (fall back)
 361      *
 362      * @return string
 363      */
 364  	public function get_reference()
 365      {
 366          return '';
 367      }
 368  
 369      /**
 370      * Get the forum of the notification reference (fall back)
 371      *
 372      * @return string
 373      */
 374  	public function get_forum()
 375      {
 376          return '';
 377      }
 378  
 379      /**
 380      * Get the reason for the notifcation (fall back)
 381      *
 382      * @return string
 383      */
 384  	public function get_reason()
 385      {
 386          return '';
 387      }
 388  
 389      /**
 390      * Get the special items to load (fall back)
 391      *
 392      * @return array
 393      */
 394  	public function get_load_special()
 395      {
 396          return array();
 397      }
 398  
 399      /**
 400      * Load the special items (fall back)
 401      */
 402  	public function load_special($data, $notifications)
 403      {
 404          return;
 405      }
 406  
 407      /**
 408      * Is available (fall back)
 409      *
 410      * @return bool
 411      */
 412  	public function is_available()
 413      {
 414          return true;
 415      }
 416  
 417      /**
 418      * Pre create insert array function (fall back)
 419      *
 420      * @return array
 421      */
 422  	public function pre_create_insert_array($type_data, $notify_users)
 423      {
 424          return array();
 425      }
 426  
 427      /**
 428      * -------------- Helper functions -------------------
 429      */
 430  
 431      /**
 432      * Find the users who want to receive notifications (helper)
 433      *
 434      * @param array $user_ids User IDs to check if they want to receive notifications
 435      *         (Bool False to check all users besides anonymous and bots (USER_IGNORE))
 436      *
 437      * @return array
 438      */
 439  	protected function check_user_notification_options($user_ids = false, $options = array())
 440      {
 441          $options = array_merge(array(
 442              'ignore_users'        => array(),
 443              'item_type'            => $this->get_type(),
 444              'item_id'            => 0, // Global by default
 445          ), $options);
 446  
 447          if ($user_ids === false)
 448          {
 449              $user_ids = array();
 450  
 451              $sql = 'SELECT user_id
 452                  FROM ' . USERS_TABLE . '
 453                  WHERE user_id <> ' . ANONYMOUS . '
 454                      AND user_type <> ' . USER_IGNORE;
 455              $result = $this->db->sql_query($sql);
 456              while ($row = $this->db->sql_fetchrow($result))
 457              {
 458                  $user_ids[] = $row['user_id'];
 459              }
 460              $this->db->sql_freeresult($result);
 461          }
 462  
 463          if (empty($user_ids))
 464          {
 465              return array();
 466          }
 467  
 468          $rowset = $resulting_user_ids = array();
 469  
 470          $sql = 'SELECT user_id, method, notify
 471              FROM ' . $this->user_notifications_table . '
 472              WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . "
 473                  AND item_type = '" . $this->db->sql_escape($options['item_type']) . "'
 474                  AND item_id = " . (int) $options['item_id'];
 475          $result = $this->db->sql_query($sql);
 476  
 477          while ($row = $this->db->sql_fetchrow($result))
 478          {
 479              $resulting_user_ids[] = $row['user_id'];
 480  
 481              if (!$row['notify'] || (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])))
 482              {
 483                  continue;
 484              }
 485  
 486              if (!isset($rowset[$row['user_id']]))
 487              {
 488                  $rowset[$row['user_id']] = array();
 489              }
 490  
 491              $rowset[$row['user_id']][] = $row['method'];
 492          }
 493  
 494          $this->db->sql_freeresult($result);
 495  
 496          foreach ($user_ids as $user_id)
 497          {
 498              if (!in_array($user_id, $resulting_user_ids) && !isset($options['ignore_users'][$user_id]))
 499              {
 500                  // No rows at all for this user, default to ''
 501                  $rowset[$user_id] = array('');
 502              }
 503          }
 504  
 505          return $rowset;
 506      }
 507  
 508      /**
 509      * Mark this item read/unread helper
 510      *
 511      * @param bool $unread Unread (True/False) (Default: False)
 512      * @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
 513      * @return string|null If $return is False, nothing will be returned, else the sql code to update this item
 514      */
 515  	protected function mark($unread = true, $return = false)
 516      {
 517          $this->notification_read = (bool) !$unread;
 518  
 519          $where = array(
 520              'notification_type_id = ' . (int) $this->notification_type_id,
 521              'item_id = ' . (int) $this->item_id,
 522              'user_id = ' . (int) $this->user_id,
 523          );
 524          $where = implode(' AND ', $where);
 525  
 526          if ($return)
 527          {
 528              return $where;
 529          }
 530  
 531          $sql = 'UPDATE ' . $this->notifications_table . '
 532              SET notification_read = ' . (int) $this->notification_read . '
 533              WHERE ' . $where;
 534          $this->db->sql_query($sql);
 535      }
 536  
 537      /**
 538       * Get a list of users that are authorised to receive notifications
 539       *
 540       * @param array $users Array of users that have subscribed to a notification
 541       * @param int $forum_id Forum ID of the forum
 542       * @param array $options Array of notification options
 543       * @param bool $sort Whether the users array should be sorted. Default: false
 544       * @return array Array of users that are authorised recipients
 545       */
 546  	protected function get_authorised_recipients($users, $forum_id, $options, $sort = false)
 547      {
 548          if (empty($users))
 549          {
 550              return array();
 551          }
 552  
 553          $users = array_unique($users);
 554  
 555          if ($sort)
 556          {
 557              sort($users);
 558          }
 559  
 560          $auth_read = $this->auth->acl_get_list($users, 'f_read', $forum_id);
 561  
 562          if (empty($auth_read))
 563          {
 564              return array();
 565          }
 566  
 567          return $this->check_user_notification_options($auth_read[$forum_id]['f_read'], $options);
 568      }
 569  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1