portal_module::__construct()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 27
nc 4
nop 0
dl 0
loc 38
ccs 32
cts 32
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
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\acp;
11
12
class portal_module
13
{
14
	public $u_action;
15
	public $new_config = array();
16
17
	/** @var \board3\portal\modules\module_interface */
18
	protected $c_class;
19
20
	protected $db, $user, $cache, $template, $display_vars, $config, $phpbb_root_path, $phpbb_admin_path, $phpEx, $phpbb_container;
21
	protected $root_path, $request, $php_ext, $portal_helper, $modules_helper, $log, $portal_columns;
22
23
	/** @var \board3\portal\portal\modules\manager */
24
	protected $modules_manager;
25
26
	/** @var \board3\portal\portal\modules\constraints_handler */
27
	protected $modules_constraints;
28
29
	/** @var \board3\portal\controller\helper */
30
	protected $board3_controller_helper;
31
32
	/** @var int Board3 module enabled */
33
	const B3_MODULE_ENABLED = 1;
34
35 34
	public function __construct()
36
	{
37 34
		global $db, $user, $cache, $request, $template, $table_prefix;
38 34
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpbb_container, $phpEx, $phpbb_log;
39
40 34
		$user->add_lang_ext('board3/portal', array('portal', 'portal_acp'));
41
42 34
		$this->root_path = $phpbb_root_path . 'ext/board3/portal/';
43
44 34
		$this->db = $db;
45 34
		$this->user = $user;
46 34
		$this->cache = $cache;
47 34
		$this->template = $template;
48 34
		$this->config = $config;
49 34
		$this->request = $request;
50 34
		$this->phpbb_root_path = $phpbb_root_path;
51 34
		$this->phpbb_admin_path = $phpbb_admin_path;
52 34
		$this->php_ext = $phpEx;
53 34
		$this->phpbb_container = $phpbb_container;
54 34
		$this->portal_helper = $this->phpbb_container->get('board3.portal.helper');
55 34
		$this->modules_helper = $this->phpbb_container->get('board3.portal.modules_helper');
56 34
		$this->log = $phpbb_log;
57 34
		$this->portal_columns = $this->phpbb_container->get('board3.portal.columns');
58 34
		$this->modules_manager = $this->phpbb_container->get('board3.portal.modules.manager');
59 34
		$this->modules_constraints = $this->phpbb_container->get('board3.portal.modules.constraints_handler');
60 34
		$this->board3_controller_helper = $this->phpbb_container->get('board3.portal.controller_helper');
61
62 34
		if (!defined('PORTAL_MODULES_TABLE'))
63 34
		{
64 1
			define('PORTAL_MODULES_TABLE', $this->phpbb_container->getParameter('board3.portal.modules.table'));
65 1
			define('PORTAL_CONFIG_TABLE', $this->phpbb_container->getParameter('board3.portal.config.table'));
66 1
		}
67
68 34
		if (!function_exists('obtain_portal_config'))
69 34
		{
70 1
			include($this->root_path . 'includes/functions.' . $this->php_ext);
71 1
		}
72 34
	}
73
74 3
	public function main($id, $mode)
75
	{
76 3
		$submit = ($this->request->is_set_post('submit')) ? true : false;
77
78 3
		$form_key = 'acp_portal';
79 3
		add_form_key($form_key);
80
81
		// Setup modules manager class
82 3
		$this->modules_manager->set_u_action($this->u_action)
83 3
			->set_acp_class(__CLASS__);
84
85
		/**
86
		*	Validation types are:
87
		*		string, int, bool,
88
		*		script_path (absolute path in url - beginning with / and no trailing slash),
89
		*		rpath (relative), rwpath (realtive, writeable), path (relative path, but able to escape the root), wpath (writeable)
90
		*/
91
		switch ($mode)
92
		{
93 3
			case 'config':
94
				$display_vars = array(
95 2
					'title'	=> 'ACP_PORTAL_GENERAL_TITLE',
96
					'vars'	=> array(
97 2
						'legend1'					=> 'ACP_PORTAL_CONFIG_INFO',
98 2
						'board3_enable'				=> array('lang' => 'PORTAL_ENABLE',				'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
99 2
						'board3_left_column'		=> array('lang' => 'PORTAL_LEFT_COLUMN',		'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
100 2
						'board3_right_column'		=> array('lang' => 'PORTAL_RIGHT_COLUMN',		'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
101 2
						'board3_display_jumpbox'	=> array('lang' => 'PORTAL_DISPLAY_JUMPBOX',	'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
102
103 2
						'legend2'					=> 'ACP_PORTAL_COLUMN_WIDTH_SETTINGS',
104 2
						'board3_left_column_width'	=> array('lang' => 'PORTAL_LEFT_COLUMN_WIDTH',	'validate' => 'int',	'type' => 'text:3:3',		'explain' => true),
105 2
						'board3_right_column_width'	=> array('lang' => 'PORTAL_RIGHT_COLUMN_WIDTH',	'validate' => 'int',	'type' => 'text:3:3',		'explain' => true),
106
107 2
						'legend3'					=> 'ACP_PORTAL_SHOW_ALL',
108 2
						'board3_show_all_pages'		=> array('lang' => 'ACP_PORTAL_SHOW_ALL',	'validate' => 'bool',	'type' => 'radio:yes_no',	'explain' => true),
109 2
						'board3_show_all_side'		=> array('lang' => 'PORTAL_SHOW_ALL_SIDE',	'validate' => 'bool',	'type' => 'custom',	'method' => array('board3.portal.modules_helper', 'display_left_right'),	'submit' => array('board3.portal.modules_helper', 'store_left_right'),	'explain' => true),
110
					)
111 2
				);
112
113 2
				$module_id = $this->request->variable('module_id', 0);
114
				if ($module_id)
115 2
				{
116
					$sql = 'SELECT *
117
						FROM ' . PORTAL_MODULES_TABLE . '
118
						WHERE module_id = ' . (int) $module_id;
119
					$result = $this->db->sql_query_limit($sql, 1);
120
					$module_data = $this->db->sql_fetchrow($result);
121
					$this->db->sql_freeresult($result);
122
123
					if ($module_data !== false)
124
					{
125
126
						if (!($this->c_class = $this->portal_helper->get_module($module_data['module_classname'])))
127
						{
128
							continue;
129
						}
130
131
						// Load module language
132
						$this->board3_controller_helper->load_module_language($this->c_class);
133
134
						$module_name = $this->user->lang[$this->c_class->get_name()];
135
						$display_vars = $this->c_class->get_template_acp($module_id);
136
						$this->template->assign_vars(array(
137
							'MODULE_NAME'			=> (isset($this->c_class->hide_name) && $this->c_class->hide_name == true)? '' : $module_data['module_name'],
138
							'MODULE_IMAGE'			=> $module_data['module_image_src'],
139
							'MODULE_IMAGE_WIDTH'	=> $module_data['module_image_width'],
140
							'MODULE_IMAGE_HEIGHT'	=> $module_data['module_image_height'],
141
							'MODULE_IMAGE_SRC'		=> ($module_data['module_image_src']) ? $this->root_path . 'styles/all/theme/images/portal/' . $module_data['module_image_src'] : '',
142
							'MODULE_ENABLED'		=> ($module_data['module_status']) ? true : false,
143
							'MODULE_SHOW_IMAGE'		=> (in_array($this->portal_columns->number_to_string($module_data['module_column']), array('center', 'top', 'bottom'))) ? false : true,
144
						));
145
146
						if ($module_data['module_classname'] != '\board3\portal\modules\custom')
147
						{
148
							$groups_ary = explode(',', $module_data['module_group_ids']);
149
150
							// get group info from database and assign the block vars
151
							$sql = 'SELECT group_id, group_name 
152
									FROM ' . GROUPS_TABLE . '
153
									ORDER BY group_id ASC';
154
							$result = $this->db->sql_query($sql);
155 View Code Duplication
							while ($row = $this->db->sql_fetchrow($result))
156
							{
157
								$this->template->assign_block_vars('permission_setting', array(
158
									'SELECTED'		=> (in_array($row['group_id'], $groups_ary)) ? true : false,
159
									'GROUP_NAME'	=> (isset($this->user->lang['G_' . $row['group_name']])) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name'],
160
									'GROUP_ID'		=> $row['group_id'],
161
								));
162
							}
163
							$this->db->sql_freeresult($result);
164
						}
165
166
						$this->template->assign_var('SHOW_MODULE_OPTIONS', true);
167
					}
168
				}
169
170 2
				$this->new_config = $this->config;
171 2
				$cfg_array = ($this->request->is_set('config')) ? $this->request->variable('config', array('' => ''), true) : $this->new_config;
172 2
				$error = array();
173
174
				// We validate the complete config if wished
175 2
				validate_config_vars($display_vars['vars'], $cfg_array, $error);
176 2
				if ($submit && !check_form_key($form_key))
177 2
				{
178 1
					$error[] = $this->user->lang['FORM_INVALID'];
179 1
				}
180
181
				// Do not write values if there is an error
182 2
				if (sizeof($error))
183 2
				{
184 1
					$submit = false;
185 1
				}
186
187
				// Reset module
188 2
				$reset_module = $this->request->variable('module_reset', 0);
189
190 2
				if ($reset_module && !empty($module_data))
191 2
				{
192
					$this->modules_manager->reset_module($id, $mode, $module_id, $module_data);
193
				}
194
195
				// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
196 2
				foreach ($display_vars['vars'] as $config_name => $null)
197
				{
198 2
					if ($submit && ((isset($null['type']) && $null['type'] == 'custom') || (isset($null['submit_type']) && $null['submit_type'] == 'custom')))
199 2
					{
200
						if (!is_array($null['submit']))
201
						{
202
							if (method_exists($this->c_class, $null['submit']))
203
							{
204
								$func = array($this->c_class, $null['submit']);
205
								$args = ($module_id != 0) ? array($config_name, $module_id) : $config_name;
206
							}
207
							else if (function_exists($null['submit']))
208
							{
209
								$func = $null['submit'];
210
								$args = ($module_id != 0) ? array($cfg_array[$config_name], $config_name, $module_id) : $config_name;
211
							}
212
							else
213
							{
214
								throw new \RuntimeException($this->user->lang('UNKNOWN_MODULE_METHOD', $module_data['module_classname']));
215
							}
216
						}
217
						else
218
						{
219
							if ($null['submit'][0] == 'board3.portal.modules_helper')
220
							{
221
								$func = array($this->modules_helper, $null['submit'][1]);
222
								$args = ($module_id != 0) ? array($config_name, $module_id) : array($config_name);
223
							}
224
							else
225
							{
226
								$args = ($module_id != 0) ? array($cfg_array[$config_name], $config_name, $module_id) : array($config_name);
227
								$func = $null['submit'];
228
							}
229
						}
230
231
						call_user_func_array($func, $args);
232
					}
233
234 2
					if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
235 2
					{
236 2
						continue;
237
					}
238
239
					if (isset($null['type']) && $null['type'] == 'custom')
240
					{
241
						continue;
242
					}
243
244
					$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];
245
246
					if ($submit)
247
					{
248
						$this->config->set($config_name, $config_value);
249
					}
250 2
				}
251
252
				if ($submit)
253 2
				{
254
					$module_permission = $this->request->variable('permission-setting', array(0 => ''));
255
					$groups_ary = array();
256
257
					// get groups and check if the selected groups actually exist
258
					$sql = 'SELECT group_id
259
							FROM ' . GROUPS_TABLE . '
260
							ORDER BY group_id ASC';
261
					$result = $this->db->sql_query($sql);
262
					while ($row = $this->db->sql_fetchrow($result))
263
					{
264
						$groups_ary[] = $row['group_id'];
265
					}
266
					$this->db->sql_freeresult($result);
267
268
					$module_permission = array_intersect($module_permission, $groups_ary);
269
					$module_permission = implode(',', $module_permission);
270
271
					$sql_ary = array(
272
						'module_image_src'		=> $this->request->variable('module_image', ''),
273
						'module_image_width'	=> $this->request->variable('module_img_width', 0),
274
						'module_image_height'	=> $this->request->variable('module_img_height', 0),
275
						'module_group_ids'		=> $module_permission,
276
						'module_status'			=> $this->request->variable('module_status', self::B3_MODULE_ENABLED),
277
					);
278
279
					if (!(isset($this->c_class->hide_name) && $this->c_class->hide_name == true))
280
					{
281
						$sql_ary['module_name'] = $this->request->variable('module_name', '', true);
282
					}
283
284
					// check if module image file actually exists
285
					$img_error = check_file_src($sql_ary['module_image_src'], '', $module_id, false);
286
287
					$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
288
						SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
289
						WHERE module_id = ' . (int) $module_id;
290
					$this->db->sql_query($sql);
291
292
					$this->cache->destroy('sql', PORTAL_MODULES_TABLE);
293
					$this->cache->destroy('sql', CONFIG_TABLE);
294
295
					if (isset($module_name))
296
					{
297
						if (isset($module_data) && $module_data['module_classname'] !== '\board3\portal\modules\custom')
298
						{
299
							$this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_CONFIG', false, array($module_name));
300
						}
301
					}
302
					else
303
					{
304
						$this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_PORTAL_CONFIG', false, array($this->user->lang['ACP_PORTAL_' . strtoupper($mode) . '_INFO']));
305
					}
306
					trigger_error($this->user->lang['CONFIG_UPDATED'] . ((!empty($img_error) ? '<br /><br />' . $this->user->lang['MODULE_IMAGE_ERROR'] . '<br />' . $img_error : '')) . adm_back_link(($module_id) ? append_sid("{$this->phpbb_admin_path}index.{$this->php_ext}", 'i=\board3\portal\acp\portal_module&amp;mode=modules') : $this->u_action));
307
				}
308
309
				// show custom HTML files on the settings page of the modules instead of the standard board3 portal one, if chosen by module
310 2
				if (!isset($this->c_class->custom_acp_tpl) || empty($this->c_class->custom_acp_tpl))
311 2
				{
312 2
					$this->tpl_name = 'portal/acp_portal_config';
313 2
				}
314
				else
315
				{
316
					$this->tpl_name = 'portal/' . $this->c_class->custom_acp_tpl;
317
				}
318 2
				$this->page_title = $display_vars['title'];
319
320 2
				$this->template->assign_vars(array(
321 2
					'L_TITLE'			=> $this->user->lang[$display_vars['title']],
322 2
					'L_TITLE_EXPLAIN'	=> (isset($this->user->lang[$display_vars['title'] . '_EXP'])) ? $this->user->lang[$display_vars['title'] . '_EXP'] : '',
323
324 2
					'S_ERROR'			=> (sizeof($error)) ? true : false,
325 2
					'ERROR_MSG'			=> implode('<br />', $error),
326
327 2
					'B3P_U_ACTION'			=> $this->modules_manager->get_module_link('config', $module_id),
328 2
					'B3P_ACP_ROOT'		=> $this->root_path,
329 2
				));
330
331
				// Output relevant page
332 2
				foreach ($display_vars['vars'] as $config_key => $vars)
333
				{
334 2
					if (!is_array($vars) && strpos($config_key, 'legend') === false)
335 2
					{
336
						continue;
337
					}
338
339 2
					if (strpos($config_key, 'legend') !== false)
340 2
					{
341 2
						$this->template->assign_block_vars('options', array(
342 2
							'S_LEGEND'		=> true,
343 2
							'LEGEND'		=> (isset($this->user->lang[$vars])) ? $this->user->lang[$vars] : $vars)
344 2
						);
345
346 2
						continue;
347
					}
348 2
					$this->new_config[$config_key] = $this->config[$config_key];
349 2
					$type = explode(':', $vars['type']);
350
351 2
					$l_explain = '';
352 2
					if ($vars['explain'])
353 2
					{
354 2
						$l_explain = (isset($this->user->lang[$vars['lang'] . '_EXP'])) ? $this->user->lang[$vars['lang'] . '_EXP'] : '';
355 2
					}
356
357 2
					if ($vars['type'] != 'custom')
358 2
					{
359 2
						$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);
360 2
					}
361
					else
362
					{
363 2
						$args = array($this->new_config[$config_key], $config_key, $module_id);
364 2
						if (!is_array($vars['method']))
365 2
						{
366
							$func = array($this->c_class, $vars['method']);
367
						}
368
						else
369
						{
370 2
							if ($vars['method'][0] == 'board3.portal.modules_helper')
371 2
							{
372 2
								$func = array($this->modules_helper, $vars['method'][1]);
373 2
							}
374
							else
375
							{
376
								$func = $vars['method'];
377
							}
378
						}
379 2
						$content = call_user_func_array($func, $args);
380
					}
381
382 2
					if (empty($content))
383 2
					{
384
						continue;
385
					}
386
387 2
					$this->template->assign_block_vars('options', array(
388 2
						'KEY'			=> $config_key,
389 2
						'TITLE'			=> (isset($this->user->lang[$vars['lang']])) ? $this->user->lang[$vars['lang']] : $vars['lang'],
390 2
						'S_EXPLAIN'		=> $vars['explain'],
391 2
						'TITLE_EXPLAIN'	=> $l_explain,
392 2
						'CONTENT'		=> $content,
393 2
					));
394
395 2
					unset($display_vars['vars'][$config_key]);
396 2
				}
397 2
			break;
398 1
			case 'modules':
399
				$action = $this->request->variable('action', '');
400
				$module_id = $this->request->variable('module_id', '');
401
402
				// Create an array of already installed modules
403
				$portal_modules = obtain_portal_modules();
404
				$installed_modules = $module_column = array();
405
406
				foreach ($portal_modules as $cur_module)
407
				{
408
					$installed_modules[] = $cur_module['module_classname'];
409
					// Create an array with the columns the module is in
410
					$module_column[$cur_module['module_classname']][] = $this->portal_columns->number_to_string($cur_module['module_column']);
411
				}
412
				$this->modules_constraints->set_module_column($module_column);
413
				unset($module_column);
414
415
				if ($action == 'move_up')
416
				{
417
					$this->modules_manager->move_module_vertical($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_UP);
418
				}
419
				else if ($action == 'move_down')
420
				{
421
					$this->modules_manager->move_module_vertical($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_DOWN);
422
				}
423
				else if ($action == 'move_right')
424
				{
425
					$this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT);
426
				}
427
				else if ($action == 'move_left')
428
				{
429
					$this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT);
430
				}
431
				else if ($action == 'delete')
432
				{
433
					$this->modules_manager->module_delete($id, $mode, $action, $module_id);
434
				}
435
436
				$add_list = $this->request->variable('add', array('' => ''));
437
				$add_module = key($add_list);
438
				$add_column = $this->request->variable('add_column', $this->portal_columns->string_to_number($add_module));
439
				if ($add_column)
440
				{
441
					$submit = ($this->request->is_set_post('submit')) ? true : false;
442
					if ($submit)
443
					{
444
						$module_classname = $this->request->variable('module_classname', '');
445
446
						if (!($this->c_class = $this->portal_helper->get_module($module_classname)))
447
						{
448
							continue;
449
						}
450
451
						// Do not add modules that shouldn't be added
452
						if (!$this->modules_constraints->can_add_module($this->c_class, $add_column))
453
						{
454
							trigger_error($this->user->lang('UNABLE_TO_ADD_MODULE') . adm_back_link($this->u_action), E_USER_WARNING);
455
						}
456
457
						// Do not install if module already exists in the
458
						// column and it can't be added more than once
459
						if (!$this->c_class->can_multi_include() && !$this->modules_constraints->can_move_module($this->portal_columns->number_to_string($add_column), $module_classname))
460
						{
461
							trigger_error($this->user->lang['MODULE_ADD_ONCE'] . adm_back_link($this->u_action), E_USER_WARNING);
462
						}
463
464
						$sql = 'SELECT module_order
465
							FROM ' . PORTAL_MODULES_TABLE . '
466
							WHERE module_column = ' . (int) $add_column . '
467
							ORDER BY module_order DESC';
468
						$result = $this->db->sql_query_limit($sql, 1);
469
						$module_order = 1 + (int) $this->db->sql_fetchfield('module_order');
470
						$this->db->sql_freeresult($result);
471
472
						$sql_ary = array(
473
							'module_classname'	=> $module_classname,
474
							'module_column'		=> $add_column,
475
							'module_order'		=> $module_order,
476
							'module_name'		=> $this->c_class->get_name(),
477
							'module_image_src'	=> $this->c_class->get_image(),
478
							'module_group_ids'	=> '',
479
							'module_image_height'	=> 16,
480
							'module_image_width'	=> 16,
481
							'module_status'		=> self::B3_MODULE_ENABLED,
482
						);
483
						$sql = 'INSERT INTO ' . PORTAL_MODULES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
484
						$this->db->sql_query($sql);
485
486
						$module_id = $this->db->sql_nextid();
487
488
						$error = $this->c_class->install($module_id);
489
490
						$this->cache->purge(); // make sure we don't get errors after re-adding a module
491
492
						// if something went wrong, handle the errors accordingly and undo the above query
493
						if (!empty($error) && $error != true)
494
						{
495
							if (is_array($error))
496
							{
497
								$error_output = '';
498
								foreach ($error as $cur_error)
499
								{
500
									$error_output .= $cur_error . '<br />';
501
								}
502
							}
503
							else
504
							{
505
								$error_output = $error;
506
							}
507
508
							$sql = 'DELETE FROM ' . PORTAL_MODULES_TABLE . ' WHERE module_id = ' . (int) $module_id;
509
							$this->db->sql_query($sql);
510
511
							trigger_error($error_output . adm_back_link($this->u_action));
512
						}
513
514
						meta_refresh(3, $this->modules_manager->get_module_link('config', $module_id));
515
516
						trigger_error($this->user->lang['SUCCESS_ADD'] . adm_back_link($this->u_action));
517
					}
518
519
					$this->template->assign_var('S_EDIT', true);
520
					$fileinfo = $name_ary = array();
521
					$modules_list = $this->portal_helper->get_all_modules();
522
523
					// Find new modules
524
					foreach ($modules_list as $module_class => $module)
525
					{
526
						// Module can't be added to this column
527
						if (!$this->modules_constraints->can_add_module($module, $add_column))
528
						{
529
							continue;
530
						}
531
532
						// Do not install if module already exists in the
533
						// column and it can't be added more than once
534
						if (!$module->can_multi_include() && !$this->modules_constraints->can_move_module($this->portal_columns->number_to_string($add_column), $module_class))
535
						{
536
							continue;
537
						}
538
539
						if ($module->get_allowed_columns() & $this->portal_columns->string_to_constant($add_module))
540
						{
541
							// Load module language
542
							$this->board3_controller_helper->load_module_language($module);
543
544
							$fileinfo[] = array(
545
								'module'		=> $module_class,
546
								'name'			=> $this->user->lang[$module->get_name()],
547
							);
548
						}
549
					}
550
551
					// we sort the $fileinfo array by the name of the modules
552
					foreach ($fileinfo as $key => $cur_file)
553
					{
554
						$name_ary[$key] = $cur_file['name'];
555
					}
556
					array_multisort($name_ary, SORT_REGULAR, $fileinfo);
557
					$options = '';
558
559
					foreach ($fileinfo as $module)
560
					{
561
						$options .= '<option value="' . $module['module'] . '">' . $module['name'] . '</option>';
562
					}
563
564
					$s_hidden_fields = build_hidden_fields(array(
565
						'add_column'	=> $this->portal_columns->string_to_number($add_module),
566
					));
567
					$this->template->assign_vars(array(
568
						'S_MODULE_NAMES'	=> $options,
569
						'S_HIDDEN_FIELDS'	=> $s_hidden_fields,
570
					));
571
572
					if ($this->request->is_ajax())
573
					{
574
						$this->template->assign_vars(array(
575
							'S_AJAX_REQUEST'	=> true,
576
							'U_ACTION'		=> str_replace('&amp;', '&', $this->modules_manager->get_module_link('modules', $module_id)),
577
						));
578
						$this->template->set_filenames(array(
579
							'body' => 'portal/acp_portal_modules.html')
580
						);
581
						$this->modules_manager->handle_ajax_request(array(
582
							'MESSAGE_BODY'		=> $this->template->assign_display('body'),
583
							'MESSAGE_TITLE'		=> $this->user->lang['ADD_MODULE'],
584
							'MESSAGE_TEXT'		=> $this->user->lang['ADD_MODULE'],
585
586
							'YES_VALUE'		=> $this->user->lang['SUBMIT'],
587
							'S_CONFIRM_ACTION'	=> str_replace('&amp;', '&', $this->modules_manager->get_module_link('modules', $module_id)), //inefficient, rewrite whole function
588
							'S_HIDDEN_FIELDS'	=> $s_hidden_fields
589
						));
590
					}
591
				}
592
				else
593
				{
594
					$portal_modules = obtain_portal_modules();
595
596
					foreach ($portal_modules as $row)
597
					{
598
						if (!($this->c_class = $this->portal_helper->get_module($row['module_classname'])))
599
						{
600
							continue;
601
						}
602
603
						// Load module language
604
						$this->board3_controller_helper->load_module_language($this->c_class);
605
606
						$template_column = $this->portal_columns->number_to_string($row['module_column']);
607
608
						// find out of we can move modules to the left or right
609
						if (($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] + 1))) || ($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] + 2)) && $row['module_column'] != 2))
