custom::manage_custom()   F
last analyzed

Complexity

Conditions 27
Paths 3328

Size

Total Lines 174
Code Lines 100

Duplication

Lines 20
Ratio 11.49 %

Code Coverage

Tests 0
CRAP Score 756

Importance

Changes 0
Metric Value
cc 27
eloc 100
nc 3328
nop 3
dl 20
loc 174
ccs 0
cts 136
cp 0
crap 756
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
*
4
* @package Board3 Portal v2.1
5
* @copyright (c) 2013 Board3 Group ( www.board3.de )
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace board3\portal\modules;
11
12
/**
13
* @package Custom
14
*/
15
class custom extends module_base
16
{
17
	/**
18
	* Allowed columns: Just sum up your options (Exp: left + right = 10)
19
	* top		1
20
	* left		2
21
	* center	4
22
	* right		8
23
	* bottom	16
24
	*/
25
	public $columns = 31;
26
27
	/**
28
	* Default modulename
29
	*/
30
	public $name = 'PORTAL_CUSTOM';
31
32
	/**
33
	* Default module-image:
34
	* file must be in "{T_THEME_PATH}/images/portal/"
35
	*/
36
	public $image_src = 'portal_custom.png';
37
38
	/**
39
	* module-language file
40
	* file must be in "language/{$user->lang}/mods/portal/"
41
	*/
42
	public $language = 'portal_custom_module';
43
44
	/**
45
	* custom acp template
46
	* file must be in "adm/style/portal/"
47
	*/
48
	public $custom_acp_tpl = 'acp_portal_custom';
49
50
	/** @var bool Can include this module multiple times */
51
	protected $multiple_includes = true;
52
53
	/** @var \phpbb\config\config */
54
	protected $config;
55
56
	/** @var \phpbb\template */
57
	protected $template;
58
59
	/** @var \phpbb\db\driver */
60
	protected $db;
61
62
	/** @var \phpbb\request\request */
63
	protected $request;
64
65
	/** @var string PHP file extension */
66
	protected $php_ext;
67
68
	/** @var string phpBB root path */
69
	protected $phpbb_root_path;
70
71
	/** @var \phpbb\user */
72
	protected $user;
73
74
	/** @var \phpbb\log\log phpBB log*/
75
	protected $log;
76
77
	/**
78
	* Construct a custom module object
79
	*
80
	* @param \phpbb\config\config $config phpBB config
81
	* @param \phpbb\template $template phpBB template
82
	* @param \phpbb\db\driver $db Database driver
83
	* @param \phpbb\request\request $request phpBB request
84
	* @param string $phpEx php file extension
85
	* @param string $phpbb_root_path phpBB root path
86
	* @param \phpbb\user $user phpBB user object
87
	* @param \phpbb\log\log $log phpBB log
88
	*/
89 View Code Duplication
	public function __construct($config, $template, $db, $request, $phpbb_root_path, $phpEx, $user, $log)
90
	{
91
		$this->config = $config;
92
		$this->template = $template;
93
		$this->db = $db;
94
		$this->request = $request;
95
		$this->php_ext = $phpEx;
96
		$this->phpbb_root_path = $phpbb_root_path;
97
		$this->user = $user;
98
		$this->log = $log;
99
	}
100
101
	/**
102
	* {@inheritdoc}
103
	*/
104
	public function get_template_center($module_id)
105
	{
106
		return $this->parse_template($module_id);
107
	}
108
109
	/**
110
	* {@inheritdoc}
111
	*/
112
	public function get_template_side($module_id)
113
	{
114
		return $this->parse_template($module_id, 'side');
115
	}
116
117
	/**
118
	* {@inheritdoc}
119
	*/
120 View Code Duplication
	public function get_template_acp($module_id)
121
	{
122
		return array(
123
			'title'	=> 'PORTAL_CUSTOM',
124
			'vars'	=> array(
125
				'legend1'								=> 'PORTAL_CUSTOM',
126
				'board3_custom_' . $module_id . '_code'	=> array('lang' => 'PORTAL_CUSTOM',		'validate' => 'string',	'type' => 'custom', 'method' => 'manage_custom', 'submit' => 'update_custom', 'explain' => true),
127
			),
128
		);
129
	}
130
131
	/**
132
	* {@inheritdoc}
133
	*/
134
	public function install($module_id)
135
	{
136
		set_portal_config('board3_custom_' . $module_id . '_code', '');
137
		$this->config->set('board3_custom_' . $module_id . '_code', '');
138
		$this->config->set('board3_custom_' . $module_id . '_bbcode', 1);
139
		$this->config->set('board3_custom_' . $module_id . '_title', '');
140
		$this->config->set('board3_custom_' . $module_id . '_image_src', '');
141
		$this->config->set('board3_custom_' . $module_id . '_uid', '');
142
		$this->config->set('board3_custom_' . $module_id . '_bitfield', '');
143
		$this->config->set('board3_custom_' . $module_id . '_permission', '');
144
		return true;
145
	}
146
147
	/**
148
	* {@inheritdoc}
149
	*/
150
	public function uninstall($module_id, $db)
151
	{
152
		$del_config = array(
153
			'board3_custom_' . $module_id . '_code',
154
		);
155
		$sql = 'DELETE FROM ' . PORTAL_CONFIG_TABLE . '
156
			WHERE ' . $db->sql_in_set('config_name', $del_config);
157
158
		$check = $db->sql_query($sql);
159
160
		$del_config = array(
161
			'board3_custom_' . $module_id . '_bbcode',
162
			'board3_custom_' . $module_id . '_title',
163
			'board3_custom_' . $module_id . '_image_src',
164
			'board3_custom_' . $module_id . '_uid',
165
			'board3_custom_' . $module_id . '_bitfield',
166
			'board3_custom_' . $module_id . '_permission',
167
		);
168
		$sql = 'DELETE FROM ' . CONFIG_TABLE . '
169
			WHERE ' . $db->sql_in_set('config_name', $del_config);
170
		return ((!$check) ? $check : $db->sql_query($sql)); // if something went wrong, make sure we are aware of the first query
171
	}
172
173
	/**
174
	* Manage custom module
175
	*
176
	* @param mixed $value Value of input
177
	* @param string $key Key name
178
	* @param int $module_id Module ID
179
	*
180
	* @return null
181
	*/
182
	public function manage_custom($value, $key, $module_id)
183
	{
184
		$action = ($this->request->is_set_post('reset')) ? 'reset' : '';
185
		$action = ($this->request->is_set_post('submit')) ? 'save' : $action;
186
		$action = ($this->request->is_set_post('preview')) ? 'preview' : $action;
187
188
		$portal_config = obtain_portal_config();
189
190
		$u_action = append_sid('index.' . $this->php_ext, 'i=-board3-portal-acp-portal_module&amp;mode=config&amp;module_id=' . $module_id);
191
192
		switch ($action)
193
		{
194
			// Save changes
195
			case 'save':
196 View Code Duplication
				if (!check_form_key('acp_portal'))
197
				{
198
					trigger_error($this->user->lang['FORM_INVALID']. adm_back_link($u_action), E_USER_WARNING);
199
				}
200
201
				$custom_code = $this->request->variable('custom_code', '', true);
202
				$custom_bbcode = $this->request->variable('custom_use_bbcode', 1); // default to BBCode
203
				$custom_permission = $this->request->variable('permission-setting', array(0 => ''));
204
				$custom_title = $this->request->variable('module_name', '', true);
205
				$custom_image_src = $this->request->variable('module_image', '', true);
206
				$groups_ary = array();
207
				$uid = $bitfield = $flags = '';
208
209
				if ($custom_bbcode)
210
				{
211
					generate_text_for_storage($custom_code, $uid, $bitfield, $flags, true, true, true);
212
				}
213
214
				// first check for obvious errors, we don't want to waste server resources
215 View Code Duplication
				if (empty($custom_code))
216
				{
217
					trigger_error($this->user->lang['ACP_PORTAL_CUSTOM_CODE_SHORT']. adm_back_link($u_action), E_USER_WARNING);
218
				}
219
220
				// get groups and check if the selected groups actually exist
221
				$sql = 'SELECT group_id
222
						FROM ' . GROUPS_TABLE . '
223
						ORDER BY group_id ASC';
224
				$result = $this->db->sql_query($sql);
225
				while ($row = $this->db->sql_fetchrow($result))
226
				{
227
					$groups_ary[] = $row['group_id'];
228
				}
229
				$this->db->sql_freeresult($result);
230
231
				$custom_permission = array_intersect($custom_permission, $groups_ary);
232
				$custom_permission = implode(',', $custom_permission);
233
234
				if (isset($this->user->lang[$custom_title]))
235
				{
236
					$log_title =  $this->user->lang[$custom_title];
237
				}
238
				else
239
				{
240
					$log_title = $custom_title;
241
				}
242
243
				$this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_CONFIG', false, array($this->user->lang['PORTAL_CUSTOM'] . ':&nbsp;' . $log_title));
244
245
				// set_portal_config will take care of escaping the welcome message
246
				set_portal_config('board3_custom_' . $module_id . '_code', $custom_code);
247
				$this->config->set('board3_custom_' . $module_id . '_bbcode', $custom_bbcode);
248
				$this->config->set('board3_custom_' . $module_id . '_title', $custom_title);
249
				$this->config->set('board3_custom_' . $module_id . '_image_src', $custom_image_src);
250
				$this->config->set('board3_custom_' . $module_id . '_uid', $uid);
251
				$this->config->set('board3_custom_' . $module_id . '_bitfield', $bitfield);
252
				$this->config->set('board3_custom_' . $module_id . '_permission', $custom_permission);
253
254
				//trigger_error($this->user->lang['CONFIG_UPDATED'] . adm_back_link(($module_id) ? append_sid("{$phpbb_admin_path}index.$phpEx", 'i=portal&mode=modules') : $u_action));
255
256
			break;
257
258
			case 'preview':
259
				$custom_code = $text = $this->request->variable('custom_code', '', true);
260
				$custom_bbcode = $this->request->variable('custom_use_bbcode', 1); // default to BBCode
261
				$custom_permission = $this->request->variable('permission-setting', array(0 => ''));
262
				$custom_title = $this->request->variable('module_name', '', true);
263
				$custom_image_src = $this->request->variable('module_image', '', true);
264
				$groups_ary = array();
265
266
				// first check for obvious errors, we don't want to waste server resources
267 View Code Duplication
				if (empty($custom_code))
268
				{
269
					trigger_error($this->user->lang['ACP_PORTAL_CUSTOM_CODE_SHORT']. adm_back_link($u_action), E_USER_WARNING);
270
				}
271
272
				if ($custom_bbcode)
273
				{
274
					$bbcode_options = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS;
275
					$uid  =  (isset($this->config['board3_custom_' . $module_id . '_uid'])) ? $this->config['board3_custom_' . $module_id . '_uid'] : '';
276
					$bitfield = (isset($this->config['board3_custom_' . $module_id . '_bitfield'])) ? $this->config['board3_custom_' . $module_id . '_bitfield'] : '';
277
					$options = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS;
278
					generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);
279
280
					$text = generate_text_for_display($text, $uid, $bitfield, $options);
281
				}
282
				else
283
				{
284
					$text = htmlspecialchars_decode($text, ENT_QUOTES);
285
				}
286
287
				$this->template->assign_vars(array(
288
					'PREVIEW_TEXT'		=> $text,
289
					'S_PREVIEW'			=> true,
290
				));
291
292
				// get groups and check if the selected groups actually exist
293
				$sql = 'SELECT group_id
294
						FROM ' . GROUPS_TABLE . '
295
						ORDER BY group_id ASC';
296
				$result = $this->db->sql_query($sql);
297
				while ($row = $this->db->sql_fetchrow($result))
298
				{
299
					$groups_ary[] = $row['group_id'];
300
				}
301
				$this->db->sql_freeresult($result);
302
303
				$temp_permissions = array_intersect($custom_permission, $groups_ary);
304
305
			// Edit or add menu item
306
			case 'reset':
307
			default:
308
				if (!isset($custom_code))
309
				{
310
					$custom_code = generate_text_for_edit($portal_config['board3_custom_' . $module_id . '_code'], $this->config['board3_custom_' . $module_id . '_uid'], '');
311
				}
312
313
				$this->template->assign_vars(array(
314
					'CUSTOM_CODE'			=> (is_array($custom_code)) ? $custom_code['text'] : $custom_code,
315
					'CUSTOM_USE_BBCODE'		=> (isset($custom_bbcode)) ? $custom_bbcode : (($this->config['board3_custom_' . $module_id . '_bbcode'] != '') ? $this->config['board3_custom_' . $module_id . '_bbcode'] : true), // BBCodes are selected by default
316
					//'U_BACK'				=> $u_action,
317
					'U_ACTION'				=> $u_action,
318
					'S_EDIT'				=> true,
319
					'S_LINKS_ALLOWED'       => true,
320
					'S_BBCODE_IMG'          => true,
321
					'S_BBCODE_FLASH'		=> true,
322
					'S_BBCODE_QUOTE'		=> true,
323
					'S_BBCODE_ALLOWED'		=> true,
324
					'MAX_FONT_SIZE'			=> (int) $this->config['max_post_font_size'],
325
				));
326
327
				$groups_ary = (isset($temp_permissions)) ? $temp_permissions : ((isset($this->config['board3_custom_' . $module_id . '_permission'])) ? explode(',', $this->config['board3_custom_' . $module_id . '_permission']) : array());
328
329
				// get group info from database and assign the block vars
330
				$sql = 'SELECT group_id, group_name 
331
						FROM ' . GROUPS_TABLE . '
332
						ORDER BY group_id ASC';
333
				$result = $this->db->sql_query($sql);
334 View Code Duplication
				while ($row = $this->db->sql_fetchrow($result))
335
				{
336
					$this->template->assign_block_vars('permission_setting', array(
337
						'SELECTED'		=> (in_array($row['group_id'], $groups_ary)) ? true : false,
338
						'GROUP_NAME'	=> (isset($this->user->lang['G_' . $row['group_name']])) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name'],
339
						'GROUP_ID'		=> $row['group_id'],
340
					));
341
				}
342
				$this->db->sql_freeresult($result);
343
344
				if (!function_exists('display_forums'))
345
				{
346
					include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
347
				}
348
349
				// Build custom bbcodes array
350
				display_custom_bbcodes();
351
				$this->user->add_lang('posting');
352
353
			break;
354
		}
355
	}
