topic   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 373
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 146
c 2
b 0
f 0
dl 0
loc 373
ccs 0
cts 211
cp 0
rs 9.44
wmc 37

15 Methods

Rating   Name   Duplication   Size   Complexity  
A get_topic_tools_data() 0 8 1
A get_delete_message() 0 9 2
A get_topic_url() 0 6 1
A show_attachments() 0 8 3
A get_bookmark_status_data() 0 14 4
A get_watch_status_data() 0 22 3
A get_topic_status_data() 0 12 1
A get_detail_template_data() 0 21 2
A __construct() 0 12 1
A get_summary_template_data() 0 28 2
A get_min_topic_info() 0 17 4
A get_mini_post_url() 0 8 2
A get_parsed_text() 0 11 3
A show_edit_reason() 0 13 4
A show_delete_reason() 0 20 4
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services;
11
12
class topic
13
{
14
	/** @var \phpbb\config\config */
15
	protected $config;
16
17
	/** @var \phpbb\controller\helper */
18
	protected $controller_helper;
19
20
	/** @var \phpbb\event\dispatcher_interface */
21
	protected $phpbb_dispatcher;
22
23
	/** @var \phpbb\language\language */
24
	protected $language;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/* @var \blitze\content\services\helper */
33
	protected $helper;
34
35
	/* @var string */
36
	protected $short_date_format;
37
38
	/* @var string */
39
	protected $long_date_format;
40
41
	/**
42
	 * Construct
43
	 *
44
	 * @param \phpbb\config\config					$config					Config object
45
	 * @param \phpbb\controller\helper				$controller_helper		Controller Helper object
46
	 * @param \phpbb\event\dispatcher_interface		$phpbb_dispatcher		Event dispatcher object
47
	 * @param \phpbb\language\language				$language				Language object
48
	 * @param \phpbb\template\template				$template				Template object
49
	 * @param \phpbb\user							$user					User object
50
	 * @param \blitze\content\services\helper		$helper					Content helper object
51
	 */
52
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $controller_helper, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \blitze\content\services\helper $helper)
53
	{
54
		$this->config = $config;
55
		$this->controller_helper = $controller_helper;
56
		$this->phpbb_dispatcher = $phpbb_dispatcher;
57
		$this->language = $language;
58
		$this->template = $template;
59
		$this->user = $user;
60
		$this->helper = $helper;
61
62
		$this->short_date_format = $this->language->lang('CONTENT_SHORT_DATE_FORMAT');
63
		$this->long_date_format = $this->language->lang('CONTENT_LONG_DATE_FORMAT');
64
	}
65
66
	/**
67
	 * @param string $type
68
	 * @param array $topic_data
69
	 * @param array $topic_tracking_info
70
	 * @return array
71
	 */
72
	public function get_min_topic_info($type, array &$topic_data, array $topic_tracking_info)
