Completed
Pull Request — master (#33)
by Matt
07:11
created

settings::set_style_theme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
/**
3
*
4
* Topic Preview
5
*
6
* @copyright (c) 2013, 2016 Matt Friedman
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace vse\topicpreview\core;
12
13
use phpbb\cache\driver\driver_interface as cache_driver;
14
use phpbb\config\config;
15
use phpbb\db\driver\driver_interface as db_driver;
16
use phpbb\extension\manager;
17
use phpbb\request\request;
18
19
/**
20
 * Class settings
21
 *
22
 * @package vse\topicpreview\core
23
 */
24
class settings
25
{
26
	const NO_THEME = 'no';
27
	const DEFAULT_THEME = 'light';
28
29
	/** @var cache_driver */
30
	protected $cache;
31
32
	/** @var config */
33
	protected $config;
34
35
	/** @var db_driver */
36
	protected $db;
37
38
	/** @var manager */
39
	protected $ext_manager;
40
41
	/** @var request */
42
	protected $request;
43
44
	/** @var string */
45
	protected $phpbb_root_path;
46
47
	/** @var string */
48
	protected $u_action;
49
50
	/**
51
	 * Constructor
52
	 *
53
	 * @param cache_driver $cache
54
	 * @param config       $config
55
	 * @param db_driver    $db
56
	 * @param manager      $phpbb_ext_manager
57
	 * @param request      $request
58
	 * @param              $phpbb_root_path
59
	 */
60 3
	public function __construct(cache_driver $cache, config $config, db_driver $db, manager $phpbb_ext_manager, request $request, $phpbb_root_path)
61
	{
62 3
		$this->cache			= $cache;
63 3
		$this->config			= $config;
64 3
		$this->db				= $db;
65 3
		$this->ext_manager		= $phpbb_ext_manager;
66 3
		$this->request			= $request;
67 3
		$this->phpbb_root_path	= $phpbb_root_path;
68 3
	}
69
70
	/**
71
	 * Display the settings with the current config values
72
	 *
73
	 * @param string $u_action
74
	 * @return array
75
	 */
76 1
	public function display_settings($u_action)
77
	{
78
		return array(
79 1
			'TOPIC_PREVIEW_LIMIT'		=> $this->config['topic_preview_limit'],
80 1
			'TOPIC_PREVIEW_WIDTH'		=> $this->config['topic_preview_width'],
81 1
			'TOPIC_PREVIEW_DELAY'		=> $this->config['topic_preview_delay'],
82 1
			'TOPIC_PREVIEW_DRIFT'		=> $this->config['topic_preview_drift'],
83 1
			'S_TOPIC_PREVIEW_AVATARS'	=> $this->config['topic_preview_avatars'],
84 1
			'S_TOPIC_PREVIEW_LAST_POST'	=> $this->config['topic_preview_last_post'],
85 1
			'TOPIC_PREVIEW_STRIP'		=> $this->config['topic_preview_strip_bbcodes'],
86 1
			'TOPIC_PREVIEW_STYLES'		=> $this->get_styles(),
87 1
			'TOPIC_PREVIEW_THEMES'		=> $this->get_themes(),
88 1
			'TOPIC_PREVIEW_DEFAULT'		=> self::DEFAULT_THEME,
89 1
			'TOPIC_PREVIEW_NO_THEME'	=> self::NO_THEME,
90 1
			'U_ACTION'					=> $u_action,
91 1
		);
92
	}
93
94
	/**
95
	 * Set the settings data from the form to the database
96
	 */
97 1
	public function set_settings()
98
	{
99 1
		$this->config->set('topic_preview_limit', abs($this->request->variable('topic_preview_limit', 0))); // abs() no negative values
100 1
		$this->config->set('topic_preview_width', abs($this->request->variable('topic_preview_width', 0))); // abs() no negative values
101 1
		$this->config->set('topic_preview_delay', abs($this->request->variable('topic_preview_delay', 0))); // abs() no negative values
102 1
		$this->config->set('topic_preview_drift', $this->request->variable('topic_preview_drift', 0));
103 1
		$this->config->set('topic_preview_avatars', $this->request->variable('topic_preview_avatars', 0));
104 1
		$this->config->set('topic_preview_last_post', $this->request->variable('topic_preview_last_post', 0));
105 1
		$this->config->set('topic_preview_strip_bbcodes', $this->request->variable('topic_preview_strip_bbcodes', ''));
106
107 1
		$styles = $this->get_styles();
108 1
		foreach ($styles as $row)
109
		{
110 1
			$this->set_style_theme($row['style_id'], $this->request->variable('style_' . $row['style_id'], ''));
111 1
		}
112 1
	}
113
114
	/**
115
	 * Update topic_preview_theme setting in the styles table
116
	 *
117
	 * @param int    $style_id Identifier of the board style
118
	 * @param string $theme    Name of the selected theme
119
	 */
120 1
	protected function set_style_theme($style_id, $theme)
121
	{
122 1
		$sql = 'UPDATE ' . STYLES_TABLE . "
123 1
			SET topic_preview_theme = '" . $this->db->sql_escape($theme) . "'
124 1
			WHERE style_id = " . (int) $style_id;
125
126 1
		$this->db->sql_query($sql);
127
128 1
		$this->cache->destroy('sql', STYLES_TABLE);
129 1
	}
130
131
	/**
132
	 * Get style data from the styles table
133
	 *
134
	 * @return array Style data
135
	 */
136 2
	protected function get_styles()
137
	{
138
		$sql = 'SELECT style_id, style_name, topic_preview_theme
139 2
			FROM ' . STYLES_TABLE . '
140 2
			WHERE style_active = 1';
141 2
		$result = $this->db->sql_query($sql);
142
143 2
		$rows = $this->db->sql_fetchrowset($result);
144 2
		$this->db->sql_freeresult($result);
145
146 2
		return $rows;
147
	}
148
149
	/**
150
	 * Get file names from Topic Preview's CSS files
151
	 *
152
	 * @return array File name data
153
	 */
154 1
	protected function get_themes()
155
	{
156 1
		$finder = $this->ext_manager->get_finder();
157
158
		// Find css files in ext/vse/topicpreview/styles/all/theme/
159
		$themes = $finder
160 1
			->extension_suffix('.css')
161 1
			->extension_directory('/styles/all/theme')
162 1
			->find_from_extension('topicpreview', $this->phpbb_root_path . 'ext/vse/topicpreview/');
163
164
		// Get just basenames of array keys
165 1
		$themes = array_map(function ($value) {
166 1
			return basename($value, '.css');
167 1
		}, array_keys($themes));
168
169
		// Add option for native browser tooltip (aka no theme)
170 1
		$themes[] = self::NO_THEME;
171
172 1
		return $themes;
173
	}
174
}
175