Staff_Block::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @package SimplePortal
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
13
/**
14
 * Staff Block, show the list of forum staff members
15
 *
16
 * @param mixed[] $parameters
17
 *   'lmod' => set to include local moderators as well
18
 * @param int $id - not used in this block
19
 * @param boolean $return_parameters if true returns the configuration options for the block
20
 */
21
class Staff_Block extends SP_Abstract_Block
22
{
23
	protected $color_ids = array();
24
25
	/**
26
	 * Constructor, used to define block parameters
27
	 *
28
	 * @param Database|null $db
29
	 */
30
	public function __construct($db = null)
31
	{
32
		$this->block_parameters = array(
33
			'lmod' => 'check',
34
		);
35
36
		parent::__construct($db);
37
	}
38
39
	/**
40
	 * Initializes a block for use.
41
	 *
42
	 * - Called from portal.subs as part of the sportal_load_blocks process
43
	 *
44
	 * @param mixed[] $parameters
45
	 * @param int $id
46
	 */
47
	public function setup($parameters, $id)
48
	{
49
		global $scripturl;
50
51
		require_once(SUBSDIR . '/Members.subs.php');
52
53
		// Including local board moderators
54
		if (empty($parameters['lmod']))
55
		{
56
			$request = $this->_db->query('', '
57
				SELECT
58
					id_member
59
				FROM {db_prefix}moderators',
60
				array()
61
			);
62
			$local_mods = array();
63
			while ($row = $this->_db->fetch_assoc($request))
64
			{
65
				$local_mods[$row['id_member']] = $row['id_member'];
66
			}
67
			$this->_db->free_result($request);
68
69
			if (count($local_mods) > 10)
70
			{
71
				$local_mods = array();
72
			}
73
		}
74
		else
75
		{
76
			$local_mods = array();
77
		}
78
79
		// Admins and global moderator list
80
		$global_mods = membersAllowedTo('moderate_board', 0);
81
		$admins = membersAllowedTo('admin_forum');
82
83
		// You only get one listing, highest authority
84
		$all_staff = array_merge($local_mods, $global_mods, $admins);
85
		$all_staff = array_unique($all_staff);
86
87
		$request = $this->_db->query('', '
88
			SELECT
89
				m.id_member, m.real_name, m.avatar, m.email_address,
90
				mg.group_name,
91
				a.id_attach, a.attachment_type, a.filename
92
			FROM {db_prefix}members AS m
93
				LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = m.id_member)
94
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN m.id_group = {int:reg_group_id} THEN m.id_post_group ELSE m.id_group END)
95
			WHERE m.id_member IN ({array_int:staff_list})',
96
			array(
97
				'staff_list' => $all_staff,
98
				'reg_group_id' => 0,
99
			)
100
		);
101
		$this->data['staff_list'] = array();
102
		while ($row = $this->_db->fetch_assoc($request))
103
		{
104
			$this->color_ids[$row['id_member']] = $row['id_member'];
105
106
			if (in_array($row['id_member'], $admins))
107
			{
108
				$row['type'] = 1;
109
			}
110
			elseif (in_array($row['id_member'], $global_mods))
111
			{
112
				$row['type'] = 2;
113
			}
114
			else
115
			{
116
				$row['type'] = 3;
117
			}
118
119
			$this->data['staff_list'][$row['type'] . '-' . $row['id_member']] = array(
120
				'id' => $row['id_member'],
121
				'name' => $row['real_name'],
122
				'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
123
				'group' => $row['group_name'],
124
				'type' => $row['type'],
125
				'avatar' => determineAvatar(array(
126
					'avatar' => $row['avatar'],
127
					'filename' => $row['filename'],
128
					'id_attach' => $row['id_attach'],
129
					'email_address' => $row['email_address'],
130
					'attachment_type' => $row['attachment_type'],
131
				)),
132
			);
133
		}
134
		$this->_db->free_result($request);
135
136
		// Get this in an order or importance
137
		ksort($this->data['staff_list']);
138
		$this->data['staff_count'] = count($this->data['staff_list']);
139
		$this->data['icons'] = array(1 => 'admin', 'gmod', 'lmod');
140
141
		// Color ID's
142
		$this->_color_ids();
143
144
		// How we will display the data
145
		$this->setTemplate('template_sp_staff');
146
	}
147
148
	/**
149
	 * Provide the color profile id's
150
	 */
151
	private function _color_ids()
152
	{
153
		global $color_profile;
154
155
		if (sp_loadColors($this->color_ids) !== false)
0 ignored issues
show
introduced by
The condition sp_loadColors($this->color_ids) !== false is always true.
Loading history...
156
		{
157
			foreach ($this->data['staff_list'] as $k => $p)
158
			{
159
				if (!empty($color_profile[$p['id']]['link']))
160
				{
161
					$this->data['staff_list'][$k]['link'] = $color_profile[$p['id']]['link'];
162
				}
163
			}
164
		}
165
	}
166
}
167
168
/**
169
 * Main template for this block
170
 *
171
 * @param mixed[] $data
172
 */
173
function template_sp_staff($data)
174
{
175
	global $scripturl;
176
177
	echo '
178
		<table class="sp_fullwidth">';
179
180
	$count = 0;
181
	foreach ($data['staff_list'] as $staff)
182
	{
183
		echo '
184
			<tr>
185
				<td class="sp_staff centertext">', !empty($staff['avatar']['href']) ? '
186
					<a href="' . $scripturl . '?action=profile;u=' . $staff['id'] . '">
187
						<img src="' . $staff['avatar']['href'] . '" alt="' . $staff['name'] . '" />
188
					</a>' : '', '
189
				</td>
190
				<td ', sp_embed_class($data['icons'][$staff['type']], '', 'sp_staff_info' . $data['staff_count'] != ++$count ? ' sp_staff_divider' : ''), '>',
191
					$staff['link'], '<br />', $staff['group'], '
192
				</td>
193
			</tr>';
194
	}
195
196
	echo '
197
		</table>';
198
}
199