356
357
	/**
358
	* Update custom module
359
	*
360
	* @param string $key Key name
361
	* @param int $module_id Module ID
362
	*
363
	* @return null
364
	*/
365
	public function update_custom($key, $module_id)
366
	{
367
		$this->manage_custom('', $key, $module_id);
368
	}
369
370
	/**
371
	* Parse template for custom blocks
372
	*
373
	* @param int $module_id	Module ID of current module
374
	* @param string $type	Type of module (center or side), default to
375
	*			center to not show module image unless wanted
376
	* @return array		An array containing the custom module data
377
	*/
378
	protected function parse_template($module_id, $type = 'center')
379
	{
380
		$portal_config = obtain_portal_config();
381
382
		/*
383
		* Run generate_text_for_display if the user uses BBCode for designing his custom block
384
		* HTML won't be parsed if the user chooses to use BBCodes in the ACP
385
		* If BBCodes are turned off, the custom Block code will be directly assigned and HTML will be parsed
386
		*/
387
		if ($this->config['board3_custom_' . $module_id . '_bbcode'])
388
		{
389
			// Generate text for display and assign template vars
390
			$uid = $this->config['board3_custom_' . $module_id . '_uid'];
391
			$bitfield = $this->config['board3_custom_' . $module_id . '_bitfield'];
392
			$bbcode_options = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS;
393
			$assign_code = generate_text_for_display($portal_config['board3_custom_' . $module_id . '_code'], $uid, $bitfield, $bbcode_options);
394
		}
395
		else
396
		{
397
			$assign_code = htmlspecialchars_decode($portal_config['board3_custom_' . $module_id . '_code'], ENT_QUOTES);
398
		}
399
400
		$title = (!empty($this->config['board3_custom_' . $module_id . '_title'])) ? ((isset($this->user->lang[$this->config['board3_custom_' . $module_id . '_title']])) ? $this->user->lang[$this->config['board3_custom_' . $module_id . '_title']] : $this->config['board3_custom_' . $module_id . '_title']) : $this->user->lang[$this->name];
401
402
		if (!empty($assign_code))
403
		{
404
			return array(
405
				'template'	=> 'custom_' . $type . '.html',
406
				'title'		=> $title,
407
				'code'		=> $assign_code,
408
				// no image for center blocks
409
				'image_src'	=> ($type === 'center') ? '' : ((!empty($this->config['board3_custom_' . $module_id . '_image_src'])) ? $this->config['board3_custom_' . $module_id . '_image_src'] : $this->image_src),
410
			);
411
		}
412
	}
413
}
414