Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

members::set_recent_sql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services;
12
13
class members
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
16
	protected $db;
17
18
	/** @var \phpbb\language\language */
19
	protected $translator;
20
21
	/** @var \phpbb\user */
22
	protected $user;
23
24
	/** @var \blitze\sitemaker\services\date_range */
25
	protected $date_range;
26
27
	/** @var string */
28
	protected $phpbb_root_path;
29
30
	/** @var string */
31
	protected $php_ext;
32
33
	protected $explain_range = '';
34
	protected $sql_date_field = '';
35
	protected $view_mode = '';
36
	protected $user_header = 'USERNAME';
37
	protected $info_header = 'MEMBERS_DATE';
38
	protected $settings = array();
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param \phpbb\db\driver\driver_interface		$db     			Database connection
44
	 * @param \phpbb\language\language				$translator			Language Object
45
	 * @param \phpbb\user							$user				User object
46
	 * @param \blitze\sitemaker\services\date_range	$date_range			Date range object
47
	 * @param string								$phpbb_root_path	Path to the phpbb includes directory.
48
	 * @param string								$php_ext			php file extension
49
	 */
50
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\date_range $date_range, $phpbb_root_path, $php_ext)
51
	{
52
		$this->db = $db;
53 6
		$this->translator = $translator;
54
		$this->user = $user;
55 6
		$this->date_range = $date_range;
56 6
		$this->phpbb_root_path = $phpbb_root_path;
57 6
		$this->php_ext = $php_ext;
58 6
59 6
		// @codeCoverageIgnoreStart
60 6
		if (!function_exists('phpbb_get_user_avatar'))
61 6
		{
62
			include($phpbb_root_path . 'includes/functions_display.' . $php_ext);
63
		}
64
		// @codeCoverageIgnoreEnd
65
	}
66
67
	/**
68
	 * get members
69 6
	 * @param array $get
70
	 * @return array
71
	 */
72
	public function get_list(array $get = array())
73
	{
74
		$this->view_mode = 'member_date';
75
		$this->settings = $get + array(
76 6
			'query_type'	=> 'recent',
77
			'date_range'	=> '',
78 6
			'max_members'	=> 5,
79 6
		);
80 6
81 6
		$sql = $this->get_sql_statement();
82 6
		$result = $this->db->sql_query_limit($sql, $this->settings['max_members']);
83
84 6
		$members = [];
85 6
		while ($row = $this->db->sql_fetchrow($result))
86
		{
87 6
			$members[] = call_user_func_array(array($this, $this->view_mode), array($row));
88 6
		}
89
		$this->db->sql_freeresult($result);
90 6
91 6
		return array(
92 6
			'S_LIST'		=> $this->settings['query_type'],
93 6
			'USER_TITLE'	=> $this->translator->lang($this->user_header),
94
			'INFO_TITLE'	=> $this->translator->lang($this->info_header),
95 6
			'MEMBERS'		=> $members,
96
		);
97
	}
98
99
	/**
100
	 * @param array $row
101
	 * @return array
102 1
	 */
103
	protected function member_posts(array $row)
104 1
	{
105 1
		$u_posts = append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&amp;sr=posts" . $this->explain_range);
106
		$user_posts = '<a href="' . $u_posts . '">' . $row['user_posts'] . '</a>';
107
108 1
		return array(
109 1
			'USERNAME'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
110
			'USER_AVATAR'	=> phpbb_get_user_avatar($row),
111 1
			'USER_INFO'		=> $user_posts
112
		);
113
	}
114
115
	/**
116
	 * @param array $row
117
	 * @return array
118 4
	 */
119
	protected function member_date(array $row)
120
	{
121 4
		return array(
122 4
			'USERNAME'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
123 4
			'USER_AVATAR'	=> phpbb_get_user_avatar($row),
124 4
			'USER_INFO'		=> $this->user->format_date($row['member_date'], $this->translator->lang('DATE_FORMAT'), true)
125
		);
126
	}
127
128
	/**
129
	 * @param array $row
130
	 * @return array
131 1
	 */
132
	protected function member_bots(array $row)
133
	{
134 1
		return array(
135 1
			'USERNAME'	=> get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
136 1
			'USER_INFO'	=> $this->user->format_date($row['member_date'])
137
		);
138
	}
