unc_table::update_user_notifications_table()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 11
c 5
b 0
f 0
dl 0
loc 20
rs 9.6111
cc 5
nc 7
nop 1
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020-forever, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\usernotificationcontrol\core;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\db\driver\driver_interface as db_driver;
17
use phpbb\cache\driver\driver_interface as cache_driver;
18
19
/**
20
 * User Notification Control Core Table Class.
21
 */
22
class unc_table
23
{
24
	/** @var string Table Name */
25
	const TABLE_NAME = 'dark1_unc';
26
27
	/** @var string Cache Key */
28
	const CACHE_KEY = '_dark1_unc_notify_matrix';
29
30
	/** @var db_driver */
31
	protected $db;
32
33
	/** @var cache_driver */
34
	protected $cache;
35
36
	/** @var string */
37
	protected $table_prefix;
38
39
	/**
40
	 * Constructor for User Notification Control Core Table Class.
41
	 *
42
	 * @param db_driver		$db				Database object
43
	 * @param cache_driver	$cache			Cache object
44
	 * @param string		$table_prefix	phpBB Table Prefix
45
	 */
46
	public function __construct(db_driver $db, cache_driver $cache, $table_prefix)
47
	{
48
		$this->db			= $db;
49
		$this->cache		= $cache;
50
		$this->table_prefix	= $table_prefix;
51
	}
52
53
54
55
	/**
56
	 * Get Notification Method & Type is Enabled {true} OR Disabled {false}.
57
	 *
58
	 * @param string $notification_method
59
	 * @param string $notification_type
60
	 *
61
	 * @return bool|null Enabled {true} OR Disabled {false}
62
	 * @access public
63
	 */
64
	public function get_notify_method_type_value($notification_method, $notification_type)
65
	{
66
		// Get notify matrix
67
		$notify_matrix = $this->get_notify_method_type_matrix();
68
69
		return isset($notify_matrix[$notification_method][$notification_type]) ? $notify_matrix[$notification_method][$notification_type] : null;
70
	}
71
72
73
74
	/**
75
	 * Get Notification Methods & Types is Enabled {true} OR Disabled {false} in Matrix.
76
	 *
77
	 * @return array in form notify_matrix['notification_method']['notification_type'] = Enabled {true} OR Disabled {false}
78
	 * @access public
79
	 */
80
	public function get_notify_method_type_matrix()
81
	{
82
		// Get notify matrix data from the cache
83
		$notify_matrix = $this->cache->get(self::CACHE_KEY);
84
85
		if ($notify_matrix === false)
86
		{
87
			$sql = 'SELECT * FROM ' . $this->table_prefix . self::TABLE_NAME;
88
			$result = $this->db->sql_query($sql);
89
			$unc_rows = $this->db->sql_fetchrowset($result);
90
			$this->db->sql_freeresult($result);
91
			foreach ($unc_rows as $row)
92
			{
93
				$notify_matrix[$row['notification_method']][$row['notification_type']] = $row['notification_value'];
94
			}
95
96
			// Cache notify matrix data
97
			$this->cache->put(self::CACHE_KEY, $notify_matrix);
98
		}
99
100
		return $notify_matrix;
101
	}
102
103
104
105
	/**
106
	 * Set Notification Methods & Types is Disabled {true}in Matrix.
107
	 *
108
	 * @param array $notify_matrix in form notify_matrix['notification_method']['notification_type'] = Enabled {true} OR Disabled {false}
109
	 *
110
	 * @return void
111
	 * @access public
112
	 */
113
	public function set_notify_method_type_matrix($notify_matrix)
114
	{
115
		// Truncate the Table
116
		$sql = 'DELETE FROM ' . $this->table_prefix . self::TABLE_NAME;
117
		$this->db->sql_query($sql);
118
119
		// Multi-Insert the
120
		$sr = 0;
121
		$sql_ary = [];
122
		foreach ($notify_matrix as $notification_method => $ary_type)
123
		{
124
			foreach ($ary_type as $notification_type => $notification_value)
125
			{
126
				if (isset($notification_value))
127
				{
128
					$sql_ary[] = [
129
						'notification_sr_no' 	=> $sr,
130
						'notification_method' 	=> $notification_method,
131
						'notification_type' 	=> $notification_type,
132
						'notification_value' 	=> $notification_value,
133
					];
134
					$sr++;
135
				}
136
			}
137
		}
138
		$this->db->sql_multi_insert($this->table_prefix . self::TABLE_NAME, $sql_ary);
139
140
		// Cache notify matrix data
141
		$this->cache->put(self::CACHE_KEY, $notify_matrix);
142
	}
143
144
145
146
	/**
147
	 * Update User Notification Table using No Notify Matrix.
148
	 *
149
	 * @param array $notify_matrix in form notify_matrix['notification_method']['notification_type'] = Enabled {true} OR Disabled {false}
150
	 *
151
	 * @return void
152
	 * @access public
153
	 */
154
	public function update_user_notifications_table($notify_matrix)
155
	{
156
		foreach ($notify_matrix as $notification_method => $notification_type_ary)
157
		{
158
			$sql_ary = [];
159
160
			foreach ($notification_type_ary as $notification_type => $notify)
161
			{
162
				$sql_ary[$notify][] = $this->db->sql_escape($notification_type);
163
			}
164
165
			foreach ($sql_ary as $notify => $item_type_ary)
166
			{
167
				if (!empty($item_type_ary))
168
				{
169
					$sql = 'UPDATE ' . USER_NOTIFICATIONS_TABLE .
170
							' SET notify = ' . (int) $notify .
171
							" WHERE method = '" . $this->db->sql_escape($notification_method) . "'" .
172
							' AND ' . $this->db->sql_in_set('item_type', $item_type_ary);
173
					$this->db->sql_query($sql);
174
				}
175
			}
176
		}
177
	}
178
}
179