Completed
Push — #12 ( cef8fd...5132ea )
by Erwan
02:09
created

qte::mcp_attr_apply()   D

Complexity

Conditions 15
Paths 83

Size

Total Lines 83
Code Lines 48

Duplication

Lines 12
Ratio 14.46 %

Importance

Changes 0
Metric Value
dl 12
loc 83
rs 4.9121
c 0
b 0
f 0
cc 15
eloc 48
nc 83
nop 3

How to fix   Long Method    Complexity   

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\db\driver\driver_interface */
27
	protected $db;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var \phpbb\user */
33
	protected $user;
34
35
	/** @var \phpbb\log\log */
36
	protected $log;
37
38
	/** @var \phpbb\auth\auth */
39
	protected $auth;
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;
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\db\driver\driver_interface 		$db					Database object
62
	* @param \phpbb\template\template				$template			Template object
63
	* @param \phpbb\user							$user				User object
64
	* @param \phpbb\log\log							$log				Log object
65
	* @param \phpbb\auth\auth						$auth				Auth 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\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\log\log $log, \phpbb\auth\auth $auth, $root_path, $php_ext, $table_prefix)
71
	{
72
		$this->request		= $request;
73
		$this->cache		= $cache;
74
		$this->db			= $db;
75
		$this->template		= $template;
76
		$this->user			= $user;
77
		$this->log			= $log;
78
		$this->auth			= $auth;
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 View Code Duplication
			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
		if (!isset($this->_name[$user_id]))
140
		{
141
			$sql = 'SELECT user_id, username, user_colour
142
				FROM ' . USERS_TABLE . '
143
				WHERE user_id = ' . (int) $user_id;
144
			$result = $this->db->sql_query($sql);
145
146 View Code Duplication
			while($row = $this->db->sql_fetchrow($result) )
147
			{
148
				$this->_name[$row['user_id']] = array(
149
					'user_id'		=> (int) $row['user_id'],
150
					'username'		=> $row['username'],
151
					'user_colour'	=> $row['user_colour'],
152
				);
153
			}
154
			$this->db->sql_freeresult();
155
		}
156
	}
157
158
	/**
159
	* Generate a list of attributes based on permissions
160
	*
161
	* @param	int		$forum_id		Forum id
162
	* @param	int		$author_id		Topic author id
163
	* @param	int		$attribute_id	Current attribute id
164
	* @param	string	$viewtopic_url	Topic's url
165
	*
166
	* @return	null
167
	*/
168
	public function attr_select($forum_id, $author_id = 0, $attribute_id = 0, $viewtopic_url = '')
169
	{
170
		$show_select	= false;
171
		$current_time	= time();
172
		$can_edit		= $this->auth->acl_get('m_qte_attr_edit', $forum_id);
173
		$can_remove		= $this->auth->acl_get('m_qte_attr_del', $forum_id);
174
		$is_author		= $this->user->data['is_registered'] && $this->user->data['user_id'] == $author_id;
175
176
		// Basic auth
177
		if (!$can_remove && !$can_edit && !$is_author)
178
		{
179
			return;
180
		}
181
182
		foreach ($this->_attr as $attr)
183
		{
184
			if (!$this->auth->acl_get('f_qte_attr_'.$attr['attr_id'], $forum_id))
185
			{
186
				continue;
187
			}
188
189
			// show the selector !
190
			$show_select = true;
191
192
			// parse the attribute name
193
			$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']));
194
195
			$this->template->assign_block_vars('attributes', array(
196
				'QTE_ID'		=> $attr['attr_id'],
197
				'QTE_NAME'		=> $attribute_name,
198
				'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
199
				'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
200
201
				'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
202
203
				'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
204
				'U_QTE_URL'		=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => $attr['attr_id'])) : false,
205
			));
206
		}
207
208
		$this->template->assign_vars(array(
209
			'S_QTE_SELECT'		=> ($show_select || $can_remove && ($attribute_id || !$author_id)),
210
			'S_QTE_REMOVE'		=> $can_remove,
211
			'S_QTE_EMPTY'		=> (empty($attribute_id)),
212
			'S_QTE_SELECTED'	=> ($can_remove && ($attribute_id == self::REMOVE)),
213
			'S_QTE_KEEP'		=> !empty($attribute_id) && ($attribute_id == self::KEEP),
214
215
			'U_QTE_URL'			=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => self::REMOVE)) : false,
216
		));
217
	}
218
219
	/**
220
	* Generate a list of all attributes for search page
221
	*
222
	* @return	null
223
	*/
