Passed
Push — develop ( 7a3a60...83f469 )
by Matt
06:55
created

similar_topics_module::end()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 *
4
 * Precise Similar Topics
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\similartopics\acp;
12
13
/**
14
 * @package acp
15
 */
16
class similar_topics_module
17
{
18
	/** @var \phpbb\cache\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\cache\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
	protected $cache;
20
21
	/** @var \phpbb\config\config */
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
	protected $config;
23
24
	/** @var \phpbb\config\db_text */
0 ignored issues
show
Bug introduced by
The type phpbb\config\db_text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
	protected $config_text;
26
27
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
	protected $db;
29
30
	/** @var \phpbb\log\log */
0 ignored issues
show
Bug introduced by
The type phpbb\log\log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
	protected $log;
32
33
	/** @var \phpbb\request\request */
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
	protected $request;
35
36
	/** @var \vse\similartopics\driver\driver_interface */
37
	protected $similartopics;
38
39
	/** @var \phpbb\template\template */
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
	protected $template;
41
42
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
	protected $user;
44
45
	/** @var string */
46
	protected $root_path;
47
48
	/** @var string */
49
	protected $php_ext;
50
51
	/** @var array */
52
	protected $times;
53
54
	/** @var string */
55
	public $page_title;
56
57
	/** @var string */
58
	public $tpl_name;
59
60
	/** @var string */
61
	public $u_action;
62
63
	/**
64
	 * ACP module constructor
65
	 *
66
	 * @access public
67
	 * @throws \Exception
68
	 */
69
	public function __construct()
70
	{
71
		global $phpbb_container;
72
73
		$this->cache         = $phpbb_container->get('cache');
74
		$this->config        = $phpbb_container->get('config');
75
		$this->config_text   = $phpbb_container->get('config_text');
76
		$this->db            = $phpbb_container->get('dbal.conn');
77
		$this->similartopics = $phpbb_container->get('vse.similartopics.driver.manager')->get_driver($this->db->get_sql_layer());
78
		$this->log           = $phpbb_container->get('log');
79
		$this->request       = $phpbb_container->get('request');
80
		$this->template      = $phpbb_container->get('template');
81
		$this->user          = $phpbb_container->get('user');
82
		$this->root_path     = $phpbb_container->getParameter('core.root_path');
83
		$this->php_ext       = $phpbb_container->getParameter('core.php_ext');
84
		$this->times         = array(
85
			'd' => 86400, // one day
86
			'w' => 604800, // one week
87
			'm' => 2626560, // one month
88
			'y' => 31536000, // one year
89
		);
90
	}
91
92
	/**
93
	 * Main ACP module
94
	 *
95
	 * @access public
96
	 */
97
	public function main()
98
	{
99
		$this->user->add_lang_ext('vse/similartopics', 'acp_similar_topics');
100
101
		$this->tpl_name = 'acp_similar_topics';
102
		$this->page_title = $this->user->lang('PST_TITLE_ACP');
103
104
		$form_key = 'acp_similar_topics';
105
		add_form_key($form_key);
0 ignored issues
show
Bug introduced by
The function add_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

105
		/** @scrutinizer ignore-call */ 
106
  add_form_key($form_key);
Loading history...
106
107
		$action = $this->request->variable('action', '');
108
109
		switch ($action)
110
		{
111
			case 'advanced':
112
				$forum_id = $this->request->variable('f', 0);
113
114
				if ($this->request->is_set_post('submit'))
115
				{
116
					$this->check_form_key($form_key);
117
118
					$similar_topic_forums = $this->request->variable('similar_forums_id', array(0));
119
					$similar_topic_forums = !empty($similar_topic_forums) ? json_encode($similar_topic_forums) : '';
120
121
					$sql = 'UPDATE ' . FORUMS_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
122
						SET similar_topic_forums = '" . $this->db->sql_escape($similar_topic_forums) . "'
123
						WHERE forum_id = $forum_id";
124
					$this->db->sql_query($sql);
125
126
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG');
127
128
					$this->end('PST_SAVED');
129
				}
130
131
				$forum_name = '';
132
				$selected = array();
133
				if ($forum_id > 0)
134
				{
135
					$sql = 'SELECT forum_name, similar_topic_forums
136
						FROM ' . FORUMS_TABLE . "
137
						WHERE forum_id = $forum_id";
138
					$result = $this->db->sql_query($sql);
139
					while ($fid = $this->db->sql_fetchrow($result))
140
					{
141
						$selected = json_decode($fid['similar_topic_forums'], true);
142
						$forum_name = $fid['forum_name'];
143
					}
144
					$this->db->sql_freeresult($result);
145
				}
146
147
				$this->template->assign_vars(array(
148
					'S_ADVANCED_SETTINGS'		=> true,
149
					'SIMILAR_FORUMS_OPTIONS'	=> make_forum_select($selected, false, false, true),
0 ignored issues
show
Bug introduced by
The function make_forum_select was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

149
					'SIMILAR_FORUMS_OPTIONS'	=> /** @scrutinizer ignore-call */ make_forum_select($selected, false, false, true),
Loading history...
150
					'PST_FORUM_NAME'			=> $forum_name,
151
					'U_ACTION'					=> $this->u_action . '&amp;action=advanced&amp;f=' . $forum_id,
152
					'U_BACK'					=> $this->u_action,
153
				));
154
			break;
155
156
			default:
157
				if ($this->request->is_set_post('submit'))
158
				{
159
					$this->check_form_key($form_key);
160
161
					// Set basic config settings
162
					$this->config->set('similar_topics', $this->request->variable('pst_enable', 0));
163
					$this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only
164
					$this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only
165
					$this->config_text_set('similar_topics_words', $this->request->variable('pst_words', '', true));
166
167
					// Set sensitivity
168
					$pst_sense = min(abs($this->request->variable('pst_sense', 5)), 10); // use abs for positive values only
169
					$this->config->set('similar_topics_sense', $pst_sense);
170
171
					// Set date/time config settings
172
					$pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only
173
					$pst_time_type = $this->request->variable('pst_time_type', '');
174
					$this->config->set('similar_topics_type', $pst_time_type);
175
					$this->config->set('similar_topics_time', $this->set_pst_time($pst_time, $pst_time_type));
0 ignored issues
show
Bug introduced by
It seems like $pst_time can also be of type double; however, parameter $length of vse\similartopics\acp\si..._module::set_pst_time() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

175
					$this->config->set('similar_topics_time', $this->set_pst_time(/** @scrutinizer ignore-type */ $pst_time, $pst_time_type));
Loading history...
176
177
					// Set checkbox array form data
178
					$this->update_forum('similar_topics_hide', $this->request->variable('mark_noshow_forum', array(0), true));
179
					$this->update_forum('similar_topics_ignore', $this->request->variable('mark_ignore_forum', array(0), true));
180
181
					// Set PostgreSQL TS Name
182
					if ($this->similartopics && $this->similartopics->get_type() === 'postgres')
183
					{
184
						$ts_name = $this->request->variable('pst_postgres_ts_name', ($this->config['pst_postgres_ts_name'] ?: 'simple'));
185
						$this->config->set('pst_postgres_ts_name', $ts_name);
186
						$this->similartopics->create_fulltext_index('topic_title');
187
					}
188
189
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG');
190
191
					$this->cache->destroy('sql', TOPICS_TABLE);
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
192
193
					$this->end('PST_SAVED');
194
				}
195
196
				// Build the time options select menu
197
				$time_options = array(
198
					'd' => 'PST_DAYS',
199
					'w' => 'PST_WEEKS',
200
					'm' => 'PST_MONTHS',
201
					'y' => 'PST_YEARS'
202
				);
203
				foreach ($time_options as $value => $label)
204
				{
205
					$this->template->assign_block_vars('similar_time_options', array(
206
						'VALUE'			=> $value,
207
						'LABEL'			=> $label,
208
						'S_SELECTED'	=> $value === $this->config['similar_topics_type'],
209
					));
210
				}
211
212
				$this->template->assign_vars(array(
213
					'S_PST_ENABLE'		=> $this->isset_or_default($this->config['similar_topics'], false),
214
					'PST_LIMIT'			=> $this->isset_or_default($this->config['similar_topics_limit'], ''),
215
					'PST_CACHE'			=> $this->isset_or_default($this->config['similar_topics_cache'], ''),
216
					'PST_SENSE'			=> $this->isset_or_default($this->config['similar_topics_sense'], ''),
217
					'PST_WORDS'			=> $this->isset_or_default($this->config_text_get('similar_topics_words'), ''),
218
					'PST_TIME'			=> $this->get_pst_time($this->config['similar_topics_time'], $this->config['similar_topics_type']),
219
					'S_PST_NO_COMPAT'	=> $this->similartopics === null || !$this->similartopics->is_fulltext('topic_title'),
220
					'U_ACTION'			=> $this->u_action,
221
				));
222
223
				// If postgresql, we need to make an options list of text search names
224
				if ($this->similartopics && $this->similartopics->get_type() === 'postgres')
225
				{
226
					$this->user->add_lang('acp/search');
227
					foreach ($this->get_cfgname_list() as $row)
228
					{
229
						$this->template->assign_block_vars('postgres_ts_names', array(
230
							'NAME'			=> $row['ts_name'],
231
							'S_SELECTED'	=> $row['ts_name'] === $this->config['pst_postgres_ts_name'],
232
						));
233
					}
234
				}
235
236
				$forum_list = $this->get_forum_list();
237
				foreach ($forum_list as $row)
238
				{
239
					$this->template->assign_block_vars('forums', array(
240
						'FORUM_NAME'			=> $row['forum_name'],
241
						'FORUM_ID'				=> $row['forum_id'],
242
						'CHECKED_IGNORE_FORUM'	=> $row['similar_topics_ignore'] ? 'checked="checked"' : '',
243
						'CHECKED_NOSHOW_FORUM'	=> $row['similar_topics_hide'] ? 'checked="checked"' : '',
244
						'S_IS_ADVANCED'			=> (bool) $row['similar_topic_forums'],
245
						'U_ADVANCED'			=> "{$this->u_action}&amp;action=advanced&amp;f=" . $row['forum_id'],
246
						'U_FORUM'				=> append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']),
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

246
						'U_FORUM'				=> /** @scrutinizer ignore-call */ append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']),
Loading history...
247
					));
248
				}
249
			break;
250
		}
251
	}
252
253
	/**
254
	 * Check form key, trigger error if invalid
255
	 *
256
	 * @access protected
257
	 * @param string $form_key The form key value
258
	 */
259
	protected function check_form_key($form_key)
260
	{
261
		if (!check_form_key($form_key))
0 ignored issues
show
Bug introduced by
The function check_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

261
		if (!/** @scrutinizer ignore-call */ check_form_key($form_key))
Loading history...
262
		{
263
			$this->end('FORM_INVALID', E_USER_WARNING);
264
		}
265
	}
266
267
	/**
268
	 * Get list of PostgreSQL text search names
269
	 *
270
	 * @return array array of text search names
271
	 */
272
	protected function get_cfgname_list()
273
	{
274
		$sql = 'SELECT cfgname AS ts_name FROM pg_ts_config';
275
		$result = $this->db->sql_query($sql);
276
		$ts_options = $this->db->sql_fetchrowset($result);
277
		$this->db->sql_freeresult($result);
278
279
		return $ts_options;
280
	}
281
282
	/**
283
	 * Get forums list
284
	 *
285
	 * @access protected
286
	 * @return array forum data rows
287
	 */
288
	protected function get_forum_list()
289
	{
290
		$sql = 'SELECT forum_id, forum_name, similar_topic_forums, similar_topics_hide, similar_topics_ignore
291
			FROM ' . FORUMS_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
292
			WHERE forum_type = ' . FORUM_POST . '
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\FORUM_POST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
293
			ORDER BY left_id ASC';
294
		$result = $this->db->sql_query($sql);
295
		$forum_list = $this->db->sql_fetchrowset($result);
296
		$this->db->sql_freeresult($result);
297
298
		return $forum_list;
299
	}
300
301
	/**
302
	 * Update the similar topics columns in the forums table
303
	 *
304
	 * @param string $column    The name of the column to update
305
	 * @param array  $forum_ids An array of forum_ids
306
	 */
307
	protected function update_forum($column, $forum_ids)
308
	{
309
		$this->db->sql_transaction('begin');
310
311
		// Set marked forums (in set) to 1
312
		$sql = 'UPDATE ' . FORUMS_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\acp\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
313
			SET $column = 1
314
			WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, false, true);
315
		$this->db->sql_query($sql);
316
317
		// Set unmarked forums (not in set) to 0
318
		$sql = 'UPDATE ' . FORUMS_TABLE . "
319
			SET $column = 0
320
			WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, true, true);
