Corrected Admin_Links Page

A mod witch will add a new links page to your froum.
fully ACP insterface, from where you may add/delete and update link categorys/sub categorys and links.

Moderator: Moderators

Forum rules
The content in this forum is dated Dec. 21 2005 and can be used as Archive only. This Forum is LOCKED and READ ONLY !

Corrected Admin_Links Page

Postby SUFanatic on Tue 10. Aug, 2004 19:43

I was having an issue with the links mod. Everytime that I added links then started arranging them, the links would all merge together.

For example...

1)Link 1
2)Link 2
3)Link 3

would all become

1)Link 1
2)Link 1
3)Link 1

I played around with the Admin_Links page and came up with this and haven't had a problem since then.

<?php
/***************************************************************************
* admin_links.php
* -------------------
* begin : Sep 17, 2003
* copyright : (C) 2003 Niels Chr. Rød
* email : ncr@db9.dk
* ver : 0.9.5
*
*
***************************************************************************/

/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/

define('IN_PHPBB', 1);

if( !empty($setmodules) )
{
$file = basename(__FILE__);
$module['Links']['Manage'] = $file;
return;
}

//
// Load default header
//
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);
//
include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_links.' . $phpEx);


//
// Mode setting
//
$mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] :
( (isset($HTTP_GET_VARS['mode']) ) ? $HTTP_GET_VARS['mode'] : '');

$cat = ( isset($HTTP_POST_VARS['cat']) ) ? intval($HTTP_POST_VARS['cat']) :
((isset($HTTP_GET_VARS['cat'])) ? intval($HTTP_GET_VARS['cat']) : 0);
$sub = ( isset($HTTP_POST_VARS['sub']) ) ? intval($HTTP_POST_VARS['sub']) :
((isset($HTTP_GET_VARS['sub'])) ? intval($HTTP_GET_VARS['sub']) : 0);
$id = ( isset($HTTP_POST_VARS['id']) ) ? intval($HTTP_POST_VARS['id']) :
((isset($HTTP_GET_VARS['id'])) ? intval($HTTP_GET_VARS['id']) : 0);

