Completed
Push — master ( 14e3cb...c4bf24 )
by Erwan
02:23
created

qte::attr_default()   C

Complexity

Conditions 9
Paths 14

Size

Total Lines 51
Code Lines 25

Duplication

Lines 51
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 51
loc 51
rs 6.2727
cc 9
eloc 25
nc 14
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
14
15
class qte
16
{
17
	const KEEP = -2;
18
	const REMOVE = -1;
19
20
	/** @var \phpbb\request\request */
21
	protected $request;
22
23
	/** @var \phpbb\cache\driver\driver_interface */
24
	protected $cache;
25
26
	/** @var \phpbb\config\config */
27
	protected $config;
28
29
	/** @var \phpbb\db\driver\driver_interface */
30
	protected $db;
31
32
	/** @var \phpbb\template\template */
33
	protected $template;
34
35
	/** @var \phpbb\user */
36
	protected $user;
37
38
	/** @var \phpbb\log\log */
39
	protected $log;
40
41
	/** @var string */
42
	protected $root_path;
43
44
	/** @var string */
45
	protected $php_ext;
46
47
	/** @var string */
48
	protected $table_prefix;
49
50
	/** @var array */
51
	private $_attr = array();
52
53
	/** @var array */
54
	private $_name = array();
55
56
	/**
57
	* Constructor
58
	*
59
	* @param \phpbb\request\request					$request			Request object
60
	* @param \phpbb\cache\driver\driver_interface	$cache				Cache object
61
	* @param \phpbb\config\config					$config				Config object
62
	* @param \phpbb\db\driver\driver_interface 		$db					Database object
63
	* @param \phpbb\template\template				$template			Template object
64
	* @param \phpbb\user							$user				User object
65
	* @param \phpbb\log\log							$log				Log object
66
	* @param string									$root_path			phpBB root path
67
	* @param string									$php_ext   			phpEx
68
	* @param string									$table_prefix   	Prefix tables
69
	*/
70
	public function __construct(\phpbb\request\request $request, \phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\log\log $log, $root_path, $php_ext, $table_prefix)
71
	{
72
		$this->request		= $request;
73
		$this->cache		= $cache;
74
		$this->config		= $config;
75
		$this->db			= $db;
76
		$this->template		= $template;
77
		$this->user			= $user;
78
		$this->log			= $log;
79
80
		$this->root_path	= $root_path;
81
		$this->php_ext		= $php_ext;
82
		$this->table_prefix = $table_prefix;
83
84
		$this->_get_attributes();
85
		$this->user->add_lang_ext('ernadoo/qte', 'attributes');
86
	}
87
88
	/**
89
	* Get topic attributes username
90
	*
91
	* @param	array	$topic_list	Topic ids
92
	*
93
	* @return	null
94
	*/
95
	public function get_users_by_topic_id($topic_list)
96
	{
97
		if (!empty($topic_list))
98
		{
99
			$sql = 'SELECT u.user_id, u.username, u.user_colour
100
				FROM ' . USERS_TABLE . ' u
101
				LEFT JOIN ' . TOPICS_TABLE . ' t ON (u.user_id = t.topic_attr_user)
102
				WHERE ' . $this->db->sql_in_set('t.topic_id', array_map('intval', $topic_list)) . '
103
					AND t.topic_attr_user <> ' . ANONYMOUS;
104
			$result = $this->db->sql_query($sql);
105
106
			while ($row = $this->db->sql_fetchrow($result))
107
			{
108
				$this->_name[$row['user_id']] = array(
109
					'user_id' => (int) $row['user_id'],
110
					'username' => $row['username'],
111
					'user_colour' => $row['user_colour'],
112
				);
113
			}
114
			$this->db->sql_freeresult();
115
		}
116
	}
117
118
	/**
119
	* Get attribute name
120
	*
121
	* @param	int		$attr_id	The attribute id
122
	*
123
	* @return	string
124
	*/
125
	public function get_attr_name_by_id($attr_id)
126
	{
127
		return $this->_attr[$attr_id]['attr_name'];
128
	}
129
130
	/**
131
	* Get attribute author
132
	*
133
	* @param	int		$user_id	User id
134
	*
135
	* @return	string
136
	*/
137
	public function get_users_by_user_id($user_id)
138
	{
139
		$sql = 'SELECT user_id, username, user_colour
140
			FROM ' . USERS_TABLE . '
141
			WHERE user_id = ' . (int) $user_id;
142
		$result = $this->db->sql_query($sql);
143
144
		$this->_name = array();
145
		while ( $row = $this->db->sql_fetchrow($result) )
146
		{
147
			$this->_name[$row['user_id']] = array(
148
				'user_id'		=> (int) $row['user_id'],
149
				'username'		=> $row['username'],
150
				'user_colour'	=> $row['user_colour'],
151
			);
152
		}
153
		$this->db->sql_freeresult();
154
	}
155
156
	/**
157
	* Generate a list of attributes based on permissions
158
	*
159
	* @param	int		$forum_id		Forum id
160
	* @param	int		$author_id		Topic author id
161
	* @param	int		$attribute_id	Current attribute id
162
	* @param	array	$hide_attr		Groups which can't delete attribute in this forum
163
	* @param	string	$viewtopic_url	Topic's url
164
	*
165
	* @return	null
166
	*/
167
	public function attr_select($forum_id = 0, $author_id = 0, $attribute_id = 0, $hide_attr = array(), $viewtopic_url = '')
168
	{
169
		// get current time once !
170
		$current_time = time();
171
172
		$show_select = false;
173
		$user_groups = array();
174
		$show_remove = $this->_check_auth_remove_attr($user_groups, $hide_attr);
175
176
		foreach ($this->_attr as $attr)
177
		{
178
			if (empty($attr['attr_auths']))
179
			{
180
				$attr_auths = array(array(
181
					'forums_ids'	=> array(),
182
					'groups_ids'	=> array(),
183
					'author'		=> false,
184
				));
185
			}
186
			else
187
			{
188
				$attr_auths = json_decode($attr['attr_auths'], true);
189
			}
190
191
			foreach ($attr_auths as $attr_auth)
192
			{
193
				if (!$this->_check_auth_attribute($attr_auth, $forum_id, $user_groups, $author_id))
194
				{
195
					continue;
196
				}
197
198
				// show the selector !
199
				$show_select = true;
200
201
				// parse the attribute name
202
				$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->data['username'], $this->user->format_date($current_time, $attr['attr_date'])), $this->user->lang($attr['attr_name']));
203
204
				$this->template->assign_block_vars('attributes', array(
205
					'QTE_ID'		=> $attr['attr_id'],
206
					'QTE_TYPE'		=> $attr['attr_type'],
207
					'QTE_NAME'		=> $attribute_name,
208
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
209
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
210
211
					'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
212
213
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
214
					'U_QTE_URL'		=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => $attr['attr_id'])) : false,
215
				));
