Completed
Push — develop-3.2.x ( 3829e8...a269e9 )
by Matt
02:35
created

similar_topics_module::set_pst_time()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
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\config\config */
19
	protected $config;
20
21
	/** @var \phpbb\db\driver\driver_interface */
22
	protected $db;
23
24
	/** @var \vse\similartopics\core\fulltext_support */
25
	protected $fulltext;
26
27
	/** @var \phpbb\language\language */
28
	protected $language;
29
30
	/** @var \phpbb\log\log */
31
	protected $log;
32
33
	/** @var \phpbb\request\request */
34
	protected $request;
35
36
	/** @var \phpbb\template\template */
37
	protected $template;
38
39
	/** @var \phpbb\user */
40
	protected $user;
41
42
	/** @var string */
43
	protected $root_path;
44
45
	/** @var string */
46
	protected $php_ext;
47
48
	/** @var array */
49
	protected $times;
50
51
	/** @var string */
52
	public $page_title;
53
54
	/** @var string */
55
	public $tpl_name;
56
57
	/** @var string */
58
	public $u_action;
59
60
	/**
61
	* ACP module constructor
62
	*
63
	* @access public
64
	*/
65
	public function __construct()
66
	{
67
		global $phpbb_container;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
68
69
		$this->config    = $phpbb_container->get('config');
70
		$this->db        = $phpbb_container->get('dbal.conn');
71
		$this->fulltext  = $phpbb_container->get('vse.similartopics.fulltext_support');
72
		$this->language  = $phpbb_container->get('language');
73
		$this->log       = $phpbb_container->get('log');
74
		$this->request   = $phpbb_container->get('request');
75
		$this->template  = $phpbb_container->get('template');
76
		$this->user      = $phpbb_container->get('user');
77
		$this->root_path = $phpbb_container->getParameter('core.root_path');
78
		$this->php_ext   = $phpbb_container->getParameter('core.php_ext');
79
		$this->times     = array(
80
			'd' => 86400, // one day
81
			'w' => 604800, // one week
82
			'm' => 2626560, // one month
83
			'y' => 31536000, // one year
84
		);
85
	}
86
87
	/**
88
	* Main ACP module
89
	*
90
	* @param int $id
91
	* @param string $mode
92
	* @access public
93
	*/
94
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $mode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
	{
96
		$this->user->add_lang('acp/common');
97
		$this->user->add_lang_ext('vse/similartopics', 'acp_similar_topics');
98
99
		$this->tpl_name = 'acp_similar_topics';
100
		$this->page_title = $this->user->lang('PST_TITLE_ACP');
101
102
		$form_key = 'acp_similar_topics';
103
		add_form_key($form_key);
104
105
		$action = $this->request->variable('action', '');
106
107
		switch ($action)
108
		{
109
			case 'advanced':
110
				$forum_id = $this->request->variable('f', 0);
111
112
				if ($this->request->is_set_post('submit'))
113
				{
114
					$this->check_form_key($form_key);
115
116
					$similar_topic_forums = implode(',', $this->request->variable('similar_forums_id', array(0)));
117
					$this->validate_config_length($similar_topic_forums);
118
119
					$sql = 'UPDATE ' . FORUMS_TABLE . "
120
						SET similar_topic_forums = '" . $this->db->sql_escape($similar_topic_forums) . "'
121
						WHERE forum_id = $forum_id";
122
					$this->db->sql_query($sql);
123
124
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG');
125
126
					$this->end('PST_SAVED');
127
				}
128
129
				$forum_name = '';
130
				$selected = array();
131
				if ($forum_id > 0)
132
				{
133
					$sql = 'SELECT forum_name, similar_topic_forums
134
						FROM ' . FORUMS_TABLE . "
135
						WHERE forum_id = $forum_id";
136
					$result = $this->db->sql_query($sql);
137
					while ($fid = $this->db->sql_fetchrow($result))
138
					{
139
						$selected = explode(',', trim($fid['similar_topic_forums']));
140
						$forum_name = $fid['forum_name'];
141
					}
142
					$this->db->sql_freeresult($result);
143
				}
144
145
				$this->template->assign_vars(array(
146
					'S_ADVANCED_SETTINGS'		=> true,
147
					'SIMILAR_FORUMS_OPTIONS'	=> make_forum_select($selected, false, false, true),
148
					'PST_FORUM_NAME'			=> $forum_name,
149
					'PST_ADVANCED_EXP'			=> $this->language->lang('PST_ADVANCED_EXP', $forum_name),
150
					'U_ACTION'					=> $this->u_action . '&amp;action=advanced&amp;f=' . $forum_id,
151
					'U_BACK'					=> $this->u_action,
152
				));
153
			break;
154
155
			default:
156
				if ($this->request->is_set_post('submit'))
157
				{
158
					$this->check_form_key($form_key);
159
160
					// Get checkbox array form data and check string length
161
					$mark_noshow_forum = implode(',', $this->request->variable('mark_noshow_forum', array(0), true));
162
					$mark_ignore_forum = implode(',', $this->request->variable('mark_ignore_forum', array(0), true));
163
					$this->validate_config_length($mark_noshow_forum, $mark_ignore_forum);
164
165
					// Set basic config settings
166
					$this->config->set('similar_topics', $this->request->variable('pst_enable', 0));
167
					$this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only
168
					$this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only
169
					$this->config->set('similar_topics_words', $this->request->variable('pst_words', '', true));
170
					$this->config->set('similar_topics_hide', $mark_noshow_forum);
171
					$this->config->set('similar_topics_ignore', $mark_ignore_forum);
172
173
					// Set date/time config settings
174
					$pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only
175
					$pst_time_type = $this->request->variable('pst_time_type', '');
176
					$this->config->set('similar_topics_type', $pst_time_type);
177
					$this->config->set('similar_topics_time', $this->set_pst_time($pst_time, $pst_time_type));
178
179
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG');
180
181
					$this->end('PST_SAVED');
182
				}
183
184
				// Allow option to update the database to enable FULLTEXT support
185
				if ($this->request->is_set_post('fulltext'))
186
				{
187
					if (confirm_box(true))
188
					{
189
						// If FULLTEXT is not supported, lets make it so
190
						if (!$this->fulltext_support_enabled())
191
						{
192
							// Alter the database to support FULLTEXT
193
							$this->enable_fulltext_support();
194
195
							// Store the original database storage engine in a config var for recovery on uninstall
196
							$this->config->set('similar_topics_fulltext', (string) $this->fulltext->get_engine());
197
198
							$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_FULLTEXT', time(), array(TOPICS_TABLE));
199
200
							$this->end('PST_SAVE_FULLTEXT');
201
						}
202
						else
203
						{
204
							$this->end('PST_ERR_FULLTEXT', E_USER_WARNING);
205
						}
206
					}
207
					else
208
					{
209
						confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
210
							'fulltext' => 1,
211
						)));
212
					}
