Passed
Branch develop (786e4f)
by Daniel
07:32
created

attachments::get_block_content()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 3
dl 0
loc 23
ccs 16
cts 16
cp 1
crap 3
rs 9.0856
c 0
b 0
f 0
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 */
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
	protected $auth;
21
22
	/** @var \phpbb\cache\service */
0 ignored issues
show
Bug introduced by
The type phpbb\cache\service was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 9
	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 9
		$this->auth = $auth;
57 9
		$this->cache = $cache;
58 9
		$this->date_range = $date_range;
59 9
		$this->forum_data = $forum_data;
60 9
		$this->forum_options = $forum_options;
61 9
		$this->phpbb_root_path = $phpbb_root_path;
62 9
		$this->php_ext = $php_ext;
63 9
	}
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->forum_options->get_topic_types();
72 1
		$range_options = $this->get_range_options();
73 1
		$attach_type_options = array('' => 'ALL_TYPES', 'IMAGES' => 'IMAGES', 'ARCHIVES' => 'ARCHIVES');
74 1
		$id_type_options = array('topic' => 'TOPICS', 'post' => 'POSTS');
75
76
		return array(
77 1
			'legend1'			=> 'SETTINGS',
78 1
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
79 1
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
80 1
			'first_only'		=> array('lang' => 'FIRST_POST_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => false),
81 1
			'ids_type'			=> array('lang' => 'TOPIC_POST_IDS_TYPE', 'validate' => 'bool', 'type' => 'radio', 'options' => $id_type_options, 'explain' => false, 'default' => 'topic'),
82 1
			'ids'				=> array('lang' => 'TOPIC_POST_IDS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
83 1
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $range_options, 'default' => '', 'explain' => false),
84 1
			'limit'				=> array('lang' => 'LIMIT', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
85 1
			'ext_type'			=> array('lang' => 'EXTENSION_GROUP', 'validate' => 'string', 'type' => 'radio', 'options' => $attach_type_options, 'default' => '', 'explain' => false),
86 1
		);
87
	}
88
89
	/**
90
	 * {@inheritdoc}
91
	 */
92 8
	public function display(array $bdata, $edit_mode = false)
93
	{
94 8
		$this->settings = $bdata['settings'];
95
96 8
		$extensions = $this->cache->obtain_attach_extensions(0);
97 8
		$ext_groups = $this->get_extension_groups($extensions);
98
99 8
		$posts_data = $this->get_posts_data();
100 8
		$attachments = $this->forum_data->get_attachments(0, $ext_groups[$this->settings['ext_type']], $this->settings['limit'], false);
101
102 8
		$content = '';
103 8
		if (sizeof($attachments))
104 8
		{
105 7
			$this->get_block_content($attachments, $posts_data, $extensions);
106
107 7
			$content = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/attachments.html', 'attachments');
108 7
		}
109
110
		return array(
111 8
			'title'		=> 'ATTACHMENTS',
112 8
			'content'	=> $content,
113 8
		);
114
	}
115
116
	/**
117
	 * @param array $attachments_ary
118
	 * @param array $posts_data
119
	 * @param array $extensions
120
	 */
121 7
	protected function get_block_content(array $attachments_ary, array $posts_data, array $extensions)
122
	{
123 7
		$message = '';
124 7
		$update_count = array();
125
126 7
		foreach ($attachments_ary as $post_id => $attachments)
127
		{
128 7
			$topic_id = $attachments[0]['topic_id'];
129 7
			$post_row = $posts_data[$topic_id][$post_id];
130
131 7
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
0 ignored issues
show
Bug introduced by
The function parse_attachments was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
			/** @scrutinizer ignore-call */ 
132
   parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
Loading history...
132
133 7
			$this->ptemplate->assign_block_vars('postrow', array());
134 7
			foreach ($attachments as $i => $attachment)
135
			{
136 7
				$row = $attachments_ary[$post_id][$i];
137 7
				$topic_id = $row['topic_id'];
138 7
				$post_id = $row['post_msg_id'];
139
140 7
				$this->ptemplate->assign_block_vars('postrow.attachment', array(
141 7
					'DISPLAY_ATTACHMENT'	=> $attachment,
142 7
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
143 7
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
					'U_VIEWTOPIC'			=> /** @scrutinizer ignore-call */ append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
Loading history...
144 7
				));
145 7
			}
146 7
		}
147 7
	}
148
149
	/**
150
	 * @return array
151
	 */
152 8
	private function get_posts_data()
153
	{
154 8
		$topic_ids = $post_ids = array();
155 8
		${$this->settings['ids_type'] . '_ids'} = array_filter(explode(',', $this->settings['ids']));
156 8
		$range_info = $this->date_range->get($this->settings['date_range']);
157
158 8
		$sql_array = $this->forum_data->query(false)
159 8
			->fetch_forum($this->get_allowed_forums())
160 8
			->fetch_topic_type($this->settings['topic_type'])
161 8
			->fetch_topic($topic_ids)
162 8
			->fetch_date_range($range_info['start'], $range_info['stop'])
163 8
			->build(true, true, false)
164 8
			->get_sql_array();
165
166 8
		$sql_array['SELECT'] = '';
167 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' : '');
168
169 8
		return $this->forum_data->get_post_data(false, $post_ids, $this->settings['limit'], 0, $sql_array);
170
	}
171
172
	/**
173
	 * @param array $extensions
174
	 * @return array
175
	 */
176 8
	protected function get_extension_groups(array $extensions)
177
	{
178 8
		array_shift($extensions);
179
180 8
		$ext_groups = array('' => array());
181 8
		foreach ($extensions as $ext => $row)
182
		{
183 8
			$ext_groups[$row['group_name']][] = $ext;
184 8
		}
185
186 8
		return $ext_groups;
187
	}
188
189
	/**
190
	 * @return array
191
	 */
192 8
	private function get_allowed_forums()
193
	{
194 8
		$allowed_forums = array_keys($this->auth->acl_getf('f_download', true));
195 8
		if (sizeof($this->settings['forum_ids']))
196 8
		{
197 1
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
198 1
		}
199
200 8
		return array_map('intval', $allowed_forums);
201
	}
202
203
	/**
204
	 * @return array
205
	 */
206 1
	private function get_range_options()
207
	{
208
		return array(
209 1
			''      => 'ALL_TIME',
210 1
			'today' => 'TODAY',
211 1
			'week'  => 'THIS_WEEK',
212 1
			'month' => 'THIS_MONTH',
213 1
			'year'  => 'THIS_YEAR',
214 1
		);
215
	}
216
}
217