610
						{
611
							/**
612
							* check if we can actually move
613
							* this only applies to modules in the center column as the side modules
614
							* will automatically skip the center column when moving if they need to
615
							*/
616 View Code Duplication
							if ($row['module_classname'] != '\board3\portal\modules\custom')
617
							{
618
								$column_string = $this->portal_columns->number_to_string($row['module_column'] + 1); // move 1 right
619
620
								if ($column_string == 'right' && !$this->modules_constraints->can_move_module(array('left', 'right'), $row['module_classname']))
621
								{
622
									$move_right = false;
623
								}
624
								else
625
								{
626
									$move_right = true;
627
								}
628
							}
629
							else
630
							{
631
								$move_right = true;
632
							}
633
						}
634
						else
635
						{
636
							$move_right = false;
637
						}
638
639
						if (($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] - 1))) || ($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] - 2)) && $row['module_column'] != 2))
640
						{
641
							/**
642
							* check if we can actually move
643
							* this only applies to modules in the center column as the side modules
644
							* will automatically skip the center column when moving if they need to
645
							*/
646 View Code Duplication
							if ($row['module_classname'] != '\board3\portal\modules\custom')
647
							{
648
								$column_string = $this->portal_columns->number_to_string($row['module_column'] - 1); // move 1 left
649
650
								if ($column_string == 'left' && !$this->modules_constraints->can_move_module(array('left', 'right'), $row['module_classname']))
651
								{
652
									$move_left = false;
653
								}
654
								else
655
								{
656
									$move_left = true;
657
								}
658
							}
659
							else
660
							{
661
								$move_left = true;
662
							}
663
						}
