Passed
Push — templating ( 0cf8c2 )
by Daniel
20:43
created

attachments::get_allowed_forums()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
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
	 */
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'	=> $this->get_attachments_data($attachments, $posts_data, $extensions),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->get_attachments_d...osts_data, $extensions) targeting blitze\sitemaker\blocks\...:get_attachments_data() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
106
		);
107
	}
108
109
	/**
110
	 * @param array $attachments_ary
111
	 * @param array $posts_data
112
	 * @param array $extensions
113
	 * @return []
0 ignored issues
show
Documentation Bug introduced by
The doc comment [] at position 0 could not be parsed: Unknown type name '[' at position 0 in [].
Loading history...
114
	 */
115
	protected function get_attachments_data(array $attachments_ary, array $posts_data, array $extensions)
116
	{
117
		$message = '';
118
		$data = $update_count = [];
119
120
		foreach ($attachments_ary as $post_id => $attachments)
121
		{
122
			$topic_id = $attachments[0]['topic_id'];
123
			$post_row = $posts_data[$topic_id][$post_id];
124
125
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
126
127
			$attachment_row = [];
128
			foreach ($attachments as $i => $attachment)
129
			{
130
				$row = $attachments_ary[$post_id][$i];
131
				$topic_id = $row['topic_id'];
132
				$post_id = $row['post_msg_id'];
133
134
				$attachment_row[] = array(
135
					'DISPLAY_ATTACHMENT'	=> $attachment,
136
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
137
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
138
				);
139
			}
140
141
			$data[] = $attachment_row;
142
		}
143
	}
144
145
	/**
146
	 * @return array
147
	 */
148
	private function get_posts_data()
149
	{
150
		$topic_ids = $post_ids = array();
151
		${$this->settings['ids_type'] . '_ids'} = array_filter(explode(',', $this->settings['ids']));
152
		$range_info = $this->date_range->get($this->settings['date_range']);
153
154
		$sql_array = $this->forum_data->query(false)
155
			->fetch_forum($this->get_allowed_forums())
156
			->fetch_topic_type($this->settings['topic_type'])
157
			->fetch_topic($topic_ids)
158
			->fetch_date_range($range_info['start'], $range_info['stop'])
159
			->build(true, true, false)
160
			->get_sql_array();
161
162
		$sql_array['SELECT'] = '';
163
		$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' : '');
164
165
		return $this->forum_data->get_post_data(false, $post_ids, $this->settings['limit'], 0, $sql_array);
166
	}
167
168
	/**
169
	 * @param array $extensions
170
	 * @return array
171
	 */
172
	protected function get_extension_groups(array $extensions)
173
	{
174
		array_shift($extensions);
175
176
		$ext_groups = array('' => array());
177
		foreach ($extensions as $ext => $row)
178
		{
179
			$ext_groups[$row['group_name']][] = $ext;
180
		}
181
182
		return $ext_groups;
183
	}
184
185
	/**
186
	 * @return array
187
	 */
188
	private function get_allowed_forums()
189
	{
190
		$allowed_forums = array_keys($this->auth->acl_getf('f_download', true));
191
		if (sizeof($this->settings['forum_ids']))
192
		{
193
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
194
		}
195
196
		return array_map('intval', $allowed_forums);
197
	}
198
199
	/**
200
	 * @return array
201
	 */
202
	private function get_range_options()
203
	{
204
		return array(
205
			''      => 'ALL_TIME',
206
			'today' => 'TODAY',
207
			'week'  => 'THIS_WEEK',
208
			'month' => 'THIS_MONTH',
209
			'year'  => 'THIS_YEAR',
210
		);
211
	}
212
213
	/**
214
	 * {@inheritdoc}
215
	 */
216
	public function get_template()
217
	{
218
		return '@blitze_sitemaker/blocks/attachments.html';
219
	}
220
}
221