Completed
Push — master ( 9866a1...02e5e5 )
by Daniel
08:18
created

attachments::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\blocks;
11
12
/**
13
 * Attachments Block
14
 */
15
class attachments extends \blitze\sitemaker\services\blocks\driver\block
16
{
17
	/** @var \phpbb\auth\auth */
18
	protected $auth;
19
20
	/** @var \phpbb\cache\service */
21
	protected $cache;
22
23
	/** @var \phpbb\user */
24
	protected $user;
25
26
	/** @var \blitze\sitemaker\services\date_range */
27
	protected $date_range;
28
29
	/** @var \blitze\sitemaker\services\forum\data */
30
	protected $forum_data;
31
32
	/** @var \blitze\sitemaker\services\forum\options */
33
	protected $forum_options;
34
35
	/** @var string */
36
	protected $phpbb_root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var array */
42
	private $settings = array();
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\auth\auth							$auth				Permission object
48
	 * @param \phpbb\cache\service						$cache				Cache Service object
49
	 * @param \phpbb\user								$user				User object
50
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
51
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
52
	 * @param \blitze\sitemaker\services\forum\options	$forum_data			Forum Data object
53
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
54
	 * @param string									$php_ext			php file extension
55
	 */
56
	public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\user $user, \blitze\sitemaker\services\date_range $date_range, \blitze\sitemaker\services\forum\data $forum_data, \blitze\sitemaker\services\forum\options $forum_options, $phpbb_root_path, $php_ext)
57
	{
58
		$this->auth = $auth;
59
		$this->cache = $cache;
60
		$this->user = $user;
61
		$this->date_range = $date_range;
62
		$this->forum_data = $forum_data;
63
		$this->forum_options = $forum_options;
64
		$this->phpbb_root_path = $phpbb_root_path;
65
		$this->php_ext = $php_ext;
66
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71
	public function get_config(array $settings)
72
	{
73
		$forum_options = $this->forum_options->get_all();
74
		$topic_type_options = $this->_get_topic_type_options();
75
		$range_options = $this->_get_range_options();
76
		$attach_type_options = array('' => 'ALL', 'IMAGES' => 'IMAGES', 'ARCHIVES' => 'ARCHIVES');
77
78
		return array(
79
			'legend1'		=> $this->user->lang('SETTINGS'),
80
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
81
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
82
			'first_only'		=> array('lang' => 'FIRST_POST_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => false),
83
			'post_ids'			=> array('lang' => 'ATTACHMENTS_FROM_POSTS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
84
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $range_options, 'default' => '', 'explain' => false),
85
			'limit'				=> array('lang' => 'LIMIT', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
86
			'ext_type'			=> array('lang' => 'EXTENSION_GROUP', 'validate' => 'string', 'type' => 'radio', 'options' => $attach_type_options, 'default' => '', 'explain' => false),
87
		);
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93
	public function display(array $bdata, $edit_mode = false)
94
	{
95
		$this->settings = $bdata['settings'];
96
97
		$extensions = $this->cache->obtain_attach_extensions(0);
98
		$ext_groups = $this->_get_extension_groups($extensions);
99
100
		$posts_data = $this->_get_posts_data();
101
		$attachments = $this->forum_data->get_attachments(0, $ext_groups[$this->settings['ext_type']], false, 'download_count DESC');
102
103
		$content = '';
104
		if (sizeof($attachments))
105
		{
106
			$this->_get_block_content($attachments, $posts_data, $extensions);
107
108
			$content = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/attachments.html', 'attachments');
109
		}
110
111
		return array(
112
			'title'		=> 'ATTACHMENTS',
113
			'content'	=> $content,
114
		);
115
	}
116
117
	/**
118
	 * @param array $attachments_ary
119
	 * @param array $posts_data
120
	 * @param array $extensions
121
	 */
122
	protected function _get_block_content(array $attachments_ary, array $posts_data, array $extensions)
123
	{
124
		$message = '';
125
		$update_count = array();
126
127
		foreach ($attachments_ary as $post_id => $attachments)
128
		{
129
			$topic_id = $attachments[0]['topic_id'];
130
			$post_row = $posts_data[$topic_id][$post_id];
131
132
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
133
134
			$this->ptemplate->assign_block_vars('postrow', array());
135
			foreach ($attachments as $i => $attachment)
136
			{
137
				$row = $attachments_ary[$post_id][$i];
138
				$topic_id = $row['topic_id'];
139
				$post_id = $row['post_msg_id'];
140
141
				$this->ptemplate->assign_block_vars('postrow.attachment', array(
142
					'DISPLAY_ATTACHMENT'	=> $attachment,
143
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
144
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
145
				));
146
			}
147
		}
148
	}
149
150
	/**
151
	 * @return array
152
	 */
153
	private function _get_posts_data()
154
	{
155
		$range_info = $this->date_range->get($this->settings['date_range']);
156
		$allowed_forums = $this->_get_allowed_forums();
157
		$post_ids = array_filter(explode(',', $this->settings['post_ids']));
158
159
		$sql_array = $this->forum_data->query()
160
			->fetch_forum($allowed_forums)
161
			->fetch_topic_type($this->settings['topic_type'])
162
			->fetch_date_range($range_info['start'], $range_info['stop'])
163
			->build()
164
			->get_sql_array();
165
166
		$sql_array['SELECT'] = '';
167
		$sql_array['WHERE'] .= ' AND p.topic_id = t.topic_id AND p.post_attachment <> 0';
168
169
		if ($this->settings['first_only'])
170
		{
171
			$sql_array['WHERE'] .= " AND p.post_id = t.topic_first_post_id";
172
		}
173
174
		return $this->forum_data->get_post_data(false, $post_ids, $this->settings['limit'], 0, $sql_array);
175
	}
176
177
	/**
178
	 * @param array $extensions
179
	 * @return array
180
	 */
181
	protected function _get_extension_groups(array $extensions)
182
	{
183
		array_shift($extensions);
184
185
		$ext_groups = array('' => array());
186
		foreach ($extensions as $ext => $row)
187
		{
188
			$ext_groups[$row['group_name']][] = $ext;
189
		}
190
191
		return $ext_groups;
192
	}
193
194
	/**
195
	 * @return array
196
	 */
197
	private function _get_allowed_forums()
198
	{
199
		$allowed_forums = array_unique(array_keys($this->auth->acl_getf('f_download', true)));
200
		if (sizeof($this->settings['forum_ids']))
201
		{
202
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
203
		}
204
205
		return array_map('intval', $allowed_forums);
206
	}
207
208
	/**
209
	 * @return array
210
	 */
211
	private function _get_topic_type_options()
212
	{
213
		return array(
214
			POST_NORMAL     => 'POST_NORMAL',
215
			POST_STICKY     => 'POST_STICKY',
216
			POST_ANNOUNCE   => 'POST_ANNOUNCEMENT',
217
			POST_GLOBAL     => 'POST_GLOBAL',
218
		);
219
	}
220
221
	/**
222
	 * @return array
223
	 */
224
	private function _get_range_options()
225
	{
226
		return array(
227
			''      => 'ALL_TIME',
228
			'today' => 'TODAY',
229
			'week'  => 'THIS_WEEK',
230
			'month' => 'THIS_MONTH',
231
			'year'  => 'THIS_YEAR',
232
		);
233
	}
234
}
235