acp_listener   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 306
Duplicated Lines 9.15 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 37
lcom 2
cbo 1
dl 28
loc 306
rs 8.6
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getSubscribedEvents() 0 15 1
A delete_user() 0 7 1
A load_log_keys() 0 4 1
B translate_attributes() 0 20 6
A get_attributes_data() 0 11 2
A define_attributes_values() 0 9 2
B save_attribute_auths() 0 12 5
D _copy_attribute_permissions() 28 138 13
A add_permission() 0 22 2
A add_attributes_features() 0 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *
4
 * @package Quick Title Edition Extension
5
 * @copyright (c) 2015 ABDev
6
 * @copyright (c) 2015 PastisD
7
 * @copyright (c) 2015 Geolim4 <http://geolim4.com>
8
 * @copyright (c) 2015 Zoddo <[email protected]>
9
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
10
 *
11
 */
12
13
namespace ernadoo\qte\event;
14
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
class acp_listener implements EventSubscriberInterface
18
{
19
	/** @var \phpbb\request\request */
20
	protected $request;
21
22
	/** @var \phpbb\cache\driver\driver_interface */
23
	protected $cache;
24
25
	/** @var \phpbb\db\driver\driver_interface */
26
	protected $db;
27
28
	/** @var \phpbb\user */
29
	protected $user;
30
31
	/** @var \ernadoo\qte\qte */
32
	protected $qte;
33
34
	/** @var string */
35
	protected $table_prefix;
36
37
	public function __construct(\phpbb\request\request $request, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, \ernadoo\qte\qte $qte, $table_prefix)
38
	{
39
		$this->request	= $request;
40
		$this->cache	= $cache;
41
		$this->db		= $db;
42
		$this->user		= $user;
43
		$this->qte		= $qte;
44
45
		$this->table_prefix = $table_prefix;
46
	}
47
48
	static public function getSubscribedEvents()
49
	{
50
		return array(
51
			'core.permissions'					=> 'add_permission',
52
			'core.delete_user_after'			=> 'delete_user',
53
			'core.get_logs_main_query_before'	=> 'load_log_keys',
54
			'core.get_logs_modify_entry_data'	=> 'translate_attributes',
55
56
			// forums admin panel
57
			'core.acp_manage_forums_request_data'		=> 'get_attributes_data',
58
			'core.acp_manage_forums_initialise_data'	=> 'define_attributes_values',
59
			'core.acp_manage_forums_display_form'		=> 'add_attributes_features',
60
			'core.acp_manage_forums_update_data_after'	=> 'save_attribute_auths',
61
		);
62
	}
63
64
	public function add_permission($event)
65
	{
66
		$categories				= $event['categories'];
67
		$categories				= array_merge($categories, array('qte' => 'ACL_CAT_QTE'));
68
		$event['categories']	= $categories;
69
70
		$permissions = $event['permissions'];
71
72
		$permissions += array(
73
			'a_attr_manage'		=> array('lang' => 'ACL_A_ATTR_MANAGE', 'cat' => 'posting'),
74
			'm_qte_attr_del'	=> array('lang' => 'ACL_M_ATTR_DEL', 'cat' => 'qte'),
75
			'm_qte_attr_edit'	=> array('lang' => 'ACL_M_ATTR_EDIT', 'cat' => 'qte'),
76
		);
77
78
		foreach ($this->qte->getAttr() as $attr)
79
		{
80
			$permissions += array(
81
				'f_qte_attr_'.$attr['attr_id'] => array('lang' => $this->user->lang('QTE_CAN_USE_ATTR', $attr['attr_name']), 'cat' => 'qte'),
82
			);
83
		}
84
		$event['permissions'] = $permissions;
85
	}
86
87
	public function delete_user($event)
88
	{
89
		$sql = 'UPDATE ' . TOPICS_TABLE . '
90
			SET topic_attr_user = ' . ANONYMOUS . '
91
			WHERE ' . $this->db->sql_in_set('topic_attr_user', $event['user_ids']);
92
		$this->db->sql_query($sql);
93
	}
94
95
	public function load_log_keys()
96
	{
97
		$this->user->add_lang_ext('ernadoo/qte', array('attributes', 'logs_attributes'));
98
	}
99
100
	public function translate_attributes($event)
101
	{
102
		$row = $event['row'];
103
104
		if (strpos($row['log_operation'], 'LOG_ATTRIBUTE_') === 0 || strpos($row['log_operation'], 'MCP_ATTRIBUTE_') === 0)
105
		{
106
			$log_data = unserialize($row['log_data']);
107
108
			if (!empty($log_data) && is_array($log_data))
109
			{
110
				foreach ($log_data as &$arg)
111
				{
112
					$arg = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($arg));
113
				}
114
			}
115
116
			$row['log_data'] = serialize($log_data);
117
			$event['row'] = $row;
118
		}
119
	}
