Completed
Push — develop ( 843010...da46c8 )
by Daniel
07:37
created

topic::show_attachments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 3
crap 12
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
	/**
36
	 * Construct
37
	 *
38
	 * @param \phpbb\config\config					$config					Config object
39
	 * @param \phpbb\controller\helper				$controller_helper		Controller Helper object
40
	 * @param \phpbb\event\dispatcher_interface		$phpbb_dispatcher		Event dispatcher object
41
	 * @param \phpbb\language\language				$language				Language object
42
	 * @param \phpbb\template\template				$template				Template object
43
	 * @param \phpbb\user							$user					User object
44
	 * @param \blitze\content\services\helper		$helper					Content helper object
45
	 */
46
	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)
47
	{
48
		$this->config = $config;
49
		$this->controller_helper = $controller_helper;
50
		$this->phpbb_dispatcher = $phpbb_dispatcher;
51
		$this->language = $language;
52
		$this->template = $template;
53
		$this->user = $user;
54
		$this->helper = $helper;
55
	}
56
57
	/**
58
	 * @param string $type
59
	 * @param array $topic_data
60
	 * @param array $topic_tracking_info
61
	 * @return array
62
	 */
63
	public function get_min_topic_info($type, array &$topic_data, array $topic_tracking_info)
64
	{
65
		$topic_id = $topic_data['topic_id'];
66
		$post_unread = (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
67
		$topic_data['topic_url'] = $this->get_topic_url($type, $topic_data);
68
69
		return array(
70
			'TOPIC_ID'			=> $topic_data['topic_id'],
71
			'TOPIC_VIEWS'		=> $topic_data['topic_views'],
72
			'TOPIC_TITLE'		=> censor_text($topic_data['topic_title']),
73
			'TOPIC_DATE'		=> $this->user->format_date($topic_data['topic_time']),
74
			'TOPIC_URL'			=> $topic_data['topic_url'],
75
			'MINI_POST'			=> ($post_unread) ? $this->user->img('icon_post_target_unread', 'UNREAD_POST') : $this->user->img('icon_post_target', 'POST'),
76
			'S_REQ_MOD_INPUT'	=> $topic_data['req_mod_input'],
77
			'S_UNREAD_POST'		=> $post_unread,
78
		);
79
	}
80
81
	/**
82
	 * @param string $type
83
	 * @param array $topic_data
84
	 * @param array $post_data
85
	 * @param array $users_cache
86
	 * @param array $attachments
87
	 * @param array $topic_tracking_info
88
	 * @param array $update_count
89
	 * @return array
90
	 */
91
	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)
92
	{
93
		$tpl_data = array_merge(
94
			(empty($topic_data['topic_url'])) ? $this->get_min_topic_info($type, $topic_data, $topic_tracking_info) : array(),
95
			array(
96
				'POST_ID'				=> $post_data['post_id'],
97
				'POSTER_ID'				=> $post_data['poster_id'],
98
				'MESSAGE'				=> $this->get_parsed_text($post_data, $attachments, $update_count),
99
100
				'S_TOPIC_TYPE'			=> $topic_data['topic_type'],
101
				'S_HAS_ATTACHMENTS'		=> $topic_data['topic_attachment'],
102
				'S_HAS_POLL'			=> (bool) $post_data['poll_start'],
103
			),
104
			$this->get_topic_status_data($type, $topic_data, $post_data),
105
			array_change_key_case($users_cache[$post_data['poster_id']], CASE_UPPER)
106
		);
107
108
		/**
109
		 * Event to modify template data
110
		 *
111
		 * @event blitze.content.modify_template_data
112
		 * @var	array	tpl_data	Array containing template data
113
		 */
114
		$vars = array('tpl_data');
115
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.modify_template_data', compact($vars)));
116
117
		return $tpl_data;
118
	}
119
120
	/**
121
	 * @param string $type
122
	 * @param array $topic_data
123
	 * @param array $post_data
124
	 * @param array $users_cache
125
	 * @param array $attachments
126
	 * @param array $topic_tracking_info
127
	 * @param array $update_count
128
	 * @param string $mode
129
	 * @return array
130
	 */
131
	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, $mode = '')
132
	{
133
		return array_merge(
134
			$this->get_summary_template_data($type, $topic_data, $post_data, $users_cache, $attachments, $topic_tracking_info, $update_count),
135
			$this->show_delete_reason($post_data, $users_cache),
136
			$this->show_edit_reason($post_data, $users_cache),
137
			array(
138
				'S_DISPLAY_NOTICE'		=> $this->helper->display_attachments_notice($post_data),
139
				'S_DELETE_PERMANENT'	=> $this->helper->permanent_delete_allowed($post_data),
140
				'S_IS_LOCKED'			=> $this->helper->topic_is_locked($topic_data),
141
142
				'U_EDIT'			=> $this->helper->get_edit_url($post_data, $topic_data, $mode),
143
				'U_QUOTE'			=> $this->helper->get_quote_url($post_data, $topic_data),
144
				'U_INFO'			=> $this->helper->get_info_url($post_data),
145
				'U_DELETE'			=> $this->helper->get_delete_url($post_data, $topic_data, $mode),
146
				'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'])) : '',