213
				}
214
215
				// Build the time options select menu
216
				$time_options = array(
217
					'd' => $this->language->lang('PST_DAYS'),
218
					'w' => $this->language->lang('PST_WEEKS'),
219
					'm' => $this->language->lang('PST_MONTHS'),
220
					'y' => $this->language->lang('PST_YEARS')
221
				);
222
				foreach ($time_options as $value => $label)
223
				{
224
					$this->template->assign_block_vars('similar_time_options', array(
225
						'VALUE'			=> $value,
226
						'LABEL'			=> $label,
227
						'S_SELECTED'	=> $value == $this->config['similar_topics_type'],
228
					));
229
				}
230
231
				$this->template->assign_vars(array(
232
					'S_PST_ENABLE'		=> $this->isset_or_default($this->config['similar_topics'], false),
233
					'PST_LIMIT'			=> $this->isset_or_default($this->config['similar_topics_limit'], ''),
234
					'PST_CACHE'			=> $this->isset_or_default($this->config['similar_topics_cache'], ''),
235
					'PST_WORDS'			=> $this->isset_or_default($this->config['similar_topics_words'], ''),
236
					'PST_TIME'			=> $this->get_pst_time($this->config['similar_topics_time'], $this->config['similar_topics_type']),
237
					'S_PST_NO_SUPPORT'	=> !$this->fulltext_support_enabled(),
238
					'S_PST_NO_MYSQL'	=> !$this->fulltext->is_mysql(),
239
					'U_ACTION'			=> $this->u_action,
240
				));
241
242
				$ignore_forums = explode(',', trim($this->config['similar_topics_ignore']));
243
				$noshow_forums = explode(',', trim($this->config['similar_topics_hide']));
244
245
				$forum_list = $this->get_forum_list();
246
				foreach ($forum_list as $row)
247
				{
248
					$this->template->assign_block_vars('forums', array(
249
						'FORUM_NAME'			=> $row['forum_name'],
250
						'FORUM_ID'				=> $row['forum_id'],
251
						'CHECKED_IGNORE_FORUM'	=> (in_array($row['forum_id'], $ignore_forums)) ? 'checked="checked"' : '',
252
						'CHECKED_NOSHOW_FORUM'	=> (in_array($row['forum_id'], $noshow_forums)) ? 'checked="checked"' : '',
253
						'S_IS_ADVANCED'			=> (bool) $row['similar_topic_forums'],
254
						'U_ADVANCED'			=> "{$this->u_action}&amp;action=advanced&amp;f=" . $row['forum_id'],
255
						'U_FORUM'				=> append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']),
256
					));
257
				}
