Completed
Pull Request — master (#32)
by Matt
07:28
created

topic_preview_module::set_themes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 3
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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('acp/common');
68 4
		$this->user->add_lang_ext('vse/topicpreview', 'topic_preview_acp');
69 4
	}
70
71
	/**
72
	 * Main ACP module
73
	 *
74
	 * @param int    $id
75
	 * @param string $mode
76
	 * @return null
77
	 */
78 3
	public function main($id, $mode)
79
	{
80 3
		$this->tpl_name = 'acp_topic_preview';
81 3
		$this->page_title = $this->user->lang('TOPIC_PREVIEW');
82
83 3
		$form_key = 'acp_topic_preview';
84 3
		add_form_key($form_key);
85
86 3
		if ($this->request->is_set_post('submit'))
87 3
		{
88 2
			if (!check_form_key($form_key))
89 2
			{
90 1
				trigger_error($this->user->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
91
			}
92
93 1
			$this->config->set('topic_preview_limit', abs($this->request->variable('topic_preview_limit', 0))); // abs() no negative values
94 1
			$this->config->set('topic_preview_width', abs($this->request->variable('topic_preview_width', 0))); // abs() no negative values
95 1
			$this->config->set('topic_preview_delay', abs($this->request->variable('topic_preview_delay', 0))); // abs() no negative values
96 1
			$this->config->set('topic_preview_drift', $this->request->variable('topic_preview_drift', 0));
97 1
			$this->config->set('topic_preview_avatars', $this->request->variable('topic_preview_avatars', 0));
98 1
			$this->config->set('topic_preview_last_post', $this->request->variable('topic_preview_last_post', 0));
99 1
			$this->config->set('topic_preview_strip_bbcodes', $this->request->variable('topic_preview_strip_bbcodes', ''));
100
101 1
			$styles = $this->get_styles();
102 1
			foreach ($styles as $row)
103
			{
104 1
				$this->set_style_theme($row['style_id'], $this->request->variable('style_' . $row['style_id'], ''));
105 1
			}
106
107 1
			trigger_error($this->user->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
108
		}
109
110 1
		$this->template->assign_vars(array(
111 1
			'TOPIC_PREVIEW_LIMIT'		=> $this->config['topic_preview_limit'],
112 1
			'TOPIC_PREVIEW_WIDTH'		=> $this->config['topic_preview_width'],
113 1
			'TOPIC_PREVIEW_DELAY'		=> $this->config['topic_preview_delay'],
114 1
			'TOPIC_PREVIEW_DRIFT'		=> $this->config['topic_preview_drift'],
115 1
			'S_TOPIC_PREVIEW_AVATARS'	=> $this->config['topic_preview_avatars'],
116 1
			'S_TOPIC_PREVIEW_LAST_POST'	=> $this->config['topic_preview_last_post'],
117 1
			'TOPIC_PREVIEW_STRIP'		=> $this->config['topic_preview_strip_bbcodes'],
118 1
			'TOPIC_PREVIEW_STYLES'		=> $this->get_styles(),
119 1
			'TOPIC_PREVIEW_THEMES'		=> $this->get_themes(),
120 1
			'TOPIC_PREVIEW_DEFAULT'		=> self::DEFAULT_THEME,
121 1
			'TOPIC_PREVIEW_NO_THEME'	=> self::NO_THEME,
122 1
			'U_ACTION'					=> $this->u_action,
123 1
		));
124 1
	}
125
126
	/**
127
	 * Update topic_preview_theme setting in the styles table
128
	 *
129
	 * @param int    $style_id Identifier of the board style
130
	 * @param string $theme    Name of the selected theme
131
	 * @return null
132
	 */
133 1
	protected function set_style_theme($style_id, $theme)
134
	{
135 1
		$sql = 'UPDATE ' . STYLES_TABLE . "
136 1
			SET topic_preview_theme = '" . $this->db->sql_escape($theme) . "'
137 1
			WHERE style_id = " . (int) $style_id;
138
139 1
		$this->db->sql_query($sql);
140
141 1
		$this->cache->destroy('sql', STYLES_TABLE);
142 1
	}
143
144
	/**
145
	 * Get style data from the styles table
146
	 *
147
	 * @return array Style data array
148
	 */
149 2
	protected function get_styles()
150
	{
151
		$sql = 'SELECT style_id, style_name, topic_preview_theme
152 2
			FROM ' . STYLES_TABLE . '
153 2
			WHERE style_active = 1';
154 2
		$result = $this->db->sql_query($sql);
155
156 2
		$rows = $this->db->sql_fetchrowset($result);
157 2
		$this->db->sql_freeresult($result);
158
159 2
		return $rows;
160
	}
161
162
	/**
163
	 * Get file names from Topic Preview's CSS files
164
	 *
165
	 * @return array File name data array
166
	 */
167 1
	protected function get_themes()
168
	{
169 1
		$finder = $this->ext_manager->get_finder();
170
171
		// Find css files in ext/vse/topicpreview/styles/all/theme/
172
		$themes = $finder
173 1
			->extension_suffix('.css')
174 1
			->extension_directory('/styles/all/theme')
175 1
			->find_from_extension('topicpreview', $this->phpbb_root_path . 'ext/vse/topicpreview/');
176
177
		// Get just basenames of array keys
178 1
		$themes = array_map(function ($value) {
179 1
			return basename($value, '.css');
180 1
		}, array_keys($themes));
181
182
		// Add option for native browser tooltip (aka no theme)
183 1
		$themes[] = self::NO_THEME;
184
185 1
		return $themes;
186
	}
187
}
188