Completed
Branch master (ccddbd)
by Matt
01:20
created

similar_topics_module::config_text_get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 10
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
					'PST_ADVANCED_EXP'			=> $this->user->lang('PST_ADVANCED_EXP', $forum_name),
152
					'U_ACTION'					=> $this->u_action . '&amp;action=advanced&amp;f=' . $forum_id,
153
					'U_BACK'					=> $this->u_action,
154
				));
155
			break;
156
157
			default:
158
				if ($this->request->is_set_post('submit'))
159
				{
160
					$this->check_form_key($form_key);
161
162
					// Set basic config settings
163
					$this->config->set('similar_topics', $this->request->variable('pst_enable', 0));
164
					$this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only
165
					$this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only
166
					$this->config_text_set('similar_topics_words', $this->request->variable('pst_words', '', true));
167
168
					// Set sensitivity
169
					$pst_sense = min(abs($this->request->variable('pst_sense', 5)), 10); // use abs for positive values only
170
					$this->config->set('similar_topics_sense', $pst_sense);
171
172
					// Set date/time config settings
173
					$pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only
174
					$pst_time_type = $this->request->variable('pst_time_type', '');
175
					$this->config->set('similar_topics_type', $pst_time_type);
176
					$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

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

247
						'U_FORUM'				=> /** @scrutinizer ignore-call */ append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']),
Loading history...
248
					));
249
				}
250
			break;
251
		}
252
	}
253
254
	/**
255
	 * Check form key, trigger error if invalid
256
	 *
257
	 * @access protected
258
	 * @param string $form_key The form key value
259
	 */
260
	protected function check_form_key($form_key)
261
	{
262
		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

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

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