if ($mode=='delete')
{
if ($sub==0 && $id==0)
{
//delete a complate cat
$sql = "DELETE FROM ".LINKS_TABLE." WHERE link_cat=$cat";
} else
if ($id==0)
{
//delete a sub cat
$sql = "DELETE FROM ".LINKS_TABLE." WHERE link_cat=$cat AND link_sub=$sub";
} else
{
//delete a link
$sql = "DELETE FROM ".LINKS_TABLE." WHERE link_cat=$cat AND link_sub=$sub AND link_id=$id";
}
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't delete links ", "", __LINE__, __FILE__, $sql);
}
} else
if ($mode=="up" || $mode=="down")
{
if ($sub==0 && $id==0)
{
//move a complate cat
$update_field = "cat";
$where_sql= "link_cat=";
} else
if ($id==0)
{
//move a sub cat
$update_field = "sub";
$where_sql= "link_cat=$cat AND link_sub=";
} else
{
//move a link up
$update_field = "id";
$where_sql= "link_cat=$cat AND link_sub=$sub AND link_id=";
}
if ($mode=='up')
{
$dir="-1";
if ($$update_field==1)
{
message_die(GENERAL_MESSAGE, "Can't move that");
}
} else
{
$dir="+1";
}
$sql_1 = "UPDATE ".LINKS_TABLE." SET link_$update_field='-1' WHERE $where_sql $$update_field $dir";
eval("\$sql_1=\"$sql_1\";");
$sql_2 = "UPDATE ".LINKS_TABLE." SET link_$update_field=link_$update_field $dir WHERE $where_sql $$update_field";
eval("\$sql_2=\"$sql_2\";");
$sql_3 = "UPDATE ".LINKS_TABLE." SET link_$update_field=$$update_field WHERE $where_sql '-1'";
eval("\$sql_3=\"$sql_3\";");
if( !$result = $db->sql_query($sql_1) )
{
message_die(GENERAL_ERROR, "Couldn't move $update_field step 1 ", "", __LINE__, __FILE__, $sql_1);
}
if( !$result = $db->sql_query($sql_2) )
{
message_die(GENERAL_ERROR, "Couldn't move $update_field step 2 ", "", __LINE__, __FILE__, $sql_2);
}
if( !$result = $db->sql_query($sql_3) )
{
message_die(GENERAL_ERROR, "Couldn't move $update_field step 3 ", "", __LINE__, __FILE__, $sql_3);
}
} else
if ($mode=='edit_submit')
{
$cat_old = ( isset($HTTP_POST_VARS['cat_old']) ) ? intval($HTTP_POST_VARS['cat_old']) :
((isset($HTTP_GET_VARS['cat_old'])) ? intval($HTTP_GET_VARS['cat_old']) : 0);
$sub_old = ( isset($HTTP_POST_VARS['sub_old']) ) ? intval($HTTP_POST_VARS['sub_old']) :
((isset($HTTP_GET_VARS['sub_old'])) ? intval($HTTP_GET_VARS['sub_old']) : 0);
$id_old = ( isset($HTTP_POST_VARS['id_old']) ) ? intval($HTTP_POST_VARS['id_old']) :
((isset($HTTP_GET_VARS['id_old'])) ? intval($HTTP_GET_VARS['id_old']) : 0);


$cat_sub = (( isset($HTTP_POST_VARS['cat_sub']) ) ? $HTTP_POST_VARS['cat_sub'] :
((isset($HTTP_GET_VARS['cat_sub'])) ? $HTTP_GET_VARS['cat_sub'] : '0,0'));
list($cat,$sub) = explode(",",$cat_sub);

$cat_sub_sql = (isset($HTTP_POST_VARS['cat_sub']) || isset($HTTP_GET_VARS['cat_sub'])) ? ", link_cat='$cat', link_sub='$sub'" : "";

$link_name = ( isset($HTTP_POST_VARS['link_name']) ) ? $HTTP_POST_VARS['link_name'] :
((isset($HTTP_GET_VARS['link_name'])) ? $HTTP_GET_VARS['link_name'] : 0);

$link_level = ( isset($HTTP_POST_VARS['link_level']) ) ? intval($HTTP_POST_VARS['link_level']) :
((isset($HTTP_GET_VARS['link_level'])) ? intval($HTTP_GET_VARS['link_level']) : 0);
$link_level_type = ( isset($HTTP_POST_VARS['link_level_type']) ) ? intval($HTTP_POST_VARS['link_level_type']) :
((isset($HTTP_GET_VARS['link_level_type'])) ? intval($HTTP_GET_VARS['link_level_type']) : 0);
$level_sql = (isset($HTTP_POST_VARS['link_level']) || isset($HTTP_GET_VARS['link_level'])) ?
", link_level='$link_level', link_level_type='$link_level_type'" : "";

$active_sql = (isset($HTTP_POST_VARS['link_active'])) ? "link_active='".(( $HTTP_POST_VARS['link_active'] == 1 ) ? TRUE : 0)."'," : "";

$link_url = ( isset($HTTP_POST_VARS['link_url']) ) ? $HTTP_POST_VARS['link_url'] :
((isset($HTTP_GET_VARS['link_url'])) ? $HTTP_GET_VARS['link_url'] : '');
$url_sql = ( isset($HTTP_POST_VARS['link_url']) || isset($HTTP_GET_VARS['link_url'])) ? ", link_url='".addslashes($link_url)."'" : "";


$sql="UPDATE ".LINKS_TABLE." SET $active_sql
link_name='".addslashes($link_name)."' $url_sql $level_sql $cat_sub_sql
WHERE link_cat='$cat_old' AND link_sub='$sub_old' AND link_id='$id_old'";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't update cat/sub/link", "", __LINE__, __FILE__, $sql);
}
// if this was a sub cat, then move links if needed
if ($cat!=$cat_old && !$id_old)
{
$sql="UPDATE ".LINKS_TABLE." SET link_cat='$cat', link_sub='$sub' WHERE link_cat='$cat_old' AND link_sub='$sub_old'";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't move links to new cat", "", __LINE__, __FILE__, $sql);
}
}
} else
if ($mode=='new_link')
{
$link_name = ( isset($HTTP_POST_VARS['link_name']) ) ? $HTTP_POST_VARS['link_name'] :
((isset($HTTP_GET_VARS['link_name'])) ? $HTTP_GET_VARS['link_name'] : 0);
$sql="SELECT MAX(link_cat) as cat, MAX(link_sub) as sub, max(link_id) as id FROM ".LINKS_TABLE." WHERE link_cat>0 AND link_sub>0 GROUP BY link_cat ORDER BY link_cat DESC LIMIT 1";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't fetch cat/sub/link count info", "", __LINE__, __FILE__, $sql);
}
if ($db->sql_numrows())
{
$count_info = $db->sql_fetchrow($result);
} else
{
message_die(GENERAL_ERROR, "It seams, that you are trying to add a link when no sub category exist, please add one as the first");
}