120
121
	public function get_attributes_data($event)
122
	{
123
		if ($event['action'] == 'edit')
124
		{
125
			$event['forum_data'] += array(
126
				'default_attr' => $this->request->variable('default_attr', 0, false, \phpbb\request\request_interface::POST),
127
			);
128
		}
129
130
		$event['forum_data'] += array('force_attr' => $this->request->variable('force_attr', false, false, \phpbb\request\request_interface::POST));
131
	}
132
133
	public function define_attributes_values($event)
134
	{
135
		if ($event['action'] == 'edit')
136
		{
137
			$event['forum_data'] += array('default_attr' => 0);
138
		}
139
140
		$event['forum_data'] += array('force_attr' => false);
141
	}
142
143
	public function add_attributes_features($event)
144
	{
145
		$this->user->add_lang_ext('ernadoo/qte', 'attributes_acp');
146
147
		// init ary
148
		$template_data = $event['template_data'];
149
150
		if ($event['action'] == 'edit')
151
		{
152
			$this->qte->attr_default($event['forum_id'], $event['forum_data']['default_attr']);
153
		}
154
155
		$template_data += array('S_FORCE_ATTR' => $event['forum_data']['force_attr'] ? true : false);
156
157
		// send to template
158
		$event['template_data'] = $template_data;
159
		return $event['template_data'];
160
	}
161
162
	public function save_attribute_auths($event)
163
	{
164
		if (!sizeof($event['errors']))
165
		{
166
			$from_attr = $this->request->variable('from_attr', 0, false, \phpbb\request\request_interface::POST);
167
168
			if ($from_attr && $from_attr != $event['forum_data']['forum_id'])
169
			{
170
				$this->_copy_attribute_permissions($from_attr, $event['forum_data']['forum_id'], $event['is_new_forum'] ? false : true);
171
			}
172
		}
173
	}
174
175
	/**
176
	* Copies attributes permissions from one forum to others
177
	*
178
	* @param int	$src_forum_id		The source forum we want to copy permissions from
179
	* @param array	$dest_forum_ids		The destination forum(s) we want to copy to
180
	* @param bool	$clear_dest_perms	True if destination permissions should be deleted
181
	*
182
	* @return bool						False on error
183
	*/
184
	private function _copy_attribute_permissions($src_forum_id, $dest_forum_ids, $clear_dest_perms)
