Completed
Push — patch_1-1-4 ( 567711...ded5b7 )
by Emanuele
17s
created

MessageTopicIcons::_setUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
ccs 6
cts 10
cp 0.6
crap 3.576
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * General class for setting the message icon array and returning index values
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.1.4
11
 *
12
 */
13
14
/**
15
 * Class MessageTopicIcons
16
 */
17
class MessageTopicIcons extends ElkArte\ValuesContainer
18
{
19
	const IMAGE_URL = 'images_url';
20
	const DEFAULT_URL = 'default_images_url';
21
22
	/**
23
	 * Whether to check if the icon exists in the expected location
24
	 * @var bool
25
	 */
26
	protected $_check = false;
27
28
	/**
29
	 * Theme directory path
30
	 * @var string
31
	 */
32
	protected $_theme_dir = '';
33
34
	/**
35
	 * Default icon code
36
	 * @var string
37
	 */
38
	protected $_default_icon = 'xx';
39
40
	/**
41
	 * Icons that are default with ElkArte
42
	 * @var array
43
	 */
44
	protected $_stable_icons = 	array();
45
46
	/**
47
	 * Icons to load in addition to the default
48
	 * @var array
49
	 */
50
	protected $_custom_icons = array();
51
52
	/**
53
	 * This simple function returns the message topic icon array.
54
	 *
55
	 * @param bool|false $icon_check
56
	 * @param string $theme_dir
57
	 * @param array topic icons to load in addition to default
58
	 * @param string $default
59
	 */
60 1
	public function __construct($icon_check = false, $theme_dir = '', $custom = array(), $default = 'xx')
61
	{
62 1
		parent::__construct();
63
64
		// Load passed parameters to the class properties
65 1
		$this->_check = $icon_check;
66 1
		$this->_theme_dir = $theme_dir;
67 1
		$this->_default_icon = $default;
68 1
		$this->_custom_icons = $custom;
69
70
		// Set default icons
71 1
		$this->_loadStableIcons();
72
73
		// Merge in additional ones
74 1
		$custom_icons = array_map(function($element) {
75 1
			return $element['first_icon'];
76 1
		}, $custom);
77 1
		$this->_stable_icons = array_merge($this->_stable_icons, $custom_icons);
78
79 1
		$this->_loadIcons();
80 1
	}
81
82
	/**
83
	 * Return the icon specified by idx, or the default icon for invalid names
84
	 *
85
	 * @param int|string $idx
86
	 * @return string
87
	 */
88 1
	public function __get($idx)
89
	{
90
		// Not a standard topic icon
91 1
		if (!isset($this->data[$idx]))
92 1
		{
93
			$this->_setUrl($idx);
94
		}
95
96 1
		return $this->data[$idx];
97
	}
98
99
	/**
100
	 * Load the stable icon array
101
	 */
102 1
	protected function _loadStableIcons()
103
	{
104
		// Setup the default topic icons...
105 1
		$this->_stable_icons = array(
106 1
			'xx',
107 1
			'thumbup',
108 1
			'thumbdown',
109 1
			'exclamation',
110 1
			'question',
111 1
			'lamp',
112 1
			'smiley',
113 1
			'angry',
114 1
			'cheesy',
115 1
			'grin',
116 1
			'sad',
117 1
			'wink',
118 1
			'poll',
119 1
			'moved',
120 1
			'recycled',
121 1
			'wireless',
122
			'clip'
123 1
		);
124 1
	}
125
126
	/**
127
	 * This simple function returns the message topic icon array.
128
	 */
129 1
	protected function _loadIcons()
130
	{
131
		// Allow addons to add to the message icon array
132 1
		call_integration_hook('integrate_messageindex_icons', array(&$this->_stable_icons));
133
134 1
		$this->data = array();
135 1
		foreach ($this->_stable_icons as $icon)
136
		{
137 1
			$this->_setUrl($icon);
138 1
		}
139 1
	}
140
141
	/**
142
	 * Set the icon URL location
143
	 *
144
	 * @param string $icon
145
	 */
146 1
	protected function _setUrl($icon)
147
	{
148 1
		global $settings;
149
150 1
		if ($this->_check)
151 1
		{
152
			$this->data[$icon] = $settings[file_exists($this->_theme_dir . '/images/post/' . $icon . '.png')
153
				 ? self::IMAGE_URL
154
				 : self::DEFAULT_URL] . '/post/' . $icon . '.png';
155
		}
156
		else
157
		{
158 1
			$this->data[$icon] = $settings[self::IMAGE_URL] . '/post/' . $icon . '.png';
159
		}
160
	}
161
}