$sql = "INSERT INTO ". LINKS_TABLE ." (link_active, link_level, link_level_type, link_name, link_cat, link_sub, link_id) VALUES ('1','-1','2','".addslashes($link_name)."' ,".intval($count_info['cat']).",".intval($count_info['sub']).",".intval($count_info['id']+1).")";

if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't insert new link", "", __LINE__, __FILE__, $sql);
}
} else
if ($mode=='new_sub')
{
$link_name = ( isset($HTTP_POST_VARS['link_name']) ) ? $HTTP_POST_VARS['link_name'] :
((isset($HTTP_GET_VARS['link_name'])) ? $HTTP_GET_VARS['link_name'] : 0);
$sql="SELECT MAX(link_cat) as cat, MAX(link_sub) as sub, max(link_id) as id FROM ".LINKS_TABLE." GROUP BY link_cat ORDER BY link_cat DESC LIMIT 1";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't fetch cat/sub/link count info", "", __LINE__, __FILE__, $sql);
}

if ($db->sql_numrows())
{
$count_info = $db->sql_fetchrow($result);
} else
{
message_die(GENERAL_ERROR, "It seams, that you are trying to add a sub category when no category exist, please add one as the first");
}
$sql = "INSERT INTO ". LINKS_TABLE ." (link_active, link_level, link_level_type, link_name, link_cat, link_sub, link_id) VALUES ('1','-1','2','".addslashes($link_name)."' ,".$count_info['cat'].",".($count_info['sub']+1).",'0')";

if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't insert new sub", "", __LINE__, __FILE__, $sql);
}
} else
if ($mode=='new_cat')
{
$link_name = ( isset($HTTP_POST_VARS['link_name']) ) ? $HTTP_POST_VARS['link_name'] :
((isset($HTTP_GET_VARS['link_name'])) ? $HTTP_GET_VARS['link_name'] : 0);
$sql="SELECT MAX(link_cat) as cat, MAX(link_sub) as sub, max(link_id) as id FROM ".LINKS_TABLE." GROUP BY link_cat ORDER BY link_cat DESC LIMIT 1";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't fetch cat/sub/link count info", "", __LINE__, __FILE__, $sql);
}
$count_info = $db->sql_fetchrow($result);

$sql = "INSERT INTO ". LINKS_TABLE ." (link_active, link_level, link_level_type, link_name, link_cat, link_sub, link_id) VALUES ('1','-1','2','".addslashes($link_name)."' ,".($count_info['cat']+1).",'0','0')";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't insert new cat", "", __LINE__, __FILE__, $sql);
}
}




if ($mode=='edit')
{
// get the link
$sql="SELECT * FROM ".LINKS_TABLE." WHERE link_cat='$cat' AND link_sub='$sub' AND link_id='$id'";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't query links cat", "", __LINE__, __FILE__, $sql);
}
$link_info = $db->sql_fetchrow($result);
$sql="SELECT link_cat as cat, MAX(link_sub) as sub, max(link_id) as id FROM ".LINKS_TABLE." GROUP BY link_cat";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't fetch cat/sub/link count info", "", __LINE__, __FILE__, $sql);
}
while($temp = $db->sql_fetchrow($result))
{
$count_info[$temp['cat']] = $temp;
}