664
						else
665
						{
666
							$move_left = false;
667
						}
668
669
						$this->template->assign_block_vars('modules_' . $template_column, array(
670
							'MODULE_NAME'		=> (isset($this->user->lang[$row['module_name']])) ? $this->user->lang[$row['module_name']] : $row['module_name'],
671
							'MODULE_IMAGE'		=> ($row['module_image_src']) ? '<img src="' . $this->root_path . 'styles/all/theme/images/portal/' . $row['module_image_src'] . '" alt="' . $row['module_name'] . '" />' : '',
672
							'MODULE_ENABLED'	=> ($row['module_status']) ? true : false,
673
674
							'U_DELETE'			=> $this->modules_manager->get_module_link('modules', $row['module_id']) . '&amp;action=delete&amp;module_classname=' . $row['module_classname'],
675
							'U_EDIT'			=> $this->modules_manager->get_module_link('config', $row['module_id']),
676
							'U_MOVE_UP'			=> $this->u_action . '&amp;module_id=' . $row['module_id'] . '&amp;action=move_up',
677
							'U_MOVE_DOWN'		=> $this->u_action . '&amp;module_id=' . $row['module_id'] . '&amp;action=move_down',
678
							'U_MOVE_RIGHT'		=> ($move_right) ? $this->u_action . '&amp;module_id=' . $row['module_id'] . '&amp;action=move_right' : '',
679
							'U_MOVE_LEFT'		=> ($move_left) ? $this->u_action . '&amp;module_id=' . $row['module_id'] . '&amp;action=move_left' : '',
680
						));
