Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

search   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 1
dl 0
loc 136
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSubscribedEvents() 0 8 1
A modify_posts_data() 0 16 1
A modify_topic_data() 0 13 1
B modify_tpl_data() 0 34 4
A modify_message() 0 9 2
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\content\event;
11
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class search implements EventSubscriberInterface
15
{
16
	/** @var \phpbb\db\driver\driver_interface */
17
	protected $db;
18
19
	/* @var \phpbb\controller\helper */
20
	protected $helper;
21
22
	/* @var \blitze\content\services\types */
23
	protected $content_types;
24
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param \phpbb\db\driver\driver_interface		$db					Database object
29
	 * @param \phpbb\controller\helper				$helper				Helper object
30
	 * @param \blitze\content\services\types		$content_types		Content types object
31
	*/
32
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $helper, \blitze\content\services\types $content_types)
33
	{
34
		$this->db = $db;
35
		$this->helper = $helper;
36
		$this->content_types = $content_types;
37
	}
38
39
	/**
40
	 * @return array
41
	 */
42
	static public function getSubscribedEvents()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
43
	{
44
		return array(
45
			'core.search_get_posts_data'	=> 'modify_posts_data',
46
			'core.search_get_topic_data'	=> 'modify_topic_data',
47
			'core.search_modify_tpl_ary'	=> 'modify_tpl_data',
48
		);
49
	}
50
51
	/**
52
	 * Since this extension allows for scheduling articles,
53
	 * We want to make sure that we do not display future articles in search results
54
	 *
55
	 * @param \phpbb\event\data $event
56
	 * @return void
57
	 */
58
	public function modify_posts_data(\phpbb\event\data $event)
59
	{
60
		$sql_array = $event['sql_array'];
61
		$sql_array['WHERE'] .= ' AND t.topic_time <= ' . time();
62
63
		$sql_count = $sql_array;
64
		$sql_count['SELECT'] = 'COUNT(p.post_id) AS total_results';
65
66
		$sql = $this->db->sql_build_query('SELECT', $sql_count);
67
		$result = $this->db->sql_query($sql);
68
		$total_results = $this->db->sql_fetchfield('total_results');
69
		$this->db->sql_freeresult($result);
70
71
		$event['sql_array'] = $sql_array;
72
		$event['total_match_count'] = $total_results;
73
	}
74
75
	/**
76
	 * Since this extension allows for scheduling articles,
77
	 * We want to make sure that we do not display future articles in search results
78
	 *
79
	 * @param \phpbb\event\data $event
80
	 * @return void
81
	 */
82
	public function modify_topic_data(\phpbb\event\data $event)
83
	{
84
		$sql_where = $event['sql_where'];
85
		$sql_where .= ' AND t.topic_time <= ' . time();
86
87
		$sql = 'SELECT COUNT(t.topic_id) AS total_results FROM ' . $event['sql_from'] . ' WHERE ' . $sql_where;
88
		$result = $this->db->sql_query($sql);
89
		$total_results = $this->db->sql_fetchfield('total_results');
90
		$this->db->sql_freeresult($result);
91
92
		$event['sql_where'] = $sql_where;
93
		$event['total_match_count'] = $total_results;
94
	}
95
96
	/**
97
	 * @param \phpbb\event\data $event
98
	 * @return void
99
	 */
100
	public function modify_tpl_data(\phpbb\event\data $event)
101
	{
102
		$row = $event['row'];
103
		if ($type = $this->content_types->get_forum_type($row['forum_id']))
104
		{
105
			$tpl_ary = $event['tpl_ary'];
106
107
			$params = array(
108
				'type'		=> $type,
109
				'topic_id'	=> $row['topic_id'],
110
				'slug'		=> $event['row']['topic_slug']
111
			);
112
113
			if (isset($row['post_id']) && $row['topic_first_post_id'] !== $row['post_id'])
114
			{
115
				$params += array(
116
					'p'	=> $row['post_id'],
117
					'#'	=> "p{$row['post_id']}",
118
				);
119
			}
120
121
			$topic_url = $this->helper->route('blitze_content_show', $params);
122
			$forum_url = $this->helper->route('blitze_content_index', array(
123
				'type' => $type
124
			));
125
126
			$tpl_ary['MESSAGE'] = $this->modify_message($row, $tpl_ary['MESSAGE']);
127
			$tpl_ary['U_VIEW_TOPIC'] = $tpl_ary['U_VIEW_POST'] = $topic_url;
128
			$tpl_ary['U_VIEW_FORUM'] = $forum_url;
129
130
			$event['tpl_ary'] = $tpl_ary;
131
			unset($tpl_ary);
132
		}
133
	}
134
135
	/**
136
	 * @param array $row
137
	 * @param string $message
138
	 * @return string
139
	 */
140
	public function modify_message(array $row, $message)
141
	{
142
		$patterns = array(
143
			'#(&\#91;)pagebreak\s?(title=(.*?))(&\#93;)#s',
144
			'#<!-- begin field --><h4>(.*?)</h4><br>#s',
145
		);
146
147
		return ($row['post_id'] === $row['topic_first_post_id']) ? preg_replace($patterns, ' ', $message) : $message;
148
	}
149
}
150