Completed
Push — master ( 4f5aad...a0199d )
by Daniel
09:18
created

attachments::get_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.4285
cc 1
eloc 14
nc 1
nop 1
crap 1
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 8
	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 8
		$this->auth = $auth;
59 8
		$this->cache = $cache;
60 8
		$this->user = $user;
61 8
		$this->date_range = $date_range;
62 8
		$this->forum_data = $forum_data;
63 8
		$this->forum_options = $forum_options;
64 8
		$this->phpbb_root_path = $phpbb_root_path;
65 8
		$this->php_ext = $php_ext;
66 8
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71 1
	public function get_config(array $settings)
72
	{
73 1
		$forum_options = $this->forum_options->get_all();
74 1
		$topic_type_options = $this->_get_topic_type_options();
75 1
		$range_options = $this->_get_range_options();
76 1
		$attach_type_options = array('' => 'ALL', 'IMAGES' => 'IMAGES', 'ARCHIVES' => 'ARCHIVES');
77
78
		return array(
79 1
			'legend1'		=> $this->user->lang('SETTINGS'),
80 1
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
81 1
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
82 1
			'first_only'		=> array('lang' => 'FIRST_POST_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => false),
83 1
			'post_ids'			=> array('lang' => 'ATTACHMENTS_FROM_POSTS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
84 1
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $range_options, 'default' => '', 'explain' => false),
85 1
			'limit'				=> array('lang' => 'LIMIT', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
86 1
			'ext_type'			=> array('lang' => 'EXTENSION_GROUP', 'validate' => 'string', 'type' => 'radio', 'options' => $attach_type_options, 'default' => '', 'explain' => false),
87 1
		);
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93 7
	public function display(array $bdata, $edit_mode = false)
94
	{
95 7
		$this->settings = $bdata['settings'];
96
97 7
		$extensions = $this->cache->obtain_attach_extensions(0);
98 7
		$ext_groups = $this->_get_extension_groups($extensions);
99
100 7
		$posts_data = $this->_get_posts_data();
101 7
		$attachments = $this->forum_data->get_attachments(0, $ext_groups[$this->settings['ext_type']], false, 'download_count DESC');
102
103 7
		$content = '';
104 7
		if (sizeof($attachments))
105 7
		{
106 6
			$this->_get_block_content($attachments, $posts_data, $extensions);
107
108 6
			$content = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/attachments.html', 'attachments');
109 6
		}
110
111
		return array(
112 7
			'title'		=> 'ATTACHMENTS',
113 7
			'content'	=> $content,
114 7
		);
115
	}
116
117
	/**
118
	 * @param array $attachments_ary
119
	 * @param array $posts_data
120
	 * @param array $extensions
121
	 */
122 6
	protected function _get_block_content(array $attachments_ary, array $posts_data, array $extensions)
123
	{
124 6
		$message = '';
125 6
		$update_count = array();
126
127 6
		foreach ($attachments_ary as $post_id => $attachments)
128
		{
129 6
			$topic_id = $attachments[0]['topic_id'];
130 6
			$post_row = $posts_data[$topic_id][$post_id];
131
132 6
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
133
134 6
			$this->ptemplate->assign_block_vars('postrow', array());
135 6
			foreach ($attachments as $i => $attachment)
136
			{
137 6
				$row = $attachments_ary[$post_id][$i];
138 6
				$topic_id = $row['topic_id'];
139 6
				$post_id = $row['post_msg_id'];
140
141 6
				$this->ptemplate->assign_block_vars('postrow.attachment', array(
142 6
					'DISPLAY_ATTACHMENT'	=> $attachment,
143 6
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
144 6
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
145 6
				));
146 6
			}
147 6
		}
148 6
	}
149
150
	/**
151
	 * @return array
152
	 */
153 7
	private function _get_posts_data()
154
	{
155 7
		$range_info = $this->date_range->get($this->settings['date_range']);
156 7
		$allowed_forums = $this->_get_allowed_forums();
157 7
		$post_ids = array_filter(explode(',', $this->settings['post_ids']));
158
159 7
		$sql_array = $this->forum_data->query()
160 7
			->fetch_forum($allowed_forums)
161 7
			->fetch_topic_type($this->settings['topic_type'])
162 7
			->fetch_date_range($range_info['start'], $range_info['stop'])
163 7
			->build()
164 7
			->get_sql_array();
165
166 7
		$sql_array['SELECT'] = '';
167 7
		$sql_array['WHERE'] .= ' AND p.topic_id = t.topic_id AND p.post_attachment <> 0';
168
169 7
		if ($this->settings['first_only'])
170 7
		{
171 2
			$sql_array['WHERE'] .= " AND p.post_id = t.topic_first_post_id";
172 2
		}
173
174 7
		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 7
	protected function _get_extension_groups(array $extensions)
182
	{
183 7
		array_shift($extensions);
184
185 7
		$ext_groups = array('' => array());
186 7
		foreach ($extensions as $ext => $row)
187
		{
188 7
			$ext_groups[$row['group_name']][] = $ext;
189 7
		}
190
191 7
		return $ext_groups;
192
	}
193
194
	/**
195
	 * @return array
196
	 */
197 7
	private function _get_allowed_forums()
198
	{
199 7
		$allowed_forums = array_unique(array_keys($this->auth->acl_getf('f_download', true)));
200 7
		if (sizeof($this->settings['forum_ids']))
201 7
		{
202 1
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
203 1
		}
204
205 7
		return array_map('intval', $allowed_forums);
206
	}
207
208
	/**
209
	 * @return array
210
	 */
211 1
	private function _get_topic_type_options()
212
	{
213
		return array(
214 1
			POST_NORMAL     => 'POST_NORMAL',
215 1
			POST_STICKY     => 'POST_STICKY',
216 1
			POST_ANNOUNCE   => 'POST_ANNOUNCEMENT',
217 1
			POST_GLOBAL     => 'POST_GLOBAL',
218 1
		);
219
	}
220
221
	/**
222
	 * @return array
223
	 */
224 1
	private function _get_range_options()
225
	{
226
		return array(
227 1
			''      => 'ALL_TIME',
228 1
			'today' => 'TODAY',
229 1
			'week'  => 'THIS_WEEK',
230 1
			'month' => 'THIS_MONTH',
231 1
			'year'  => 'THIS_YEAR',
232 1
		);
233
	}
234
}
235