224
	public function attr_search()
225
	{
226
		foreach ($this->_attr as $attr)
227
		{
228
			// parse the attribute name
229
			$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']));
230
231
			$this->template->assign_block_vars('attributes', array(
232
				'QTE_ID'		=> $attr['attr_id'],
233
				'QTE_NAME'		=> $attribute_name,
234
				'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
235
				'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
236
237
				'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
238
			));
239
		}
240
	}
241
242
	/**
243
	* Generate a list of attributes for viewforum page
244
	*
245
	* @param	int		$forum_id		Forum id
246
	* @param	int		$attribute_id	Current attribute id
247
	*
248
	* @return	null
249
	*/
250
	public function attr_sort($forum_id = 0, $attribute_id = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $forum_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
251
	{
252
		foreach ($this->_attr as $attr)
253
		{
254
			// parse the attribute name
255
			$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']));
256
257
			$this->template->assign_block_vars('attributes', array(
258
				'QTE_ID'		=> $attr['attr_id'],
259
				'QTE_NAME'		=> $attribute_name,
260
				'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
261
				'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
262
263
				'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)) ? true : false,
264
265
				'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
266
			));
267
		}
268
	}
269
270
	/**
271
	* Generate a default attribute list for a forum
272
	*
273
	* @param	int		$forum_id		Forum id
274
	* @param	int		$attribute_id	Current attribute id
275
	*
276
	* @return	null
277
	*/
278
	public function attr_default($forum_id = 0, $attribute_id = 0)
279
	{
280
		foreach ($this->_attr as $attr)
281
		{
282
			$forum_allowed = $this->auth->acl_getf('f_qte_attr_'.$attr['attr_id'], true);
283
284
			if (isset($forum_allowed[$forum_id]))
285
			{
286
				// parse the attribute name
287
				$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']));
288
289
				$this->template->assign_block_vars('attributes', array(
290
					'QTE_ID'		=> $attr['attr_id'],
291
					'QTE_NAME'		=> $attribute_name,
292
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
293
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
294
295
					'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
296
297
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
298
				));
299
			}
300
		}
301
	}
302
303
	/**
304
	* Generate attribute for topic title
305
	*
306
	* @param	int		$attribute_id	Current attribute id
307
	* @param	int		$user_id		Current attribute user id
308
	* @param	int		$timestamp		Attribute timestamp
309
	*
310
	* @return	string					Attribute html code
311
	*/
312
	public function attr_display($attribute_id = 0, $user_id = 0, $timestamp = 0)
313
	{
314
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
315
		{
316
			return false;
317
		}
318
319
		if (isset($this->_attr[$attribute_id]))
320
		{
321
			$attribute_colour = $this->attr_colour($this->_attr[$attribute_id]['attr_name'], $this->_attr[$attribute_id]['attr_colour']);
322
323
			if (isset($this->_name[$user_id]['user_id']))
324
			{
325
				$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']);
326
			}
327
			else
328
			{
329
				$attribute_username = $this->user->lang['GUEST'];
330
			}
331
332
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
333
334
			$attribute_name = str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
335
336
			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);
337
		}
338
	}
339
340
	/**
341
	* Generate attribute for page title
342
	*
343
	* @param	int		$attribute_id	Current attribute id
344
	* @param	int		$user_id		Current attribute user id
345
	* @param	int		$timestamp		Attribute timestamp
346
	*
347
	* @return	string					attribute html code
348
	*/
349
	public function attr_title($attribute_id = 0, $user_id = 0, $timestamp = 0)
350
	{
351
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
352
		{
353
			return false;
354
		}
355
356
		if (isset($this->_attr[$attribute_id]))
357
		{
358
			if (isset($this->_name[$user_id]['user_id']))
359
			{
360
				$attribute_username = get_username_string('username', $this->_name[$user_id]['user_id'], $this->_name[$user_id]['username'], $this->_name[$user_id]['user_colour']);
361
			}
362
			else
363
			{
364
				$attribute_username = $this->user->lang['GUEST'];
365
			}
366
367
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
368
369
			return str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
370
		}
371
	}
372
373
374
	/**
375
	* Change topic attribute
376
	*
377
	* @param	int		$attribute_id		New attribute id
378
	* @param	int		$topic_id			The id of the topic
379
	* @param	int		$forum_id			The id of the forum
380
	* @param	int		$topic_attribute	Current attribute id
381
	* @param	int		$author_id			Topic author id
382
	* @param	string	$viewtopic_url		URL to the topic page
383
	*
384
	* @return	null
385
	*/
