Added portal_config table for large data that needs to inserted into the database (adds 1 query but we won't have to tamper with phpBB3's config table);

Added serialize and unserialize to the main menu block for a better way of working with the links;
This commit is contained in:
Marc Alexander
2010-10-05 14:57:17 +00:00
parent 73681a4b43
commit 1731999649
6 changed files with 148 additions and 133 deletions

View File

@@ -35,6 +35,11 @@ class acp_portal
{
include($phpbb_root_path . $portal_root_path . 'includes/functions_version_check.' . $phpEx);
}
if(!function_exists('obtain_portal_config'))
{
include($phpbb_root_path . $portal_root_path . 'includes/functions.' . $phpEx);
}
$user->add_lang('mods/portal');
$submit = (isset($_POST['submit'])) ? true : false;

View File

@@ -76,6 +76,14 @@ $versions = array(
'PRIMARY_KEY' => 'module_id',
)),
array(phpbb_portal_config, array(
'COLUMNS' => array(
'config_name' => array('VCHAR:255', ''),
'config_value'=> array('MTEXT', ''),
),
'PRIMARY_KEY' => 'config_name',
));
),

View File

@@ -34,6 +34,8 @@ if (!$config['board3_enable'])
redirect(reapply_sid($phpbb_root_path . 'index.' . $phpEx));
}
$portal_config = obtain_portal_config();
$sql = 'SELECT *
FROM ' . PORTAL_MODULES_TABLE . '
ORDER BY module_order ASC';

View File

@@ -21,5 +21,6 @@ define('B3_LINKS_EXT', 2);
// Tables and paths
define('PORTAL_ROOT_PATH', 'portal/');
define('PORTAL_MODULES_TABLE', $table_prefix . 'portal_modules');
define('PORTAL_CONFIG_TABLE', $table_prefix . 'portal_config');
?>

View File

