Passed
Push — patch_1-1-7 ( b8231c...f1963e )
by Emanuele
01:10 queued 23s
created

MessageTopicIcons::_checkValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
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.7
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
	public function __construct($icon_check = false, $theme_dir = '', $custom = array(), $default = 'xx')
61
	{
62
		parent::__construct();
63
64
		// Load passed parameters to the class properties
65
		$this->_check = $icon_check;
66
		$this->_theme_dir = $theme_dir;
67
		$this->_default_icon = $default;
68
		$this->_custom_icons = $custom;
69
70
		// Set default icons
71
		$this->_loadStableIcons();
72
73
		// Merge in additional ones
74
		$this->_loadCustomIcons();
75
		$this->_merge_all_icons();
76
		$this->_loadIcons();
77
	}
78
79
	/**
80
	 * Use a passed custom array or fetch the ones available for the board in use
81
	 */
82
	private function _loadCustomIcons()
83
	{
84
		global $board;
85
86
		// Are custom even an option?
87
		if ($this->_allowCustomIcons() && empty($this->_custom_icons))
88
		{
89
			// Fetch any additional ones
90
			require_once(SUBSDIR . '/MessageIcons.subs.php');
91
			$this->_custom_icons = getMessageIcons(empty($board) ? 0 : $board);
92
		}
93
	}
94
95
	/**
96
	 * This function merges in any custom icons with our standard ones.
97
	 *
98
	 * @return array
99
	 */
100
	private function _merge_all_icons()
101
	{
102
		// Are custom even an option?
103
		if ($this->_allowCustomIcons())
104
		{
105
			// Merge in additional ones
106
			$custom_icons = array_map(function ($element) {
107
				return $element['name'];
108
			}, $this->_custom_icons);
109
110
			$this->_stable_icons = array_merge($this->_stable_icons, $custom_icons);
111
		}
112
	}
113
114
	/**
115
	 * Simply checks the ACP status of custom icons
116
	 *
117
	 * @return bool
118
	 */
119
	private function _allowCustomIcons()
120
	{
121
		global $modSettings;
122
123
		return !empty($modSettings['messageIcons_enable']);
124
	}
125
126
	/**
127
	 * Return the icon specified by idx
128
	 *
129
	 * @param int|string $idx
130
	 * @return string
131
	 */
132
	public function __get($idx)
133
	{
134
		// Not a standard topic icon
135
		if (!isset($this->data[$idx]))
136
		{
137
			$this->_setUrl($idx);
138
		}
139
140
		return $this->data[$idx]['url'];
141
	}
142
143
	/**
144
	 * Return the icon URL specified by idx
145
	 *
146
	 * @param int|string $idx
147
	 * @return string
148
	 */
149
	public function getIconURL($idx)
150
	{
151
		$this->_checkValue($idx);
152
153
		return $this->data[$idx]['url'];
154
	}
155
156
	/**
157
	 * Return the name of the icon specified by idx
158
	 *
159
	 * @param int|string $idx
160
	 * @return string
161
	 */
162
	public function getIconName($idx)
163
	{
164
		$this->_checkValue($idx);
165
166
		return $this->data[$idx]['name'];
167
	}
168
169
	/**
170
	 * If the icon does not exist, sets a default
171
	 *
172
	 * @param $idx
173
	 */
174
	private function _checkValue($idx)
175
	{
176
		// Not a standard topic icon
177
		if (!isset($this->data[$idx]))
178
		{
179
			$this->data[$idx]['url'] = $this->data[$this->_default_icon]['url'];
180
			$this->data[$idx]['name'] = $this->_default_icon;
181
		}
182
	}
183
184
	/**
185
	 * Load the stable icon array
186
	 */
187
	protected function _loadStableIcons()
188
	{
189
		// Setup the default topic icons...
190
		$this->_stable_icons = array(
191
			'xx',
192
			'thumbup',
193
			'thumbdown',
194
			'exclamation',
195
			'question',
196
			'lamp',
197
			'smiley',
198
			'angry',
199
			'cheesy',
200
			'grin',
201
			'sad',
202
			'wink',
203
			'poll',
204
			'moved',
205
			'recycled',
206
			'wireless',
207
			'clip'
208
		);
209
	}
210
211
	/**
212
	 * This simple function returns the message topic icon array.
213
	 */
214
	protected function _loadIcons()
215
	{
216
		// Allow addons to add to the message icon array
217
		call_integration_hook('integrate_messageindex_icons', array(&$this->_stable_icons));
218
219
		$this->data = array();
220
		foreach ($this->_stable_icons as $icon)
221
		{
222
			$this->_setUrl($icon);
223
		}
224
	}
225
226
	/**
227
	 * Set the icon URL location
228
	 *
229
	 * @param string $icon
230
	 */
231
	protected function _setUrl($icon)
232
	{
233
		global $settings;
234
235
		if ($this->_check)
236
		{
237
			$this->data[$icon]['url'] = $settings[file_exists($this->_theme_dir . '/images/post/' . $icon . '.png')
238
				 ? self::IMAGE_URL
239
				 : self::DEFAULT_URL] . '/post/' . $icon . '.png';
240
		}
241
		else
242
		{
243
			$this->data[$icon]['url'] = $settings[self::IMAGE_URL] . '/post/' . $icon . '.png';
244
		}
245
246
		$this->data[$icon]['name'] = $icon;
247
	}
248
}