139
140
	/**
141
	 * @return string
142
	 */
143 6
	protected function get_sql_statement()
144
	{
145 6
		$sql_ary = array(
146
			'SELECT'		=> 'u.user_id, u.username, u.user_colour, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height',
147 6
			'FROM'			=> array(
148 6
				USERS_TABLE => 'u'
149 6
			),
150 6
			'WHERE'			=> $this->db->sql_in_set('u.user_type', array(USER_NORMAL, USER_FOUNDER)),
151 6
		);
152 6
153
		$sql_method = 'set_' . $this->settings['query_type'] . '_sql';
154 6
		call_user_func_array(array($this, $sql_method), array(&$sql_ary));
155 6
156
		$this->set_range_sql($sql_ary);
157 6
158
		return $this->db->sql_build_query('SELECT', $sql_ary);
159
	}
160
161
	/**
162
	 * @param array $sql_ary
163 6
	 */
164
	protected function set_visits_sql(array &$sql_ary)
165
	{
166 6
		$sql_ary['SELECT'] .= ', u.user_lastvisit as member_date';
167
		$sql_ary['WHERE'] .= ' AND u.user_lastvisit <> 0 AND u.user_id <> ' . (int) $this->user->data['user_id'];
168 6
		$sql_ary['ORDER_BY'] = 'u.user_lastvisit DESC';
169 6
170 6
        $this->sql_date_field = 'user_lastvisit';
171 6
	}
172
173 6
	/**
174 6
	 * @param array $sql_ary
175
	 */
176 6
	protected function set_bots_sql(array &$sql_ary)
177
	{
178 6
		$this->set_visits_sql($sql_ary);
179
		$this->user_header = '';
180
		$this->info_header = '';
181
		$this->view_mode = 'member_bots';
182
183
		$sql_ary['WHERE'] = 'u.user_type = ' . USER_IGNORE;
184 3
	}
185
186 3
	/**
187 3
	 * @param array $sql_ary
188 3
	 */
189
	protected function set_tenured_sql(array &$sql_ary)
190 3
	{
191 3
		$sql_ary['SELECT'] .= ', u.user_regdate as member_date';
192
		$sql_ary['ORDER_BY'] = 'u.user_regdate ' . (($this->settings['query_type'] == 'tenured') ? 'ASC' : 'DESC');
193
194
		$this->sql_date_field = 'u.user_regdate';
195
		$this->settings['date_range'] = '';
196 1
	}
197
198 1
	/**
199 1
	 * @param array $sql_ary
200 1
	 */
201 1
	protected function set_recent_sql(array &$sql_ary)
202
	{
203 1
		$this->set_tenured_sql($sql_ary);
204 1
		$this->info_header = 'JOIN_DATE';
205
	}
206
207
	/**
208
	 * @param array $sql_ary
209 2
	 */
210
	protected function set_posts_sql(array &$sql_ary)
211 2
	{
212 2
		$sql_ary['SELECT'] .= ', COUNT(p.post_id) as user_posts';
213
		$sql_ary['FROM'] += array(TOPICS_TABLE => 't');
214 2
		$sql_ary['FROM'] += array(POSTS_TABLE => 'p');
215 2
		$sql_ary['WHERE'] .= ' AND ' . time() . ' > t.topic_time AND t.topic_id = p.topic_id AND p.post_visibility = ' . ITEM_APPROVED . ' AND p.poster_id = u.user_id';
216 2
		$sql_ary['GROUP_BY'] = 'u.user_id';
217
		$sql_ary['ORDER_BY'] = 'user_posts DESC, u.username ASC';
218
219
		$this->info_header = 'POSTS';
220
		$this->view_mode = 'member_posts';
221 1
		$this->sql_date_field = 'p.post_time';
222
	}
223 1
224 1
	/**
225 1
	 * @param array $sql_ary
226
	 */
227
	protected function set_range_sql(array &$sql_ary)
228
	{
229
		if ($this->settings['range'] && $this->sql_date_field)
230 1
		{
231
			$range = $this->date_range->get($this->settings['range']);
232 1
			$this->explain_range = '&amp;date=' . $range['date'];
233 1
234 1
			$sql_ary['WHERE'] .= " AND {$this->sql_date_field} BETWEEN {$range['start']} AND {$range['stop']}";
235 1
		}
236 1
	}
237
}
238