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

attachments::get_range_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
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\blocks;
12
13
use blitze\sitemaker\services\blocks\driver\block;
14
15
/**
16
 * Attachments Block
17
 */
18
class attachments extends block
19
{
20
	/** @var \phpbb\auth\auth */
21
	protected $auth;
22
23
	/** @var \phpbb\cache\service */
24
	protected $cache;
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 \blitze\sitemaker\services\date_range		$date_range			Date Range Object
50
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
51
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Options object
52
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
53
	 * @param string									$php_ext			php file extension
54 9
	 */
55
	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)
56 9
	{
57 9
		$this->auth = $auth;
58 9
		$this->cache = $cache;
59 9
		$this->date_range = $date_range;
60 9
		$this->forum_data = $forum_data;
61 9
		$this->forum_options = $forum_options;
62 9
		$this->phpbb_root_path = $phpbb_root_path;
63 9
		$this->php_ext = $php_ext;
64
	}
65
66
	/**
67
	 * @inheritdoc
68 1
	 */
69
	public function get_config(array $settings)
70 1
	{
71 1
		$forum_options = $this->forum_options->get_all();
72 1
		$topic_type_options = $this->forum_options->get_topic_types();
73 1
		$range_options = $this->get_range_options();
74 1
		$attach_type_options = array('' => 'ALL_TYPES', 'IMAGES' => 'IMAGES', 'ARCHIVES' => 'ARCHIVES');
75
		$id_type_options = array('topic' => 'TOPICS', 'post' => 'POSTS');
76
77 1
		return array(
78 1
			'legend1'			=> 'SETTINGS',
79 1
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
80 1
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
81 1
			'first_only'		=> array('lang' => 'FIRST_POST_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => false),
82 1
			'ids_type'			=> array('lang' => 'TOPIC_POST_IDS_TYPE', 'validate' => 'bool', 'type' => 'radio', 'options' => $id_type_options, 'explain' => false, 'default' => 'topic'),
83 1
			'ids'				=> array('lang' => 'TOPIC_POST_IDS', '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', 'type' => 'number:0', '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
		);
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92 8
	 */
93
	public function display(array $bdata, $edit_mode = false)
94 8
	{
95
		$this->settings = $bdata['settings'];
96 8
97 8
		$extensions = $this->cache->obtain_attach_extensions(0);
98
		$ext_groups = $this->get_extension_groups($extensions);
99 8
100 8
		$posts_data = $this->get_posts_data();
101
		$attachments = $this->forum_data->get_attachments(0, $ext_groups[$this->settings['ext_type']], $this->settings['limit'], false);
102 8
103 8
		return array(
104 8
			'title'	=> 'ATTACHMENTS',
105 7
			'data'	=> array(
106
				'attachments'	=> $this->get_attachments_data($attachments, $posts_data, $extensions),
107 7
			)
108 7
		);
109
	}
110
111 8
	/**
112 8
	 * @param array $attachments_ary
113 8
	 * @param array $posts_data
114
	 * @param array $extensions
115
	 * @return array
116
	 */
117
	protected function get_attachments_data(array $attachments_ary, array $posts_data, array $extensions)
118
	{
119
		$message = '';
120
		$attachments_row = $update_count = [];
121 7
122
		foreach ($attachments_ary as $post_id => $attachments)
123 7
		{
124 7
			$topic_id = $attachments[0]['topic_id'];
125
			$post_row = $posts_data[$topic_id][$post_id];
126 7
127
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
128 7
129 7
			foreach ($attachments as $i => $attachment)
130
			{
131 7
				$row = $attachments_ary[$post_id][$i];
132
				$topic_id = $row['topic_id'];
133 7
				$post_id = $row['post_msg_id'];
134 7
135
				$attachments_row[] = array(
136 7
					'DISPLAY_ATTACHMENT'	=> $attachment,
137 7
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
138 7
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
139
				);
140 7
			}
141 7
		}
142 7
143 7
		return $attachments_row;
144 7
	}
145 7
146 7
	/**
147 7
	 * @return array
148
	 */
149
	private function get_posts_data()
150
	{
151
		$topic_ids = $post_ids = array();
152 8
		${$this->settings['ids_type'] . '_ids'} = array_filter(explode(',', $this->settings['ids']));
153
		$range_info = $this->date_range->get($this->settings['date_range']);
154 8
155 8
		$sql_array = $this->forum_data->query(false)
156 8
			->fetch_forum($this->get_allowed_forums())
157
			->fetch_topic_type($this->settings['topic_type'])
158 8
			->fetch_topic($topic_ids)
159 8
			->fetch_date_range($range_info['start'], $range_info['stop'])
160 8
			->build(true, true, false)
161 8
			->get_sql_array();
162 8
163 8
		$sql_array['SELECT'] = '';
164 8
		$sql_array['WHERE'] .= ' AND p.topic_id = t.topic_id AND p.post_attachment <> 0' . (($this->settings['first_only']) ? ' AND p.post_id = t.topic_first_post_id' : '');
165
166 8
		return $this->forum_data->get_post_data(false, $post_ids, $this->settings['limit'], 0, $sql_array);
167 8
	}
168
169 8
	/**
170
	 * @param array $extensions
171
	 * @return array
172
	 */
173
	protected function get_extension_groups(array $extensions)
174
	{
175
		array_shift($extensions);
176 8
177
		$ext_groups = array('' => array());
178 8
		foreach ($extensions as $ext => $row)
179
		{
180 8
			$ext_groups[$row['group_name']][] = $ext;
181 8
		}
182
183 8
		return $ext_groups;
184 8
	}
185
186 8
	/**
187
	 * @return array
188
	 */
189
	private function get_allowed_forums()
190
	{
191
		$allowed_forums = array_keys($this->auth->acl_getf('f_download', true));
192 8
		if (sizeof($this->settings['forum_ids']))
193
		{
194 8
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
195 8
		}
196 8
197 1
		return array_map('intval', $allowed_forums);
198 1
	}
199
200 8
	/**
201
	 * @return array
202
	 */
203
	private function get_range_options()
204
	{
205
		return array(
206 1
			''      => 'ALL_TIME',
207
			'today' => 'TODAY',
208
			'week'  => 'THIS_WEEK',
209 1
			'month' => 'THIS_MONTH',
210 1
			'year'  => 'THIS_YEAR',
211 1
		);
212 1
	}
213 1
214 1
	/**
215
	 * {@inheritdoc}
216
	 */
217
	public function get_template()
218
	{
219
		return '@blitze_sitemaker/blocks/attachments.html';
220
	}
221
}
222