386
	public function attr_apply($attribute_id = 0, $topic_id = 0, $forum_id = 0, $topic_attribute = 0, $author_id = 0, $viewtopic_url = '')
387
	{
388
		if (empty($topic_id) || empty($forum_id) || empty($attribute_id))
389
		{
390
			return;
391
		}
392
393
		$can_edit		= $this->auth->acl_get('m_qte_attr_edit', $forum_id) || $this->auth->acl_get('f_qte_attr_'.$attribute_id, $forum_id) && $this->user->data['is_registered'] && $this->user->data['user_id'] == $author_id;
394
		$can_remove		= $this->auth->acl_get('m_qte_attr_del', $forum_id);
395
396 View Code Duplication
		if (!$can_edit && $attribute_id != self::REMOVE || !$can_remove && $attribute_id == self::REMOVE)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
397
		{
398
			return;
399
		}
400
401
		// Default values
402
		$fields = array('topic_attr_id' => 0, 'topic_attr_user'	=> 0, 'topic_attr_time'	=> 0);
403
404
		// time !
405
		$current_time = time();
406
407 View Code Duplication
		if ($attribute_id != self::REMOVE)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
408
		{
409
			$fields = array(
410
				'topic_attr_id'		=> $attribute_id,
411
				'topic_attr_user'	=> $this->user->data['user_id'],
412
				'topic_attr_time'	=> $current_time,
413
			);
414
		}
415
416
		$sql = 'UPDATE ' . TOPICS_TABLE . '
417
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
418
			WHERE topic_id = ' . (int) $topic_id;
419
		$this->db->sql_query($sql);
420
421
		$sql = 'SELECT topic_id
422
			FROM ' . TOPICS_TABLE . '
423
			WHERE topic_moved_id = ' . (int) $topic_id;
424
		$result = $this->db->sql_query($sql);
425
		$shadow_topic_id = (int) $this->db->sql_fetchfield('topic_id');
426
		$this->db->sql_freeresult($result);
427
428
		if (!empty($shadow_topic_id))
429
		{
430
			$sql = 'UPDATE ' . TOPICS_TABLE . '
431
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
432
				WHERE topic_id = ' . $shadow_topic_id;
433
			$this->db->sql_query($sql);
434
		}
435
436
		meta_refresh(2, $viewtopic_url);
437
438
		$message = $this->user->lang['QTE_ATTRIBUTE_' . ($attribute_id == -1 ? 'REMOVED' : (empty($topic_attribute) ? 'ADDED' : 'UPDATED'))];
439
440
		if ($this->request->is_ajax())
441
		{
442
			//echo $this->attr_display($attribute_id, $this->user->data['user_id'], $current_time); exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
443
			$json_response = new \phpbb\json_response;
444
			$json_response->send(array(
445
				'success' => true,
446
447
				'MESSAGE_TITLE'	=> $this->user->lang['INFORMATION'],
448
				'MESSAGE_TEXT'	=> $message,
449
				'NEW_ATTRIBUTE'	=> $this->attr_display($attribute_id, $this->user->data['user_id'], $current_time),
450
			));
451
		}
452
453
		$message .= '<br /><br />' . $this->user->lang('RETURN_PAGE', '<a href="' . $viewtopic_url . '">', '</a>');
454
455
		trigger_error($message);
456
	}
457
458
	/**
459
	* Change topic attribute in mcp
460
	*
461
	* @param	int		$attribute_id		New attribute id
462
	* @param	int		$forum_id			The id of the forum
463
	* @param	array	$topic_ids			Topics ids
464
	*
465
	* @return	null
466
	*/
467
	public function mcp_attr_apply($attribute_id = 0, $forum_id = 0, $topic_ids = array())
