filter   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 89
c 1
b 0
f 0
dl 0
loc 251
ccs 0
cts 98
cp 0
rs 10
wmc 29

13 Methods

Rating   Name   Duplication   Size   Complexity  
A get_filter_type_base_url() 0 5 1
A get_topic_types_filters() 0 7 1
A generate_search_filter() 0 8 1
A apply_search_filter() 0 18 4
A apply_topic_status_filter() 0 29 4
A set_topic_type_sql() 0 3 1
A is_callable() 0 3 3
A get_topic_status_filters() 0 7 1
A set_visibility_sql() 0 3 1
A generate_topic_status_filter() 0 17 4
A __construct() 0 5 1
A generate_content_type_filter() 0 18 4
A apply_content_type_filter() 0 12 3
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\actions\topic;
11
12
class filter
13
{
14
	/** @var \phpbb\db\driver\driver_interface */
15
	protected $db;
16
17
	/** @var \phpbb\request\request_interface */
18
	protected $request;
19
20
	/** @var \phpbb\template\template */
21
	protected $template;
22
23
	/** @var array */
24
	protected $content_forums;
25
26
	/** @var array */
27
	protected $params = array();
28
29
	/** @var string */
30
	protected $content_type_base_url = '';
31
32
	/** @var string */
33
	protected $topic_status_base_url = '';
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \phpbb\db\driver\driver_interface				$db						Database connection
39
	 * @param \phpbb\request\request_interface				$request				Request object
40
	 * @param \phpbb\template\template						$template				Template object
41
	 */
42
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\template\template $template)
43
	{
44
		$this->db = $db;
45
		$this->request = $request;
46
		$this->template = $template;
47
	}
48
49
	/**
50
	 * @param array $search_info
51
	 * @param string $u_action
52
	 * @return void
53
	 */
54
	protected function generate_search_filter(array $search_info, $u_action)
55
	{
56
		$this->template->assign_vars(array(
57
			'search_info'	=> $search_info,
58
			'search_url'	=> $u_action,
59
			'search_types'	=> array(
60
				'topic_title'				=> 'TOPIC_TITLE',
61
				'topic_first_poster_name'	=> 'AUTHOR',
62
			),
63
		));
64
	}
65
66
	/**
67
	 * @param array $sql_where_array
68
	 * @return void
69
	 */
70
	protected function apply_search_filter(array &$sql_where_array)
71
	{
72
		if ($search = $this->request->variable('search', '', true))
73
		{
74
			$default_type = 'topic_title';
75
			$table_field = $this->request->variable('search_type', $default_type);
76
			$table_field = (in_array($table_field, ['topic_title', 'topic_first_poster_name'])) ? $table_field : $default_type;
77
78
			$sql_where_array[] = 't.' . $table_field . ' ' . $this->db->sql_like_expression($this->db->get_any_char() . $search . $this->db->get_any_char());
79
80
			$this->params = array(
81
				'search'		=> $search,
82
				'search_type'	=> $table_field,
83
			);
84
		}
85
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('search' =>...field ?: 'topic_title') returns the type array<string,mixed|string> which is incompatible with the documented return type void.
Loading history...
86
			'search'	=> $search,
87
			'type'		=> $table_field ?: 'topic_title',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table_field does not seem to be defined for all execution paths leading up to this point.
Loading history...
88
		);
89
	}
90
91
	/**
92
	 * @param string $type
93
	 * @param array $content_types
94
	 * @param string $u_action
95
	 * @return void
96
	 */
97
	protected function generate_content_type_filter($type, array $content_types, $u_action)
98
	{
99
		$this->content_type_base_url = $this->get_filter_type_base_url($u_action, 'type');
100
101
		$this->template->assign_block_vars('content', array(
102
			'TITLE'			=> 'TOPIC_ALL',
103
			'S_SELECTED'	=> (!$type) ? true : false,
104
			'U_VIEW'		=> $this->content_type_base_url,
105
		));
106
107
		foreach ($content_types as $entity)
108
		{
109
			$content_name = $entity->get_content_name();
110
			$this->template->assign_block_vars('content', array(
111
				'TITLE'			=> $entity->get_content_langname(),
112
				'COLOUR'		=> $entity->get_content_colour(),
113
				'S_SELECTED'	=> ($type === $content_name) ? true : false,
114
				'U_VIEW'		=> $this->content_type_base_url . '&amp;type=' . $content_name,
115
			));
116
		}
117
	}
118
119
	/**
120
	 * @return string
121
	 */
122
	protected function apply_content_type_filter()
123
	{
124
		$content_type = $this->request->variable('type', '');
125
126
		if ($content_type && in_array($content_type, $this->content_forums))
127
		{
128
			$this->params['type'] = $content_type;
129
			$this->content_forums = array_intersect($this->content_forums, array($content_type));
130
			$this->template->assign_vars(array('S_CONTENT_FILTER' => true));
131
		}
132
133
		return $content_type;
134
	}
