6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * - Able to check for new module versions (modes changed/adjusted/added/removed)
22 * - module enabled and displayed (common)
23 * - module enabled and not displayed
24 * - module deactivated
25 * - category (enabled)
34 var $module_class = '';
38 function main($id, $mode)
40 global $db, $user, $auth, $template, $module;
41 global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
43 // Set a global define for modules we might include (the author is able to prevent execution of code by checking this constant)
44 define('MODULE_INCLUDE', true);
46 $user->add_lang('acp/modules');
47 $this->tpl_name = 'acp_modules';
50 $this->module_class = $mode;
52 if ($this->module_class == 'ucp')
54 $user->add_lang('ucp');
56 else if ($this->module_class == 'mcp')
58 $user->add_lang('mcp');
61 if ($module->p_class != $this->module_class)
63 $module->add_mod_info($this->module_class);
66 $this->page_title = strtoupper($this->module_class);
68 $this->parent_id = request_var('parent_id', 0);
69 $module_id = request_var('m', 0);
70 $action = request_var('action', '');
78 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
81 if (confirm_box(true))
83 // Make sure we are not directly within a module
84 if ($module_id == $this->parent_id)
86 $sql = 'SELECT parent_id
87 FROM ' . MODULES_TABLE . '
88 WHERE module_id = ' . $module_id;
89 $result = $db->sql_query($sql);
90 $this->parent_id = (int) $db->sql_fetchfield('parent_id');
91 $db->sql_freeresult($result);
94 $errors = $this->delete_module($module_id);
98 $this->remove_cache_file();
99 trigger_error($user->lang['MODULE_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
104 confirm_box(false, 'DELETE_MODULE', build_hidden_fields(array(
107 'parent_id' => $this->parent_id,
108 'module_id' => $module_id,
119 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
123 FROM ' . MODULES_TABLE . "
124 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
125 AND module_id = $module_id";
126 $result = $db->sql_query($sql);
127 $row = $db->sql_fetchrow($result);
128 $db->sql_freeresult($result);
132 trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
135 $sql = 'UPDATE ' . MODULES_TABLE . '
136 SET module_enabled = ' . (($action == 'enable') ? 1 : 0) . "
137 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
138 AND module_id = $module_id";
139 $db->sql_query($sql);
141 add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']));
142 $this->remove_cache_file();
150 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
154 FROM ' . MODULES_TABLE . "
155 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
156 AND module_id = $module_id";
157 $result = $db->sql_query($sql);
158 $row = $db->sql_fetchrow($result);
159 $db->sql_freeresult($result);
163 trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
166 $move_module_name = $this->move_module_by($row, $action, 1);
168 if ($move_module_name !== false)
170 add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']), $move_module_name);
171 $this->remove_cache_file();
177 $quick_install = request_var('quick_install', '');
179 if (confirm_box(true))
181 if (!$quick_install || strpos($quick_install, '::') === false)
186 list($module_basename, $module_mode) = explode('::', $quick_install);
188 // Check if module name and mode exist...
189 $fileinfo = $this->get_module_infos($module_basename);
190 $fileinfo = $fileinfo[$module_basename];
192 if (isset($fileinfo['modes'][$module_mode]))
194 $module_data = array(
195 'module_basename' => $module_basename,
196 'module_enabled' => 0,
197 'module_display' => (isset($fileinfo['modes'][$module_mode]['display'])) ? $fileinfo['modes'][$module_mode]['display'] : 1,
198 'parent_id' => $this->parent_id,
199 'module_class' => $this->module_class,
200 'module_langname' => $fileinfo['modes'][$module_mode]['title'],
201 'module_mode' => $module_mode,
202 'module_auth' => $fileinfo['modes'][$module_mode]['auth'],
205 $errors = $this->update_module_data($module_data);
207 if (!sizeof($errors))
209 $this->remove_cache_file();
211 trigger_error($user->lang['MODULE_ADDED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
217 confirm_box(false, 'ADD_MODULE', build_hidden_fields(array(
220 'parent_id' => $this->parent_id,
221 'action' => 'quickadd',
222 'quick_install' => $quick_install,
232 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
235 $module_row = $this->get_module_row($module_id);
241 if ($action == 'add')
244 'module_basename' => '',
245 'module_enabled' => 0,
246 'module_display' => 1,
248 'module_langname' => utf8_normalize_nfc(request_var('module_langname', '', true)),
254 $module_data = array();
256 $module_data['module_basename'] = request_var('module_basename', (string) $module_row['module_basename']);
257 $module_data['module_enabled'] = request_var('module_enabled', (int) $module_row['module_enabled']);
258 $module_data['module_display'] = request_var('module_display', (int) $module_row['module_display']);
259 $module_data['parent_id'] = request_var('module_parent_id', (int) $module_row['parent_id']);
260 $module_data['module_class'] = $this->module_class;
261 $module_data['module_langname'] = utf8_normalize_nfc(request_var('module_langname', (string) $module_row['module_langname'], true));
262 $module_data['module_mode'] = request_var('module_mode', (string) $module_row['module_mode']);
264 $submit = (isset($_POST['submit'])) ? true : false;
268 if (!$module_data['module_langname'])
270 trigger_error($user->lang['NO_MODULE_LANGNAME'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
273 $module_type = request_var('module_type', 'category');
275 if ($module_type == 'category')
277 $module_data['module_basename'] = $module_data['module_mode'] = $module_data['module_auth'] = '';
278 $module_data['module_display'] = 1;
281 if ($action == 'edit')
283 $module_data['module_id'] = $module_id;
287 if ($module_data['module_basename'] && $module_data['module_mode'])
289 $fileinfo = $this->get_module_infos($module_data['module_basename']);
290 $module_data['module_auth'] = $fileinfo[$module_data['module_basename']]['modes'][$module_data['module_mode']]['auth'];
293 $errors = $this->update_module_data($module_data);
295 if (!sizeof($errors))
297 $this->remove_cache_file();
299 trigger_error((($action == 'add') ? $user->lang['MODULE_ADDED'] : $user->lang['MODULE_EDITED']) . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
303 // Category/not category?
304 $is_cat = (!$module_data['module_basename']) ? true : false;
306 // Get module information
307 $module_infos = $this->get_module_infos();
309 // Build name options
310 $s_name_options = $s_mode_options = '';
311 foreach ($module_infos as $option => $values)
313 if (!$module_data['module_basename'])
315 $module_data['module_basename'] = $option;
319 $s_name_options .= '<option value="' . $option . '"' . (($option == $module_data['module_basename']) ? ' selected="selected"' : '') . '>' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']</option>';
321 $template->assign_block_vars('m_names', array('NAME' => $option, 'A_NAME' => addslashes($option)));
323 // Build module modes
324 foreach ($values['modes'] as $m_mode => $m_values)
326 if ($option == $module_data['module_basename'])
328 $s_mode_options .= '<option value="' . $m_mode . '"' . (($m_mode == $module_data['module_mode']) ? ' selected="selected"' : '') . '>' . $this->lang_name($m_values['title']) . '</option>';
331 $template->assign_block_vars('m_names.modes', array(
333 'VALUE' => $this->lang_name($m_values['title']),
334 'A_OPTION' => addslashes($m_mode),
335 'A_VALUE' => addslashes($this->lang_name($m_values['title'])))
340 $s_cat_option = '<option value="0"' . (($module_data['parent_id'] == 0) ? ' selected="selected"' : '') . '>' . $user->lang['NO_PARENT'] . '</option>';
342 $template->assign_vars(array_merge(array(
343 'S_EDIT_MODULE' => true,
344 'S_IS_CAT' => $is_cat,
345 'S_CAT_OPTIONS' => $s_cat_option . $this->make_module_select($module_data['parent_id'], ($action == 'edit') ? $module_row['module_id'] : false, false, false, false, true),
346 'S_MODULE_NAMES' => $s_name_options,
347 'S_MODULE_MODES' => $s_mode_options,
348 'U_BACK' => $this->u_action . '&parent_id=' . $this->parent_id,
349 'U_EDIT_ACTION' => $this->u_action . '&parent_id=' . $this->parent_id,
351 'L_TITLE' => $user->lang[strtoupper($action) . '_MODULE'],
353 'MODULENAME' => $this->lang_name($module_data['module_langname']),
355 'MODULE_ID' => $module_id,
358 array_change_key_case($module_data, CASE_UPPER))
363 $template->assign_vars(array(
365 'ERROR_MSG' => implode('<br />', $errors))
374 // Default management page
377 $template->assign_vars(array(
379 'ERROR_MSG' => implode('<br />', $errors))
383 if (!$this->parent_id)
385 $navigation = strtoupper($this->module_class);
389 $navigation = '<a href="' . $this->u_action . '">' . strtoupper($this->module_class) . '</a>';
391 $modules_nav = $this->get_module_branch($this->parent_id, 'parents', 'descending');
393 foreach ($modules_nav as $row)
395 $langname = $this->lang_name($row['module_langname']);
397 if ($row['module_id'] == $this->parent_id)
399 $navigation .= ' -> ' . $langname;
403 $navigation .= ' -> <a href="' . $this->u_action . '&parent_id=' . $row['module_id'] . '">' . $langname . '</a>';
409 $module_box = $this->make_module_select($this->parent_id, false, false, false, false);
412 FROM ' . MODULES_TABLE . "
413 WHERE parent_id = {$this->parent_id}
414 AND module_class = '" . $db->sql_escape($this->module_class) . "'
416 $result = $db->sql_query($sql);
418 if ($row = $db->sql_fetchrow($result))
422 $langname = $this->lang_name($row['module_langname']);
424 if (!$row['module_enabled'])
426 $module_image = '<img src="images/icon_folder_lock.gif" alt="' . $user->lang['DEACTIVATED_MODULE'] .'" />';
430 $module_image = (!$row['module_basename'] || $row['left_id'] + 1 != $row['right_id']) ? '<img src="images/icon_subfolder.gif" alt="' . $user->lang['CATEGORY'] . '" />' : '<img src="images/icon_folder.gif" alt="' . $user->lang['MODULE'] . '" />';
433 $url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
435 $template->assign_block_vars('modules', array(
436 'MODULE_IMAGE' => $module_image,
437 'MODULE_TITLE' => $langname,
438 'MODULE_ENABLED' => ($row['module_enabled']) ? true : false,
439 'MODULE_DISPLAYED' => ($row['module_display']) ? true : false,
441 'S_ACP_CAT_SYSTEM' => ($this->module_class == 'acp' && $row['module_langname'] == 'ACP_CAT_SYSTEM') ? true : false,
442 'S_ACP_MODULE_MANAGEMENT' => ($this->module_class == 'acp' && ($row['module_basename'] == 'modules' || $row['module_langname'] == 'ACP_MODULE_MANAGEMENT')) ? true : false,
444 'U_MODULE' => $this->u_action . '&parent_id=' . $row['module_id'],
445 'U_MOVE_UP' => $url . '&action=move_up',
446 'U_MOVE_DOWN' => $url . '&action=move_down',
447 'U_EDIT' => $url . '&action=edit',
448 'U_DELETE' => $url . '&action=delete',
449 'U_ENABLE' => $url . '&action=enable',
450 'U_DISABLE' => $url . '&action=disable')
453 while ($row = $db->sql_fetchrow($result));
455 else if ($this->parent_id)
457 $row = $this->get_module_row($this->parent_id);
459 $url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
461 $template->assign_vars(array(
462 'S_NO_MODULES' => true,
463 'MODULE_TITLE' => $langname,
464 'MODULE_ENABLED' => ($row['module_enabled']) ? true : false,
465 'MODULE_DISPLAYED' => ($row['module_display']) ? true : false,
467 'U_EDIT' => $url . '&action=edit',
468 'U_DELETE' => $url . '&action=delete',
469 'U_ENABLE' => $url . '&action=enable',
470 'U_DISABLE' => $url . '&action=disable')
473 $db->sql_freeresult($result);
475 // Quick adding module
476 $module_infos = $this->get_module_infos();
478 // Build quick options
479 $s_install_options = '';
480 foreach ($module_infos as $option => $values)
483 $s_install_options .= '<optgroup label="' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']">';
485 // Build module modes
486 foreach ($values['modes'] as $m_mode => $m_values)
488 $s_install_options .= '<option value="' . $option . '::' . $m_mode . '"> ' . $this->lang_name($m_values['title']) . '</option>';
491 $s_install_options .= '</optgroup>';
494 $template->assign_vars(array(
495 'U_SEL_ACTION' => $this->u_action,
496 'U_ACTION' => $this->u_action . '&parent_id=' . $this->parent_id,
497 'NAVIGATION' => $navigation,
498 'MODULE_BOX' => $module_box,
499 'PARENT_ID' => $this->parent_id,
500 'S_INSTALL_OPTIONS' => $s_install_options,
506 * Get row for specified module
508 function get_module_row($module_id)
513 FROM ' . MODULES_TABLE . "
514 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
515 AND module_id = $module_id";
516 $result = $db->sql_query($sql);
517 $row = $db->sql_fetchrow($result);
518 $db->sql_freeresult($result);
522 trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
529 * Get available module information from module files
531 function get_module_infos($module = '', $module_class = false)
533 global $phpbb_root_path, $phpEx;
535 $module_class = ($module_class === false) ? $this->module_class : $module_class;
537 $directory = $phpbb_root_path . 'includes/' . $module_class . '/info/';
542 $dh = @opendir($directory);
549 while (($file = readdir($dh)) !== false)
552 if (preg_match('/^' . $module_class . '_.+\.' . $phpEx . '$/', $file))
554 $class = str_replace(".$phpEx", '', $file) . '_info';
556 if (!class_exists($class))
558 include($directory . $file);
561 // Get module title tag
562 if (class_exists($class))
564 $c_class = new $class();
565 $module_info = $c_class->module();
566 $fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
576 $filename = $module_class . '_' . basename($module);
577 $class = $module_class . '_' . basename($module) . '_info';
579 if (!class_exists($class))
581 include($directory . $filename . '.' . $phpEx);
584 // Get module title tag
585 if (class_exists($class))
587 $c_class = new $class();
588 $module_info = $c_class->module();
589 $fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
597 * Simple version of jumpbox, just lists modules
599 function make_module_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $ignore_noncat = false)
601 global $db, $user, $auth, $config;
603 $sql = 'SELECT module_id, module_enabled, module_basename, parent_id, module_langname, left_id, right_id, module_auth
604 FROM ' . MODULES_TABLE . "
605 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
606 ORDER BY left_id ASC";
607 $result = $db->sql_query($sql);
609 $right = $iteration = 0;
610 $padding_store = array('0' => '');
611 $module_list = $padding = '';
613 while ($row = $db->sql_fetchrow($result))
615 if ($row['left_id'] < $right)
617 $padding .= ' ';
618 $padding_store[$row['parent_id']] = $padding;
620 else if ($row['left_id'] > $right + 1)
622 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
625 $right = $row['right_id'];
627 if (!$ignore_acl && $row['module_auth'])
629 // We use zero as the forum id to check - global setting.
630 if (!p_master::module_auth($row['module_auth'], 0))
636 // ignore this module?
637 if ((is_array($ignore_id) && in_array($row['module_id'], $ignore_id)) || $row['module_id'] == $ignore_id)
643 if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
648 // ignore non-category?
649 if ($row['module_basename'] && $ignore_noncat)
654 $selected = (is_array($select_id)) ? ((in_array($row['module_id'], $select_id)) ? ' selected="selected"' : '') : (($row['module_id'] == $select_id) ? ' selected="selected"' : '');
656 $langname = $this->lang_name($row['module_langname']);
657 $module_list .= '<option value="' . $row['module_id'] . '"' . $selected . ((!$row['module_enabled']) ? ' class="disabled"' : '') . '>' . $padding . $langname . '</option>';
661 $db->sql_freeresult($result);
663 unset($padding_store);
671 function get_module_branch($module_id, $type = 'all', $order = 'descending', $include_module = true)
678 $condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
682 $condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
686 $condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
693 FROM ' . MODULES_TABLE . ' m1
694 LEFT JOIN ' . MODULES_TABLE . " m2 ON ($condition)
695 WHERE m1.module_class = '" . $db->sql_escape($this->module_class) . "'
696 AND m2.module_class = '" . $db->sql_escape($this->module_class) . "'
697 AND m1.module_id = $module_id
698 ORDER BY m2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
699 $result = $db->sql_query($sql);
701 while ($row = $db->sql_fetchrow($result))
703 if (!$include_module && $row['module_id'] == $module_id)
710 $db->sql_freeresult($result);
716 * Remove modules cache file
718 function remove_cache_file()
722 // Sanitise for future path use, it's escaped as appropriate for queries
723 $p_class = str_replace(array('.', '/', '\\'), '', basename($this->module_class));
725 $cache->destroy('_modules_' . $p_class);
727 // Additionally remove sql cache
728 $cache->destroy('sql', MODULES_TABLE);
732 * Return correct language name
734 function lang_name($module_langname)
738 return (!empty($user->lang[$module_langname])) ? $user->lang[$module_langname] : $module_langname;
744 * @param bool $run_inline if set to true errors will be returned and no logs being written
746 function update_module_data(&$module_data, $run_inline = false)
750 if (!isset($module_data['module_id']))
752 // no module_id means we're creating a new category/module
753 if ($module_data['parent_id'])
755 $sql = 'SELECT left_id, right_id
756 FROM ' . MODULES_TABLE . "
757 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
758 AND module_id = " . (int) $module_data['parent_id'];
759 $result = $db->sql_query($sql);
760 $row = $db->sql_fetchrow($result);
761 $db->sql_freeresult($result);
767 return 'PARENT_NO_EXIST';
770 trigger_error($user->lang['PARENT_NO_EXIST'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
774 $row['left_id'] = (int) $row['left_id'];
775 $row['right_id'] = (int) $row['right_id'];
777 $sql = 'UPDATE ' . MODULES_TABLE . "
778 SET left_id = left_id + 2, right_id = right_id + 2
779 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
780 AND left_id > {$row['right_id']}";
781 $db->sql_query($sql);
783 $sql = 'UPDATE ' . MODULES_TABLE . "
784 SET right_id = right_id + 2
785 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
786 AND {$row['left_id']} BETWEEN left_id AND right_id";
787 $db->sql_query($sql);
789 $module_data['left_id'] = (int) $row['right_id'];
790 $module_data['right_id'] = (int) $row['right_id'] + 1;
794 $sql = 'SELECT MAX(right_id) AS right_id
795 FROM ' . MODULES_TABLE . "
796 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'";
797 $result = $db->sql_query($sql);
798 $row = $db->sql_fetchrow($result);
799 $db->sql_freeresult($result);
801 $module_data['left_id'] = (int) $row['right_id'] + 1;
802 $module_data['right_id'] = (int) $row['right_id'] + 2;
805 $sql = 'INSERT INTO ' . MODULES_TABLE . ' ' . $db->sql_build_array('INSERT', $module_data);
806 $db->sql_query($sql);
808 $module_data['module_id'] = $db->sql_nextid();
812 add_log('admin', 'LOG_MODULE_ADD', $this->lang_name($module_data['module_langname']));
817 $row = $this->get_module_row($module_data['module_id']);
819 if ($module_data['module_basename'] && !$row['module_basename'])
821 // we're turning a category into a module
822 $branch = $this->get_module_branch($module_data['module_id'], 'children', 'descending', false);
826 return array($user->lang['NO_CATEGORY_TO_MODULE']);
830 if ($row['parent_id'] != $module_data['parent_id'])
832 $this->move_module($module_data['module_id'], $module_data['parent_id']);
835 $update_ary = $module_data;
836 unset($update_ary['module_id']);
838 $sql = 'UPDATE ' . MODULES_TABLE . '
839 SET ' . $db->sql_build_array('UPDATE', $update_ary) . "
840 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
841 AND module_id = " . (int) $module_data['module_id'];
842 $db->sql_query($sql);
846 add_log('admin', 'LOG_MODULE_EDIT', $this->lang_name($module_data['module_langname']));
854 * Move module around the tree
856 function move_module($from_module_id, $to_parent_id)
860 $moved_modules = $this->get_module_branch($from_module_id, 'children', 'descending');
861 $from_data = $moved_modules[0];
862 $diff = sizeof($moved_modules) * 2;
864 $moved_ids = array();
865 for ($i = 0; $i < sizeof($moved_modules); ++$i)
867 $moved_ids[] = $moved_modules[$i]['module_id'];
871 $sql = 'UPDATE ' . MODULES_TABLE . "
872 SET right_id = right_id - $diff
873 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
874 AND left_id < " . (int) $from_data['right_id'] . '
875 AND right_id > ' . (int) $from_data['right_id'];
876 $db->sql_query($sql);
878 // Resync righthand side of tree
879 $sql = 'UPDATE ' . MODULES_TABLE . "
880 SET left_id = left_id - $diff, right_id = right_id - $diff
881 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
882 AND left_id > " . (int) $from_data['right_id'];
883 $db->sql_query($sql);
885 if ($to_parent_id > 0)
887 $to_data = $this->get_module_row($to_parent_id);
889 // Resync new parents
890 $sql = 'UPDATE ' . MODULES_TABLE . "
891 SET right_id = right_id + $diff
892 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
893 AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
894 AND ' . $db->sql_in_set('module_id', $moved_ids, true);
895 $db->sql_query($sql);
897 // Resync the righthand side of the tree
898 $sql = 'UPDATE ' . MODULES_TABLE . "
899 SET left_id = left_id + $diff, right_id = right_id + $diff
900 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
901 AND left_id > " . (int) $to_data['right_id'] . '
902 AND ' . $db->sql_in_set('module_id', $moved_ids, true);
903 $db->sql_query($sql);
905 // Resync moved branch
906 $to_data['right_id'] += $diff;
907 if ($to_data['right_id'] > $from_data['right_id'])
909 $diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
913 $diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
918 $sql = 'SELECT MAX(right_id) AS right_id
919 FROM ' . MODULES_TABLE . "
920 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
921 AND " . $db->sql_in_set('module_id', $moved_ids, true);
922 $result = $db->sql_query($sql);
923 $row = $db->sql_fetchrow($result);
924 $db->sql_freeresult($result);
926 $diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
929 $sql = 'UPDATE ' . MODULES_TABLE . "
930 SET left_id = left_id $diff, right_id = right_id $diff
931 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
932 AND " . $db->sql_in_set('module_id', $moved_ids);
933 $db->sql_query($sql);
937 * Remove module from tree
939 function delete_module($module_id)
943 $row = $this->get_module_row($module_id);
945 $branch = $this->get_module_branch($module_id, 'children', 'descending', false);
949 return array($user->lang['CANNOT_REMOVE_MODULE']);
954 $sql = 'DELETE FROM ' . MODULES_TABLE . "
955 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
956 AND module_id = $module_id";
957 $db->sql_query($sql);
959 $row['right_id'] = (int) $row['right_id'];
960 $row['left_id'] = (int) $row['left_id'];
963 $sql = 'UPDATE ' . MODULES_TABLE . "
964 SET right_id = right_id - $diff
965 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
966 AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
967 $db->sql_query($sql);
969 $sql = 'UPDATE ' . MODULES_TABLE . "
970 SET left_id = left_id - $diff, right_id = right_id - $diff
971 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
972 AND left_id > {$row['right_id']}";
973 $db->sql_query($sql);
975 add_log('admin', 'LOG_MODULE_REMOVED', $this->lang_name($row['module_langname']));
982 * Move module position by $steps up/down
984 function move_module_by($module_row, $action = 'move_up', $steps = 1)
989 * Fetch all the siblings between the module's current spot
990 * and where we want to move it to. If there are less than $steps
991 * siblings between the current spot and the target then the
992 * module will move as far as possible
994 $sql = 'SELECT module_id, left_id, right_id, module_langname
995 FROM ' . MODULES_TABLE . "
996 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
997 AND parent_id = " . (int) $module_row['parent_id'] . '
998 AND ' . (($action == 'move_up') ? 'right_id < ' . (int) $module_row['right_id'] . ' ORDER BY right_id DESC' : 'left_id > ' . (int) $module_row['left_id'] . ' ORDER BY left_id ASC');
999 $result = $db->sql_query_limit($sql, $steps);
1002 while ($row = $db->sql_fetchrow($result))
1006 $db->sql_freeresult($result);
1008 if (!sizeof($target))
1010 // The module is already on top or bottom
1015 * $left_id and $right_id define the scope of the nodes that are affected by the move.
1016 * $diff_up and $diff_down are the values to substract or add to each node's left_id
1017 * and right_id in order to move them up or down.
1018 * $move_up_left and $move_up_right define the scope of the nodes that are moving
1019 * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
1021 if ($action == 'move_up')
1023 $left_id = (int) $target['left_id'];
1024 $right_id = (int) $module_row['right_id'];
1026 $diff_up = (int) ($module_row['left_id'] - $target['left_id']);
1027 $diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
1029 $move_up_left = (int) $module_row['left_id'];
1030 $move_up_right = (int) $module_row['right_id'];
1034 $left_id = (int) $module_row['left_id'];
1035 $right_id = (int) $target['right_id'];
1037 $diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
1038 $diff_down = (int) ($target['right_id'] - $module_row['right_id']);
1040 $move_up_left = (int) ($module_row['right_id'] + 1);
1041 $move_up_right = (int) $target['right_id'];
1044 // Now do the dirty job
1045 $sql = 'UPDATE ' . MODULES_TABLE . "
1046 SET left_id = left_id + CASE
1047 WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
1050 right_id = right_id + CASE
1051 WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
1054 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1055 AND left_id BETWEEN {$left_id} AND {$right_id}
1056 AND right_id BETWEEN {$left_id} AND {$right_id}";
1057 $db->sql_query($sql);
1059 $this->remove_cache_file();
1061 return $this->lang_name($target['module_langname']);