Completed
Push — develop ( 660ae6...f77be0 )
by Daniel
08:50
created

settings_module::main()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 25
cts 25
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 0
crap 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\model\mapper_factory */
42
	protected $mapper_factory;
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
	protected $filemanger_config_file;
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->trigger_errors = $trigger_errors;
85
86 2
		$this->filemanager_config_file = $this->phpbb_root_path . 'ext/blitze/sitemaker/styles/all/theme/vendor/ResponsiveFilemanager/filemanager/config/config.' . $this->php_ext;
0 ignored issues
show
Bug introduced by
The property filemanager_config_file does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
87 2
	}
88
89
	/**
90
	 * @return void
91
	 */
92 2
	public function main()
93
	{
94 2
		$this->translator->add_lang('acp/board');
95 2
		$this->translator->add_lang('blocks_admin', 'blitze/sitemaker');
96
97 2
		$form_key = 'blitze/sitemaker/settings';
98
99 2
		if ($this->request->is_set_post('submit'))
100 2
		{
101 1
			$this->check_form_key($form_key);
102 1
			$this->save_filemanager_settings();
103 1
			$this->save_config_settings();
104
105 1
			$this->trigger_error($this->translator->lang('SETTINGS_SAVED') . adm_back_link($this->u_action));
106 1
		}
107
108 2
		add_form_key($form_key);
109
110 2
		$layouts = $this->get_layouts();
111 2
		$this->template->assign_vars(array(
112 2
			'u_action'			=> $this->u_action,
113 2
			'icon_picker'		=> $this->icon->picker(),
114 2
			'config'			=> $this->config,
115 2
			'filemanager'		=> $this->get_filemanager_settings(),
116 2
			'styles'			=> $this->get_styles_data($layouts),
117 2
			'layouts'			=> $layouts,
118 2
			'menu_options'		=> $this->get_menu_options(),
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 $path
127
	 * @return void
128
	 */
129 2
	public function set_filemanager_config_file($path)
130
	{
131 2
		$this->filemanager_config_file = $path;
0 ignored issues
show
Bug introduced by
The property filemanager_config_file does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
132 2
	}
133
134
	/**
135
	 * @param array $layouts
136
	 * @return array
137
	 */
138 2
	protected function get_styles_data(array $layouts)
139
	{
140 2
		$style_prefs = (array) json_decode($this->config_text->get('sm_layout_prefs'), true);
141
142 2
		$result = $this->db->sql_query('SELECT style_id, style_name FROM ' . STYLES_TABLE);
143
144 2
		$styles = array();
145 2
		while ($row = $this->db->sql_fetchrow($result))
146
		{
147 2
			$id = $row['style_id'];
148
149 2
			$pref = $this->get_style_pref($id, $style_prefs, $layouts['portal']);
150
151 2
			$styles[] = array(
152 2
				'id'		=> $id,
153 2
				'name'		=> $row['style_name'],
154 2
				'layout'	=> $pref['layout'],
155 2
				'view'		=> $pref['view'],
156
			);
157 2
		}
158 2
		$this->db->sql_freeresult();
159
160 2
		return $styles;
161
	}
162
163
	/**
164
	 * @param int $style_id
165
	 * @param array $style_prefs
166
	 * @param string $default_layout
167
	 * @return array
168
	 */
169 2
	protected function get_style_pref($style_id, array $style_prefs, $default_layout)
170
	{
171
		$pref = array(
172 2
			'layout'	=> $default_layout,
173 2
			'view'		=> '',
174 2
		);
175
176 2
		if (isset($style_prefs[$style_id]))
177 2
		{
178 2
			$pref = $style_prefs[$style_id];
179 2
		}
180
181 2
		return $pref;
182
	}
183
184
	/**
185
	 * @param string $form_key
186
	 */
187 1
	protected function check_form_key($form_key)
188
	{
189 1
		if (!check_form_key($form_key))
190 1
		{
191 1
			$this->trigger_error('FORM_INVALID');
192 1
		}
193 1
	}
194
195
	/**
196
	 * @param string $message
197
	 */
198 1
	protected function trigger_error($message)
199
	{
200 1
		$this->trigger_errors ? trigger_error($message) : null;
201 1
	}
202
203
	/**
204
	 * @return array
205
	 */
206 2
	protected function get_layouts()
207
	{
208 2
		$files = $this->finder
209 2
			->suffix('_layout.twig')
210 2
			->extension_directory('/styles')
211 2
			->find();
212 2
		$files = array_keys($files);
213
214 2
		$layouts = array();
215 2
		foreach ($files as $path)
216
		{
217 2
			$path = dirname($path);
218 2
			$name = basename($path);
219
220 2
			$layouts[$name] = $this->phpbb_root_path . $path . '/';
221 2
		}
222 2
		ksort($layouts);
223
224 2
		return $layouts;
225
	}
226
227
	/**
228
	 * @return void
229
	 */
230 1
	protected function save_config_settings()
231
	{
232 1
		$settings = $this->request->variable('config', array('' => ''));
233 1
		$layout_prefs = $this->request->variable('layouts', array(0 => array('' => '')));
234
235 1
		$this->config_text->set('sm_layout_prefs', json_encode($layout_prefs));
236
237 1
		foreach ($settings as $key => $value)
238
		{
239 1
			$this->config->set($key, $value);
240 1
		}
241 1
	}
242
243
	/**
244
	 * @return void
245
	 */
246 1
	protected function save_filemanager_settings()
247
	{
248 1
		$settings = $this->request->variable('filemanager', array('' => ''));
249
250 1
		$settings['aviary_active'] = ($settings['aviary_apiKey']) ? 'true' : 'false';
251 1
		$settings['image_watermark_position'] = ($settings['image_watermark_coordinates']) ? $settings['image_watermark_coordinates'] : $settings['image_watermark_position'];
252 1
		unset($settings['image_watermark_coordinates']);
253
254 1
		$curr_settings = (array) $this->get_filemanager_settings();
255 1
		$file = file_get_contents($this->filemanager_config_file);
0 ignored issues
show
Bug introduced by
The property filemanager_config_file does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
256
257 1
		foreach ($settings as $prop => $value)
258
		{
259 1
			$this->type_cast_filemanager_config_value($curr_settings[$prop], $value);
260 1
			$file = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", "	'$prop'$1=> $value,", $file);
261 1
		}
262
263 1
		file_put_contents($this->filemanager_config_file, $file);
0 ignored issues
show
Bug introduced by
The property filemanager_config_file does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
264 1
	}
265
266
	/**
267
	 * @return array
268
	 */
269 2
	protected function get_filemanager_settings()
270
	{
271 2
		$editing = true;
272 2
		return include($this->filemanager_config_file);
273
	}
274
275
	/**
276
	 * @param mixed $curr_val
277
	 * @param mixed $value
278
	 * @return void
279
	 */
280 1
	protected function type_cast_filemanager_config_value($curr_val, &$value)
281
	{
282 1
		$type = gettype($curr_val);
283
		switch($type)
284
		{
285 1
			case 'string':
286 1
				$value = "'$value'";
287 1
			break;
288 1
			case 'integer':
289 1
				$value = (int) $value;
290 1
			break;
291
		}
292 1
	}
293
294
	/**
295
	 * @return string
296
	 */
297 2
	protected function get_menu_options()
298
	{
299 2
		$menu_mapper = $this->mapper_factory->create('menus');
300
301
		// Get all menus
302 2
		$collection = $menu_mapper->find();
303
304 2
		$options = '';
305 2
		foreach ($collection as $entity)
306
		{
307 2
			$id = $entity->get_menu_id();
308 2
			$name = $entity->get_menu_name();
309 2
			$selected = ($id == $this->config['sm_navbar_menu']) ? ' selected="selected"' : '';
310 2
			$options .= '<option value="' . $id . '"' . $selected . '>' . $name . '</option>';
311 2
		}
312
313 2
		return $options;
314
	}
315
}
316