258
			break;
259
		}
260
	}
261
262
	/**
263
	* Check if config field values exceed 255 chars
264
	*
265
	* @return null
266
	* @access protected
267
	*/
268
	protected function validate_config_length()
269
	{
270
		$arg_list = func_get_args();
271
		foreach ($arg_list as $arg)
272
		{
273
			if (strlen($arg) > 255)
274
			{
275
				$this->end('PST_ERR_CONFIG', E_USER_WARNING);
276
			}
277
		}
278
	}
279
280
	/**
281
	* Check form key, trigger error if invalid
282
	*
283
	* @param string $form_key The form key value
284
	* @return null
285
	* @access protected
286
	*/
287
	protected function check_form_key($form_key)
288
	{
289
		if (!check_form_key($form_key))
290
		{
291
			$this->end('FORM_INVALID', E_USER_WARNING);
292
		}
293
	}
294
295
	/**
296
	* Get forums list
297
	*
298
	* @return array	forum data rows
299
	* @access protected
300
	*/
301
	protected function get_forum_list()
302
	{
303
		$sql = 'SELECT forum_id, forum_name, similar_topic_forums
304
			FROM ' . FORUMS_TABLE . '
305
			WHERE forum_type = ' . FORUM_POST . '
306
			ORDER BY left_id ASC';
307
		$result = $this->db->sql_query($sql);
308
		$forum_list = $this->db->sql_fetchrowset($result);
309
		$this->db->sql_freeresult($result);
310
311
		return $forum_list;
312
	}
313
314
	/**
315
	* Calculate the $pst_time based on user input
316
	*
317
	* @param int $length user entered value
318
	* @param string $type years, months, weeks, days (y|m|w|d)
319
	* @return int time in seconds
320
	* @access protected
321
	*/
322
	protected function set_pst_time($length, $type = 'y')
323
	{
324
		$type = isset($this->times[$type]) ? $type : 'y';
325
326
		return (int) ($length * $this->times[$type]);
327
	}
328
329
	/**
330
	* Get the correct time $length value for the form
331
	*
332
	* @param int $time as a timestamp
333
	* @param string $type years, months, weeks, days (y|m|w|d)
334
	* @return int time converted to the given $type
335
	* @access protected
336
	*/
337
	protected function get_pst_time($time, $type = '')
338
	{
339
		return isset($this->times[$type]) ? (int) round($time / $this->times[$type]) : 0;
340
	}
341
342
	/**
343
	* Check for FULLTEXT index support
344
	*
345
	* @return bool True if FULLTEXT is fully supported, false otherwise
346
	* @access protected
347
	*/
348
	protected function fulltext_support_enabled()
349
	{
350
		if ($this->fulltext->is_supported())
351
		{
352
			return $this->fulltext->index('topic_title');
353
		}
354
355
		return false;
356
	}
357
358
	/**
359
	* Enable FULLTEXT support for the topic_title
360
	*
361
	* @return null
362
	* @access protected
363
	*/
364
	protected function enable_fulltext_support()
365
	{
366
		if (!$this->fulltext->is_mysql())
367
		{
368
			$this->end('PST_NO_MYSQL', E_USER_WARNING);
369
		}
370
371
		// Alter the storage engine
372
		$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' ENGINE = MYISAM';
373
		$this->db->sql_query($sql);
374
375
		// Prevent adding extra indeces.
376
		if ($this->fulltext->index('topic_title'))
377
		{
378
			return;
379
		}
380
381
		$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' ADD FULLTEXT (topic_title)';
382
		$this->db->sql_query($sql);
383
	}
384
385
	/**
386
	* Return a variable if it is set, otherwise default
387
	*
388
	* @param mixed $var The variable to test
389
	* @param mixed $default The default value to use
390
	* @return mixed The value of the variable if set, otherwise default value
391
	* @access protected
392
	*/
393
	protected function isset_or_default($var, $default)
394
	{
395
		return (isset($var)) ? $var : $default;
396
	}
397
398
	/**
399
	 * End script execution with a trigger_error message
400
	 *
401
	 * @param string $message Language key string
402
	 * @param int    $code    E_USER_NOTICE|E_USER_WARNING
403
	 * @return null
404
	 * @access protected
405
	 */
406
	protected function end($message, $code = E_USER_NOTICE)
407
	{
408
		trigger_error($this->language->lang($message) . adm_back_link($this->u_action), $code);
409
	}
410
}
411