@@ -17,24 +17,27 @@ if (!defined('IN_PHPBB'))
// Get portal config
function obtain_portal_config()
{
global $db;
global $db, $portal_config;
$sql = 'SELECT config_name, config_value
FROM ' . PORTAL_CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
if(sizeof($portal_config) < 1)
{
$cached_portal_config[$row['config_name']] = $row['config_value'];
$portal_config[$row['config_name']] = $row['config_value'];
$sql = 'SELECT config_name, config_value
FROM ' . PORTAL_CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$portal_config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
}
$db->sql_freeresult($result);
return $portal_config;
}
/**
* Set config value. Creates missing config entry.
* Only use this if your config value might exceed 255 characters, otherwise please use set_config
*/
function set_portal_config($config_name, $config_value)
{
@@ -124,16 +127,16 @@ function phpbb_fetch_posts($forum_from, $permissions, $number_of_posts, $text_le
case "news":
$topic_type = 't.topic_type = ' . POST_NORMAL;
$str_where = (strlen($str_where) > 0) ? 'AND (' . trim(substr($str_where, 0, -4)) . ')' : '';
$user_link = ($portal_config['portal_news_style']) ? 't.topic_poster = u.user_id' : (($portal_config['portal_news_show_last']) ? 't.topic_last_poster_id = u.user_id' : 't.topic_poster = u.user_id' ) ;
$post_link = ($portal_config['portal_news_style']) ? 't.topic_first_post_id = p.post_id' : (($portal_config['portal_news_show_last']) ? 't.topic_last_post_id = p.post_id' : 't.topic_first_post_id = p.post_id' ) ;
$topic_order = ($portal_config['portal_news_show_last']) ? 't.topic_last_post_time DESC' : 't.topic_time DESC' ;
$user_link = ($config['board3_news_style']) ? 't.topic_poster = u.user_id' : (($config['board3_news_show_last']) ? 't.topic_last_poster_id = u.user_id' : 't.topic_poster = u.user_id' ) ;
$post_link = ($config['board3_news_style']) ? 't.topic_first_post_id = p.post_id' : (($config['board3_news_show_last']) ? 't.topic_last_post_id = p.post_id' : 't.topic_first_post_id = p.post_id' ) ;
$topic_order = ($config['board3_news_show_last']) ? 't.topic_last_post_time DESC' : 't.topic_time DESC' ;
break;
case "news_all":
$topic_type = '(t.topic_type <> ' . POST_ANNOUNCE . ') AND (t.topic_type <> ' . POST_GLOBAL . ')';
$str_where = (strlen($str_where) > 0) ? 'AND (' . trim(substr($str_where, 0, -4)) . ')' : '';
$user_link = ($portal_config['portal_news_style']) ? 't.topic_poster = u.user_id' : (($portal_config['portal_news_show_last']) ? 't.topic_last_poster_id = u.user_id' : 't.topic_poster = u.user_id' ) ;
$post_link = ($portal_config['portal_news_style']) ? 't.topic_first_post_id = p.post_id' : (($portal_config['portal_news_show_last']) ? 't.topic_last_post_id = p.post_id' : 't.topic_first_post_id = p.post_id' ) ;
$topic_order = ($portal_config['portal_news_show_last']) ? 't.topic_last_post_time DESC' : 't.topic_time DESC' ;
$user_link = ($config['board3_news_style']) ? 't.topic_poster = u.user_id' : (($config['board3_news_show_last']) ? 't.topic_last_poster_id = u.user_id' : 't.topic_poster = u.user_id' ) ;
$post_link = ($config['board3_news_style']) ? 't.topic_first_post_id = p.post_id' : (($config['board3_news_show_last']) ? 't.topic_last_post_id = p.post_id' : 't.topic_first_post_id = p.post_id' ) ;
$topic_order = ($config['board3_news_show_last']) ? 't.topic_last_post_time DESC' : 't.topic_time DESC' ;
break;
}

View File

@@ -56,17 +56,15 @@ class portal_main_menu_module
{
global $config, $template, $phpEx, $phpbb_root_path, $user, $db;
$links_urls = $links_options = $links_titles = $groups_ary = array();
$links = array();
$portal_config = obtain_portal_config();
$links_urls = explode(';', $config['board3_links_urls_' . $module_id]);
$links_options = explode(';', $config['board3_links_options_' . $module_id]);
$links_titles = explode(';', $config['board3_links_titles_' . $module_id]);
$links_permissions = explode(';', $config['board3_links_permissions_' . $module_id]);
$links = $this->utf_unserialize($portal_config['board3_menu_array_' . $module_id]);
// get user's groups
$sql = 'SELECT group_id
FROM ' . USER_GROUP_TABLE . '
WHERE user_id = ' . $user->data['user_id'] . '
WHERE user_id = ' . (int) $user->data['user_id'] . '
ORDER BY group_id ASC';
$result = $db->sql_query($sql);
while($row = $db->sql_fetchrow($result))
@@ -76,33 +74,33 @@ class portal_main_menu_module
$db->sql_freeresult($result);
for ($i = 0; $i < sizeof($links_urls); $i++)
for ($i = 0; $i < sizeof($links); $i++)
{
if($links_options[$i] == B3_LINKS_CAT)
if($links[$i]['type'] == B3_LINKS_CAT)
{
$template->assign_block_vars('portalmenu', array(
'CAT_TITLE' => (isset($user->lang[$links_titles[$i]])) ? $user->lang[$links_titles[$i]] : $links_titles[$i],
'CAT_TITLE' => (isset($user->lang[$links[$i]['title']])) ? $user->lang[$links[$i]['title']] : $links[$i]['title'],
));
}
else
{
if($links_options[$i] == B3_LINKS_INT)
if($links[$i]['type'] == B3_LINKS_INT)
{
$links_urls[$i] = str_replace('&', '&amp;', $links_urls[$i]); // we need to do this in order to prevent XHTML validation errors
$cur_url = append_sid($phpbb_root_path . $links_urls[$i]); // the user should know what kind of file it is
$links[$i]['url'] = str_replace('&', '&amp;', $links[$i]['url']); // we need to do this in order to prevent XHTML validation errors
$cur_url = append_sid($phpbb_root_path . $links[$i]['url']); // the user should know what kind of file it is
}
else
{
$cur_url = $links_urls[$i];
$cur_url = $links[$i]['url'];
}
$cur_permissions = explode(',', $links_permissions[$i]);
$cur_permissions = explode(',', $links[$i]['permission']);
$permission_check = array_intersect($groups_ary, $cur_permissions);
if(!empty($permission_check) || $links_permissions[$i] == '')
if(!empty($permission_check) || $links[$i]['permission'] == '')
{
$template->assign_block_vars('portalmenu.links', array(
'LINK_TITLE' => (isset($user->lang[$links_titles[$i]])) ? $user->lang[$links_titles[$i]] : $links_titles[$i],
'LINK_TITLE' => (isset($user->lang[$links[$i]['title']])) ? $user->lang[$links[$i]['title']] : $links[$i]['title'],
'LINK_URL' => $cur_url,
));
}
@@ -119,7 +117,7 @@ class portal_main_menu_module
'title' => 'ACP_PORTAL_MENU',
'vars' => array(
'legend1' => 'ACP_PORTAL_MENU',
'board3_links_urls_' . $module_id => array('lang' => 'ACP_PORTAL_MENU_MANAGE', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => 'manage_links', 'submit' => 'update_links'),
'board3_menu_' . $module_id => array('lang' => 'ACP_PORTAL_MENU_MANAGE', 'validate' => 'string', 'type' => 'custom', 'explain' => true, 'method' => 'manage_links', 'submit' => 'update_links'),
),
);
}
@@ -141,6 +139,8 @@ class portal_main_menu_module
$groups_ary[$row['group_name']] = $row['group_id'];
}
$links = array();
$links_titles = array(
'M_CONTENT',
'INDEX',
@@ -155,7 +155,7 @@ class portal_main_menu_module
'M_PRV',
);
$links_options = array(
$links_types = array(
B3_LINKS_CAT,
B3_LINKS_INT,
B3_LINKS_INT,
@@ -197,10 +197,20 @@ class portal_main_menu_module
'',
);
set_config('board3_links_urls_' . $module_id, implode(';', $links_urls));
set_config('board3_links_options_' . $module_id, implode(';', $links_options));
set_config('board3_links_titles_' . $module_id, implode(';', $links_titles));
set_config('board3_links_permissions_' . $module_id, implode(';', $links_permissions));
foreach($links_urls as $i => $url)
{
$links[] = array(
'title' => $links_titles[$i],
'url' => $links_urls[$i],
'type' => $links_types[$i],
'permission' => $links_permissions[$i],
);
}
$board3_menu_array = serialize($links);
set_portal_config('board3_menu_array_' . $module_id, $board3_menu_array);
set_config('board3_menu_' . $module_id, '');
return true;
}
@@ -209,16 +219,20 @@ class portal_main_menu_module
global $db;
$del_config = array(
'board3_links_urls_' . $module_id,
'board3_links_options_' . $module_id,
'board3_links_titles_' . $module_id,
'board3_links_permissions_' . $module_id,
'board3_menu_array_' . $module_id,
);
$sql = 'DELETE FROM ' . PORTAL_CONFIG_TABLE . '
WHERE ' . $db->sql_in_set('config_name', $del_config);
$del_config = array(
'board3_menu_' . $module_id,
);
$sql = 'DELETE FROM ' . CONFIG_TABLE . '
WHERE ' . $db->sql_in_set('config_name', $del_config);
return $db->sql_query($sql);
}
// Manage the menu links
function manage_links($value, $key, $module_id)
{
global $config, $phpbb_admin_path, $user, $phpEx, $db, $template;
@@ -227,18 +241,16 @@ class portal_main_menu_module
$action = (isset($_POST['add'])) ? 'add' : $action;
$action = (isset($_POST['save'])) ? 'save' : $action;
$link_id = request_var('id', 99999999); // 0 will trigger unwanted behavior, therefore we set a number we should never reach
$portal_config = obtain_portal_config();
$sql = 'SELECT module_id FROM ' . PORTAL_MODULES_TABLE . " WHERE module_classname = 'main_menu'";
$result = $db->sql_query($sql);
$module_id = $db->sql_fetchfield('module_id');
$db->sql_freeresult($result);
$links_urls = $links_options = $links_titles = array();
$links_urls = explode(';', $config['board3_links_urls_' . $module_id]);
$links_options = explode(';', $config['board3_links_options_' . $module_id]);
$links_titles = explode(';', $config['board3_links_titles_' . $module_id]);
$links_permissions = explode(';', $config['board3_links_permissions_' . $module_id]);
$links = array();
$links = $this->utf_unserialize($portal_config['board3_menu_array_' . $module_id]);
$u_action = append_sid($phpbb_admin_path . 'index.' . $phpEx, 'i=portal&amp;mode=config&amp;module_id=' . $module_id);
@@ -273,6 +285,7 @@ class portal_main_menu_module
$link_permissions = array_intersect($link_permission, $groups_ary);
$link_permissions = implode(',', $link_permissions);
// Check for errors
if (!$link_title)
{
trigger_error($user->lang['NO_LINK_TITLE'] . adm_back_link($u_action), E_USER_WARNING);
@@ -283,26 +296,17 @@ class portal_main_menu_module
trigger_error($user->lang['NO_LINK_URL'] . adm_back_link($u_action), E_USER_WARNING);
}
// overwrite already existing links and make sure we don't try to save a link outside of the normal array size of $links_urls
if (isset($link_id) && $link_id < sizeof($links_urls))
// overwrite already existing links and make sure we don't try to save a link outside of the normal array size of $links
if (isset($link_id) && $link_id < sizeof($links))
{
$message = $user->lang['LINK_UPDATED'];
// always check if the links already exist
if(isset($links_titles[$link_id]) && isset($links_options[$link_id]) && ($link_is_cat || isset($links_urls[$link_id])))
{
$links_titles[$link_id] = $link_title;
$links_urls[$link_id] = htmlspecialchars_decode($link_url);
$links_options[$link_id] = $link_type;
$links_permissions[$link_id] = $link_permissions;
}
else
{
$links_titles[] = $link_title;
$links_urls[] = $link_url;
$links_options[] = $link_type;
$links_permissions[$link_id] = $link_permissions;
}
$links[$link_id] = array(
'title' => $link_title,
'url' => htmlspecialchars_decode($link_url),
'type' => $link_type,
'permission' => $link_permissions,
);
add_log('admin', 'LOG_PORTAL_LINK_UPDATED', $link_title);
}
@@ -310,35 +314,21 @@ class portal_main_menu_module
{
$message = $user->lang['LINK_ADDED'];
if($links_titles[0] == '')
if($link_type != B3_LINKS_CAT && sizeof($links) < 1)
{
if($link_type != B3_LINKS_CAT && sizeof($links_titles) < 1)
{
trigger_error($user->lang['ACP_PORTAL_MENU_CREATE_CAT'] . adm_back_link($u_action), E_USER_WARNING);
}
$links_titles[0] = $link_title;
$links_urls[0] = $link_url;
$links_options[0] = $link_type;
$links_permissions[0] = $link_permissions;
}
else
{
if($link_type != B3_LINKS_CAT && sizeof($links_titles) < 1)
{
trigger_error($user->lang['ACP_PORTAL_MENU_CREATE_CAT'] . adm_back_link($u_action), E_USER_WARNING);
}
$links_titles[] = $link_title;
$links_urls[] = $link_url;
$links_options[] = $link_type;
$links_permissions[] = $link_permissions;
trigger_error($user->lang['ACP_PORTAL_MENU_CREATE_CAT'] . adm_back_link($u_action), E_USER_WARNING);
}
$links[] = array(
'title' => $link_title,
'url' => htmlspecialchars_decode($link_url),
'type' => $link_type,
'permission' => $link_permissions,
);
add_log('admin', 'LOG_PORTAL_LINK_ADDED', $link_title);
}
set_config('board3_links_urls_' . $module_id, implode(';', $links_urls));
set_config('board3_links_options_' . $module_id, implode(';', $links_options));
set_config('board3_links_titles_' . $module_id, implode(';', $links_titles));
set_config('board3_links_permissions_' . $module_id, implode(';', $links_permissions));
$board3_menu_array = serialize($links);
set_portal_config('board3_menu_array_' . $module_id, $board3_menu_array);
trigger_error($message . adm_back_link($u_action));
@@ -347,7 +337,7 @@ class portal_main_menu_module
// Delete link
case 'delete':
if (!isset($link_id) && $link_id >= sizeof($links_urls))
if (!isset($link_id) && $link_id >= sizeof($links))
{
trigger_error($user->lang['MUST_SELECT_LINK'] . adm_back_link($u_action), E_USER_WARNING);
}
@@ -355,25 +345,25 @@ class portal_main_menu_module
if (confirm_box(true))
{
$cur_link_title = $links_titles[$link_id];
// make sure we don't delete links that weren't supposed to be deleted
$title_ary = array('{remove_link}');
$links_titles[$link_id] = '{remove_link}';
$option_ary = array('{remove_link}');
$links_options[$link_id] = '{remove_link}';
$url_ary = array('{remove_link}');
$links_urls[$link_id] = '{remove_link}';
$permission_ary = array('{remove_link}');
$links_permissions[$link_id] = '{remove_link}';
// make sure we don't delete links that weren't supposed to be deleted, i.e. duplicate links
$del_ary = array(
'title' => '{remove_link}',
'url' => '{remove_link}',
'type' => '{remove_link}',
'permission' => '{remove_link}',
);
$links_titles = array_diff($links_titles, $title_ary);
$links_urls = array_diff($links_urls, $url_ary);
$links_options = array_diff($links_options, $url_ary);
$links_permissions = array_diff($links_permissions, $permission_ary);
$links[$link_id] = array(
'title' => '{remove_link}',
'url' => '{remove_link}',
'type' => '{remove_link}',
'permission' => '{remove_link}',
);
$links = array_diff($links, $del_ary);
set_config('board3_links_urls_' . $module_id, implode(';', $links_urls));
set_config('board3_links_options_' . $module_id, implode(';', $links_options));
set_config('board3_links_titles_' . $module_id, implode(';', $links_titles));
set_config('board3_links_permissions_' . $module_id, implode(';', $links_permissions));
$board3_menu_array = serialize($links);
set_portal_config('board3_menu_array_' . $module_id, $board3_menu_array);
add_log('admin', 'LOG_PORTAL_LINK_REMOVED', $cur_link_title);
}
@@ -391,13 +381,13 @@ class portal_main_menu_module
case 'move_up':
case 'move_down':
if (!isset($link_id) && $link_id >= sizeof($links_urls))
if (!isset($link_id) && $link_id >= sizeof($links))
{
trigger_error($user->lang['MUST_SELECT_LINK'] . adm_back_link($u_action), E_USER_WARNING);
}
// make sure we don't try to move a link where it can't be moved
if (($link_id == 0 && $action == 'move_up') || ($link_id == (sizeof($links_urls) - 1) && $action == 'move_down'))
if (($link_id == 0 && $action == 'move_up') || ($link_id == (sizeof($links) - 1) && $action == 'move_down'))
{
break;
}
@@ -410,27 +400,26 @@ class portal_main_menu_module
$switch_order_id = ($action == 'move_down') ? $link_id + 1 : $link_id - 1;
// back up the info of the link we want to move
$cur_url = $links_urls[$link_id];
$cur_title = $links_titles[$link_id];
$cur_option = $links_options[$link_id];
$cur_permission = $links_permissions[$link_id];
$cur_link = array(
'title' => $links[$link_id]['title'],
'url' => $links[$link_id]['url'],
'type' => $links[$link_id]['type'],
'permission' => $links[$link_id]['permission'],
);
// move the info of the links we replace in the order
$links_urls[$link_id] = $links_urls[$switch_order_id];
$links_titles[$link_id] = $links_titles[$switch_order_id];
$links_options[$link_id] = $links_options[$switch_order_id];
$links_permissions[$link_id] = $links_permissions[$switch_order_id];
$links[$link_id] = array(
'title' => $links[$switch_order_id]['title'],
'url' => $links[$switch_order_id]['url'],
'type' => $links[$switch_order_id]['type'],
'permission' => $links[$switch_order_id]['permission'],
);
// insert the info of the moved link
$links_urls[$switch_order_id] = $cur_url;
$links_titles[$switch_order_id] = $cur_title;
$links_options[$switch_order_id] = $cur_option;
$links_permissions[$switch_order_id] = $cur_permission;
$links[$switch_order_id] = $cur_link;
set_config('board3_links_urls_' . $module_id, implode(';', $links_urls));
set_config('board3_links_options_' . $module_id, implode(';', $links_options));
set_config('board3_links_titles_' . $module_id, implode(';', $links_titles));
set_config('board3_links_permissions_' . $module_id, implode(';', $links_permissions));
$board3_menu_array = serialize($links);
set_portal_config('board3_menu_array_' . $module_id, $board3_menu_array);
break;
@@ -438,18 +427,18 @@ class portal_main_menu_module
case 'edit':
case 'add':
$template->assign_vars(array(
'LINK_TITLE' => (isset($links_titles[$link_id]) && $action != 'add') ? $links_titles[$link_id] : '',
'LINK_URL' => (isset($links_urls[$link_id]) && $links_options[$link_id] != B3_LINKS_CAT && $action != 'add') ? str_replace('&', '&amp;', $links_urls[$link_id]) : '',
'LINK_TITLE' => (isset($links[$link_id]['title']) && $action != 'add') ? $links[$link_id]['title'] : '',
'LINK_URL' => (isset($links[$link_id]['url']) && $links[$link_id]['type'] != B3_LINKS_CAT && $action != 'add') ? str_replace('&', '&amp;', $links[$link_id]['url']) : '',
//'U_BACK' => $u_action,
'U_ACTION' => $u_action . '&amp;id=' . $link_id,
'S_EDIT' => true,
'S_LINK_IS_CAT' => (!isset($links_options[$link_id]) || $links_options[$link_id] == B3_LINKS_CAT) ? true : false,
'S_LINK_IS_INT' => (isset($links_options[$link_id]) && $links_options[$link_id] == B3_LINKS_INT) ? true : false,
'S_LINK_IS_CAT' => (!isset($links[$link_id]['type']) || $links[$link_id]['type'] == B3_LINKS_CAT) ? true : false,
'S_LINK_IS_INT' => (isset($links[$link_id]['type']) && $links[$link_id]['type'] == B3_LINKS_INT) ? true : false,
));
$groups_ary = explode(',', $links_permissions[$link_id]);
$groups_ary = (isset($links[$link_id]['permission'])) ? explode(',', $links[$link_id]['permission']) : array();
// get group info from database and assign the block vars
$sql = 'SELECT group_id, group_name
@@ -471,18 +460,18 @@ class portal_main_menu_module
break;
}
for ($i = 0; $i < sizeof($links_urls); $i++)
for ($i = 0; $i < sizeof($links); $i++)
{
$template->assign_block_vars('links', array(
'LINK_TITLE' => ($action != 'add') ? $links_titles[$i] : '',
'LINK_URL' => ($action != 'add') ? str_replace('&', '&amp;', $links_urls[$i]) : '',
'LINK_TITLE' => ($action != 'add') ? $links[$i]['title'] : '',
'LINK_URL' => ($action != 'add') ? str_replace('&', '&amp;', $links[$i]['url']) : '',
'U_EDIT' => $u_action . '&amp;action=edit&amp;id=' . $i,
'U_DELETE' => $u_action . '&amp;action=delete&amp;id=' . $i,
'U_MOVE_UP' => $u_action . '&amp;action=move_up&amp;id=' . $i,
'U_MOVE_DOWN' => $u_action . '&amp;action=move_down&amp;id=' . $i,
'S_LINK_IS_CAT' => ($links_options[$i] == B3_LINKS_CAT) ? true : false,
'S_LINK_IS_CAT' => ($links[$i]['type'] == B3_LINKS_CAT) ? true : false,
));
}
}
@@ -491,6 +480,13 @@ class portal_main_menu_module
{
$this->manage_links('', $key, $module_id);
}
// Unserialize links array
function utf_unserialize($serial_str)
{
$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
return unserialize($out);
}
}
?>