216
			}
217
		}
218
219
		if ($show_select)
220
		{
221
			$this->template->assign_vars(array(
222
				'S_QTE_SELECT'		=> true,
223
				'S_QTE_REMOVE'		=> $show_remove,
224
				'S_QTE_EMPTY'		=> (empty($attribute_id) || ($attribute_id == -1) || ($attribute_id == -2)),
225
				'S_QTE_SELECTED'	=> ($show_remove && ($attribute_id == -1)),
226
227
				'L_QTE_SELECT'		=> $this->user->lang['QTE_ATTRIBUTE_' . (!empty($attribute_id) ? ($show_remove ? 'REMOVE' : 'RESTRICT') : 'ADD')],
228
				'U_QTE_URL'			=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => -1)) : false,
229
			));
230
		}
231
	}
232
233
	/**
234
	* Generate a list of all attributes for search page
235
	*
236
	* @return	null
237
	*/
238
	public function attr_search()
239
	{
240
		$show_select = false;
241
242
		foreach ($this->_attr as $attr)
243
		{
244
			if (empty($attr['attr_auths']))
245
			{
246
				$attr_auths = array(array(
247
					'forums_ids'	=> array(),
248
					'groups_ids'	=> array(),
249
					'author'		=> false,
250
				));
251
			}
252
			else
253
			{
254
				$attr_auths = json_decode($attr['attr_auths'], true);
255
			}
256
257
			foreach ($attr_auths as $attr_auth)
258
			{
259
				// show the selector !
260
				$show_select = true;
261
262
				// parse the attribute name
263
				$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($attr['attr_name']));
264
265
				$this->template->assign_block_vars('attributes', array(
266
					'QTE_ID'		=> $attr['attr_id'],
267
					'QTE_TYPE'		=> $attr['attr_type'],
268
					'QTE_NAME'		=> $attribute_name,
269
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
270
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
271
272
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
273
				));
274
			}
275
		}