135
136
	/**
137
	 * @param string $topic_status
138
	 * @param string $u_action
139
	 * @return void
140
	 */
141
	protected function generate_topic_status_filter($topic_status, $u_action)
142
	{
143
		$this->topic_status_base_url = $this->get_filter_type_base_url($u_action, 'status');
144
145
		$this->template->assign_block_vars('status', array(
146
			'TITLE'			=> 'TOPIC_ALL',
147
			'S_SELECTED'	=> (!$topic_status) ? true : false,
148
			'U_VIEW'		=> $this->topic_status_base_url,
149
		));
150
151
		$topic_status_ary = array_unique(array_merge($this->get_topic_status_filters(), $this->get_topic_types_filters()));
152
		foreach ($topic_status_ary as $status)
153
		{
154
			$this->template->assign_block_vars('status', array(
155
				'TITLE'			=> 'TOPIC_' . strtoupper($status),
156
				'S_SELECTED'	=> ($status === $topic_status) ? true : false,
157
				'U_VIEW'		=> $this->topic_status_base_url . '&amp;status=' . $status
158
			));
159
		}
160
	}
161
162
	/**
163
	 * @param array $sql_where_array
164
	 * @return string
165
	 */
166
	protected function apply_topic_status_filter(array &$sql_where_array)
167
	{
168
		if ($topic_status = $this->request->variable('status', ''))
169
		{
170
			$this->template->assign_vars(array('S_STATUS_FILTER' => true));
171
			$this->params['status'] = $topic_status;
172
173
			$filter_types = array(
174
				$topic_status . '_topics' => [$topic_status],
175
				'visibility'	=> ['published', 'deleted', 'unapproved'],
176
				'topic_type'	=> ['featured', 'must_read', 'recommended'],
177
			);
178
179
			do
180
			{
181
				$filter = key($filter_types);
182
				$type = array_shift($filter_types);
183
				$method = 'set_' . $filter . '_sql';
184
185
				if ($this->is_callable($topic_status, $type, $method))
186
				{
187
					call_user_func_array(array($this, $method), array($topic_status, &$sql_where_array));
188
					break;
189
				}
190
			}
191
			while (sizeof($filter_types));
192
		}
193
194
		return $topic_status;
195
	}
196
197
	/**
198
	 * @param string $topic_status
199
	 * @param array $sql_where_array
200
	 * @return void
201
	 */
202
	protected function set_visibility_sql($topic_status, array &$sql_where_array)
203
	{
204
		$sql_where_array[] = 't.topic_visibility = ' . (int) array_search($topic_status, $this->get_topic_status_filters());
205
	}
206
207
	/**
208
	 * @param string $topic_status
209
	 * @param array $sql_where_array
210
	 * @return void
211
	 */
212
	protected function set_topic_type_sql($topic_status, array &$sql_where_array)
213
	{
214
		$sql_where_array[] = 't.topic_type = ' . (int) array_search($topic_status, $this->get_topic_types_filters());
215
	}
216
217
	/**
218
	 * @param string $topic_status
219
	 * @param string $filter_type
220
	 * @param string $method
221
	 * @return bool
222
	 */
223
	protected function is_callable($topic_status, $filter_type, $method)
224
	{
225
		return (in_array($topic_status, $filter_type) && is_callable(array($this, $method))) ? true : false;
0 ignored issues
show
Bug introduced by
$filter_type of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
		return (in_array($topic_status, /** @scrutinizer ignore-type */ $filter_type) && is_callable(array($this, $method))) ? true : false;
Loading history...
226
	}
227
228
	/**
229
	 * @param string $u_action
230
	 * @param string $type
231
	 * @return string
232
	 */
233
	protected function get_filter_type_base_url($u_action, $type = '')
234
	{
235
		$copy_params = $this->params;
236
		unset($copy_params[$type]);
237
		return $u_action . '&amp;' . http_build_query($copy_params);
238
	}
239
240
	/**
241
	 * @return array
242
	 */
243
	protected function get_topic_status_filters()
244
	{
245
		return array(
246
			ITEM_UNAPPROVED		=> 'unapproved',
247
			ITEM_APPROVED		=> 'published',
248
			ITEM_DELETED		=> 'deleted',
249
			ITEM_REAPPROVE		=> 'unapproved',
250
		);
251
	}
252
253
	/**
254
	 * @return array
255
	 */
256
	protected function get_topic_types_filters()
257
	{
258
		return array(
259
			POST_NORMAL		=> 'published',
260
			POST_STICKY		=> 'featured',
261
			POST_ANNOUNCE	=> 'recommended',
262
			POST_GLOBAL		=> 'must_read',
263
		);
264
	}
265
}
266