Completed
Pull Request — master (#13)
by Erwan
01:28
created

qte   D

Complexity

Total Complexity 95

Size/Duplication

Total Lines 605
Duplicated Lines 11.9 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 95
c 3
b 0
f 0
lcom 1
cbo 0
dl 72
loc 605
rs 4.8519

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A get_users_by_topic_id() 0 22 3
A get_attr_name_by_id() 0 4 1
A get_users_by_user_id() 0 20 3
D attr_select() 0 47 16
A attr_search() 0 17 3
B attr_sort() 24 24 6
B attr_default() 24 24 5
C attr_display() 0 27 8
B attr_title() 0 23 6
C attr_apply() 12 70 16
D mcp_attr_apply() 12 83 15
A getAttr() 0 4 1
A attr_img_key() 0 4 4
A attr_colour() 0 9 4
B _get_attributes() 0 28 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like qte often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use qte, and based on these observations, apply Extract Interface, too.

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
			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
			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
	* @param	string	$mode			Post mode
166
	*
167
	* @return	null
168
	*/
169
	public function attr_select($forum_id, $author_id = 0, $attribute_id = 0, $viewtopic_url = '', $mode = '')
170
	{
171
		$show_select	= false;
172
		$current_time	= time();
173
		$can_edit		= (bool) $this->auth->acl_get('m_qte_attr_edit', $forum_id);
174
		$can_remove		= (bool) $this->auth->acl_get('m_qte_attr_del', $forum_id);
175
		$is_author		= (bool) ($this->user->data['is_registered'] && $this->user->data['user_id'] == $author_id);
176
177
		if ($can_edit || $is_author || $mode == 'post')
178
		{
179
			foreach ($this->_attr as $attr)
180
			{
181
				if (!$this->auth->acl_get('f_qte_attr_'.$attr['attr_id'], $forum_id))
182
				{
183
					continue;
184
				}
185
186
				// show the selector !
187
				$show_select = true;
188
189
				// parse the attribute name
190
				$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']));
191
192
				$this->template->assign_block_vars('attributes', array(
193
					'QTE_ID'		=> $attr['attr_id'],
194
					'QTE_NAME'		=> $attribute_name,
195
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
196
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
197
198
					'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
199
200
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
201
					'U_QTE_URL'		=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => $attr['attr_id'])) : false,
202
				));
203
			}
204
		}
205
206
		$this->template->assign_vars(array(
207
			'S_QTE_SELECT'		=> ($show_select || $can_remove && ($attribute_id || !$author_id)),
208
			'S_QTE_REMOVE'		=> $can_remove,
209
			'S_QTE_EMPTY'		=> (empty($attribute_id)),
210
			'S_QTE_SELECTED'	=> ($can_remove && ($attribute_id == self::REMOVE)),
211
			'S_QTE_KEEP'		=> !empty($attribute_id) && ($attribute_id == self::KEEP),
212
213
			'U_QTE_URL'			=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => self::REMOVE)) : false,
214
		));
215
	}
216
217
	/**
218
	* Generate a list of all attributes for search page
219
	*
220
	* @return	null
221
	*/
222
	public function attr_search()
223
	{
224
		foreach ($this->_attr as $attr)
225
		{
226
			// parse the attribute name
227
			$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']));
228
229
			$this->template->assign_block_vars('attributes', array(
230
				'QTE_ID'		=> $attr['attr_id'],
231
				'QTE_NAME'		=> $attribute_name,
232
				'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
233
				'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
234
235
				'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
236
			));
237
		}
238
	}
239
240
	/**
241
	* Generate a list of attributes for viewforum page
242
	*
243
	* @param	int		$forum_id		Forum id
244
	* @param	int		$attribute_id	Current attribute id
245
	*
246
	* @return	null
247
	*/
248 View Code Duplication
	public function attr_sort($forum_id = 0, $attribute_id = 0)