681
					}
682
683
					$this->template->assign_vars(array(
684
						'ICON_MOVE_LEFT'				=> '<img src="' . $this->root_path . 'adm/images/icon_left.gif" alt="' . $this->user->lang['MOVE_LEFT'] . '" title="' . $this->user->lang['MOVE_LEFT'] . '" />',
685
						'ICON_MOVE_LEFT_DISABLED'		=> '<img src="' . $this->root_path . 'adm/images/icon_left_disabled.gif" alt="' . $this->user->lang['MOVE_LEFT'] . '" title="' . $this->user->lang['MOVE_LEFT'] . '" />',
686
						'ICON_MOVE_RIGHT'				=> '<img src="' . $this->root_path . 'adm/images/icon_right.gif" alt="' . $this->user->lang['MOVE_RIGHT'] . '" title="' . $this->user->lang['MOVE_RIGHT'] . '" />',
687
						'ICON_MOVE_RIGHT_DISABLED'		=> '<img src="' . $this->root_path . 'adm/images/icon_right_disabled.gif" alt="' . $this->user->lang['MOVE_RIGHT'] . '" title="' . $this->user->lang['MOVE_RIGHT'] . '" />',
688
						'B3P_U_ACTION'					=> $this->modules_manager->get_module_link('modules', $module_id),
689
					));
690
				}
691
692
				$this->tpl_name = 'portal/acp_portal_modules';
693
				$this->page_title = 'ACP_PORTAL_MODULES';
694
			break;
695
696 1
			default:
697 1
				trigger_error('NO_MODE', E_USER_ERROR);
698
			break;
699 1
		}
700 2
	}
701
}
702