321
		$this->db->sql_query($sql);
322
323
		$this->db->sql_transaction('commit');
324
	}
325
326
	/**
327
	 * Store a config_text item in the database.
328
	 *
329
	 * @param string $name Name of a config_text item
330
	 * @param string $value Value of a config_text item
331
	 */
332
	protected function config_text_set($name, $value)
333
	{
334
		$this->config_text->set($name, $value);
335
		$this->cache->put("_$name", $value);
336
	}
337
338
	/**
339
	 * Get a config_text value from the cache if it is cached, otherwise
340
	 * get it directly from the database.
341
	 *
342
	 * @param string $name Name of a config_text item
343
	 * @return string|null Value of a config_text item, either cached or from db
344
	 */
345
	protected function config_text_get($name)
346
	{
347
		if (($value = $this->cache->get("_$name")) === false)
348
		{
349
			$value = $this->config_text->get($name);
350
351
			$this->cache->put("_$name", $value);
352
		}
353
354
		return !empty($value) ? $value : null;
355
	}
356
357
	/**
358
	 * Calculate the time in seconds based on requested time period length
359
	 *
360
	 * @access protected
361
	 * @param int    $length user entered value
362
	 * @param string $type   years, months, weeks, days (y|m|w|d)
363
	 * @return int time in seconds
364
	 */
