[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/notification/type/ -> post.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  * Post notifications class
  18  * This class handles notifications for replies to a topic
  19  */
  20  
  21  class post extends \phpbb\notification\type\base
  22  {
  23      /**
  24      * Get notification type name
  25      *
  26      * @return string
  27      */
  28  	public function get_type()
  29      {
  30          return 'notification.type.post';
  31      }
  32  
  33      /**
  34      * Language key used to output the text
  35      *
  36      * @var string
  37      */
  38      protected $language_key = 'NOTIFICATION_POST';
  39  
  40      /**
  41      * Inherit notification read status from post.
  42      *
  43      * @var bool
  44      */
  45      protected $inherit_read_status = true;
  46  
  47      /**
  48      * Notification option data (for outputting to the user)
  49      *
  50      * @var bool|array False if the service should use it's default data
  51      *                     Array of data (including keys 'id', 'lang', and 'group')
  52      */
  53      public static $notification_option = array(
  54          'lang'    => 'NOTIFICATION_TYPE_POST',
  55          'group'    => 'NOTIFICATION_GROUP_POSTING',
  56      );
  57  
  58      /**
  59      * Is available
  60      */
  61  	public function is_available()
  62      {
  63          return $this->config['allow_topic_notify'];
  64      }
  65  
  66      /**
  67      * Get the id of the item
  68      *
  69      * @param array $post The data from the post
  70      */
  71  	public static function get_item_id($post)
  72      {
  73          return (int) $post['post_id'];
  74      }
  75  
  76      /**
  77      * Get the id of the parent
  78      *
  79      * @param array $post The data from the post
  80      */
  81  	public static function get_item_parent_id($post)
  82      {
  83          return (int) $post['topic_id'];
  84      }
  85  
  86      /**
  87      * Find the users who want to receive notifications
  88      *
  89      * @param array $post Data from submit_post
  90      * @param array $options Options for finding users for notification
  91      *
  92      * @return array
  93      */
  94  	public function find_users_for_notification($post, $options = array())
  95      {
  96          $options = array_merge(array(
  97              'ignore_users'        => array(),
  98          ), $options);
  99  
 100          $users = array();
 101  
 102          $sql = 'SELECT user_id
 103              FROM ' . TOPICS_WATCH_TABLE . '
 104              WHERE topic_id = ' . (int) $post['topic_id'] . '
 105                  AND notify_status = ' . NOTIFY_YES . '
 106                  AND user_id <> ' . (int) $post['poster_id'];
 107          $result = $this->db->sql_query($sql);
 108          while ($row = $this->db->sql_fetchrow($result))
 109          {
 110              $users[] = (int) $row['user_id'];
 111          }
 112          $this->db->sql_freeresult($result);
 113  
 114          $sql = 'SELECT user_id
 115              FROM ' . FORUMS_WATCH_TABLE . '
 116              WHERE forum_id = ' . (int) $post['forum_id'] . '
 117                  AND notify_status = ' . NOTIFY_YES . '
 118                  AND user_id <> ' . (int) $post['poster_id'];
 119          $result = $this->db->sql_query($sql);
 120          while ($row = $this->db->sql_fetchrow($result))
 121          {
 122              $users[] = (int) $row['user_id'];
 123          }
 124          $this->db->sql_freeresult($result);
 125  
 126          $notify_users = $this->get_authorised_recipients($users, $post['forum_id'], $options, true);
 127  
 128          if (empty($notify_users))
 129          {
 130              return array();
 131          }
 132  
 133          // Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications
 134          $update_notifications = array();
 135          $sql = 'SELECT n.*
 136              FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt
 137              WHERE n.notification_type_id = ' . (int) $this->notification_type_id . '
 138                  AND n.item_parent_id = ' . (int) static::get_item_parent_id($post) . '
 139                  AND n.notification_read = 0
 140                  AND nt.notification_type_id = n.notification_type_id
 141                  AND nt.notification_type_enabled = 1';
 142          $result = $this->db->sql_query($sql);
 143          while ($row = $this->db->sql_fetchrow($result))
 144          {
 145              // Do not create a new notification
 146              unset($notify_users[$row['user_id']]);
 147  
 148              $notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
 149              $update_responders = $notification->add_responders($post);
 150              if (!empty($update_responders))
 151              {
 152                  $sql = 'UPDATE ' . $this->notifications_table . '
 153                      SET ' . $this->db->sql_build_array('UPDATE', $update_responders) . '
 154                      WHERE notification_id = ' . $row['notification_id'];
 155                  $this->db->sql_query($sql);
 156              }
 157          }
 158          $this->db->sql_freeresult($result);
 159  
 160          return $notify_users;
 161      }
 162  
 163      /**
 164      * Get the user's avatar
 165      */
 166  	public function get_avatar()
 167      {
 168          return $this->user_loader->get_avatar($this->get_data('poster_id'), false, true);
 169      }
 170  
 171      /**
 172      * Get the HTML formatted title of this notification
 173      *
 174      * @return string
 175      */
 176  	public function get_title()
 177      {
 178          $responders = $this->get_data('responders');
 179          $usernames = array();
 180  
 181          if (!is_array($responders))
 182          {
 183              $responders = array();
 184          }
 185  
 186          $responders = array_merge(array(array(
 187              'poster_id'        => $this->get_data('poster_id'),
 188              'username'        => $this->get_data('post_username'),
 189          )), $responders);
 190  
 191          $responders_cnt = sizeof($responders);
 192          $responders = $this->trim_user_ary($responders);
 193          $trimmed_responders_cnt = $responders_cnt - sizeof($responders);
 194  
 195          foreach ($responders as $responder)
 196          {
 197              if ($responder['username'])
 198              {
 199                  $usernames[] = $responder['username'];
 200              }
 201              else
 202              {
 203                  $usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile');
 204              }
 205          }
 206  
 207          if ($trimmed_responders_cnt > 20)
 208          {
 209              $usernames[] = $this->user->lang('NOTIFICATION_MANY_OTHERS');
 210          }
 211          else if ($trimmed_responders_cnt)
 212          {
 213              $usernames[] = $this->user->lang('NOTIFICATION_X_OTHERS', $trimmed_responders_cnt);
 214          }
 215  
 216          return $this->user->lang(
 217              $this->language_key,
 218              phpbb_generate_string_list($usernames, $this->user),
 219              $responders_cnt
 220          );
 221      }
 222  
 223      /**
 224      * Get the HTML formatted reference of the notification
 225      *
 226      * @return string
 227      */
 228  	public function get_reference()
 229      {
 230          return $this->user->lang(
 231              'NOTIFICATION_REFERENCE',
 232              censor_text($this->get_data('topic_title'))
 233          );
 234      }
 235  
 236      /**
 237      * Get email template
 238      *
 239      * @return string|bool
 240      */
 241  	public function get_email_template()
 242      {
 243          return 'topic_notify';
 244      }
 245  
 246      /**
 247      * Get email template variables
 248      *
 249      * @return array
 250      */
 251  	public function get_email_template_variables()
 252      {
 253          if ($this->get_data('post_username'))
 254          {
 255              $username = $this->get_data('post_username');
 256          }
 257          else
 258          {
 259              $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username');
 260          }
 261  
 262          return array(
 263              'AUTHOR_NAME'                => htmlspecialchars_decode($username),
 264              'POST_SUBJECT'                => htmlspecialchars_decode(censor_text($this->get_data('post_subject'))),
 265              'TOPIC_TITLE'                => htmlspecialchars_decode(censor_text($this->get_data('topic_title'))),
 266  
 267              'U_VIEW_POST'                => generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}",
 268              'U_NEWEST_POST'                => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}&e=1&view=unread#unread",
 269              'U_TOPIC'                    => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}",
 270              'U_VIEW_TOPIC'                => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}",
 271              'U_FORUM'                    => generate_board_url() . "/viewforum.{$this->php_ext}?f={$this->get_data('forum_id')}",
 272              'U_STOP_WATCHING_TOPIC'        => generate_board_url() . "/viewtopic.{$this->php_ext}?uid={$this->user_id}&f={$this->get_data('forum_id')}&t={$this->item_parent_id}&unwatch=topic",
 273          );
 274      }
 275  
 276      /**
 277      * Get the url to this item
 278      *
 279      * @return string URL
 280      */
 281  	public function get_url()
 282      {
 283          return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "p={$this->item_id}#p{$this->item_id}");
 284      }
 285  
 286      /**
 287      * {inheritDoc}
 288      */
 289  	public function get_redirect_url()
 290      {
 291          return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t={$this->item_parent_id}&amp;view=unread#unread");
 292      }
 293  
 294      /**
 295      * Users needed to query before this notification can be displayed
 296      *
 297      * @return array Array of user_ids
 298      */
 299  	public function users_to_query()
 300      {
 301          $responders = $this->get_data('responders');
 302          $users = array(
 303              $this->get_data('poster_id'),
 304          );
 305  
 306          if (is_array($responders))
 307          {
 308              foreach ($responders as $responder)
 309              {
 310                  $users[] = $responder['poster_id'];
 311              }
 312          }
 313  
 314          return $this->trim_user_ary($users);
 315      }
 316  
 317      /**
 318      * Trim the user array passed down to 3 users if the array contains
 319      * more than 4 users.
 320      *
 321      * @param array $users Array of users
 322      * @return array Trimmed array of user_ids
 323      */
 324  	public function trim_user_ary($users)
 325      {
 326          if (sizeof($users) > 4)
 327          {
 328              array_splice($users, 3);
 329          }
 330          return $users;
 331      }
 332  
 333      /**
 334      * Pre create insert array function
 335      * This allows you to perform certain actions, like run a query
 336      * and load data, before create_insert_array() is run. The data
 337      * returned from this function will be sent to create_insert_array().
 338      *
 339      * @param array $post Post data from submit_post
 340      * @param array $notify_users Notify users list
 341      *         Formated from find_users_for_notification()
 342      * @return array Whatever you want to send to create_insert_array().
 343      */
 344  	public function pre_create_insert_array($post, $notify_users)
 345      {
 346          if (!sizeof($notify_users) || !$this->inherit_read_status)
 347          {
 348              return array();
 349          }
 350  
 351          $tracking_data = array();
 352          $sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . '
 353              WHERE topic_id = ' . (int) $post['topic_id'] . '
 354                  AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users));
 355          $result = $this->db->sql_query($sql);
 356          while ($row = $this->db->sql_fetchrow($result))
 357          {
 358              $tracking_data[$row['user_id']] = $row['mark_time'];
 359          }
 360          $this->db->sql_freeresult($result);
 361  
 362          return $tracking_data;
 363      }
 364  
 365      /**
 366      * Function for preparing the data for insertion in an SQL query
 367      * (The service handles insertion)
 368      *
 369      * @param array $post Data from submit_post
 370      * @param array $pre_create_data Data from pre_create_insert_array()
 371      *
 372      * @return array Array of data ready to be inserted into the database
 373      */
 374  	public function create_insert_array($post, $pre_create_data = array())
 375      {
 376          $this->set_data('poster_id', $post['poster_id']);
 377  
 378          $this->set_data('topic_title', $post['topic_title']);
 379  
 380          $this->set_data('post_subject', $post['post_subject']);
 381  
 382          $this->set_data('post_username', (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''));
 383  
 384          $this->set_data('forum_id', $post['forum_id']);
 385  
 386          $this->set_data('forum_name', $post['forum_name']);
 387  
 388          $this->notification_time = $post['post_time'];
 389  
 390          // Topics can be "read" before they are public (while awaiting approval).
 391          // Make sure that if the user has read the topic, it's marked as read in the notification
 392          if ($this->inherit_read_status && isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->notification_time)
 393          {
 394              $this->notification_read = true;
 395          }
 396  
 397          return parent::create_insert_array($post, $pre_create_data);
 398      }
 399  
 400      /**
 401      * Add responders to the notification
 402      *
 403      * @param mixed $post
 404      */
 405  	public function add_responders($post)
 406      {
 407          // Do not add them as a responder if they were the original poster that created the notification
 408          if ($this->get_data('poster_id') == $post['poster_id'])
 409          {
 410              return array();
 411          }
 412  
 413          $responders = $this->get_data('responders');
 414  
 415          $responders = ($responders === null) ? array() : $responders;
 416  
 417          // Do not add more than 25 responders,
 418          // we trim the username list to "a, b, c and x others" anyway
 419          // so there is no use to add all of them anyway.
 420          if (sizeof($responders) > 25)
 421          {
 422              return array();
 423          }
 424  
 425          foreach ($responders as $responder)
 426          {
 427              // Do not add them as a responder multiple times
 428              if ($responder['poster_id'] == $post['poster_id'])
 429              {
 430                  return array();
 431              }
 432          }
 433  
 434          $responders[] = array(
 435              'poster_id'        => $post['poster_id'],
 436              'username'        => (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''),
 437          );
 438  
 439          $this->set_data('responders', $responders);
 440  
 441          $serialized_data = serialize($this->get_data(false));
 442  
 443          // If the data is longer then 4000 characters, it would cause a SQL error.
 444          // We don't add the username to the list if this is the case.
 445          if (utf8_strlen($serialized_data) >= 4000)
 446          {
 447              return array();
 448          }
 449  
 450          return array('notification_data' => $serialized_data);
 451      }
 452  }


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