276
277
		if ($show_select)
278
		{
279
			$this->template->assign_var('S_QTE_SELECT', true);
280
		}
281
	}
282
283
	/**
284
	* Generate a list of attributes for viewforum page
285
	*
286
	* @param	int		$forum_id		Forum id
287
	* @param	int		$attribute_id	Current attribute id
288
	*
289
	* @return	null
290
	*/
291 View Code Duplication
	public function attr_sort($forum_id = 0, $attribute_id = 0)
292
	{
293
		$show_select = false;
294
295
		foreach ($this->_attr as $attr)
296
		{
297
			if (empty($attr['attr_auths']))
298
			{
299
				$attr_auths = array(array(
300
					'forums_ids'	=> array(),
301
					'groups_ids'	=> array(),
302
					'author'		=> false,
303
				));
304
			}
305
			else
306
			{
307
				$attr_auths = json_decode($attr['attr_auths'], true);
308
			}
309
310
			foreach ($attr_auths as $attr_auth)
311
			{
312
				$forum_ids = $attr_auth['forums_ids'];
313
314
				if (is_array($forum_ids) && in_array($forum_id, $forum_ids))
315
				{
316
					// show the selector !
317
					$show_select = true;
318
319
					// parse the attribute name
320
					$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($attr['attr_name']));
321
322
					$this->template->assign_block_vars('attributes', array(
323
						'QTE_ID'		=> $attr['attr_id'],
324
						'QTE_TYPE'		=> $attr['attr_type'],
325
						'QTE_NAME'		=> $attribute_name,
326
						'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
327
						'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
328
329
						'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)) ? true : false,
330
331
						'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
332
					));
333
				}
334
			}
335
		}
336
337
		if ($show_select)
338
		{
339
			$this->template->assign_var('S_QTE_SELECT', true);
340
		}
341
	}
342
343
	/**
344
	* Generate a default attribute list for a forum
345
	*
346
	* @param	int		$forum_id		Forum id
347
	* @param	int		$attribute_id	Current attribute id
348
	*
349
	* @return	null
350
	*/
351 View Code Duplication
	public function attr_default($forum_id = 0, $attribute_id = 0)
352
	{
353
		$show_select = false;
354
355
		foreach ($this->_attr as $attr)
356
		{
357
			if (empty($attr['attr_auths']))
358
			{
359
				$attr_auths = array(array(
360
					'forums_ids'	=> array(),
361
					'groups_ids'	=> array(),
362
					'author'		=> false,
363
				));
364
			}
365
			else
366
			{
367
				$attr_auths = json_decode($attr['attr_auths'], true);
368
			}
369
370
			foreach ($attr_auths as $attr_auth)
371
			{
372
				$forum_ids = $attr_auth['forums_ids'];
373
374
				if (is_array($forum_ids) && in_array($forum_id, $forum_ids))
375
				{
376
					// show the selector !
377
					$show_select = true;
378
379
					// parse the attribute name
380
					$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($attr['attr_name']));
381
382
					$this->template->assign_block_vars('attributes', array(
383
						'QTE_ID'		=> $attr['attr_id'],
384
						'QTE_TYPE'		=> $attr['attr_type'],
385
						'QTE_NAME'		=> $attribute_name,
386
						'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
387
						'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
388
389
						'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
390
391
						'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
392
					));
393
				}
394
			}
395
		}