468
	{
469
		$can_edit		= $this->auth->acl_get('m_qte_attr_edit', $forum_id);
470
		$can_remove		= $this->auth->acl_get('m_qte_attr_del', $forum_id);
471
472 View Code Duplication
		if (!$can_edit && $attribute_id != self::REMOVE || !$can_remove && $attribute_id == self::REMOVE)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
473
		{
474
			return;
475
		}
476
477
		if (!sizeof($topic_ids))
478
		{
479
			trigger_error('NO_TOPIC_SELECTED');
480
		}
481
482
		if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id'))
483
		{
484
			return;
485
		}
486
487
		// Default values
488
		$fields = array('topic_attr_id' => 0, 'topic_attr_user'	=> 0, 'topic_attr_time'	=> 0);
489
490
		// time !
491
		$current_time = time();
492
493 View Code Duplication
		if ($attribute_id != self::REMOVE)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
		{
495
			$fields = array(
496
				'topic_attr_id'		=> $attribute_id,
497
				'topic_attr_user'	=> $this->user->data['user_id'],
498
				'topic_attr_time'	=> $current_time,
499
			);
500
		}
501
502
		$sql = 'SELECT topic_id, forum_id, topic_title, topic_attr_id
503
			FROM ' . TOPICS_TABLE . '
504
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
505
		$result = $this->db->sql_query($sql);
506
507
		// log this action
508
		while ($row = $this->db->sql_fetchrow($result))
509
		{
510
			$message = ($attribute_id == -1) ? 'REMOVED' : (empty($row['topic_attr_id']) ? 'ADDED' : 'UPDATED');
511
			$additional_data = array(
512
				'forum_id'	=> $row['forum_id'],
513
				'topic_id'	=> $row['topic_id'],
514
				$row['topic_title'],
515
			);
516
			$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, 'MCP_ATTRIBUTE_' . $message, $current_time, $additional_data);
517
		}
518
		$this->db->sql_freeresult($result);
519
520
		$sql = 'UPDATE ' . TOPICS_TABLE . '
521
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
522
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
523
		$this->db->sql_query($sql);
524
525
		$sql = 'SELECT topic_id
526
			FROM ' . TOPICS_TABLE . '
527
			WHERE ' . $this->db->sql_in_set('topic_moved_id', array_map('intval', $topic_ids));
528
		$result = $this->db->sql_query($sql);
529
530
		$shadow_topic_ids = array();
531
		while ($row = $this->db->sql_fetchrow($result))
532
		{
533
			$shadow_topic_ids[] = (int) $row['topic_id'];
534
		}
535
		$this->db->sql_freeresult($result);
536
537
		if (sizeof($shadow_topic_ids))
538
		{
539
			$sql = 'UPDATE ' . TOPICS_TABLE . '
540
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
541
				WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $shadow_topic_ids));
542
			$this->db->sql_query($sql);
543
		}
544
545
		$redirect = $this->request->variable('redirect', $this->user->data['session_page']);
546
547
		meta_refresh(3, $redirect);
548
		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>'));
549
	}
550
551
	/**
552
	* Getter...
553
	*
554
	* @return	array
555
	*/
556
	public function getAttr()
557
	{
558
		return $this->_attr;
559
	}
560
561
	// borrowed from "Categories Hierarchy" : used to check if a image key exists
562
	public function attr_img_key($key, $alt)
563
	{
564
		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 . '" />');
565
	}
566
567
	/**
568
	* Build class and style attribute
569
	*
570
	* @param	string	$a_name			Attribute name
571
	* @param	string	$a_colour		Attribute color
572
	* @return	string					html code
573
	*/
574
	public function attr_colour($a_name, $a_colour)
575
	{
576
		if ($a_name != $this->user->lang($a_name))
577
		{
578
			$a_class_name = preg_replace('#[^a-z0-9 _-]#', '', strtolower($a_name));
579
		}
580
581
		return ' class="qte-attr ' . (isset($a_class_name) ?  $a_class_name : '') . '"' . (!empty($a_colour) ? ' style="color:#' . $a_colour . '; font-weight:bold;"' : '');
582
	}
583
584
	/**
585
	* Get attributes from database
586
	*
587
	* @return	null
588
	*/
589
	private function _get_attributes()
590
	{
591
		if (($this->_attr = $this->cache->get('_attr')) === false)
592
		{
593
			$sql = 'SELECT *
594
				FROM ' . $this->table_prefix . 'topics_attr
595
				ORDER BY left_id ASC';
596
			$result = $this->db->sql_query($sql);
597
598
			$this->_attr = array();
599
			while ($row = $this->db->sql_fetchrow($result))
600
			{
601
				$this->_attr[$row['attr_id']] = array(
602
					'attr_id'			=> (int) $row['attr_id'],
603
					'attr_type'			=> (bool) $row['attr_type'],
604
					'attr_name'			=> $row['attr_name'],
605
					'attr_desc'			=> $row['attr_desc'],
606
					'attr_img'			=> $row['attr_img'],
607
					'attr_colour'		=> $row['attr_colour'],
608
					'attr_date'			=> $row['attr_date'],
609
					'attr_user_colour'	=> (bool) $row['attr_user_colour'],
610
				);
611
			}
612
			$this->db->sql_freeresult();
613
614
			$this->cache->put('_attr', $this->_attr);
615
		}
616
	}
617
}
618