185
	{
186
		// Only one forum id specified
187
		if (!is_array($dest_forum_ids))
188
		{
189
			$dest_forum_ids = array($dest_forum_ids);
190
		}
191
192
		// Make sure forum ids are integers
193
		$src_forum_id = (int) $src_forum_id;
194
		$dest_forum_ids = array_map('intval', $dest_forum_ids);
195
196
		// No source forum or no destination forums specified
197
		if (empty($src_forum_id) || empty($dest_forum_ids))
198
		{
199
			return false;
200
		}
201
202
		// Check if source forum exists
203
		$sql = 'SELECT forum_name
204
			FROM ' . FORUMS_TABLE . '
205
			WHERE forum_id = ' . $src_forum_id;
206
		$result = $this->db->sql_query($sql);
207
		$src_forum_name = $this->db->sql_fetchfield('forum_name');
208
		$this->db->sql_freeresult($result);
209
210
		// Source forum doesn't exist
211
		if (empty($src_forum_name))
212
		{
213
			return false;
214
		}
215
216
		// Check if destination forums exists
217
		$sql = 'SELECT forum_id, forum_name
218
			FROM ' . FORUMS_TABLE . '
219
			WHERE ' . $this->db->sql_in_set('forum_id', $dest_forum_ids);
220
		$result = $this->db->sql_query($sql);
221
222
		$dest_forum_ids = array();
223
		while ($row = $this->db->sql_fetchrow($result))
224
		{
225
			$dest_forum_ids[]	= (int) $row['forum_id'];
226
		}
227
		$this->db->sql_freeresult($result);
228
229
		// No destination forum exists
230
		if (empty($dest_forum_ids))
231
		{
232
			return false;
233
		}
234
235
		// Get informations about acl options
236
		$sql = 'SELECT auth_option_id FROM ' . ACL_OPTIONS_TABLE . '
237
			WHERE auth_option ' . $this->db->sql_like_expression($this->db->get_any_char() . '_qte_attr_' . $this->db->get_any_char());
238
		$result = $this->db->sql_query($sql);
239
240
		$acl_options_ids = array();
241
		while ($row = $this->db->sql_fetchrow($result))
242
		{
243
			$acl_options_ids[]	= (int) $row['auth_option_id'];
244
		}
245
246
		// From the mysql documentation:
247
		// Prior to MySQL 4.0.14, the target table of the INSERT statement cannot appear
248
		// in the FROM clause of the SELECT part of the query. This limitation is lifted in 4.0.14.
249
		// Due to this we stay on the safe side if we do the insertion "the manual way"
250
251
		// Rowsets we're going to insert
252
		$users_sql_ary = $groups_sql_ary = array();
253
254
		// Query acl users table for source forum data
255
		$sql = 'SELECT user_id, auth_option_id, auth_role_id, auth_setting
256
			FROM ' . ACL_USERS_TABLE . '
257
			WHERE '. $this->db->sql_in_set('auth_option_id', $acl_options_ids) . '
258
				AND forum_id = ' . $src_forum_id;
259
		$result = $this->db->sql_query($sql);
260
261 View Code Duplication
		while ($row = $this->db->sql_fetchrow($result))
262
		{
263
			$row = array(
264
				'user_id'			=> (int) $row['user_id'],
265
				'auth_option_id'	=> (int) $row['auth_option_id'],
266
				'auth_role_id'		=> (int) $row['auth_role_id'],
267
				'auth_setting'		=> (int) $row['auth_setting'],
268
			);
269
270
			foreach ($dest_forum_ids as $dest_forum_id)
271
			{
272
				$users_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
273
			}
274
		}
275
		$this->db->sql_freeresult($result);
276
277
		// Query acl groups table for source forum data
278
		$sql = 'SELECT group_id, auth_option_id, auth_role_id, auth_setting
279
			FROM ' . ACL_GROUPS_TABLE . '
280
			WHERE '. $this->db->sql_in_set('auth_option_id', $acl_options_ids) . '
281
				AND forum_id = ' . $src_forum_id;
282
		$result = $this->db->sql_query($sql);
283
284 View Code Duplication
		while ($row = $this->db->sql_fetchrow($result))
285
		{
286
			$row = array(
287
				'group_id'			=> (int) $row['group_id'],
288
				'auth_option_id'	=> (int) $row['auth_option_id'],
289
				'auth_role_id'		=> (int) $row['auth_role_id'],
290
				'auth_setting'		=> (int) $row['auth_setting'],
291
			);
292
293
			foreach ($dest_forum_ids as $dest_forum_id)
294
			{
295
				$groups_sql_ary[] = $row + array('forum_id' => $dest_forum_id);
296
			}
297
		}
298
		$this->db->sql_freeresult($result);
299
300
		$this->db->sql_transaction('begin');
301
302
		if ($clear_dest_perms)
303
		{
304
			// Clear current permissions of destination forums
305
			$sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
306
				WHERE ' . $this->db->sql_in_set('auth_option_id', $acl_options_ids) . '
307
					AND ' . $this->db->sql_in_set('forum_id', $dest_forum_ids);
308
			$this->db->sql_query($sql);
309
310
			$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
311
				WHERE ' . $this->db->sql_in_set('auth_option_id', $acl_options_ids) . '
312
					AND ' . $this->db->sql_in_set('forum_id', $dest_forum_ids);
313
			$this->db->sql_query($sql);
314
		}
315
		$this->db->sql_multi_insert(ACL_USERS_TABLE, $users_sql_ary);
316
		$this->db->sql_multi_insert(ACL_GROUPS_TABLE, $groups_sql_ary);
317
318
		$this->db->sql_transaction('commit');
319
320
		return true;
321
	}
322
}
323