396
397
		if ($show_select)
398
		{
399
			$this->template->assign_var('S_QTE_SELECT', true);
400
		}
401
	}
402
403
	/**
404
	* Generate attribute for topic title
405
	*
406
	* @param	int		$attribute_id	Current attribute id
407
	* @param	int		$user_id		Current attribute user id
408
	* @param	int		$timestamp		Attribute timestamp
409
	*
410
	* @return	string					Attribute html code
411
	*/
412
	public function attr_display($attribute_id = 0, $user_id = 0, $timestamp = 0)
413
	{
414
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
415
		{
416
			return false;
417
		}
418
419
		if (isset($this->_attr[$attribute_id]))
420
		{
421
			$attribute_colour = $this->attr_colour($this->_attr[$attribute_id]['attr_name'], $this->_attr[$attribute_id]['attr_colour']);
422
423
			if (isset($this->_name[$user_id]['user_id']))
424
			{
425
				$attribute_username = get_username_string(($this->_attr[$attribute_id]['attr_user_colour'] ? 'no_profile' : 'username'), $this->_name[$user_id]['user_id'], $this->_name[$user_id]['username'], $this->_name[$user_id]['user_colour']);
426
			}
427
			else
428
			{
429
				$attribute_username = $this->user->lang['GUEST'];
430
			}
431
432
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
433
434
			$attribute_name = str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
435
436
			return !$this->_attr[$attribute_id]['attr_type'] ? '<span' . $attribute_colour . '>' . $attribute_name . '</span>' : $this->attr_img_key($this->_attr[$attribute_id]['attr_img'], $attribute_name);
437
		}
438
	}
439
440
	/**
441
	* Generate attribute for page title
442
	*
443
	* @param	int		$attribute_id	Current attribute id
444
	* @param	int		$user_id		Current attribute user id
445
	* @param	int		$timestamp		Attribute timestamp
446
	*
447
	* @return	string					attribute html code
448
	*/
449
	public function attr_title($attribute_id = 0, $user_id = 0, $timestamp = 0)
450
	{
451
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
452
		{
453
			return false;
454
		}
455
456
		if (isset($this->_attr[$attribute_id]))
457
		{
458
			if (isset($this->_name[$user_id]['user_id']))
459
			{
460
				$attribute_username = get_username_string('username', $this->_name[$user_id]['user_id'], $this->_name[$user_id]['username'], $this->_name[$user_id]['user_colour']);
461
			}
462
			else
463
			{
464
				$attribute_username = $this->user->lang['GUEST'];
465
			}
466
467
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
468
469
			$attribute_name = str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
470
471
			return $attribute_name;
472
		}
473
	}
474
475
476
	/**
477
	* Change topic attribute
478
	*
479
	* @param	int		$attribute_id		New attribute id
480
	* @param	int		$topic_id			The id of the topic
481
	* @param	int		$forum_id			The id of the forum
482
	* @param	int		$topic_attribute	Current attribute id
483
	* @param	array	$hide_attr			Groups which can't delete attribute in this forum
484
	*
485
	* @return	null
486
	*/
487
	public function attr_apply($attribute_id = 0, $topic_id = 0, $forum_id = 0, $topic_attribute = 0, $hide_attr = array())
