Passed
Push — develop ( 624076...843971 )
by Daniel
32:12
created

forum_topics::get_forum_image()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 1
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
/**
14
 * Forum Topics Block
15
 */
16
class forum_topics extends forum_topics_config
17
{
18
	/** @var \phpbb\auth\auth */
19
	protected $auth;
20
21
	/** @var \phpbb\content_visibility */
22
	protected $content_visibility;
23
24
	/** @var \phpbb\language\language */
25
	protected $translator;
26
27
	/** @var \phpbb\user */
28
	protected $user;
29
30
	/** @var \Urodoz\Truncate\TruncateService */
31
	protected $truncator;
32
33
	/** @var \blitze\sitemaker\services\date_range */
34
	protected $date_range;
35
36
	/** @var \blitze\sitemaker\services\forum\data */
37
	protected $forum_data;
38
39
	/** @var \blitze\sitemaker\services\forum\options */
40
	protected $forum_options;
41
42
	/** @var string */
43
	protected $phpbb_root_path;
44
45
	/** @var string */
46
	protected $php_ext;
47
48
	/** @var array */
49
	private $fields = array();
50
51
	/** @var array */
52
	private $settings = array();
53
54
	/** @var array */
55
	private $topic_tracking_info = array();
56
57
	/**
58
	 * Constructor
59
	 *
60
	 * @param \phpbb\auth\auth							$auth				Permission object
61
	 * @param \phpbb\content_visibility					$content_visibility	Content visibility object
62
	 * @param \phpbb\language\language					$translator			Language object
63
	 * @param \phpbb\user								$user				User object
64
	 * @param \Urodoz\Truncate\TruncateService			$truncator			Truncator service
65
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
66
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
67
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
68 7
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
69
	 * @param string									$php_ext			php file extension
70 7
	 */
71
	public function __construct(\phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, \phpbb\language\language $translator, \phpbb\user $user, \Urodoz\Truncate\TruncateService $truncator, \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)
72 7
	{
73 7
		parent::__construct($forum_options);
74 7
75 7
		$this->auth = $auth;
76 7
		$this->content_visibility = $content_visibility;
77 7
		$this->translator = $translator;
78 7
		$this->user = $user;
79 7
		$this->truncator = $truncator;
80 7
		$this->date_range = $date_range;
81
		$this->forum_data = $forum_data;
82
		$this->phpbb_root_path = $phpbb_root_path;
83
		$this->php_ext = $php_ext;
84
	}
85 6
86
	/**
87 6
	 * {@inheritdoc}
88
	 */
89 6
	public function display(array $bdata, $edit_mode = false)
90
	{
91 6
		$this->settings = $bdata['settings'];
92 6
93 6
		$topic_data = $this->get_topic_data();
94 6
95 6
		$data = null;
96
		if (sizeof($topic_data))
97
		{
98 6
			$data = $this->get_block_content($topic_data);
99 6
		}
100 6
101
		return array(
102
			'title'	=> $this->get_block_title(),
103
			'data'	=> $data,
104
		);
105
	}
106
107 6
	/**
108
	 * @param array $topic_data
109 6
	 * @return array
110
	 */
111 6
	protected function get_block_content(array $topic_data)
112 6
	{
113 6
		$this->set_display_fields();
114
115 6
		$post_data = $this->get_post_data($topic_data);
116 6
		$topic_data = array_values($topic_data);
117
118 6
		return array(
119 6
			'CONTEXT'			=> $this->settings['context'],
120 6
			'TEMPLATE'			=> $this->settings['template'],
121 6
			'TOPICS'			=> $this->get_topics($topic_data, $post_data),
122 6
			'S_IS_BOT'			=> $this->user->data['is_bot'],
123 6
			'LAST_POST_IMG'		=> $this->user->img('icon_topic_latest'),
124
			'NEWEST_POST_IMG'	=> $this->user->img('icon_topic_newest'),
125 6
		);
126
	}
127
128
	/**
129
	 * @param array $topic_data
130
	 * @param array $post_data
131
	 * @return array
132 6
	 */
133
	protected function get_topics(array &$topic_data, array &$post_data)
134 6
	{
135
		$user_data = $this->forum_data->get_posters_info();
136 6
137
		$topics = [];
138 6
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
139 6
		{
140 6
			$row = $topic_data[$i];
141 6
			$forum_id = $row['forum_id'];
142 6
			$topic_id = $row['topic_id'];
143
			$author = $user_data[$row[$this->fields['user_id']]];
144 6
			$last_poster = $user_data[$row['topic_last_poster_id']];
145 6
146 6
			$topics[] = array(
147 6
				'USERNAME'			=> $author['username_full'],
148 6
				'AVATAR'			=> $author['avatar'],
149
				'LAST_POSTER'		=> $last_poster['username_full'],
150 6
				'LAST_AVATAR'		=> $last_poster['avatar'],
151 6
152 6
				'FORUM_TITLE'		=> $row['forum_name'],
153 6
				'FORUM_IMAGE'		=> $this->get_forum_image($row['forum_image']),
154 6
				'TOPIC_TITLE'		=> truncate_string(censor_text($row['topic_title']), $this->settings['topic_title_limit'], 255, false, '...'),
155 6
				'TOPIC_PREVIEW'		=> $this->get_post_preview(array_pop($post_data[$topic_id])),
156 6
				'TOPIC_POST_TIME'	=> $this->user->format_date($row[$this->fields['time']]),
157 6
				'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($forum_id, $row['topic_attachment']),
158
				'REPLIES'			=> $this->content_visibility->get_count('topic_posts', $row, $forum_id) - 1,
159 6
				'VIEWS'				=> (int) $row['topic_views'],
160 6
				'S_UNREAD_TOPIC'	=> $this->is_unread_topic($forum_id, $topic_id, $row['topic_last_post_time']),
161 6
162 6
				'U_VIEWPROFILE'		=> $author['u_viewprofile'],
163 6
				'U_VIEWTOPIC'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
164 6
				'U_VIEWFORUM'		=> append_sid($this->phpbb_root_path . 'viewforum.' . $this->php_ext, "f=$forum_id"),
165 6
				'U_NEW_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
166 6
				'U_LAST_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;p=" . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
167
			);
168
			unset($topic_data[$i], $post_data[$topic_id]);
169
		}
170
171 6
		return $topics;
172
	}
173
174 6
	/**
175 6
	 * @return string
176 6
	 */
177 6
	protected function get_block_title()
178 6
	{
179
		$types = array(
180
			POST_GLOBAL		=> 'FORUM_GLOBAL_ANNOUNCEMENTS',
181 6
			POST_ANNOUNCE	=> 'FORUM_ANNOUNCEMENTS',
182
			POST_STICKY		=> 'FORUM_STICKY_POSTS',
183 6
			POST_NORMAL		=> 'FORUM_RECENT_TOPICS',
184
		);
185
186
		// if more than one topic type is selected, we default to RECENT_TOPICS
187
		$topic_type = join(',', $this->settings['topic_type']);
188
189
		return ($this->settings['order_by'] !== self::FORUMS_ORDER_LAST_READ) ? (isset($types[$topic_type]) ? $types[$topic_type] : 'FORUM_RECENT_TOPICS') : 'TOPICS_LAST_READ';
190 6
	}
191
192 6
	/**
193 6
	 * @param array $row
194 6
	 * @return string
195 3
	 */
196 3
	protected function get_post_preview(array $row)
197 3
	{
198
		$preview = '';
199 6
		if ($this->settings['preview_chars'])
200
		{
201
			$method = ($this->settings['template'] === 'context') ? 'get_trimmed_text' : 'get_tooltip_text';
202
			$preview = call_user_func_array(array($this, $method), array($row));
203
		}
204
205 1
		return $preview;
206
	}
207 1
208 1
	/**
209
	 * @param array $row
210 1
	 */
211 1
	protected function get_trimmed_text(array $row)
212
	{
213
		$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
214
		$row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
215
216
		return $this->truncator->truncate($row['post_text'], $this->settings['preview_chars']);
217
	}
218 2
219
	/**
220 2
	 * @param array $row
221
	 * @return string
222 2
	 */
223 2
	protected function get_tooltip_text(array $row)
224
	{
225
		strip_bbcode($row['post_text'], $row['bbcode_uid']);
226
227
		$row['post_text'] = truncate_string($row['post_text'], $this->settings['preview_chars']);
228
		return wordwrap($row['post_text'], 40, "\n");
229 6
	}
230
231
	/**
232 6
	 * @return array
233 6
	 */
234 6
	private function get_topic_data()
235 6
	{
236
		$sort_order = array(
237 6
			self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
238
			self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
239 6
			self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
240 6
		);
241 6
242 6
		$range_info = $this->date_range->get($this->settings['date_range']);
243 6
244 6
		$this->forum_data->query($this->settings['enable_tracking'])
245
			->fetch_forum($this->settings['forum_ids'])
246 6
			->fetch_topic_type($this->settings['topic_type'])
247 6
			->fetch_date_range($range_info['start'], $range_info['stop'])
248
			->set_sorting($sort_order[$this->settings['order_by']])
249 6
			->build();
250
251
		$topic_data = $this->forum_data->get_topic_data($this->settings['max_topics']);
252
		$this->topic_tracking_info = $this->forum_data->get_topic_tracking_info();
253
254
		return $topic_data;
255
	}
256 6
257
	/**
258 6
	 * @param array $topic_data
259 6
	 * @return array
260 3
	 */
261 3
	private function get_post_data(array $topic_data)
262
	{
263
		if ($this->settings['context'] && $this->settings['preview_chars'])
264 3
		{
265
			$post_data = $this->forum_data->get_post_data($this->settings['context']);
266
		}
267 6
		else
268
		{
269
			$post_data = array_fill_keys(array_keys($topic_data), array(array('post_text' => '', 'bbcode_uid' => '', 'bbcode_bitfield' => '')));
270
		}
271
272
		return $post_data;
273 6
	}
274
275 6
	/**
276 6
	 * @return void
277 3
	 */
278 3
	private function set_display_fields()
279
	{
280 3
		if ($this->settings['context'] === 'last')
281 3
		{
282
			$this->fields['time'] = 'topic_last_post_time';
283
			$this->fields['user_id'] = 'topic_last_poster_id';
284 3
		}
285 3
		else
286
		{
287 6
			$this->fields['time'] = 'topic_time';
288
			$this->fields['user_id'] = 'topic_poster';
289
		}
290
	}
291
292
	/**
293
	 * @param int $forum_id
294 6
	 * @param int $topic_attachment
295
	 * @return string
296 6
	 */
297
	private function get_attachment_icon($forum_id, $topic_attachment)
298
	{
299
		return ($this->user_can_view_attachments($forum_id) && $topic_attachment) ? $this->user->img('icon_topic_attach', $this->translator->lang('TOTAL_ATTACHMENTS')) : '';
300
	}
301
302
	/**
303 6
	 * @param int $forum_id
304
	 * @return bool
305 6
	 */
306
	private function user_can_view_attachments($forum_id)
307
	{
308
		return ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $forum_id)) ? true : false;
309
	}
310
311
	/**
312
	 * @param int $forum_id
313
	 * @param int $topic_id
314 6
	 * @param int $topic_last_post_time
315
	 * @return bool
316 6
	 */
317
	private function is_unread_topic($forum_id, $topic_id, $topic_last_post_time)
318
	{
319
		return (isset($this->topic_tracking_info[$forum_id][$topic_id]) && $topic_last_post_time > $this->topic_tracking_info[$forum_id][$topic_id]) ? true : false;
320
	}
321
322
	/**
323
	 * @param string $forum_image
324
	 * @return string
325
	 */
326
	private function get_forum_image($forum_image)
327
	{
328
		return $forum_image ? '<img src="' . $this->phpbb_root_path . $forum_image . '" alt="' . $this->translator->lang('FORUM_CAT') . '" />' : '';
329
	}
330
}
331