Completed
Push — master ( bb090d...ec5717 )
by Matt
14:41
created

topic_preview_module   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.53%

Importance

Changes 16
Bugs 0 Features 3
Metric Value
wmc 10
c 16
b 0
f 3
lcom 1
cbo 0
dl 0
loc 186
ccs 79
cts 81
cp 0.9753
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A main() 0 21 3
A display_settings() 0 17 1
A submit_settings() 0 16 2
A set_style_theme() 0 10 1
A get_styles() 0 12 1
A get_themes() 0 20 1
1
<?php
2
/**
3
*
4
* Topic Preview
5
*
6
* @copyright (c) 2013 Matt Friedman
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace vse\topicpreview\acp;
12
13
/**
14
* @package acp
15
*/
16
class topic_preview_module
17
{
18
	const NO_THEME = 'no';
19
	const DEFAULT_THEME = 'light';
20
21
	/** @var \phpbb\cache\driver\driver_interface */
22
	protected $cache;
23
24
	/** @var \phpbb\config\config */
25
	protected $config;
26
27
	/** @var \phpbb\db\driver\driver_interface */
28
	protected $db;
29
30
	/** @var \phpbb\extension\manager */
31
	protected $ext_manager;
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 $phpbb_root_path;
44
45
	/** @var array */
46
	protected $themes;
47
48
	/** @var string */
49
	public $u_action;
50
51
	/**
52
	 * Constructor
53
	 */
54 4
	public function __construct()
55
	{
56 4
		global $cache, $config, $db, $phpbb_extension_manager, $request, $template, $user, $phpbb_root_path;
57
58 4
		$this->cache = $cache;
59 4
		$this->config = $config;
60 4
		$this->db = $db;
61 4
		$this->ext_manager = $phpbb_extension_manager;
62 4
		$this->request = $request;
63 4
		$this->template = $template;
64 4
		$this->user = $user;
65 4
		$this->phpbb_root_path = $phpbb_root_path;
66
67 4
		$this->user->add_lang_ext('vse/topicpreview', 'topic_preview_acp');
68 4
	}
69
70
	/**
71
	 * Main ACP module
72
	 *
73
	 * @param int    $id
74
	 * @param string $mode
75
	 * @return null
76
	 */
77 3
	public function main($id, $mode)
78
	{
79 3
		$this->tpl_name = 'acp_topic_preview';
80 3
		$this->page_title = $this->user->lang('TOPIC_PREVIEW');
81
82 3
		$form_key = 'acp_topic_preview';
83 3
		add_form_key($form_key);
84
85 3
		if ($this->request->is_set_post('submit'))
86 3
		{
87 2
			if (!check_form_key($form_key))
88 2
			{
89 1
				trigger_error($this->user->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
90
			}
91
92 1
			$this->submit_settings();
93 1
			trigger_error($this->user->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
94
		}
95
96 1
		$this->display_settings();
97 1
	}
98
99
	/**
100
	 * Display the settings with the current config values
101
	 */
102 1
	protected function display_settings()
103
	{
104 1
		$this->template->assign_vars(array(
105 1
			'TOPIC_PREVIEW_LIMIT'		=> $this->config['topic_preview_limit'],
106 1
			'TOPIC_PREVIEW_WIDTH'		=> $this->config['topic_preview_width'],
107 1
			'TOPIC_PREVIEW_DELAY'		=> $this->config['topic_preview_delay'],
108 1
			'TOPIC_PREVIEW_DRIFT'		=> $this->config['topic_preview_drift'],
109 1
			'S_TOPIC_PREVIEW_AVATARS'	=> $this->config['topic_preview_avatars'],
110 1
			'S_TOPIC_PREVIEW_LAST_POST'	=> $this->config['topic_preview_last_post'],
111 1
			'TOPIC_PREVIEW_STRIP'		=> $this->config['topic_preview_strip_bbcodes'],
112 1
			'TOPIC_PREVIEW_STYLES'		=> $this->get_styles(),
113 1
			'TOPIC_PREVIEW_THEMES'		=> $this->get_themes(),
114 1
			'TOPIC_PREVIEW_DEFAULT'		=> self::DEFAULT_THEME,
115 1
			'TOPIC_PREVIEW_NO_THEME'	=> self::NO_THEME,
116 1
			'U_ACTION'					=> $this->u_action,
117 1
		));
118 1
	}
119
120
	/**
121
	 * Submit the settings from the form to the database
122
	 */
123 1
	protected function submit_settings()
124
	{
125 1
		$this->config->set('topic_preview_limit', abs($this->request->variable('topic_preview_limit', 0))); // abs() no negative values
126 1
		$this->config->set('topic_preview_width', abs($this->request->variable('topic_preview_width', 0))); // abs() no negative values
127 1
		$this->config->set('topic_preview_delay', abs($this->request->variable('topic_preview_delay', 0))); // abs() no negative values
128 1
		$this->config->set('topic_preview_drift', $this->request->variable('topic_preview_drift', 0));
129 1
		$this->config->set('topic_preview_avatars', $this->request->variable('topic_preview_avatars', 0));
130 1
		$this->config->set('topic_preview_last_post', $this->request->variable('topic_preview_last_post', 0));
131 1
		$this->config->set('topic_preview_strip_bbcodes', $this->request->variable('topic_preview_strip_bbcodes', ''));
132
133 1
		$styles = $this->get_styles();
134 1
		foreach ($styles as $row)
135
		{
136 1
			$this->set_style_theme($row['style_id'], $this->request->variable('style_' . $row['style_id'], ''));
137 1
		}
138 1
	}
139
140
	/**
141
	 * Update topic_preview_theme setting in the styles table
142
	 *
143
	 * @param int    $style_id Identifier of the board style
144
	 * @param string $theme    Name of the selected theme
145
	 * @return null
146
	 */
147 1
	protected function set_style_theme($style_id, $theme)
148
	{
149 1
		$sql = 'UPDATE ' . STYLES_TABLE . "
150 1
			SET topic_preview_theme = '" . $this->db->sql_escape($theme) . "'
151 1
			WHERE style_id = " . (int) $style_id;
152
153 1
		$this->db->sql_query($sql);
154
155 1
		$this->cache->destroy('sql', STYLES_TABLE);
156 1
	}
157
158
	/**
159
	 * Get style data from the styles table
160
	 *
161
	 * @return array Style data array
162
	 */
163 2
	protected function get_styles()
164
	{
165
		$sql = 'SELECT style_id, style_name, topic_preview_theme
166 2
			FROM ' . STYLES_TABLE . '
167 2
			WHERE style_active = 1';
168 2
		$result = $this->db->sql_query($sql);
169
170 2
		$rows = $this->db->sql_fetchrowset($result);
171 2
		$this->db->sql_freeresult($result);
172
173 2
		return $rows;
174
	}
175
176
	/**
177
	 * Get file names from Topic Preview's CSS files
178
	 *
179
	 * @return array File name data array
180
	 */
181 3
	protected function get_themes()
182
	{
183 1
		$finder = $this->ext_manager->get_finder();
184
185
		// Find css files in ext/vse/topicpreview/styles/all/theme/
186
		$themes = $finder
187 1
			->extension_suffix('.css')
188 1
			->extension_directory('/styles/all/theme')
189 1
			->find_from_extension('topicpreview', $this->phpbb_root_path . 'ext/vse/topicpreview/');
190
191
		// Get just basenames of array keys
192 3
		$themes = array_map(function ($value) {
193 3
			return basename($value, '.css');
194 1
		}, array_keys($themes));
195
196
		// Add option for native browser tooltip (aka no theme)
197 1
		$themes[] = self::NO_THEME;
198
199 1
		return $themes;
200
	}
201
}
202