488
	{
489
		if (empty($topic_id) || empty($forum_id) || empty($attribute_id))
490
		{
491
			return;
492
		}
493
494
		if ($attribute_id == \ernadoo\qte\qte::REMOVE && !$this->_check_auth_remove_attr($user_groups, $hide_attr))
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
495
		{
496
			return;
497
		}
498
499
		// time !
500
		$current_time = time();
501
502
		if ($attribute_id == \ernadoo\qte\qte::REMOVE)
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
503
		{
504
			$fields = array(
505
				'topic_attr_id'		=> 0,
506
				'topic_attr_user'	=> 0,
507
				'topic_attr_time'	=> 0,
508
			);
509
		}
510
		else
511
		{
512
			$fields = array(
513
				'topic_attr_id'		=> $attribute_id,
514
				'topic_attr_user'	=> $this->user->data['user_id'],
515
				'topic_attr_time'	=> $current_time,
516
			);
517
		}
518
519
		$sql = 'UPDATE ' . TOPICS_TABLE . '
520
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
521
			WHERE topic_id = ' . (int) $topic_id;
522
		$this->db->sql_query($sql);
523
524
		$sql = 'SELECT topic_id
525
			FROM ' . TOPICS_TABLE . '
526
			WHERE topic_moved_id = ' . (int) $topic_id;
527
		$result = $this->db->sql_query($sql);
528
		$shadow_topic_id = (int) $this->db->sql_fetchfield('topic_id');
529
		$this->db->sql_freeresult($result);
530
531
		if (!empty($shadow_topic_id))
532
		{
533
			$sql = 'UPDATE ' . TOPICS_TABLE . '
534
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
535
				WHERE topic_id = ' . $shadow_topic_id;
536
			$this->db->sql_query($sql);
537
		}
538
539
		$meta_url = append_sid($this->root_path . 'viewtopic.' . $this->php_ext, array('f' => $forum_id, 't' => $topic_id));
540
		meta_refresh(3, $meta_url);
541
542
		// load language
543
		$this->user->add_lang('posting');
544
545
		$message = $this->user->lang['QTE_ATTRIBUTE_' . ($attribute_id == -1 ? 'REMOVED' : (empty($topic_attribute) ? 'ADDED' : 'UPDATED'))] . '<br /><br />' . sprintf($this->user->lang['VIEW_MESSAGE'], '<a href="' . $meta_url . '">', '</a>');
546
		$message .= '<br /><br />' . sprintf($this->user->lang['RETURN_FORUM'], '<a href="' . append_sid($this->root_path . 'viewforum.' . $this->php_ext, array('f' => $forum_id)) . '">', '</a>');
547
548
		if ($this->request->is_ajax())
549
		{
550
			$json_response = new \phpbb\json_response;
551
			$json_response->send(array(
552
				'success' => true,
553
554
				'MESSAGE_TITLE'	=> $this->user->lang['INFORMATION'],
555
				'MESSAGE_TEXT'	=> $message,
556
				'NEW_ATTRIBUTE'	=> $this->attr_display($attribute_id, $this->user->data['user_id'], $current_time),
557
			));
558
		}
559
560
		trigger_error($message);
561
	}
562
563
	/**
564
	* Change topic attribute in mcp
565
	*
566
	* @param	int		$attribute_id		New attribute id
567
	* @param	array	$topic_ids			Topics ids
568
	*
569
	* @return	null
570
	*/
571
	public function mcp_attr_apply($attribute_id = 0, $topic_ids = array())
572
	{
573
		if (!sizeof($topic_ids))
574
		{
575
			trigger_error('NO_TOPIC_SELECTED');
576
		}
577
578
		if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id'))
579
		{
580
			return;
581
		}
582
583
		// time !
584
		$current_time = time();
585
586
		$sql = 'SELECT topic_id, forum_id, topic_title, topic_attr_id
587
			FROM ' . TOPICS_TABLE . '
588
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
589
		$result = $this->db->sql_query($sql);
590
591
		// log this action
592
		while ($row = $this->db->sql_fetchrow($result))
593
		{
594
			$message = ($attribute_id == -1) ? 'REMOVED' : (empty($row['topic_attr_id']) ? 'ADDED' : 'UPDATED');
595
			$additional_data = array(
596
				'forum_id'	=> $row['forum_id'],
597
				'topic_id'	=> $row['topic_id'],
598
				$row['topic_title'],
599
			);
600
			$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, 'MCP_ATTRIBUTE_' . $message, $current_time, $additional_data);
601
		}
