Completed
Push — develop ( 18ce4b...3c36de )
by Daniel
11:04
created

settings_module   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 220
ccs 101
cts 101
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
B main() 0 32 1
A save_settings() 0 18 2
B get_styles_data() 0 24 2
A get_style_pref() 0 14 2
A check_form_key() 0 7 2
A trigger_error() 0 4 2
A get_layouts() 0 20 2
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\acp;
11
12
/**
13
* @package acp
14
*/
15
class settings_module
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\config\db_text */
21
	protected $config_text;
22
23
	/** @var \phpbb\db\driver\driver_interface */
24
	protected $db;
25
26
	/** @var \phpbb\finder */
27
	protected $finder;
28
29
	/** @var \phpbb\request\request_interface */
30
	protected $request;
31
32
	/** @var \phpbb\template\template */
33
	protected $template;
34
35
	/** @var \phpbb\language\language */
36
	protected $translator;
37
38
	/** @var \blitze\sitemaker\services\icon_picker */
39
	protected $icon;
40
41
	/** @var \blitze\sitemaker\services\util */
42
	protected $util;
43
44
	/** @var string phpBB root path */
45
	protected $phpbb_root_path;
46
47
	/** @var string phpEx */
48
	protected $php_ext;
49
50
	/** @var string */
51
	public $tpl_name;
52
53
	/** @var string */
54
	public $page_title;
55
56
	/** @var string */
57
	public $u_action;
58
59
	/** @var bool */
60
	public $trigger_errors;
61
62
	/**
63
	 * settings_module constructor.
64
	 */
65 2
	public function __construct($trigger_errors = true)
66
	{
67 2
		global $phpbb_container, $config, $db, $request, $template, $phpbb_root_path, $phpEx;
68
69 2
		$this->db = $db;
70 2
		$this->config = $config;
71 2
		$this->request = $request;
72 2
		$this->template = $template;
73 2
		$this->phpbb_root_path = $phpbb_root_path;
74 2
		$this->php_ext = $phpEx;
75
76 2
		$this->config_text = $phpbb_container->get('config_text');
77 2
		$this->finder = $phpbb_container->get('ext.manager')->get_finder();
78 2
		$this->translator = $phpbb_container->get('language');
79 2
		$this->icon = $phpbb_container->get('blitze.sitemaker.icon_picker');
80 2
		$this->util = $phpbb_container->get('blitze.sitemaker.util');
81 2
		$this->trigger_errors = $trigger_errors;
82 2
	}
83
84
	/**
85
	 *
86
	 */
87 2
	public function main()
88
	{
89 2
		$this->translator->add_lang('blocks_admin', 'blitze/sitemaker');
90
91 2
		$form_key = 'blitze/sitemaker';
92
93 2
		add_form_key($form_key);
94
95 2
		$this->save_settings($form_key);
96
97 2
		$layouts = $this->get_layouts();
98
99 2
		$this->template->assign_vars(array(
100 2
			'u_action'			=> $this->u_action,
101 2
			'icon_picker'		=> $this->icon->picker(),
102 2
			'forum_icon'		=> $this->config['sm_forum_icon'],
103 2
			'show_forum_nav'	=> (bool) $this->config['sm_show_forum_nav'],
104 2
			'hide_login'		=> (bool) $this->config['sm_hide_login'],
105 2
			'hide_online'		=> (bool) $this->config['sm_hide_online'],
106 2
			'hide_birthday'		=> (bool) $this->config['sm_hide_birthday'],
107 2
			'styles'			=> $this->get_styles_data($layouts),
108 2
			'layouts'			=> $layouts,
109 2
		));
110
111 2
		$this->util->add_assets(array(
112 2
			'js'	=> array('@blitze_sitemaker/assets/settings/admin.min.js'),
113 2
			'css'	=> array('@blitze_sitemaker/assets/settings/admin.min.css'),
114 2
		));
115
116 2
		$this->tpl_name = 'acp_settings';
117 2
		$this->page_title = 'ACP_SM_SETTINGS';
118 2
	}
