Completed
Push — develop ( f6ad79...49e718 )
by Daniel
09:04
created

settings_module::save_filemanager_settings()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 1
crap 4.0119
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\event\dispatcher_interface */
30
	protected $phpbb_dispatcher;
31
32
	/** @var \phpbb\request\request_interface */
33
	protected $request;
34
35
	/** @var \phpbb\template\template */
36
	protected $template;
37
38
	/** @var \phpbb\language\language */
39
	protected $translator;
40
41
	/** @var \blitze\sitemaker\services\icon_picker */
42
	protected $icon;
43
44
	/** @var \blitze\sitemaker\services\filemanager\settings */
45
	protected $filemanager;
46
47
	/** @var \blitze\sitemaker\model\mapper_factory */
48
	protected $mapper_factory;
49
50
	/** @var string phpBB root path */
51
	protected $phpbb_root_path;
52
53
	/** @var string phpEx */
54
	protected $php_ext;
55
56
	/** @var string */
57
	public $tpl_name;
58
59
	/** @var string */
60
	public $page_title;
61
62
	/** @var string */
63
	public $u_action;
64
65
	/** @var bool */
66
	public $trigger_errors;
67
68
	/**
69
	 * settings_module constructor.
70
	 */
71 2
	public function __construct($trigger_errors = true)
72
	{
73 2
		global $phpbb_container, $phpbb_dispatcher, $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...
74
75 2
		$this->db = $db;
76 2
		$this->config = $config;
77 2
		$this->phpbb_dispatcher = $phpbb_dispatcher;
78 2
		$this->request = $request;
79 2
		$this->template = $template;
80 2
		$this->phpbb_root_path = $phpbb_root_path;
81 2
		$this->php_ext = $phpEx;
82
83 2
		$this->config_text = $phpbb_container->get('config_text');
84 2
		$this->finder = $phpbb_container->get('ext.manager')->get_finder();
85 2
		$this->translator = $phpbb_container->get('language');
86 2
		$this->filemanager = $phpbb_container->get('blitze.sitemaker.filemanager.settings');
87 2
		$this->icon = $phpbb_container->get('blitze.sitemaker.icon_picker');
88 2
		$this->mapper_factory = $phpbb_container->get('blitze.sitemaker.mapper.factory');
89 2
		$this->trigger_errors = $trigger_errors;
90 2
	}
91
92
	/**
93
	 * @return void
94
	 */
95 1
	public function main()
96
	{
97 1
		$this->translator->add_lang('acp/board');
98 1
		$this->translator->add_lang('blocks_admin', 'blitze/sitemaker');
99
100 1
		$form_key = 'blitze/sitemaker/settings';
101
102 1
		$this->handle_submit($form_key);
103
104 1
		add_form_key($form_key);
105
106 1
		$layouts = $this->get_layouts();
107
108
		/**
109
		 * Event to display acp settings form
110
		 *
111
		 * @event blitze.sitemaker.acp_display_settings_form
112
		 * @var	array	layouts		Array of layout settings
113
		 * @since 3.1.0
114
		 */
115 1
		$vars = array('layouts');
116 1
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.acp_display_settings_form', compact($vars)));
0 ignored issues
show
Bug introduced by
$this->phpbb_dispatcher-..._form', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
117
118 1
		$this->template->assign_vars(array(
119 1
			'u_action'			=> $this->u_action,
120 1
			'icon_picker'		=> $this->icon->picker(),
121 1
			'config'			=> $this->config,
122 1
			'filemanager'		=> $this->filemanager->get_settings(),
123 1
			'styles'			=> $this->get_styles_data($layouts),
124 1
			'layouts'			=> $layouts,
125 1
			'menu_options'		=> $this->get_menu_options(),
126 1
		));
127
128 1
		$this->tpl_name = 'acp_settings';
129 1
		$this->page_title = 'ACP_SM_SETTINGS';
130 1
	}
131
132
	/**
133
	 * @param string $form_key
134
	 * @return void
135
	 */
136 2
	protected function handle_submit($form_key)
137
	{
138 2
		if ($this->request->is_set_post('submit'))
139 2
		{
140 1
			$settings = $this->request->variable('config', array('' => ''));
141
142 1
			$this->check_form_key($form_key);
143
144
			/**
145
			 * Event to save acp settings
146
			 *
147
			 * @event blitze.sitemaker.acp_save_settings
148
			 * @var	array	settings	Array of settings: [config_key] => [config_value]
149
			 * @since 3.1.0
150
			 */
151 1
			$vars = array('settings');
152 1
			extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.acp_save_settings', compact($vars)));
0 ignored issues
show
Bug introduced by
$this->phpbb_dispatcher-...tings', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
153
154 1
			$this->save_filemanager_settings($settings);
155 1
			$this->save_config_settings($settings);
156 1
			$this->trigger_error($this->translator->lang('SETTINGS_SAVED') . adm_back_link($this->u_action));
157 1
		}
158 2
	}
