Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

attachments::get_posts_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

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