119
120
	/**
121
	 * @param string $form_key
122
	 */
123 2
	protected function save_settings($form_key)
124
	{
125 2
		if ($this->request->is_set_post('submit'))
126 2
		{
127 1
			$this->check_form_key($form_key);
128
129 1
			$layout_prefs = $this->request->variable('layouts', array(0 => array('' => '')));
130 1
			$this->config_text->set('sm_layout_prefs', json_encode($layout_prefs));
131
132 1
			$this->config->set('sm_hide_login', $this->request->variable('hide_login', 0));
133 1
			$this->config->set('sm_hide_online', $this->request->variable('hide_online', 0));
134 1
			$this->config->set('sm_hide_birthday', $this->request->variable('hide_birthday', 0));
135 1
			$this->config->set('sm_show_forum_nav', $this->request->variable('show_forum_nav', 0));
136 1
			$this->config->set('sm_forum_icon', $this->request->variable('forum_icon', ''));
137
138 1
			$this->trigger_error($this->translator->lang('SETTINGS_SAVED') . adm_back_link($this->u_action));
139 1
		}
140 2
	}
141
142
	/**
143
	 * @param array $layouts
144
	 * @return array
145
	 */
146 2
	protected function get_styles_data(array $layouts)
147
	{
148 2
		$style_prefs = (array) json_decode($this->config_text->get('sm_layout_prefs'), true);
149
150 2
		$result = $this->db->sql_query('SELECT style_id, style_name FROM ' . STYLES_TABLE);
151
152 2
		$styles = array();
153 2
		while ($row = $this->db->sql_fetchrow($result))
154
		{
155 2
			$id = $row['style_id'];
156
157 2
			$pref = $this->get_style_pref($id, $style_prefs, $layouts['portal']);
158
159 2
			$styles[] = array(
160 2
				'id'		=> $id,
161 2
				'name'		=> $row['style_name'],
162 2
				'layout'	=> $pref['layout'],
163 2
				'view'		=> $pref['view'],
164
			);
165 2
		}
166 2
		$this->db->sql_freeresult();
167
168 2
		return $styles;
169
	}
170
171
	/**
172
	 * @param int $style_id
173
	 * @param array $style_prefs
174
	 * @param string $default_layout
175
	 * @return array
176
	 */
177 2
	protected function get_style_pref($style_id, array $style_prefs, $default_layout)
178
	{
179
		$pref = array(
180 2
			'layout'	=> $default_layout,
181 2
			'view'		=> '',
182 2
		);
183
184 2
		if (isset($style_prefs[$style_id]))
185 2
		{
186 2
			$pref = $style_prefs[$style_id];
187 2
		}
188
189 2
		return $pref;
190
	}
191
192
	/**
193
	 * @param string $form_key
194
	 */
195 1
	protected function check_form_key($form_key)
196 1
	{
197 1
		if (!check_form_key($form_key))
198 1
		{
199 1
			$this->trigger_error('FORM_INVALID');
200 1
		}
201 1
	}
202
203
	/**
204
	 * @param string $message
205
	 */
206 1
	protected function trigger_error($message)
207
	{
208 1
		$this->trigger_errors ? trigger_error($message) : null;
209 1
	}
210
211
	/**
212
	 * @return array
213
	 */
214 2
	protected function get_layouts()
215
	{
216 2
		$files = $this->finder
217 2
			->suffix('_layout.twig')
218 2
			->extension_directory('/styles')
219 2
			->find();
220 2
		$files = array_keys($files);
221
222 2
		$layouts = array();
223 2
		foreach ($files as $path)
224
		{
225 2
			$path = dirname($path);
226 2
			$name = basename($path);
227
228 2
			$layouts[$name] = $this->phpbb_root_path . $path . '/';
229 2
		}
230 2
		ksort($layouts);
231
232 2
		return $layouts;
233
	}
234
}
235