602
		$this->db->sql_freeresult($result);
603
604
		if ($attribute_id == -1)
605
		{
606
			$fields = array(
607
				'topic_attr_id'		=> 0,
608
				'topic_attr_user'	=> 0,
609
				'topic_attr_time'	=> 0,
610
			);
611
		}
612
		else
613
		{
614
			$fields = array(
615
				'topic_attr_id'		=> $attribute_id,
616
				'topic_attr_user'	=> $this->user->data['user_id'],
617
				'topic_attr_time'	=> $current_time,
618
			);
619
		}
620
621
		$sql = 'UPDATE ' . TOPICS_TABLE . '
622
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
623
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
624
		$this->db->sql_query($sql);
625
626
		$sql = 'SELECT topic_id
627
			FROM ' . TOPICS_TABLE . '
628
			WHERE ' . $this->db->sql_in_set('topic_moved_id', array_map('intval', $topic_ids));
629
		$result = $this->db->sql_query($sql);
630
631
		$shadow_topic_ids = array();
632
		while ($row = $this->db->sql_fetchrow($result))
633
		{
634
			$shadow_topic_ids[] = (int) $row['topic_id'];
635
		}
636
		$this->db->sql_freeresult($result);
637
638
		if (sizeof($shadow_topic_ids))
639
		{
640
			$sql = 'UPDATE ' . TOPICS_TABLE . '
641
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
642
				WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $shadow_topic_ids));
643
			$this->db->sql_query($sql);
644
		}
645
646
		$redirect = $this->request->variable('redirect', $this->user->data['session_page']);
647
648
		meta_refresh(3, $redirect);
