Completed
Push — develop ( 7d0f73...48b424 )
by Daniel
12:13 queued 09:25
created

forum_topics::get_preview_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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