Passed
Push — release-3.2.0 ( 8165d1...607af8 )
by Daniel
03:27
created

settings_module::get_menu_options()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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