Completed
Push — develop ( 86b038...fd19b8 )
by Daniel
09:51
created

settings_module::get_menu_options()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
crap 3
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\model\mapper_factory */
42
	protected $mapper_factory;
43
44
	/** @var \blitze\sitemaker\services\util */
45
	protected $util;
46
47
	/** @var string phpBB root path */
48
	protected $phpbb_root_path;
49
50
	/** @var string phpEx */
51
	protected $php_ext;
52
53
	/** @var string */
54
	public $tpl_name;
55
56
	/** @var string */
57
	public $page_title;
58
59
	/** @var string */
60
	public $u_action;
61
62
	/** @var bool */
63
	public $trigger_errors;
64
65
	/**
66
	 * settings_module constructor.
67
	 */
68 2
	public function __construct($trigger_errors = true)
69
	{
70 2
		global $phpbb_container, $config, $db, $request, $template, $phpbb_root_path, $phpEx;
71
72 2
		$this->db = $db;
73 2
		$this->config = $config;
74 2
		$this->request = $request;
75 2
		$this->template = $template;
76 2
		$this->phpbb_root_path = $phpbb_root_path;
77 2
		$this->php_ext = $phpEx;
78
79 2
		$this->config_text = $phpbb_container->get('config_text');
80 2
		$this->finder = $phpbb_container->get('ext.manager')->get_finder();
81 2
		$this->translator = $phpbb_container->get('language');
82 2
		$this->icon = $phpbb_container->get('blitze.sitemaker.icon_picker');
83 2
		$this->mapper_factory = $phpbb_container->get('blitze.sitemaker.mapper.factory');
84 2
		$this->util = $phpbb_container->get('blitze.sitemaker.util');
85 2
		$this->trigger_errors = $trigger_errors;
86 2
	}
87
88
	/**
89
	 *
90
	 */
91 2
	public function main()
92
	{
93 2
		$this->translator->add_lang('blocks_admin', 'blitze/sitemaker');
94
95 2
		$form_key = 'blitze/sitemaker';
96
97 2
		add_form_key($form_key);
98
99 2
		$this->save_settings($form_key);
100
101 2
		$layouts = $this->get_layouts();
102
103 2
		$this->template->assign_vars(array(
104 2
			'u_action'			=> $this->u_action,
105 2
			'icon_picker'		=> $this->icon->picker(),
106 2
			'forum_icon'		=> $this->config['sm_forum_icon'],
107 2
			'show_forum_nav'	=> (bool) $this->config['sm_show_forum_nav'],
108 2
			'hide_login'		=> (bool) $this->config['sm_hide_login'],
109 2
			'hide_online'		=> (bool) $this->config['sm_hide_online'],
110 2
			'hide_birthday'		=> (bool) $this->config['sm_hide_birthday'],
111 2
			'styles'			=> $this->get_styles_data($layouts),
112 2
			'layouts'			=> $layouts,
113 2
			'menu_options'		=> $this->get_menu_options(),
114 2
		));
115
116 2
		$this->util->add_assets(array(
117 2
			'js'	=> array('@blitze_sitemaker/assets/settings/admin.min.js'),
118 2
			'css'	=> array('@blitze_sitemaker/assets/settings/admin.min.css'),
119 2
		));
120
121 2
		$this->tpl_name = 'acp_settings';
122 2
		$this->page_title = 'ACP_SM_SETTINGS';
123 2
	}
124
125
	/**
126
	 * @param string $form_key
127
	 */
128 2
	protected function save_settings($form_key)
