<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\notification\type;

/**
 * Forum notifications class
 * This class handles notifications for replies to a topic in a forum user subscribed to
 */

class forum extends \phpbb\notification\type\post
{
	/**
	 * Get notification type name
	 *
	 * @return string
	 */
	public function get_type()
	{
		return 'notification.type.forum';
	}

	/**
	 * Notification option data (for outputting to the user)
	 *
	 * @var bool|array False if the service should use its default data
	 * 					Array of data (including keys 'id', 'lang', and 'group')
	 */
	public static $notification_option = [
		'lang'	=> 'NOTIFICATION_TYPE_FORUM',
		'group'	=> 'NOTIFICATION_GROUP_POSTING',
	];

	/**
	 * Find the users who want to receive notifications
	 *
	 * @param array $type_data Data from submit_post
	 * @param array $options Options for finding users for notification
	 *
	 * @return array
	 */
	public function find_users_for_notification($type_data, $options = [])
	{
		$options = array_merge([
			'ignore_users'		=> [],
		], $options);

		$users = [];

		$sql = 'SELECT user_id
			FROM ' . FORUMS_WATCH_TABLE . '
			WHERE forum_id = ' . (int) $type_data['forum_id'] . '
				AND notify_status = ' . NOTIFY_YES . '
				AND user_id <> ' . (int) $type_data['poster_id'];
		$result = $this->db->sql_query($sql);
		while ($row = $this->db->sql_fetchrow($result))
		{
			$users[] = (int) $row['user_id'];
		}
		$this->db->sql_freeresult($result);

		$notify_users = $this->get_authorised_recipients($users, $type_data['forum_id'], $options, true);

		if (empty($notify_users))
		{
			return [];
		}

		// Try to find the users who already have been notified about replies and have not read them
		// Just update their notifications
		$notified_users = $this->notification_manager->get_notified_users($this->get_type(), [
			'item_parent_id'	=> static::get_item_parent_id($type_data),
			'read'				=> 0,
		]);

		foreach ($notified_users as $user => $notification_data)
		{
			unset($notify_users[$user]);

			/** @var post $notification */
			$notification = $this->notification_manager->get_item_type_class($this->get_type(), $notification_data);
			$update_responders = $notification->add_responders($type_data);
			if (!empty($update_responders))
			{
				$this->notification_manager->update_notification($notification, $update_responders, [
					'item_parent_id'	=> self::get_item_parent_id($type_data),
					'read'				=> 0,
					'user_id'			=> $user,
				]);
			}
		}

		return $notify_users;
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_email_template()
	{
		return 'forum_notify';
	}

	/**
	 * Get email template variables
	 *
	 * @return array
	 */
	public function get_email_template_variables()
	{
		if ($this->get_data('post_username'))
		{
			$username = $this->get_data('post_username');
		}
		else
		{
			$username = $this->user_loader->get_username($this->get_data('poster_id'), 'username');
		}

		return [
			'AUTHOR_NAME'				=> html_entity_decode($username, ENT_COMPAT),
			'FORUM_NAME'				=> html_entity_decode(censor_text($this->get_data('forum_name')), ENT_COMPAT),
			'POST_SUBJECT'				=> html_entity_decode(censor_text($this->get_data('post_subject')), ENT_COMPAT),
			'TOPIC_TITLE'				=> html_entity_decode(censor_text($this->get_data('topic_title')), ENT_COMPAT),

			'U_VIEW_POST'				=> generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}",
			'U_NEWEST_POST'				=> generate_board_url() . "/viewtopic.{$this->php_ext}?t={$this->item_parent_id}&e=1&view=unread#unread",
			'U_TOPIC'					=> generate_board_url() . "/viewtopic.{$this->php_ext}?t={$this->item_parent_id}",
			'U_VIEW_TOPIC'				=> generate_board_url() . "/viewtopic.{$this->php_ext}?t={$this->item_parent_id}",
			'U_FORUM'					=> generate_board_url() . "/viewforum.{$this->php_ext}?f={$this->get_data('forum_id')}",
			'U_STOP_WATCHING_FORUM'		=> generate_board_url() . "/viewforum.{$this->php_ext}?uid={$this->user_id}&f={$this->get_data('forum_id')}&unwatch=forum",
		];
	}
}