// get all cat/sub's
$sql="SELECT * FROM ".LINKS_TABLE." WHERE link_id=0 ORDER BY link_cat, link_sub";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, "Couldn't query links cat", "", __LINE__, __FILE__, $sql);
}
$cat_select = '<select name="cat_sub">';
while ( $row = $db->sql_fetchrow($result) )
{
if ($row['link_sub']==0 && $row['link_name']!=$cat_name)
{
$cat_name = $row['link_name'];
if (!$link_info['link_id'])
{
$selected = ( $row['link_cat'] == $cat ) ? ' selected="selected">* ' : '>';
$sub_value = ($row['link_cat']==$link_info['link_cat']) ? $link_info['link_sub'] : ($count_info[$row['link_cat']]['sub']+1);
$cat_select .= '<option value="' . $row['link_cat'].',' .$sub_value .'"' . $selected . $cat_name . '</option>';
}
} else
{
if ($link_info['link_id'])
{
$selected = ( $row['link_cat'].','.$row['link_sub'] == $cat.','.$sub ) ? ' selected="selected">* ' : '>';
$cat_select .= '<option value="' . $row['link_cat'].',' .$row['link_sub']. '"' . $selected . $cat_name .' - '.$row['link_name'] . '</option>';
}
}
}
$cat_select .= "</select>";

$n='-1';
$s_level='<select name="link_level">';
while ( $lang['Link_level'][$n] )
{
$s_level.= ( $link_info['link_level']==$n ) ?
'<option value="'.$n.'" SELECTED>* '.$lang['Link_level'][$n] :
'<option value="'.$n.'" >'.$lang['Link_level'][$n];
$n++;
}
$s_level .='</select>';
$n=0;
$s_level_type = '<select name="link_level_type">';
while ( $lang['Link_level_type'][$n] )
{
$s_level_type.= ( $link_info['link_level_type']==$n ) ?
'<option value="'.$n.'" SELECTED>* '.$lang['Link_level_type'][$n] :
'<option value="'.$n.'" >'.$lang['Link_level_type'][$n];
$n++;
}
$s_level_type .='</select>';
$link_is_active = ( $link_info['link_active'] ) ? "checked=\"checked\"" : "";
$link_is_not_active = ( !$link_info['link_active'] ) ? "checked=\"checked\"" : "";

$hidden_fields = '<input type="hidden" name="mode" value="edit_submit" />';
$hidden_fields .= '<input type="hidden" name="cat_old" value="' . $cat . '" />';
$hidden_fields .= '<input type="hidden" name="sub_old" value="' . $sub . '" />';
$hidden_fields .= '<input type="hidden" name="id_old" value="' . $id . '" />';
$template->set_filenames(array(
'body' => 'admin/link_edit_body.tpl'));
// enable extra if sub or link
if ($link_info['link_sub'])
{
$template->assign_block_vars('switch_limit_2', array());
// enable extra if link
if ($link_info['link_id'])
{
$template->assign_block_vars('switch_limit', array());
}
}