249
	{
250
		foreach ($this->_attr as $attr)
251
		{
252
			$forum_allowed = $this->auth->acl_getf('f_qte_attr_'.$attr['attr_id'], true);
253
254
			if (isset($forum_allowed[$forum_id]))
255
			{
256
				// parse the attribute name
257
				$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']));
258
259
				$this->template->assign_block_vars('attributes', array(
260
					'QTE_ID'		=> $attr['attr_id'],
261
					'QTE_NAME'		=> $attribute_name,
262
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
263
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
264
265
					'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)) ? true : false,
266
267
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
268
				));
269
			}
270
		}
271
	}
272
273
	/**
274
	* Generate a default attribute list for a forum
275
	*
276
	* @param	int		$forum_id		Forum id
277
	* @param	int		$attribute_id	Current attribute id
278
	*
279
	* @return	null
280
	*/
281 View Code Duplication
	public function attr_default($forum_id = 0, $attribute_id = 0)
282
	{
283
		foreach ($this->_attr as $attr)
284
		{
285
			$forum_allowed = $this->auth->acl_getf('f_qte_attr_'.$attr['attr_id'], true);
286
287
			if (isset($forum_allowed[$forum_id]))
288
			{
289
				// parse the attribute name
290
				$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']));
291
292
				$this->template->assign_block_vars('attributes', array(
293
					'QTE_ID'		=> $attr['attr_id'],
294
					'QTE_NAME'		=> $attribute_name,
295
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
296
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
297
298
					'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
299
300
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
301
				));
302
			}
303
		}
304
	}
305
306
	/**
307
	* Generate attribute for topic title
308
	*
309
	* @param	int		$attribute_id	Current attribute id
310
	* @param	int		$user_id		Current attribute user id
311
	* @param	int		$timestamp		Attribute timestamp
312
	*
313
	* @return	string					Attribute html code
314
	*/
315
	public function attr_display($attribute_id = 0, $user_id = 0, $timestamp = 0)
316
	{
317
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
318
		{
319
			return false;
320
		}
321
322
		if (isset($this->_attr[$attribute_id]))
323
		{
324
			$attribute_colour = $this->attr_colour($this->_attr[$attribute_id]['attr_name'], $this->_attr[$attribute_id]['attr_colour']);
325
326
			if (isset($this->_name[$user_id]['user_id']))
327
			{
328
				$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']);
329
			}
330
			else
331
			{
332
				$attribute_username = $this->user->lang['GUEST'];
333
			}
334
335
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
336
337
			$attribute_name = str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
338
339
			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);
340
		}
341
	}
342
343
	/**
344
	* Generate attribute for page title
345
	*
346
	* @param	int		$attribute_id	Current attribute id
347
	* @param	int		$user_id		Current attribute user id
348
	* @param	int		$timestamp		Attribute timestamp
349
	*
350
	* @return	string					attribute html code
351
	*/
352
	public function attr_title($attribute_id = 0, $user_id = 0, $timestamp = 0)
353
	{
354
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
355
		{
356
			return false;
357
		}
358
359
		if (isset($this->_attr[$attribute_id]))
360
		{
361
			if (isset($this->_name[$user_id]['user_id']))
362
			{
363
				$attribute_username = get_username_string('username', $this->_name[$user_id]['user_id'], $this->_name[$user_id]['username'], $this->_name[$user_id]['user_colour']);
364
			}
365
			else
366
			{
367
				$attribute_username = $this->user->lang['GUEST'];
368
			}
369
370
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
371
372
			return str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
373
		}
374
	}
375
376
377
	/**
378
	* Change topic attribute
379
	*
380
	* @param	int		$attribute_id		New attribute id
381
	* @param	int		$topic_id			The id of the topic
382
	* @param	int		$forum_id			The id of the forum
383
	* @param	int		$topic_attribute	Current attribute id
384
	* @param	int		$author_id			Topic author id
385
	* @param	string	$viewtopic_url		URL to the topic page
386
	*
387
	* @return	null
388
	*/
389
	public function attr_apply($attribute_id = 0, $topic_id = 0, $forum_id = 0, $topic_attribute = 0, $author_id = 0, $viewtopic_url = '')
