Completed
Push — develop ( a3ab64...524863 )
by Daniel
13:34
created

settings_module::get_styles_data()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 15
nc 2
nop 1
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\finder */
24
	protected $finder;
25
26
	/** @var \phpbb\request\request_interface */
27
	protected $request;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var \phpbb\language\language */
33
	protected $translator;
34
35
	/** @var \blitze\sitemaker\services\icon_picker */
36
	protected $icon;
37
38
	/** @var \blitze\sitemaker\services\util */
39
	protected $util;
40
41
	/** @var string phpBB root path */
42
	protected $phpbb_root_path;
43
44
	/** @var string phpEx */
45
	protected $php_ext;
46
47
	/** @var string */
48
	public $tpl_name;
49
50
	/** @var string */
51
	public $page_title;
52
53
	/** @var string */
54
	public $u_action;
55
56
	/** @var bool */
57
	public $trigger_errors;
58
59
	/**
60
	 * settings_module constructor.
61
	 */
62
	public function __construct($trigger_errors = true)
63
	{
64
		global $phpbb_container, $config, $db, $request, $template, $phpbb_root_path, $phpEx;
1 ignored issue
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...
65
66
		$this->db = $db;
0 ignored issues
show
Bug introduced by
The property db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
		$this->config = $config;
68
		$this->request = $request;
69
		$this->template = $template;
70
		$this->phpbb_root_path = $phpbb_root_path;
71
		$this->php_ext = $phpEx;
72
73
		$this->config_text = $phpbb_container->get('config_text');
74
		$this->finder = $phpbb_container->get('ext.manager')->get_finder();
75
		$this->translator = $phpbb_container->get('language');
76
		$this->icon = $phpbb_container->get('blitze.sitemaker.icon_picker');
77
		$this->util = $phpbb_container->get('blitze.sitemaker.util');
78
		$this->trigger_errors = $trigger_errors;
79
	}
80
81
	/**
82
	 *
83
	 */
84
	public function main()
85
	{
86
		$this->translator->add_lang('blocks_admin', 'blitze/sitemaker');
87
88
		$form_key = 'blitze/sitemaker';
89
90
		add_form_key($form_key);
91
92
		$this->save_settings($form_key);
93
94
		$layouts = $this->get_layouts();
95
96
        $this->template->assign_vars(array(
97
            'u_action'			=> $this->u_action,
98
			'icon_picker'		=> $this->icon->picker(),
99
            'forum_icon'		=> $this->config['sm_forum_icon'],
100
            'show_forum_nav'	=> (bool) $this->config['sm_show_forum_nav'],
101
			'hide_login'		=> (bool) $this->config['sm_hide_login'],
102
			'hide_online'		=> (bool) $this->config['sm_hide_online'],
103
			'hide_birthday'		=> (bool) $this->config['sm_hide_birthday'],
104
            'styles'			=> $this->get_styles_data($layouts),
105
            'layouts'			=> $layouts,
106
		));
107
108
		$this->util->add_assets(array(
109
			'js'	=> array('@blitze_sitemaker/assets/settings/admin.min.js'),
110
			'css'	=> array('@blitze_sitemaker/assets/settings/admin.min.css'),
111
		));
112
113
		$this->tpl_name = 'acp_settings';
114
		$this->page_title = 'ACP_SM_SETTINGS';
115
	}
116
117
	/**
118
	 * @param string $form_key
119
	 */
120
	protected function save_settings($form_key)
121
	{
122
        if ($this->request->is_set_post('submit'))
123
        {
124
            $this->check_form_key($form_key);
125
126
			$layout_prefs = $this->request->variable('layouts', array(0 => array('' => '')));
127
            $this->config_text->set('sm_layout_prefs', json_encode($layout_prefs));
128
129
            $this->config->set('sm_hide_login', $this->request->variable('hide_login', 0));
130
            $this->config->set('sm_hide_online', $this->request->variable('hide_online', 0));
131
            $this->config->set('sm_hide_birthday', $this->request->variable('hide_birthday', 0));
132
            $this->config->set('sm_show_forum_nav', $this->request->variable('show_forum_nav', 0));
133
            $this->config->set('sm_forum_icon', $this->request->variable('forum_icon', ''));
134
135
            $this->trigger_error($this->translator->lang('SETTINGS_SAVED') . adm_back_link($this->u_action));
136
        }
137
	}
