Passed
Push — renovate/configure ( c923d4...2c70da )
by
unknown
20:13
created

attachments::get_attachments_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 3
dl 0
loc 27
rs 9.7666
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
	 */
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
	{
57
		$this->auth = $auth;
58
		$this->cache = $cache;
59
		$this->date_range = $date_range;
60
		$this->forum_data = $forum_data;
61
		$this->forum_options = $forum_options;
62
		$this->phpbb_root_path = $phpbb_root_path;
63
		$this->php_ext = $php_ext;
64
	}
65
66
	/**
67
	 * @inheritdoc
68
	 */
69
	public function get_config(array $settings)
70
	{
71
		$forum_options = $this->forum_options->get_all();
72
		$topic_type_options = $this->forum_options->get_topic_types();
73
		$range_options = $this->get_range_options();
74
		$attach_type_options = array('' => 'ALL_TYPES', 'IMAGES' => 'IMAGES', 'ARCHIVES' => 'ARCHIVES');
75
		$id_type_options = array('topic' => 'TOPICS', 'post' => 'POSTS');
76
77
		return array(
78
			'legend1'			=> 'SETTINGS',
79
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
80
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
81
			'first_only'		=> array('lang' => 'FIRST_POST_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => false),
82
			'ids_type'			=> array('lang' => 'TOPIC_POST_IDS_TYPE', 'validate' => 'bool', 'type' => 'radio', 'options' => $id_type_options, 'explain' => false, 'default' => 'topic'),
83
			'ids'				=> array('lang' => 'TOPIC_POST_IDS', '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', 'type' => 'number:0', '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']], $this->settings['limit'], false);
102
103
		return array(
104
			'title'	=> 'ATTACHMENTS',
105
			'data'	=> array(
106
				'attachments'	=> $this->get_attachments_data($attachments, $posts_data, $extensions),
107
			)
108
		);
109
	}
110
111
	/**
112
	 * @param array $attachments_ary
113
	 * @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
122
		foreach ($attachments_ary as $post_id => $attachments)
123
		{
124
			$topic_id = $attachments[0]['topic_id'];
125
			$post_row = $posts_data[$topic_id][$post_id];
126
127
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
128
129
			foreach ($attachments as $i => $attachment)
130
			{
131
				$row = $attachments_ary[$post_id][$i];
132
				$topic_id = $row['topic_id'];
133
				$post_id = $row['post_msg_id'];
134
135
				$attachments_row[] = array(
136
					'DISPLAY_ATTACHMENT'	=> $attachment,
137
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
138
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
139
				);
140
			}
141
		}
142
143
		return $attachments_row;
144
	}
145
146
	/**
147
	 * @return array
148
	 */
149
	private function get_posts_data()
150
	{
151
		$topic_ids = $post_ids = array();
152
		${$this->settings['ids_type'] . '_ids'} = array_filter(explode(',', $this->settings['ids']));
153
		$range_info = $this->date_range->get($this->settings['date_range']);
154
155
		$sql_array = $this->forum_data->query(false)
156
			->fetch_forum($this->get_allowed_forums())
157
			->fetch_topic_type($this->settings['topic_type'])
158
			->fetch_topic($topic_ids)
159
			->fetch_date_range($range_info['start'], $range_info['stop'])
160
			->build(true, true, false)
161
			->get_sql_array();
162
163
		$sql_array['SELECT'] = '';
164
		$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
		return $this->forum_data->get_post_data(false, $post_ids, $this->settings['limit'], 0, $sql_array);
167
	}
168
169
	/**
170
	 * @param array $extensions
171
	 * @return array
172
	 */
173
	protected function get_extension_groups(array $extensions)
174
	{
175
		array_shift($extensions);
176
177
		$ext_groups = array('' => array());
178
		foreach ($extensions as $ext => $row)
179
		{
180
			$ext_groups[$row['group_name']][] = $ext;
181
		}
182
183
		return $ext_groups;
184
	}
185
186
	/**
187
	 * @return array
188
	 */
189
	private function get_allowed_forums()
190
	{
191
		$allowed_forums = array_keys($this->auth->acl_getf('f_download', true));
192
		if (sizeof($this->settings['forum_ids']))
193
		{
194
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
195
		}
196
197
		return array_map('intval', $allowed_forums);
198
	}
199
200
	/**
201
	 * @return array
202
	 */
203
	private function get_range_options()
204
	{
205
		return array(
206
			''      => 'ALL_TIME',
207
			'today' => 'TODAY',
208
			'week'  => 'THIS_WEEK',
209
			'month' => 'THIS_MONTH',
210
			'year'  => 'THIS_YEAR',
211
		);
212
	}
213
214
	/**
215
	 * {@inheritdoc}
216
	 */
217
	public function get_template()
218
	{
219
		return '@blitze_sitemaker/blocks/attachments.html';
220
	}
221
}
222