129
	{
130 2
		if ($this->request->is_set_post('submit'))
131 2
		{
132 1
			$this->check_form_key($form_key);
133
134 1
			$layout_prefs = $this->request->variable('layouts', array(0 => array('' => '')));
135 1
			$this->config_text->set('sm_layout_prefs', json_encode($layout_prefs));
136
137 1
			$this->config->set('sm_hide_login', $this->request->variable('hide_login', 0));
138 1
			$this->config->set('sm_hide_online', $this->request->variable('hide_online', 0));
139 1
			$this->config->set('sm_hide_birthday', $this->request->variable('hide_birthday', 0));
140 1
			$this->config->set('sm_show_forum_nav', $this->request->variable('show_forum_nav', 0));
141 1
			$this->config->set('sm_forum_icon', $this->request->variable('forum_icon', ''));
142 1
			$this->config->set('sm_navbar_menu', $this->request->variable('navbar_menu', 0));
143
144 1
			$this->trigger_error($this->translator->lang('SETTINGS_SAVED') . adm_back_link($this->u_action));
145 1
		}
146 2
	}
147
148
	/**
149
	 * @param array $layouts
150
	 * @return array
151
	 */
152 2
	protected function get_styles_data(array $layouts)
153
	{
154 2
		$style_prefs = (array) json_decode($this->config_text->get('sm_layout_prefs'), true);
155
156 2
		$result = $this->db->sql_query('SELECT style_id, style_name FROM ' . STYLES_TABLE);
157
158 2
		$styles = array();
159 2
		while ($row = $this->db->sql_fetchrow($result))
160
		{
161 2
			$id = $row['style_id'];
162
163 2
			$pref = $this->get_style_pref($id, $style_prefs, $layouts['portal']);
164
165 2
			$styles[] = array(
166 2
				'id'		=> $id,
167 2
				'name'		=> $row['style_name'],
168 2
				'layout'	=> $pref['layout'],
169 2
				'view'		=> $pref['view'],
170
			);
171 2
		}
172 2
		$this->db->sql_freeresult();
173
174 2
		return $styles;
175
	}
176
177
	/**
178
	 * @param int $style_id
179
	 * @param array $style_prefs
180
	 * @param string $default_layout
181
	 * @return array
182
	 */
183 2
	protected function get_style_pref($style_id, array $style_prefs, $default_layout)
184
	{
185
		$pref = array(
186 2
			'layout'	=> $default_layout,
187 2
			'view'		=> '',
188 2
		);
189
190 2
		if (isset($style_prefs[$style_id]))
191 2
		{
192 2
			$pref = $style_prefs[$style_id];
193 2
		}
194
195 2
		return $pref;
196 1
	}
197
198
	/**
199
	 * @param string $form_key
200
	 */
201 1
	protected function check_form_key($form_key)
202
	{
203 1
		if (!check_form_key($form_key))
204 1
		{
205 1
			$this->trigger_error('FORM_INVALID');
206 1
		}
207 1
	}
208
209
	/**
210
	 * @param string $message
211
	 */
212 1
	protected function trigger_error($message)
213
	{
214 1
		$this->trigger_errors ? trigger_error($message) : null;
215 1
	}
216
217
	/**
218
	 * @return array
219
	 */
220 2
	protected function get_layouts()
221
	{
222 2
		$files = $this->finder
223 2
			->suffix('_layout.twig')
224 2
			->extension_directory('/styles')
225 2
			->find();
226 2
		$files = array_keys($files);
227
228 2
		$layouts = array();
229 2
		foreach ($files as $path)
230
		{
231 2
			$path = dirname($path);
232 2
			$name = basename($path);
233
234 2
			$layouts[$name] = $this->phpbb_root_path . $path . '/';
235 2
		}
236 2
		ksort($layouts);
237
238 2
		return $layouts;
239
	}
240
241
	/**
242
	 * @return string
243
	 */
244 2
	protected function get_menu_options()
245
	{
246 2
		$menu_mapper = $this->mapper_factory->create('menus');
247
248
		// Get all menus
249 2
		$collection = $menu_mapper->find();
250
251 2
		$options = '';
252 2
		foreach ($collection as $entity)
253
		{
254 2
			$id = $entity->get_menu_id();
255 2
			$name = $entity->get_menu_name();
256 2
			$selected = ($id == $this->config['sm_navbar_menu']) ? ' selected="selected"' : '';
257 2
			$options .= '<option value="' . $id . '"' . $selected . '>' . $name . '</option>';
258 2
		}
259
260 2
		return $options;
261
	}
262
}
263