$template->assign_vars(array(
'L_LINK_TITLE' => $lang['Edit'],
'L_LINK_EXPLAIN' => ($link_info['link_id']==0) ? (($link_info['link_sub']==0) ? $lang['Link_edit_cat_explain'] : $lang['Link_edit_sub_explain'] ) : $lang['Link_edit_explain'],
'L_LINK_NAME' => $lang['Link_name'],
'L_LINK_URL' => $lang['Link_url'],
'L_LINK_URL_EXPLAIN' => $lang['Link_url_explain'],
'L_SUBMIT' => $lang['Submit'],
'L_RESET' => $lang['Reset'],
'L_YES' => $lang['Yes'],
'L_NO' => $lang['No'],
'L_CATEGORY' => $lang['Link_category'],

'L_LINK_ACTIVATE' => $lang['Link_activate'],
'LINK_NOT_ACTIVE' => $link_is_not_active,
'LINK_ACTIVE' => $link_is_active,
'LINK_URL' => $link_info['link_url'],
'LINK_NAME' => $link_info['link_name'],

'S_CAT_LIST' => $cat_select,
'S_HIDDEN_FIELDS' => $hidden_fields,
'L_LINK_SHOW_TO' => $lang['Show_to_users'],
'L_LINK_SHOW_TO_EXPLAIN' => $lang['Show_to_users_explain'],
'S_LINK_SHOW_TO' => sprintf($lang['Show_to_users_select'],$s_level_type,$s_level)
));

} else
{
$template->set_filenames(array(
'body' => 'admin/links_body.tpl'));

$template->assign_vars(array(
'L_NEW_CAT' => $lang['Link_add_cat'],
'L_NEW_SUB' => $lang['Link_add_sub'],
'L_NEW_LINK' => $lang['Link_add_link'],
'L_SUBMIT' => $lang['Submit'],

'L_LINKS' => $lang['Links'],
'L_EDIT' => $lang['Edit'],
'L_DELETE' => $lang['Delete'],
'L_DOWN' => $lang['Move_down'],
'L_UP' => $lang['Move_up'],
'S_NEW_LINK' => append_sid("admin_links.$phpEx?mode=new_link&amp;$identifyer"),
'S_NEW_SUB' => append_sid("admin_links.$phpEx?mode=new_sub&amp;$identifyer"),
'S_NEW_CAT' => append_sid("admin_links.$phpEx?mode=new_cat&amp;$identifyer")
));
$sql = "SELECT * FROM ".LINKS_TABLE." ORDER BY link_cat , link_sub, link_id";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not get shoutbox information', '', __LINE__, __FILE__, $sql);
}
$links = $db->sql_fetchrowset($result);
$i=0;
if (!empty($links))
{
while (list ($id,$link) = each($links))
{
if ($link['link_id']==0)
{
if ($link['link_sub']==0)
{
// new cat
$template->assign_block_vars('row', array());
$identifyer = "id=".$link['link_id']."&cat=".$link['link_cat']."&sub=".$link['link_sub'];
$template->assign_block_vars('row.cat', array(
'CAT_NAME' => $link['link_name'],
'DELETE' => append_sid("admin_links.$phpEx?mode=delete&amp;$identifyer"),
'EDIT' => append_sid("admin_links.$phpEx?mode=edit&amp;$identifyer"),
'UP' => append_sid("admin_links.$phpEx?mode=up&amp;$identifyer"),
'DOWN' => append_sid("admin_links.$phpEx?mode=down&amp;$identifyer")));
} else
{
$identifyer = "id=".$link['link_id']."&cat=".$link['link_cat']."&sub=".$link['link_sub'];
$template->assign_block_vars('row.cat.sub', array(
'SUB_NAME' => $link['link_name'],
'DELETE' => append_sid("admin_links.$phpEx?mode=delete&amp;$identifyer"),
'EDIT' => append_sid("admin_links.$phpEx?mode=edit&amp;$identifyer"),
'UP' => append_sid("admin_links.$phpEx?mode=up&amp;$identifyer"),
'DOWN' => append_sid("admin_links.$phpEx?mode=down&amp;$identifyer")));
}
} else
{
$l_id++;
$identifyer = "id=".$link['link_id']."&cat=".$link['link_cat']."&sub=".$link['link_sub'];
$row_color = ( !($l_id % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
$row_class = ( !($l_id % 2) ) ? $theme['td_class1'] : $theme['td_class2'];
$template->assign_block_vars('row.cat.sub.link', array(
'LINK_NAME' => $link['link_name'],
'LINK_URL' => $link['link_url'],
'DELETE' => append_sid("admin_links.$phpEx?mode=delete&amp;$identifyer"),
'EDIT' => append_sid("admin_links.$phpEx?mode=edit&amp;$identifyer"),
'UP' => append_sid("admin_links.$phpEx?mode=up&amp;$identifyer"),
'DOWN' => append_sid("admin_links.$phpEx?mode=down&amp;$identifyer"),
'ROW_COLOR' => '#' . $row_color,
'ROW_CLASS' => $row_class));
}
}
}
}
$template->pparse('body');
include('./page_footer_admin.'.$phpEx);

?>
SUFanatic
Poster
Poster
 
Posts: 1
Joined: Tue 10. Aug, 2004 19:35

Postby darakhshan on Sat 12. Feb, 2005 18:12

I get error

what exactly have you changed?
darakhshan
Poster
Poster
 
Posts: 46
Joined: Sat 15. May, 2004 12:19


Return to Links [2.0.6/EM]

Who is online

Users browsing this forum: No registered users and 1 guest

cron