159
160
	/**
161
	 * @param string $form_key
162
	 */
163 1
	protected function check_form_key($form_key)
164
	{
165 1
		if (!check_form_key($form_key))
166 1
		{
167 1
			$this->trigger_error('FORM_INVALID', E_USER_WARNING);
168 1
		}
169 1
	}
170
171
	/**
172
	 * @param array $layouts
173
	 * @return array
174
	 */
175 1
	protected function get_styles_data(array $layouts)
176
	{
177 1
		$style_prefs = (array) json_decode($this->config_text->get('sm_layout_prefs'), true);
178
179 1
		$result = $this->db->sql_query('SELECT style_id, style_name FROM ' . STYLES_TABLE);
180
181 1
		$styles = array();
182 1
		while ($row = $this->db->sql_fetchrow($result))
183
		{
184 1
			$id = $row['style_id'];
185
186 1
			$pref = $this->get_style_pref($id, $style_prefs, $layouts['portal']);
187
188 1
			$styles[] = array(
189 1
				'id'		=> $id,
190 1
				'name'		=> $row['style_name'],
191 1
				'layout'	=> $pref['layout'],
192 1
				'view'		=> $pref['view'],
193
			);
194 1
		}
195 1
		$this->db->sql_freeresult();
196
197 1
		return $styles;
198
	}
199
200
	/**
201
	 * @param int $style_id
202
	 * @param array $style_prefs
203
	 * @param string $default_layout
204
	 * @return array
205
	 */
206 1
	protected function get_style_pref($style_id, array $style_prefs, $default_layout)
207
	{
208
		$pref = array(
209 1
			'layout'	=> $default_layout,
210 1
			'view'		=> '',
211 1
		);
212
213 1
		if (isset($style_prefs[$style_id]))
214 1
		{
215 1
			$pref = $style_prefs[$style_id];
216 1
		}
217
218 1
		return $pref;
219
	}
220
221
	/**
222
	 * @param string $message
223
	 * @param int $error_type
224
	 * @return void
225
	 */
226 1
	protected function trigger_error($message, $error_type = E_USER_NOTICE)
227
	{
228 1
		$this->trigger_errors ? trigger_error($message, $error_type) : null;
229 1
	}
230
231
	/**
232
	 * @return array
233
	 */
234 1
	protected function get_layouts()
235
	{
236 1
		$files = $this->finder
237 1
			->suffix('_layout.twig')
238 1
			->extension_directory('/styles')
239 1
			->find();
240 1
		$files = array_keys($files);
241
242 1
		$layouts = array();
243 1
		foreach ($files as $path)
244
		{
245 1
			$path = dirname($path);
246 1
			$name = basename($path);
247
248 1
			$layouts[$name] = $this->phpbb_root_path . $path . '/';
249 1
		}
250 1
		ksort($layouts);
251
252 1
		return $layouts;
253
	}
254
255
	/**
256
	 * @param array $settings
257
	 * @return void
258
	 */
259 1
	protected function save_config_settings(array $settings)
260
	{
261 1
		$layout_prefs = $this->request->variable('layouts', array(0 => array('' => '')));
262
263 1
		$this->config_text->set('sm_layout_prefs', json_encode($layout_prefs));
264
265 1
		foreach ($settings as $key => $value)
266
		{
267 1
			$this->config->set($key, $value);
268 1
		}
269 1
	}
270
271
	/**
272
	 * @param array $config
273
	 * @return void
274
	 */
275 1
	protected function save_filemanager_settings(array &$config)
276
	{
277 1
		$settings = $this->request->variable('filemanager', array('' => ''));
278
279 1
		if (sizeof($settings))
280 1
		{
281 1
			$settings['aviary_active'] = ($settings['aviary_apiKey']) ? 'true' : 'false';
282 1
			$settings['image_watermark_position'] = ($settings['image_watermark_coordinates']) ? $settings['image_watermark_coordinates'] : $settings['image_watermark_position'];
283 1
			unset($settings['image_watermark_coordinates']);
284
285 1
			$this->filemanager->save($settings);
286 1
		}
287
		else
288
		{
289
			$config['sm_filemanager'] = 0;
290
		}
291 1
	}
292
293
	/**
294
	 * @return string
295
	 */
296 1
	protected function get_menu_options()
297
	{
298 1
		$menu_mapper = $this->mapper_factory->create('menus');
299
300
		// Get all menus
301 1
		$collection = $menu_mapper->find();
302
303 1
		$options = '';
304 1
		foreach ($collection as $entity)
305
		{
306 1
			$id = $entity->get_menu_id();
307 1
			$name = $entity->get_menu_name();
308 1
			$selected = ($id == $this->config['sm_navbar_menu']) ? ' selected="selected"' : '';
309 1
			$options .= '<option value="' . $id . '"' . $selected . '>' . $name . '</option>';
310 1
		}
311
312 1
		return $options;
313
	}
314
}
315