649
		trigger_error($this->user->lang['QTE_TOPIC' . (sizeof($topic_ids) == 1 ? '' : 'S') . '_ATTRIBUTE_' . (isset($message) ? $message : 'ADDED')] . '<br /><br />' . sprintf($this->user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
650
651
		return;
652
	}
653
654
	/**
655
	* Getter...
656
	*
657
	* @return	array
658
	*/
659
	public function getAttr()
660
	{
661
		return $this->_attr;
662
	}
663
664
	/**
665
	* Generate list of groups
666
	*
667
	* @param int		$group_ids		The default groups id to mark as selected
668
	* @param array|bool	$exclude_ids	The group ids to exclude from the list, false (default) if you whish to exclude no id
669
	* @param bool		$manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only.
670
	*
671
	* @return string The list of options.
672
	*/
673
	public function qte_group_select($group_ids, $exclude_ids = array(), $manage_founder = false)
674
	{
675
		$exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $this->db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
676
		$sql_and = !$this->config['coppa_enable'] ? ($exclude_sql ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
677
		$sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
678
679
		$sql = 'SELECT group_id, group_name, group_type
680
			FROM ' . GROUPS_TABLE . "
681
			$exclude_sql
682
			$sql_and
683
			$sql_founder
684
			ORDER BY group_type DESC, group_name ASC";
685
		$result = $this->db->sql_query($sql);
686
687
		$s_group_options = '';
688
		while ($row = $this->db->sql_fetchrow($result))
689
		{
690
			$selected = in_array($row['group_id'], $group_ids) ? ' selected="selected"' : '';
691
			$s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
692
		}
693
		$this->db->sql_freeresult($result);
694
695
		return $s_group_options;
696
	}
697
698
	// borrowed from "Categories Hierarchy" : used to check if a image key exists
699
	public function attr_img_key($key, $alt)
700
	{
701
		return empty($key) ? '' : (preg_match('#^[a-z0-9_-]+$#i', $key) ? $this->user->img($key, $alt) : '<img src="' . (preg_match('#^(ht|f)tp[s]?\://#i', $key) ? $key : $this->root_path . $key) . '" alt="' . $alt . '" title="' . $alt . '" />');
702
	}
703
704
	/**
705
	* Build class and style attribute
706
	*
707
	* @param	string	$a_name			Attribute name
708
	* @param	string	$a_colour		Attribute color
709
	* @return	string					html code
710
	*/
711
	public function attr_colour($a_name, $a_colour)
712
	{
713
		$a_name = preg_replace('#[^a-z0-9 _-]#', '', strtolower($a_name));
714
		if (!empty($a_name))
715
		{
716
			$a_name .= '-qte';
717
		}
718
719
		return ' class="qte-attr ' . $a_name . '"' . (!empty($a_colour) ? ' style="color:#' . $a_colour . '; font-weight:bold;"' : '');
720
	}
721
722
	/**
723
	* Check if user can apply an attribute
724
	*
725
	* @param	array	$attr_auth		Forum auth
726
	* @param	int		$forum_id		Forum id
727
	* @param	array	$user_groups	User's groups
728
	* @param	int		$author_id		Topic author id
729
	* @return	bool
730
	*/
731
	private function _check_auth_attribute($attr_auth, $forum_id, $user_groups, $author_id)
732
	{
733
		$forum_ids = $attr_auth['forums_ids'];
734
		$group_ids = $attr_auth['groups_ids'];
735
736
		if (is_array($forum_ids) && in_array($forum_id, $forum_ids))
737
		{
738
			if (is_array($group_ids) && array_intersect($group_ids, $user_groups) || ($attr_auth['author'] && ($author_id == $this->user->data['user_id']) && ($this->user->data['user_id'] != ANONYMOUS)))
739
			{
740
				return true;
741
			}
742
		}
743
744
		return false;
745
	}
746
747
	/**
748
	* Check if user can delete an attribute
749
	*
750
	* @param	array	$user_groups	User's groups
751
	* @param	array	$hide_attr		Groups which can't delete attribute in a forum
752
	* @return	bool
753
	*/
754
	private function _check_auth_remove_attr(&$user_groups, $hide_attr)
755
	{
756
		// include that file !
757
		if (!function_exists('group_memberships'))
758
		{
759
			include $this->root_path . 'includes/functions_user.' . $this->php_ext;
760
		}
761
762
		// get groups membership !
763
		$user_membership = group_memberships(false, $this->user->data['user_id']);
764
765
		$user_groups = array();
766
		if (!empty($user_membership))
767
		{
768
			foreach ($user_membership as $row)
769
			{
770
				$user_groups[$row['group_id']] = (int) $row['group_id'];
771
			}
772
		}
773
774
		$groups_removed = array_intersect($user_groups, $hide_attr);
775
		return (empty($hide_attr) || (count($groups_removed) < count($user_groups)));
776
	}
777
778
	/**
779
	* Get attributes from database
780
	*
781
	* @return	null
782
	*/
783
	private function _get_attributes()
784
	{
785
		if (($this->_attr = $this->cache->get('_attr')) === false)
786
		{
787
			$sql = 'SELECT *
788
				FROM ' . $this->table_prefix . 'topics_attr
789
				ORDER BY left_id ASC';
790
			$result = $this->db->sql_query($sql);
791
792
			$this->_attr = array();
793
			while ($row = $this->db->sql_fetchrow($result))
794
			{
795
				$this->_attr[$row['attr_id']] = array(
796
					'attr_id'			=> (int) $row['attr_id'],
797
					'attr_type'			=> (bool) $row['attr_type'],
798
					'attr_name'			=> $row['attr_name'],
799
					'attr_desc'			=> $row['attr_desc'],
800
					'attr_img'			=> $row['attr_img'],
801
					'attr_colour'		=> $row['attr_colour'],
802
					'attr_date'			=> $row['attr_date'],
803
					'attr_user_colour'	=> (bool) $row['attr_user_colour'],
804
					'attr_auths'		=> $row['attr_auths'],
805
				);
806
			}
807
			$this->db->sql_freeresult();
808
809
			$this->cache->put('_attr', $this->_attr);
810
		}
811
	}
812
}
813