365
	protected function set_pst_time($length, $type = 'y')
366
	{
367
		$type = isset($this->times[$type]) ? $type : 'y';
368
369
		return (int) ($length * $this->times[$type]);
370
	}
371
372
	/**
373
	 * Get the correct time period length value for the form
374
	 *
375
	 * @access protected
376
	 * @param int    $time as a timestamp
377
	 * @param string $type years, months, weeks, days (y|m|w|d)
378
	 * @return int time converted to the given $type
379
	 */
380
	protected function get_pst_time($time, $type = '')
381
	{
382
		return isset($this->times[$type]) ? (int) round($time / $this->times[$type]) : 0;
383
	}
384
385
	/**
386
	 * Return a variable if it is set, otherwise default
387
	 *
388
	 * @access protected
389
	 * @param mixed $var     The variable to test
390
	 * @param mixed $default The default value to use
391
	 * @return mixed The value of the variable if set, otherwise default value
392
	 */
393
	protected function isset_or_default($var, $default)
394
	{
395
		return null !== $var ? $var : $default;
396
	}
397
398
	/**
399
	 * End script execution with a trigger_error message
400
	 *
401
	 * @access protected
402
	 * @param string $message Language key string
403
	 * @param int    $code    E_USER_NOTICE|E_USER_WARNING
404
	 * @return void
405
	 */
406
	protected function end($message, $code = E_USER_NOTICE)
407
	{
408
		trigger_error($this->user->lang($message) . adm_back_link($this->u_action), $code);
0 ignored issues
show
Bug introduced by
The function adm_back_link was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

408
		trigger_error($this->user->lang($message) . /** @scrutinizer ignore-call */ adm_back_link($this->u_action), $code);
Loading history...
409
	}
410
}
411