138
139
	/**
140
	 * @param array $layouts
141
	 * @return array
142
	 */
143
	protected function get_styles_data(array $layouts)
144
	{
145
		$style_prefs = json_decode($this->config_text->get('sm_layout_prefs'), true);
146
147
		$result = $this->db->sql_query('SELECT style_id, style_name FROM ' . STYLES_TABLE);
148
149
		$styles = array();
150
		while ($row = $this->db->sql_fetchrow($result))
151
		{
152
			$id = $row['style_id'];
153
154
			$pref = $this->get_style_pref($id, $style_prefs);
155
156
			$styles[] = array(
157
				'id'		=> $id,
158
				'name'		=> $row['style_name'],
159
				'layout'	=> $pref['layout'],
160
				'layouts'	=> $this->get_layout_options($layouts, $pref['layout']),
161
				'views'		=> $this->get_view_options($pref['view']),
162
			);
163
		}
164
		$this->db->sql_freeresult();
165
166
		return $styles;
167
	}
168
169
	/**
170
	 * @param int $style_id
171
	 * @param array $style_prefs
172
	 * @return array
173
	 */
174
	protected function get_style_pref($style_id, array $style_prefs)
175
	{
176
		$pref = array(
177
			'layout'	=> '',
178
			'view'		=> '',
179
		);
180
181
		if (isset($style_prefs[$style_id]))
182
		{
183
			$pref = $style_prefs[$style_id];
184
		}
185
186
		return $pref;
187
	}
188
189
	/**
190
	 * @param array $layouts
191
	 * @param string $pref
192
	 * @return string
193
	 */
194
	protected function get_layout_options(array $layouts, $pref)
195
	{
196
		$options = '';
197
		foreach ($layouts as $name => $path)
198
		{
199
			$selected = ($path == $pref) ? ' selected="selected"' : '';
200
			$options .= '<option value="' . $path . '"' . $selected . '>' . $this->translator->lang('LAYOUT_' . strtoupper($name)) . '</option>';
201
		}
202
203
		return $options;
204
	}
205
206
	/**
207
	 * @param string $pref
208
	 * @return string
209
	 */
210
	protected function get_view_options($pref)
211
	{
212
		$views = array('basic', 'boxed', 'simple');
213
214
		$options = '<option value="">' . $this->translator->lang('BLOCK_VIEW_DEFAULT') . '</option>';
215
		foreach ($views as $view)
216
		{
217
			$selected = ($view == $pref) ? ' selected="selected"' : '';
218
			$options .= '<option value="' . $view . '"' . $selected . '>' . $this->translator->lang('BLOCK_VIEW_' . strtoupper($view)) . '</option>';
219
		}
220
221
		return $options;
222
	}
223
224
	/**
225
	 * @param string $form_key
226
	 */
227
	protected function check_form_key($form_key)
228
	{
229
		if (!check_form_key($form_key))
230
        {
231
            $this->trigger_error('FORM_INVALID');
232
        }
233
	}
234
235
	/**
236
	 * @param string $message
237
	 */
238
	protected function trigger_error($message)
239
	{
240
		$this->trigger_errors ? trigger_error($message) : null;
241
	}
242
243
	/**
244
	 * @return array
245
	 */
246
	protected function get_layouts()
247
	{
248
		$files = $this->finder
249
			->suffix('_layout.twig')
250
			->extension_directory('/styles')
251
			->find();
252
		$files = array_keys($files);
253
254
		$layouts = array();
255
		foreach ($files as $path)
256
		{
257
			$path = dirname($path);
258
			$name = basename($path);
259
260
			$layouts[$name] = $this->phpbb_root_path . $path . '/';
261
		}
262
263
		return $layouts;
264
	}
265
}
266