147
				'U_APPROVE_ACTION'	=> $this->helper->get_approve_url($post_data, $topic_data['topic_url']),
148
				'U_MCP_EDIT'		=> $this->helper->get_mcp_edit_url($post_data, $topic_data),
149
				'U_MCP_RESTORE'		=> $this->helper->get_mcp_restore_url($post_data, $topic_data),
150
				'U_NOTES'			=> $this->helper->get_notes_url($post_data),
151
				'U_WARN'			=> $this->helper->get_warning_url($post_data),
152
			)
153
		);
154
	}
155
156
	/**
157
	 * @param array $topic_data
158
	 * @return array
159
	 */
160
	public function get_topic_tools_data(array $topic_data)
161
	{
162
		return array_merge(array(
163
				'U_PRINT_TOPIC'		=> $this->helper->get_print_topic_url($topic_data),
164
				'U_EMAIL_TOPIC'		=> $this->helper->get_email_topic_url($topic_data),
165
			),
166
			$this->get_watch_status_data($topic_data),
167
			$this->get_bookmark_status_data($topic_data)
168
		);
169
	}
170
171
	/**
172
	 * @param string $type
173
	 * @param array $topic_data
174
	 * @return string
175
	 */
176
	public function get_topic_url($type, array $topic_data)
177
	{
178
		return $this->controller_helper->route('blitze_content_show', array(
179
			'type'		=> $type,
180
			'topic_id'	=> $topic_data['topic_id'],
181
			'slug'		=> $topic_data['topic_slug']
182
		));
183
	}
184
185
	/**
186
	 * @param array $attachments
187
	 * @param int $post_id
188
	 * @param string $handle
189
	 * @return void
190
	 */
191
	 public function show_attachments(array $attachments, $post_id, $handle = 'attachment')
192
	 {
193
		if (!empty($attachments[$post_id]))
194
		{
195
			foreach ($attachments[$post_id] as $attachment)
196
			{
197
				$this->template->assign_block_vars($handle, array(
198
					'DISPLAY_ATTACHMENT' => $attachment)
199
				);
200
			}
201
		}
202
	 }
203
204
	/**
205
	 * @param array $post_data
206
	 * @param array $attachments
207
	 * @param array $update_count
208
	 * @return array
209
	 */
210
	protected function get_parsed_text(array $post_data, array &$attachments, array &$update_count)
211
	{
212
		$parse_flags = ($post_data['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
213
		$message = generate_text_for_display($post_data['post_text'], $post_data['bbcode_uid'], $post_data['bbcode_bitfield'], $parse_flags, true);
214
	
215
		if (!empty($attachments[$post_data['post_id']]))
216
		{
217
			parse_attachments($post_data['forum_id'], $message, $attachments[$post_data['post_id']], $update_count);
218
		}
219
220
		return $message;
221
	}
222
223
	/**
224
	 * @param string $type
225
	 * @param array $topic_data
226
	 * @param array $post_data
227
	 * @return array
228
	 */
229
	protected function get_topic_status_data($type, array $topic_data, array $post_data)
230
	{
231
		return array(
232
			'S_POST_UNAPPROVED'		=> $this->helper->post_is_unapproved($post_data),
233
			'S_POSTS_UNAPPROVED'	=> $this->helper->topic_has_unapproved_posts($topic_data),
234
			'S_TOPIC_REPORTED'		=> $this->helper->topic_is_reported($topic_data),
235
			'S_TOPIC_DELETED'		=> $topic_data['topic_visibility'] == ITEM_DELETED,
236
237
			'U_MINI_POST'			=> $this->get_mini_post_url($topic_data, $post_data),
238
			'U_MCP_REPORT'			=> $this->helper->get_mcp_report_url($post_data),
239
			'U_MCP_REVIEW'			=> $this->helper->get_mcp_review_url($type, $topic_data['topic_id']),
240
			'U_MCP_QUEUE'			=> $this->helper->get_mcp_queue_url($topic_data['topic_id']),
241
		);
242
	}
243
244
	/**
245
	 * @param array $topic_data
246
	 * @param array $post_data
247
	 * @return array
248
	 */
249
	protected function get_mini_post_url(array $topic_data, array $post_data)
250
	{
251
		if ($topic_data['topic_first_post_id'] === $post_data['post_id'])
252
		{
253
			return append_sid($topic_data['topic_url'], 'view=unread') . '#unread';
254
		}
255
256
		return append_sid($topic_data['topic_url'], 'p=' . $post_data['post_id']) . '#p' . $post_data['post_id'];
257
	}
258
259
	/**
260
	 * @param array $row
261
	 * @param array $users_cache
262
	 * @return array
263
	 */
264
	protected function show_edit_reason(array $row, array $users_cache)
265
	{
266
		$l_edited_by = $edit_reason = '';
267
		if (($row['post_edit_count'] && $this->config['display_last_edited']) || $row['post_edit_reason'])
268
		{
269
			$display_username	= $users_cache[$row['poster_id']]['username_full'];
270
			$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));
271
			$edit_reason = $row['post_edit_reason'];
272
		}
273
274
		return array(
275
			'EDITED_MESSAGE'	=> $l_edited_by,
276
			'EDIT_REASON'		=> $edit_reason,
277
		);
278
	}
