Completed
Branch master (1410b2)
by Tobias
02:09
created

listener   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 26
Bugs 4 Features 4
Metric Value
wmc 26
c 26
b 4
f 4
lcom 2
cbo 1
dl 0
loc 343
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 19 1
A append_sid() 0 8 2
A display_forums_modify_sql() 0 10 1
A display_forums_modify_forum_rows() 0 11 2
B display_forums_modify_template_vars() 0 27 2
B pagination_generate_page_link() 0 20 6
A search_modify_tpl_ary() 0 12 1
A viewforum_modify_topicrow() 0 15 1
A viewforum_get_topic_data() 0 8 1
A viewtopic_get_post_data() 0 8 1
A viewtopic_assign_template_vars_before() 0 7 1
A viewtopic_modify_page_title() 0 8 1
A viewtopic_modify_post_row() 0 8 1
A generate_forum_nav() 0 15 2
A make_jumpbox_modify_tpl_ary() 0 11 2
1
<?php
2
/**
3
 *
4
 * @package phpBB Extension - tas2580 SEO URLs
5
 * @copyright (c) 2016 tas2580 (https://tas2580.net)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace tas2580\seourls\event;
11
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Event listener
16
 */
17
class listener implements EventSubscriberInterface
18
{
19
	/** @var \tas2580\seourls\event\base */
20
	protected $base;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\request\request */
26
	protected $request;
27
28
	/** @var \phpbb\path_helper */
29
	protected $path_helper;
30
31
	/** @var string phpbb_root_path */
32
	protected $phpbb_root_path;
33
34
	/** @var string php_ext */
35
	protected $php_ext;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \tas2580\seourls\event\base		$base
41
	 * @param \phpbb\template\template		$template				Template object
42
	 * @param \phpbb\request\request			$request				Request object
43
	 * @param \phpbb\path_helper			$path_helper			Controller helper object
44
	 * @param string						$phpbb_root_path		phpbb_root_path
45
	 * @param string						$php_ext				php_ext
46
	 * @access public
47
	 */
48
	public function __construct(\tas2580\seourls\event\base $base, \phpbb\template\template $template, \phpbb\request\request $request, \phpbb\path_helper $path_helper, $phpbb_root_path, $php_ext)
49
	{
50
		$this->base = $base;
51
		$this->template = $template;
52
		$this->request = $request;
53
		$this->path_helper = $path_helper;
54
		$this->phpbb_root_path = $phpbb_root_path;
55
		$this->php_ext = $php_ext;
56
	}
57
58
	/**
59
	 * Assign functions defined in this class to event listeners in the core
60
	 *
61
	 * @return array
62
	 * @static
63
	 * @access public
64
	 */
65
	public static function getSubscribedEvents()
66
	{
67
		return array(
68
			'core.append_sid'						=> 'append_sid',
69
			'core.display_forums_modify_sql'			=> 'display_forums_modify_sql',
70
			'core.display_forums_modify_template_vars'	=> 'display_forums_modify_template_vars',
71
			'core.display_forums_modify_forum_rows'		=> 'display_forums_modify_forum_rows',
72
			'core.generate_forum_nav'				=> 'generate_forum_nav',
73
			'core.make_jumpbox_modify_tpl_ary'			=> 'make_jumpbox_modify_tpl_ary',				// Not in phpBB
74
			'core.pagination_generate_page_link'		=> 'pagination_generate_page_link',
75
			'core.search_modify_tpl_ary'				=> 'search_modify_tpl_ary',
76
			'core.viewforum_modify_topicrow'			=> 'viewforum_modify_topicrow',
77
			'core.viewforum_get_topic_data'			=> 'viewforum_get_topic_data',
78
			'core.viewtopic_assign_template_vars_before'	=> 'viewtopic_assign_template_vars_before',
79
			'core.viewtopic_modify_page_title'			=> 'viewtopic_modify_page_title',
80
			'core.viewtopic_modify_post_row'			=> 'viewtopic_modify_post_row',
81
			'core.viewtopic_get_post_data'				=> 'viewtopic_get_post_data',
82
		);
83
	}
84
85
	/**
86
	 * Correct the path of $viewtopic_url
87
	 *
88
	 * @param	object	$event	The event object
89
	 * @return	null
90
	 * @access	public
91
	 */
92
	public function append_sid($event)
93
	{
94
		if (preg_match('#./../viewtopic.' . $this->php_ext  . '#', $event['url']))
95
		{
96
			$url = $this->phpbb_root_path . 'viewtopic.' . $this->php_ext ;
97
			$event['url'] = $url;
98
		}
99
	}
100
101
	/**
102
	 * Get informations for the last post from Database
103
	 *
104
	 * @param	object	$event	The event object
105
	 * @return	null
106
	 * @access	public
107
	 */
108
	public function display_forums_modify_sql($event)
109
	{
110
		$sql_array = $event['sql_ary'];
111
		$sql_array['LEFT_JOIN'][] = array(
112
			'FROM' => array(TOPICS_TABLE => 't'),
113
			'ON' => "f.forum_last_post_id = t.topic_last_post_id"
114
		);
115
		$sql_array['SELECT'] .= ', t.topic_title, t.topic_id, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted';
116
		$event['sql_ary'] = $sql_array;
117
	}
118
119
	/**
120
	 * Store informations for the last post in forum_rows array
121
	 *
122
	 * @param	object	$event	The event object
123
	 * @return	null
124
	 * @access	public
125
	 */
126
	public function display_forums_modify_forum_rows($event)
127
	{
128
		$forum_rows = $event['forum_rows'];
129
		if ($event['row']['forum_last_post_time'] == $forum_rows[$event['parent_id']]['forum_last_post_time'])
130
		{
131
			$forum_rows[$event['parent_id']]['forum_name_last_post'] =$event['row']['forum_name'];
132
			$forum_rows[$event['parent_id']]['topic_id_last_post'] =$event['row']['topic_id'];
133
			$forum_rows[$event['parent_id']]['topic_title_last_post'] =$event['row']['topic_title'];
134
			$event['forum_rows'] = $forum_rows;
135
		}
136
	}
137
138
	/**
139
	 * Rewrite links to forums and subforums in forum index
140
	 * also correct the path of the forum images if we are in a forum
141
	 *
142
	 * @param	object	$event	The event object
143
	 * @return	null
144
	 * @access	public
145
	 */
146
	public function display_forums_modify_template_vars($event)
147
	{
148
		$subforums_row = $event['subforums_row'];
149
		$forum_row = $event['forum_row'];
150
151
		// Rewrite URLs of sub forums
152
		foreach ($subforums_row as $i => $subforum)
153
		{
154
			// A little bit a dirty way, but there is no better solution
155
			$query = str_replace('&amp;', '&', parse_url($subforum['U_SUBFORUM'], PHP_URL_QUERY));
156
			parse_str($query, $id);
157
			$subforums_row[$i]['U_SUBFORUM'] = append_sid($this->base->generate_forum_link($id['f'], $subforum['SUBFORUM_NAME']));
158
		}
159
160
		// Update the image source in forums
161
		$img = $this->path_helper->update_web_root_path($forum_row['FORUM_IMAGE_SRC']);
162
		$forum_row['FORUM_IMAGE'] = preg_replace('#img src=\"(.*)\" alt#', 'img src="' . $img . '" alt', $forum_row['FORUM_IMAGE']);
163
164
		// Rewrite links to topics, posts and forums
165
		$replies = $this->base->get_count('topic_posts', $event['row'], $event['row']['forum_id']) - 1;
166
		$url = $this->base->generate_topic_link($event['row']['forum_id_last_post'], $event['row']['forum_name_last_post'], $event['row']['topic_id_last_post'], $event['row']['topic_title_last_post']);
167
		$forum_row['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($replies, $url) . '#p' . $event['row']['forum_last_post_id']);
168
		$forum_row['U_VIEWFORUM'] = append_sid($this->base->generate_forum_link($forum_row['FORUM_ID'], $forum_row['FORUM_NAME']));
169
170
		$event['subforums_row'] = $subforums_row;
171
		$event['forum_row'] = $forum_row;
172
	}
173
174
	/**
175
	 * Rewrite links in breadcrumbs
176
	 *
177
	 * @param	object	$event	The event object
178
	 * @return	null
179
	 * @access	public
180
	 */
181
	public function generate_forum_nav($event)
182
	{
183
		$forum_data = $event['forum_data'];
184
		$navlinks = $event['navlinks'];
185
		$navlinks_parents = $event['navlinks_parents'];
186
187
		foreach ($navlinks_parents as $id => $data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
		{
189
			$navlinks_parents[$id]['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($data['FORUM_ID'] , $data['FORUM_NAME']));
190
		}
191
192
		$navlinks['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($forum_data['forum_id'], $forum_data['forum_name']));
193
		$event['navlinks'] = $navlinks;
194
		$event['navlinks_parents'] = $navlinks_parents;
195
	}
196
197
	// Not in phpBB
198
	public function make_jumpbox_modify_tpl_ary($event)
199
	{
200
		$tpl_ary = $event['tpl_ary'];
201
		$row = $event['row'];
202
		foreach ($tpl_ary as $id => $data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
		{
204
			$tpl_ary[$id]['LINK']	 = append_sid($this->base->generate_forum_link($row['forum_id'], $row['forum_name']));
205
		}
206
207
		$event['tpl_ary'] = $tpl_ary;
208
	}
209
210
	/**
211
	 * Rewrite pagination links
212
	 *
213
	 * @param	object	$event	The event object
214
	 * @return	null
215
	 * @access	public
216
	 */
217
	public function pagination_generate_page_link($event)
218
	{
219
		// If we have a sort key we do not rewrite the URL
220
		$query = str_replace('&amp;', '&', parse_url($event['base_url'], PHP_URL_QUERY));
221
		parse_str($query, $param);
222
		if (isset($param['sd']) || isset($param['sk']) || isset($param['st']))
223
		{
224
			return;
225
		}
226
227
		$start = (($event['on_page'] - 1) * $event['per_page']);
228
		if (!empty($this->topic_title))
229
		{
230
			$event['generate_page_link_override'] = append_sid($this->base->generate_topic_link($this->forum_id, $this->forum_title, $this->topic_id, $this->topic_title, $start));
231
		}
232
		else if (!empty($this->forum_title))
233
		{
234
			$event['generate_page_link_override'] = append_sid($this->base->generate_forum_link($this->forum_id, $this->forum_title, $start));
235
		}
236
	}
237
238
	/**
239
	 * Rewrite links in the search result
240
	 *
241
	 * @param	object	$event	The event object
242
	 * @return	null
243
	 * @access	public
244
	 */
245
	public function search_modify_tpl_ary($event)
246
	{
247
		$replies = $this->base->get_count('topic_posts', $event['row'], $event['row']['forum_id']) - 1;
248
		$u_view_topic = $this->base->generate_topic_link($event['row']['forum_id'], $event['row']['forum_name'], $event['row']['topic_id'], $event['row']['topic_title']);
249
250
		$tpl_ary = $event['tpl_ary'];
251
		$tpl_ary['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($replies, $u_view_topic) . '#p' . $event['row']['topic_last_post_id']);
252
		$tpl_ary['U_VIEW_TOPIC'] = append_sid($u_view_topic);
253
		$tpl_ary['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($event['row']['forum_id'], $event['row']['forum_name']));
254
255
		$event['tpl_ary'] = $tpl_ary;
256
	}
257
258
	/**
259
	 * Rewrite links to topics in forum view
260
	 *
261
	 * @param	object	$event	The event object
262
	 * @return	null
263
	 * @access	public
264
	 */
265
	public function viewforum_modify_topicrow($event)
266
	{
267
		$topic_row = $event['topic_row'];
268
		$this->forum_title = $topic_row['FORUM_NAME'];
269
		$this->forum_id = $topic_row['FORUM_ID'];
270
		$this->topic_title = $topic_row['TOPIC_TITLE'];
271
		$this->topic_id = $topic_row['TOPIC_ID'];
272
273
		$u_view_topic = $this->base->generate_topic_link($this->forum_id, $this->forum_title, $this->topic_id, $this->topic_title);
274
		$topic_row['U_VIEW_TOPIC'] = append_sid($u_view_topic);
275
		$topic_row['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($this->forum_id, $this->forum_title));
276
		$topic_row['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($event['topic_row']['REPLIES'], $u_view_topic) . '#p' . $event['row']['topic_last_post_id']);
277
278
		$event['topic_row'] = $topic_row;
279
	}
280
281
	/**
282
	 * Rewrite the canonical URL on viewforum.php
283
	 *
284
	 * @param	object	$event	The event object
285
	 * @return	null
286
	 * @access	public
287
	 */
288
	public function viewforum_get_topic_data($event)
289
	{
290
		$start = $this->request->variable('start', 0);
291
		$this->template->assign_vars(array(
292
			'U_VIEW_FORUM'	=> append_sid($this->base->generate_forum_link($event['forum_data']['forum_id'], $event['forum_data']['forum_name'], $start)),
293
			'U_CANONICAL'		=> $this->base->generate_forum_link($event['forum_data']['forum_id'], $event['forum_data']['forum_name'], $start, true),
294
		));
295
	}
296
297
	/**
298
	 * Rewrite the topic URL for the headline of the topic page and the link back to forum
299
	 *
300
	 * @param	object	$event	The event object
301
	 * @return	null
302
	 * @access	public
303
	 */
304
	public function viewtopic_get_post_data($event)
305
	{
306
		$data = $event['topic_data'];
307
		$this->template->assign_vars(array(
308
			'U_VIEW_TOPIC'		=> append_sid($this->base->generate_topic_link($event['forum_id'] , $data['forum_name'], $event['topic_id'], $data['topic_title'], $event['start'])),
309
			'U_VIEW_FORUM'	=> append_sid($this->base->generate_forum_link($event['forum_id'] , $data['forum_name'])),
310
		));
311
	}
312
313
	/**
314
	 * Assign topic data to global variables for pagination
315
	 *
316
	 * @param	object	$event	The event object
317
	 * @return	null
318
	 * @access	public
319
	 */
320
	public function viewtopic_assign_template_vars_before($event)
321
	{
322
		$this->forum_title = $event['topic_data']['forum_name'];
323
		$this->forum_id = $event['topic_data']['forum_id'];
324
		$this->topic_title = $event['topic_data']['topic_title'];
325
		$this->topic_id = $event['topic_data']['topic_id'];
326
	}
327
328
	/**
329
	 * Rewrite the canonical URL on viewtopic.php
330
	 *
331
	 * @param	object	$event	The event object
332
	 * @return	null
333
	 * @access	public
334
	 */
335
	public function viewtopic_modify_page_title($event)
336
	{
337
		$start = $this->request->variable('start', 0);
338
		$data = $event['topic_data'];
339
		$this->template->assign_vars(array(
340
			'U_CANONICAL'		=> $this->base->generate_topic_link($data['forum_id'], $data['forum_name'], $data['topic_id'], $data['topic_title'], $start, true),
341
		));
342
	}
343
344
	/**
345
	 * Rewrite mini post img link
346
	 *
347
	 * @param	object	$event	The event object
348
	 * @return	null
349
	 * @access	public
350
	 */
351
	public function viewtopic_modify_post_row($event)
352
	{
353
		$row = $event['post_row'];
354
		$start = $this->request->variable('start', 0);
355
		$data = $event['topic_data'];
356
		$row['U_MINI_POST'] = append_sid($this->base->generate_topic_link($data['forum_id'], $data['forum_name'], $data['topic_id'], $data['topic_title'], $start) . '#p' . $event['row']['post_id']);
357
		$event['post_row'] = $row;
358
	}
359
}
360