73
	{
74
		$topic_id = $topic_data['topic_id'];
75
		$post_unread = (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
76
		$topic_data['topic_url'] = $this->get_topic_url($type, $topic_data);
77
78
		return array(
79
			'TOPIC_ID'			=> $topic_data['topic_id'],
80
			'TOPIC_VIEWS'		=> $topic_data['topic_views'],
81
			'TOPIC_TITLE'		=> censor_text($topic_data['topic_title']),
82
			'TOPIC_DATE'		=> $this->user->format_date($topic_data['topic_time'], $this->short_date_format),
83
			'TOPIC_DATE_LONG'	=> $this->user->format_date($topic_data['topic_time'], $this->long_date_format),
84
			'TOPIC_UNIX_TIME'	=> $topic_data['topic_time'],
85
			'TOPIC_URL'			=> $topic_data['topic_url'],
86
			'MINI_POST'			=> ($post_unread) ? $this->user->img('icon_post_target_unread', 'UNREAD_POST') : $this->user->img('icon_post_target', 'POST'),
87
			'S_REQ_MOD_INPUT'	=> $topic_data['req_mod_input'],
88
			'S_UNREAD_POST'		=> $post_unread,
89
		);
90
	}
91
92
	/**
93
	 * @param string $type
94
	 * @param array $topic_data
95
	 * @param array $post_data
96
	 * @param array $users_cache
97
	 * @param array $attachments
98
	 * @param array $topic_tracking_info
99
	 * @param array $update_count
100
	 * @return array
101
	 */
102
	public function get_summary_template_data($type, array &$topic_data, array $post_data, array $users_cache, array &$attachments, array $topic_tracking_info, array &$update_count)
103
	{
104
		$tpl_data = array_merge(
105
			(empty($topic_data['topic_url'])) ? $this->get_min_topic_info($type, $topic_data, $topic_tracking_info) : array(),
106
			array(
107
				'POST_ID'				=> $post_data['post_id'],
108
				'POSTER_ID'				=> $post_data['poster_id'],
109
				'UPDATED'				=> max($post_data['post_edit_time'], $topic_data['topic_time']),
110
				'MESSAGE'				=> $this->get_parsed_text($post_data, $attachments, $update_count),
111
112
				'S_TOPIC_TYPE'			=> $topic_data['topic_type'],
113
				'S_HAS_ATTACHMENTS'		=> $topic_data['topic_attachment'],
114
				'S_HAS_POLL'			=> (bool) $topic_data['poll_start'],
115
			),
116
			$this->get_topic_status_data($type, $topic_data, $post_data),
117
			array_change_key_case($users_cache[$post_data['poster_id']], CASE_UPPER)
118
		);
119
120
		/**
121
		 * Event to modify template data
122
		 *
123
		 * @event blitze.content.modify_template_data
124
		 * @var	array	tpl_data	Array containing template data
125
		 */
126
		$vars = array('tpl_data');
127
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.modify_template_data', compact($vars)));
128
129
		return $tpl_data;
130
	}
131
132
	/**
133
	 * @param string $type
134
	 * @param array $topic_data
135
	 * @param array $post_data
136
	 * @param array $users_cache
137
	 * @param array $attachments
138
	 * @param array $topic_tracking_info
139
	 * @param array $update_count
140
	 * @param string $redirect_url
141
	 * @return array
142
	 */
143
	public function get_detail_template_data($type, array &$topic_data, array $post_data, array $users_cache, array &$attachments, array $topic_tracking_info, array &$update_count, $redirect_url = '')
144
	{
145
		return array_merge(
146
			$this->get_summary_template_data($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count),
147
			$this->show_delete_reason($post_data, $users_cache),
148
			$this->show_edit_reason($post_data, $users_cache),
149
			array(
150
				'S_DISPLAY_NOTICE'		=> $this->helper->display_attachments_notice($post_data),
151
				'S_DELETE_PERMANENT'	=> $this->helper->permanent_delete_allowed($post_data),
152
				'S_IS_LOCKED'			=> $this->helper->topic_is_locked($topic_data),
153
154
				'U_EDIT'			=> $this->helper->get_edit_url($post_data, $topic_data, $redirect_url),
155
				'U_QUOTE'			=> $this->helper->get_quote_url($post_data, $topic_data),
156
				'U_INFO'			=> $this->helper->get_info_url($post_data),
157
				'U_DELETE'			=> $this->helper->get_delete_url($post_data, $topic_data, $redirect_url),
158
				'U_REPORT'			=> $this->helper->can_report_post($post_data['forum_id']) ? $this->controller_helper->route('phpbb_report_post_controller', array('id' => $post_data['post_id'])) : '',
159
				'U_APPROVE_ACTION'	=> $this->helper->get_approve_url($post_data, $topic_data['topic_url']),
160
				'U_MCP_EDIT'		=> $this->helper->get_mcp_edit_url($post_data, $topic_data, $redirect_url),
161
				'U_MCP_RESTORE'		=> $this->helper->get_mcp_restore_url($post_data, $topic_data),
162
				'U_NOTES'			=> $this->helper->get_notes_url($post_data),
163
				'U_WARN'			=> $this->helper->get_warning_url($post_data),
164
			)
165
		);
166
	}
167
168
	/**
169
	 * @param array $topic_data
170
	 * @return array
171
	 */
172
	public function get_topic_tools_data(array $topic_data)
173
	{
174
		return array_merge(array(
175
				'U_PRINT_TOPIC'		=> $this->helper->get_print_topic_url($topic_data),
176
				'U_EMAIL_TOPIC'		=> $this->helper->get_email_topic_url($topic_data),
177
			),
178
			$this->get_watch_status_data($topic_data),
179
			$this->get_bookmark_status_data($topic_data)
180
		);
181
	}
182
183
	/**
184
	 * @param string $type
185
	 * @param array $topic_data
186
	 * @return string
187
	 */
188
	public function get_topic_url($type, array $topic_data)
189
	{
190
		return $this->controller_helper->route('blitze_content_show', array(
191
			'type'		=> $type,
192
			'topic_id'	=> $topic_data['topic_id'],
193
			'slug'		=> $topic_data['topic_slug']
194
		));
195
	}
196
197
	/**
198
	 * @param array $attachments
199
	 * @param int $post_id
200
	 * @param string $handle
201
	 * @return void
202
	 */
203
	 public function show_attachments(array $attachments, $post_id, $handle = 'attachment')
204
	 {
205
		if (!empty($attachments[$post_id]))
206
		{
207
			foreach ($attachments[$post_id] as $attachment)
208
			{
209
				$this->template->assign_block_vars($handle, array(
210
					'DISPLAY_ATTACHMENT' => $attachment)
211
				);
212
			}
213
		}
214
	 }
215
216
	/**
217
	 * @param array $post_data
218
	 * @param array $attachments
219
	 * @param array $update_count
220
	 * @return string
221
	 */
222
	protected function get_parsed_text(array $post_data, array &$attachments, array &$update_count)
223
	{
224
		$parse_flags = ($post_data['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
225
		$message = generate_text_for_display($post_data['post_text'], $post_data['bbcode_uid'], $post_data['bbcode_bitfield'], $parse_flags, true);
226
227
		if (!empty($attachments[$post_data['post_id']]))
228
		{
229
			parse_attachments($post_data['forum_id'], $message, $attachments[$post_data['post_id']], $update_count);
230
		}
231
232
		return $message;
233
	}
234
235
	/**
236
	 * @param string $type
237
	 * @param array $topic_data
238
	 * @param array $post_data
239
	 * @return array
240
	 */
241
	protected function get_topic_status_data($type, array $topic_data, array $post_data)
242
	{
243
		return array(
244
			'S_POST_UNAPPROVED'		=> $this->helper->post_is_unapproved($post_data),
245
			'S_POSTS_UNAPPROVED'	=> $this->helper->topic_has_unapproved_posts($topic_data),
246
			'S_TOPIC_REPORTED'		=> $this->helper->topic_is_reported($topic_data),
247
			'S_TOPIC_DELETED'		=> $topic_data['topic_visibility'] == ITEM_DELETED,
248
249
			'U_MINI_POST'			=> $this->get_mini_post_url($topic_data, $post_data),
250
			'U_MCP_REPORT'			=> $this->helper->get_mcp_report_url($post_data),
251
			'U_MCP_REVIEW'			=> $this->helper->get_mcp_review_url($type, $topic_data['topic_id']),
252
			'U_MCP_QUEUE'			=> $this->helper->get_mcp_queue_url($topic_data['topic_id']),
253
		);
254
	}
255
256
	/**
257
	 * @param array $topic_data
258
	 * @param array $post_data
259
	 * @return string
260
	 */
261
	protected function get_mini_post_url(array $topic_data, array $post_data)
262
	{
263
		if ($topic_data['topic_first_post_id'] === $post_data['post_id'])
264
		{
265
			return append_sid($topic_data['topic_url'], 'view=unread') . '#unread';
266
		}
267
268
		return append_sid($topic_data['topic_url'], 'p=' . $post_data['post_id']) . '#p' . $post_data['post_id'];
269
	}
270
271
	/**
272
	 * @param array $row
273
	 * @param array $users_cache
274
	 * @return array
275
	 */
276
	protected function show_edit_reason(array $row, array $users_cache)
277
	{
278
		$l_edited_by = $edit_reason = '';
279
		if (($row['post_edit_count'] && $this->config['display_last_edited']) || $row['post_edit_reason'])
280
		{
281
			$display_username	= $users_cache[$row['poster_id']]['username_full'];
282
			$l_edited_by = $this->language->lang('EDITED_TIMES_TOTAL', (int) $row['post_edit_count'], $display_username, $this->user->format_date($row['post_edit_time'], false, true));
283
			$edit_reason = $row['post_edit_reason'];
284
		}
285
286
		return array(
287
			'EDITED_MESSAGE'	=> $l_edited_by,
288
			'EDIT_REASON'		=> $edit_reason,
289
		);
290
	}
291
292
	/**
293
	 * @param array $row
294
	 * @param array $users_cache
295
	 * @return array
296
	 */
297
	protected function show_delete_reason(array $row, array $users_cache)
298
	{
299
		$l_deleted_by = $delete_reason = $l_deleted_message = '';
300
		$s_post_deleted = ($row['post_visibility'] == ITEM_DELETED) ? true : false;
301
302
		if ($s_post_deleted && $row['post_delete_user'])
303
		{
304
			$display_postername	= $users_cache[$row['poster_id']]['username_full'];
305
			$display_username	= $users_cache[$row['post_delete_user']]['username_full'];
306
307
			$l_deleted_message = $this->get_delete_message($row, $display_postername, $display_username);
308
			$l_deleted_by = $this->language->lang('DELETED_INFORMATION', $display_username, $this->user->format_date($row['post_delete_time'], false, true));
309
			$delete_reason = $row['post_delete_reason'];
310
		}
311
312
		return array(
313
			'DELETED_MESSAGE'			=> $l_deleted_by,
314
			'DELETE_REASON'				=> $delete_reason,
315
			'L_POST_DELETED_MESSAGE'	=> $l_deleted_message,
316
			'S_POST_DELETED'			=> $s_post_deleted,
317
		);
318
	}
319
320
	/**
321
	 * @param array $row
322
	 * @param string $display_postername
323
	 * @param string $display_username
324
	 * @return string
325
	 */
326
	protected function get_delete_message(array $row, $display_postername, $display_username)
327
	{
328
		if ($row['post_delete_reason'])
329
		{
330
			return $this->language->lang('POST_DELETED_BY_REASON', $display_postername, $display_username, $this->user->format_date($row['post_delete_time'], false, true), $row['post_delete_reason']);
331
		}
332
		else
333
		{
334
			return $this->language->lang('POST_DELETED_BY', $display_postername, $display_username, $this->user->format_date($row['post_delete_time'], false, true));
335
		}
336
	}
337
338
	/**
339
	 * @param array $topic_data
340
	 * @return array
341
	 */
342
	protected function get_watch_status_data(array $topic_data)
343
	{
344
		$s_watching_topic = array(
345
			'link'			=> '',
346
			'link_toggle'	=> '',
347
			'title'			=> '',
348
			'title_toggle'	=> '',
349
			'is_watching'	=> false,
350
		);
351
352
		if ($this->config['allow_topic_notify'])
353
		{
354
			$notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
355
			watch_topic_forum('topic', $s_watching_topic, $this->user->data['user_id'], $topic_data['forum_id'], $topic_data['topic_id'], $notify_status, 0, $topic_data['topic_title']);
356
		}
357
358
		return array(
359
			'U_WATCH_TOPIC'			=> $s_watching_topic['link'],
360
			'U_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['link_toggle'],
361
			'S_WATCH_TOPIC_TITLE'	=> $s_watching_topic['title'],
362
			'S_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['title_toggle'],
363
			'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
364
		);
365
	}
366
367
	/**
368
	 * @param array $topic_data
369
	 * @return array
370
	 */
371
	protected function get_bookmark_status_data(array $topic_data)
372
	{
373
		$bookmarked = (bool) (isset($topic_data['bookmarked']) ? $topic_data['bookmarked'] : false);
374
		$state_key = (int) $bookmarked;
375
		$lang_keys = array(
376
			'toggle'	=> array('BOOKMARK_TOPIC_REMOVE', 'BOOKMARK_TOPIC'),
377
			'bookmark'	=> array('BOOKMARK_TOPIC', 'BOOKMARK_TOPIC_REMOVE'),
378
		);
379
380
		return array(
381
			'U_BOOKMARK_TOPIC'		=> ($this->user->data['is_registered'] && $this->config['allow_bookmarks']) ? $this->helper->get_viewtopic_url($topic_data) . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_{$topic_data['topic_id']}") : '',
382
			'S_BOOKMARK_TOPIC'		=> $this->language->lang($lang_keys['bookmark'][$state_key]),
383
			'S_BOOKMARK_TOGGLE'		=> $this->language->lang($lang_keys['toggle'][$state_key]),
384
			'S_BOOKMARKED_TOPIC'	=> $bookmarked,
385
		);
386
387
	}
388
}
389