279
280
	/**
281
	 * @param array $row
282
	 * @param array $users_cache
283
	 * @return array
284
	 */
285
	protected function show_delete_reason(array $row, array $users_cache)
286
	{
287
		$l_deleted_by = $delete_reason = $l_deleted_message = '';
288
		$s_post_deleted = ($row['post_visibility'] == ITEM_DELETED) ? true : false;
289
290
		if ($s_post_deleted && $row['post_delete_user'])
291
		{
292
			$display_postername	= $users_cache[$row['poster_id']]['username_full'];
0 ignored issues
show
Unused Code introduced by
$display_postername is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
293
			$display_username	= $users_cache[$row['post_delete_user']]['username_full'];
294
295
			$l_deleted_message = $this->get_delete_message($row, $display_poster, $display_username);
0 ignored issues
show
Bug introduced by
The variable $display_poster does not exist. Did you mean $display_postername?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
296
			$l_deleted_by = $this->language->lang('DELETED_INFORMATION', $display_username, $this->user->format_date($row['post_delete_time'], false, true));
297
			$delete_reason = $row['post_delete_reason'];
298
		}
299
300
		return array(
301
			'DELETED_MESSAGE'			=> $l_deleted_by,
302
			'DELETE_REASON'				=> $delete_reason,
303
			'L_POST_DELETED_MESSAGE'	=> $l_deleted_message,
304
			'S_POST_DELETED'			=> $s_post_deleted,
305
		);
306
	}
307
308
	/**
309
	 * @param array $row
310
	 * @param string $display_postername
311
	 * @param string $display_username
312
	 * @return string
313
	 */
314
	protected function get_delete_message(array $row, $display_postername, $display_username)
315
	{
316
		if ($row['post_delete_reason'])
317
		{
318
			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']);
319
		}
320
		else
321
		{
322
			return $this->language->lang('POST_DELETED_BY', $display_postername, $display_username, $this->user->format_date($row['post_delete_time'], false, true));
323
		}
324
	}
325
326
	/**
327
	 * @param array $row
0 ignored issues
show
Bug introduced by
There is no parameter named $row. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
328
	 * @return array
329
	 */
330
	protected function get_watch_status_data(array $topic_data)
331
	{
332
		$s_watching_topic = array(
333
			'link'			=> '',
334
			'link_toggle'	=> '',
335
			'title'			=> '',
336
			'title_toggle'	=> '',
337
			'is_watching'	=> false,
338
		);
339
340
		if ($this->config['allow_topic_notify'])
341
		{
342
			$notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
343
			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']);
344
		}
345
346
		return array(
347
			'U_WATCH_TOPIC'			=> $s_watching_topic['link'],
348
			'U_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['link_toggle'],
349
			'S_WATCH_TOPIC_TITLE'	=> $s_watching_topic['title'],
350
			'S_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['title_toggle'],
351
			'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
352
		);
353
	}
354
355
	/**
356
	 * @param array $topic_data
357
	 * @return array
358
	 */
359
	protected function get_bookmark_status_data(array $topic_data)
360
	{
361
		$bookmarked = (bool) $topic_data['bookmarked'];
362
		$state_key = (int) $bookmarked;
363
		$lang_keys = array(
364
			'toggle'	=> array('BOOKMARK_TOPIC_REMOVE', 'BOOKMARK_TOPIC'),
365
			'bookmark'	=> array('BOOKMARK_TOPIC', 'BOOKMARK_TOPIC_REMOVE'),
366
		);
367
368
		return array(
369
			'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']}") : '',
370
			'S_BOOKMARK_TOPIC'		=> $this->language->lang($lang_keys['bookmark'][$state_key]),
371
			'S_BOOKMARK_TOGGLE'		=> $this->language->lang($lang_keys['toggle'][$state_key]),
372
			'S_BOOKMARKED_TOPIC'	=> $bookmarked,
373
		);
374
375
	}
376
}
377