390
	{
391
		if (empty($topic_id) || empty($forum_id) || empty($attribute_id))
392
		{
393
			return;
394
		}
395
396
		$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;
397
		$can_remove		= $this->auth->acl_get('m_qte_attr_del', $forum_id);
398
399 View Code Duplication
		if (!$can_edit && $attribute_id != self::REMOVE || !$can_remove && $attribute_id == self::REMOVE)
400
		{
401
			return;
402
		}
403
404
		// Default values
405
		$fields = array('topic_attr_id' => 0, 'topic_attr_user'	=> 0, 'topic_attr_time'	=> 0);
406
407
		// time !
408
		$current_time = time();
409
410 View Code Duplication
		if ($attribute_id != self::REMOVE)
411
		{
412
			$fields = array(
413
				'topic_attr_id'		=> $attribute_id,
414
				'topic_attr_user'	=> $this->user->data['user_id'],
415
				'topic_attr_time'	=> $current_time,
416
			);
417
		}
418
419
		$sql = 'UPDATE ' . TOPICS_TABLE . '
420
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
421
			WHERE topic_id = ' . (int) $topic_id;
422
		$this->db->sql_query($sql);
423
424
		$sql = 'SELECT topic_id
425
			FROM ' . TOPICS_TABLE . '
426
			WHERE topic_moved_id = ' . (int) $topic_id;
427
		$result = $this->db->sql_query($sql);
428
		$shadow_topic_id = (int) $this->db->sql_fetchfield('topic_id');
429
		$this->db->sql_freeresult($result);
430
431
		if (!empty($shadow_topic_id))
432
		{
433
			$sql = 'UPDATE ' . TOPICS_TABLE . '
434
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
435
				WHERE topic_id = ' . $shadow_topic_id;
436
			$this->db->sql_query($sql);
437
		}
438
439
		meta_refresh(2, $viewtopic_url);
440
441
		$message = $this->user->lang['QTE_ATTRIBUTE_' . ($attribute_id == -1 ? 'REMOVED' : (empty($topic_attribute) ? 'ADDED' : 'UPDATED'))];
442
443
		if ($this->request->is_ajax())
444
		{
445
			$json_response = new \phpbb\json_response;
446
			$json_response->send(array(
447
				'success' => true,
448
449
				'MESSAGE_TITLE'	=> $this->user->lang['INFORMATION'],
450
				'MESSAGE_TEXT'	=> $message,
451
				'NEW_ATTRIBUTE'	=> $this->attr_display($attribute_id, $this->user->data['user_id'], $current_time),
452
			));
453
		}
454
455
		$message .= '<br /><br />' . $this->user->lang('RETURN_PAGE', '<a href="' . $viewtopic_url . '">', '</a>');
456
457
		trigger_error($message);
458
	}
459
460
	/**
461
	* Change topic attribute in mcp
462
	*
463
	* @param	int		$attribute_id		New attribute id
464
	* @param	int		$forum_id			The id of the forum
465
	* @param	array	$topic_ids			Topics ids
466
	*
467
	* @return	null
468
	*/
469
	public function mcp_attr_apply($attribute_id = 0, $forum_id = 0, $topic_ids = array())
470
	{
471
		$can_edit		= $this->auth->acl_get('m_qte_attr_edit', $forum_id);
472
		$can_remove		= $this->auth->acl_get('m_qte_attr_del', $forum_id);
473
474 View Code Duplication
		if (!$can_edit && $attribute_id != self::REMOVE || !$can_remove && $attribute_id == self::REMOVE)
475
		{
476
			return;
477
		}
478
479
		if (!sizeof($topic_ids))
480
		{
481
			trigger_error('NO_TOPIC_SELECTED');
482
		}
483
484
		if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id'))
485
		{
486
			return;
487
		}
488
489
		// Default values
490
		$fields = array('topic_attr_id' => 0, 'topic_attr_user'	=> 0, 'topic_attr_time'	=> 0);
491
492
		// time !
493
		$current_time = time();
494
495 View Code Duplication
		if ($attribute_id != self::REMOVE)
496
		{
497
			$fields = array(
498
				'topic_attr_id'		=> $attribute_id,
499
				'topic_attr_user'	=> $this->user->data['user_id'],
500
				'topic_attr_time'	=> $current_time,
501
			);
502
		}
503
504
		$sql = 'SELECT topic_id, forum_id, topic_title, topic_attr_id
505
			FROM ' . TOPICS_TABLE . '
506
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
507
		$result = $this->db->sql_query($sql);
508
509
		// log this action
510
		while ($row = $this->db->sql_fetchrow($result))
511
		{
512
			$message = ($attribute_id == -1) ? 'REMOVED' : (empty($row['topic_attr_id']) ? 'ADDED' : 'UPDATED');
513
			$additional_data = array(
514
				'forum_id'	=> $row['forum_id'],
515
				'topic_id'	=> $row['topic_id'],
516
				$row['topic_title'],
517
			);
518
			$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, 'MCP_ATTRIBUTE_' . $message, $current_time, $additional_data);
519
		}
520
		$this->db->sql_freeresult($result);
521
522
		$sql = 'UPDATE ' . TOPICS_TABLE . '
523
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
524
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
525
		$this->db->sql_query($sql);
526
527
		$sql = 'SELECT topic_id
528
			FROM ' . TOPICS_TABLE . '
529
			WHERE ' . $this->db->sql_in_set('topic_moved_id', array_map('intval', $topic_ids));
530
		$result = $this->db->sql_query($sql);
531
532
		$shadow_topic_ids = array();
533
		while ($row = $this->db->sql_fetchrow($result))
534
		{
535
			$shadow_topic_ids[] = (int) $row['topic_id'];
536
		}
537
		$this->db->sql_freeresult($result);
538
539
		if (sizeof($shadow_topic_ids))
540
		{
541
			$sql = 'UPDATE ' . TOPICS_TABLE . '
542
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
543
				WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $shadow_topic_ids));
544
			$this->db->sql_query($sql);
545
		}
546
547
		$redirect = $this->request->variable('redirect', $this->user->data['session_page']);
548
549
		meta_refresh(3, $redirect);
550
		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>'));
551
	}
552
553
	/**
554
	* Getter...
555
	*
556
	* @return	array
557
	*/
558
	public function getAttr()
559
	{
560
		return $this->_attr;
561
	}
562
563
	// borrowed from "Categories Hierarchy" : used to check if a image key exists
564
	public function attr_img_key($key, $alt)
565
	{
566
		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 . '" />');
567
	}
568
569
	/**
570
	* Build class and style attribute
571
	*
572
	* @param	string	$a_name			Attribute name
573
	* @param	string	$a_colour		Attribute color
574
	* @return	string					html code
575
	*/
576
	public function attr_colour($a_name, $a_colour)
577
	{
578
		if ($a_name != $this->user->lang($a_name))
579
		{
580
			$a_class_name = preg_replace('#[^a-z0-9 _-]#', '', strtolower($a_name));
581
		}
582
583
		return ' class="qte-attr ' . (isset($a_class_name) ?  $a_class_name : '') . '"' . (!empty($a_colour) ? ' style="color:#' . $a_colour . '; font-weight:bold;"' : '');
584
	}
585
586
	/**
587
	* Get attributes from database
588
	*
589
	* @return	null
590
	*/
591
	private function _get_attributes()
592
	{
593
		if (($this->_attr = $this->cache->get('_attr')) === false)
594
		{
595
			$sql = 'SELECT *
596
				FROM ' . $this->table_prefix . 'topics_attr
597
				ORDER BY left_id ASC';
598
			$result = $this->db->sql_query($sql);
599
600
			$this->_attr = array();
601
			while ($row = $this->db->sql_fetchrow($result))
602
			{
603
				$this->_attr[$row['attr_id']] = array(
604
					'attr_id'			=> (int) $row['attr_id'],
605
					'attr_type'			=> (bool) $row['attr_type'],
606
					'attr_name'			=> $row['attr_name'],
607
					'attr_desc'			=> $row['attr_desc'],
608
					'attr_img'			=> $row['attr_img'],
609
					'attr_colour'		=> $row['attr_colour'],
610
					'attr_date'			=> $row['attr_date'],
611
					'attr_user_colour'	=> (bool) $row['attr_user_colour'],
612
				);
613
			}
614
			$this->db->sql_freeresult();
615
616
			$this->cache->put('_attr', $this->_attr);
617
		}
618
	}
619
}
620