Просмотр файла admin/module_links.php

Размер файла: 10.6Kb
<?php

define('IN_PHPBB', 1);

if( !empty($setmodules) )
{
	$filename = basename(__FILE__);
	$module['Modules']['Ссылки, счётчики'] = $filename;

	return;
}

$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);


$sql = "SELECT * FROM " . LINKS_TABLE . " ORDER BY link_text";
if( !$result = $db->sql_query($sql) )
{
	message_die(GENERAL_ERROR, "Couldn't obtain link data from database", "", __LINE__, __FILE__, $sql);
}
$links_data = $db->sql_fetchrowset($result);

$sql = "SELECT * FROM " . LINKS_SETS_TABLE . " ORDER BY set_id";
if( !$result = $db->sql_query($sql) )
{
	message_die(GENERAL_ERROR, "Couldn't obtain link set data from database", "", __LINE__, __FILE__, $sql);
}
$links_set_data = $db->sql_fetchrowset($result);

if( isset($HTTP_POST_VARS['mode']) || isset($HTTP_GET_VARS['mode']) )
{
	$mode = ( isset($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
}
else
{
	$mode = "";
}

if ( isset($HTTP_POST_VARS['id']) )
{
	$id = intval($HTTP_POST_VARS['id']);
}
else if ( isset($HTTP_GET_VARS['id']) )
{
	$id = intval($HTTP_GET_VARS['id']);
}

switch( $mode )
{	
	case 'addlink':
		
		$template->set_filenames(array(
			"body" => "admin/links_edit_body.tpl")
		);		

		$s_hidden_fields = '<input type="hidden" name="mode" value="savenewlink" /><input type="hidden" name="id" value="' . $id . '" />';

		$template->assign_vars(array(			
			"L_SUBMIT" => $lang['Submit'],
			"L_RESET" => $lang['Reset'],
			"URL" => $links_data['link_url'],
			"S_LINKS_ACTION" => append_sid("module_links.$phpEx"), 
			"S_HIDDEN_FIELDS" => $s_hidden_fields, 
			"S_FILENAME_OPTIONS" => $filename_list)
		);

		$template->pparse("body");

		break;

	/*case 'addset':
		
		$template->set_filenames(array(
			"body" => "admin/links_set_edit_body.tpl")
		);

		$s_hidden_fields = '<input type="hidden" name="mode" value="savenewset" />';

		$template->assign_vars(array(			
			"L_SUBMIT" => $lang['Submit'],
			"L_RESET" => $lang['Reset'],
			"LINKS_SET" => '',
			"S_LINKS_SET_ACTION" => append_sid("module_links.$phpEx"),
			"S_HIDDEN_FIELDS" => $s_hidden_fields)
		);

		$template->pparse("body");

		break;*/
	case 'deletelink':
		
		$link_id = $id;

		$sql = "DELETE FROM " . LINKS_TABLE . "
			WHERE link_id = " . $link_id . "";
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't delete link", "", __LINE__, __FILE__, $sql);
		}

		message_die(GENERAL_MESSAGE, "Ссылка удалена!<br /><br /><a href=\"" . append_sid("module_links.$phpEx") . "\">Вернуться</a>");

		break;
	/*case 'deleteset':
		
		$set_id = $id;

		$sql = "DELETE FROM " . LINKS_TABLE . "
			WHERE link_set = $set_id";
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't delete links under this link set", "", __LINE__, __FILE__, $sql);
		}

		$sql = "DELETE FROM " . LINKS_SETS_TABLE . "
			WHERE set_id = $set_id";
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't delete links under this link set", "", __LINE__, __FILE__, $sql);
		}

		message_die(GENERAL_MESSAGE, "Категория удалена!<br /><br /><a href=\"" . append_sid("module_links.$phpEx") . "\">Вернуться</a>");

		break;*/

	case 'editlink':
		
		$link_id = $id;

		$sql = "SELECT * FROM " . LINKS_TABLE . " WHERE link_id = " . $link_id;
		
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain link information', "", __LINE__, __FILE__, $sql);
		}
		$links_data = $db->sql_fetchrow($result);
		
		$template->set_filenames(array(
			"body" => "admin/links_edit_body.tpl")
		);
		
                $links_data['link_text'] = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $links_data['link_text']);
                
		$s_hidden_fields = '<input type="hidden" name="mode" value="savelink" /><input type="hidden" name="link_id" value="' . $links_data['link_id'] . '" />';

		$template->assign_vars(array(			
			"L_SUBMIT" => $lang['Submit'],
			"L_RESET" => $lang['Reset'],			
			"URL" => $links_data['link_url'],
			"NAME" => $links_data['link_text'],
			"S_LINKS_ACTION" => append_sid("module_links.$phpEx"),
			"S_HIDDEN_FIELDS" => $s_hidden_fields, 
			"S_FILENAME_OPTIONS" => $filename_list)
		);

		$template->pparse("body");

		break;

	case 'editset':
		
		$set_id = $id;

		$sql = "SELECT *
			FROM " . LINKS_SETS_TABLE . "
			WHERE set_id = " . $set_id;
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain link set information', "", __LINE__, __FILE__, $sql);
		}
		$links_set_data = $db->sql_fetchrow($result);

		$template->set_filenames(array(
			"body" => "admin/links_set_edit_body.tpl")
		);

		$s_hidden_fields = '<input type="hidden" name="mode" value="saveset" /><input type="hidden" name="id" value="' . $links_set_data['set_id'] . '" />';

		$template->assign_vars(array(			
			"L_SUBMIT" => $lang['Submit'],
			"L_RESET" => $lang['Reset'],
			"LINKS_SET" => $links_set_data['set_name'],
			"S_LINKS_SET_ACTION" => append_sid("module_links.$phpEx"),
			"S_HIDDEN_FIELDS" => $s_hidden_fields, 
			"S_FILENAME_OPTIONS" => $filename_list)
		);

		$template->pparse("body");

		break;

	case 'savelink':
		
		$link_url = ( isset($HTTP_POST_VARS['link_url']) ) ? trim($HTTP_POST_VARS['link_url']) : trim($HTTP_GET_VARS['link_url']);
		$link_text = ( isset($HTTP_POST_VARS['link_text']) ) ? trim($HTTP_POST_VARS['link_text']) : trim($HTTP_GET_VARS['link_text']);
		$link_id = ( isset($HTTP_POST_VARS['link_id']) ) ? intval($HTTP_POST_VARS['link_id']) : intval($HTTP_GET_VARS['link_id']);

		if ( $link_url == '')
		{
			message_die(MESSAGE, $lang['Fields_empty']);
		}

		$sql = "UPDATE " . LINKS_TABLE . "
			SET link_url = '" . str_replace("\'", "''", $link_url) . "', link_text = '" . str_replace("\'", "''", $link_text) . "'
			WHERE link_id = $link_id";
		
		if( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, "Couldn't update link info", "", __LINE__, __FILE__, $sql);
		}

		message_die(GENERAL_MESSAGE, "Изменения сохранены!<br /><br /><a href=\"" . append_sid("module_links.$phpEx") . "\">Вернуться</a>");

		break;

	case 'saveset':
		
		$set_name = ( isset($HTTP_POST_VARS['set_name']) ) ? trim($HTTP_POST_VARS['set_name']) : trim($HTTP_GET_VARS['set_name']);
		$set_id = ( isset($HTTP_POST_VARS['set_id']) ) ? intval($HTTP_POST_VARS['set_id']) : intval($HTTP_GET_VARS['set_id']);

		// If no code was entered complain ...
		if ( $set_name == '')
		{
			message_die(MESSAGE, $lang['Fields_empty']);
		}

		$sql = "UPDATE " . LINKS_SETS_TABLE . "
			SET set_name = '" . str_replace("\'", "''", $set_name) . "'
			WHERE set_id = $set_id";
		if( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, "Couldn't update link set info", "", __LINE__, __FILE__, $sql);
		}

		message_die(GENERAL_MESSAGE, "Изменения сохранены!<br /><br /><a href=\"" . append_sid("module_links.$phpEx") . "\">Вернуться</a>");

		break;
	case 'savenewlink':
		
		$link_url = ( isset($HTTP_POST_VARS['link_url']) ) ? $HTTP_POST_VARS['link_url'] : $HTTP_GET_VARS['link_url'];
		$link_text = ( isset($HTTP_POST_VARS['link_text']) ) ? $HTTP_POST_VARS['link_text'] : $HTTP_GET_VARS['link_text'];
	
		if ($link_url == '')
		{
			message_die(MESSAGE, $lang['Fields_empty']);
		}

		
		$sql = "SELECT MAX(link_id) AS max_id FROM " . LINKS_TABLE;
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't find maximum links", "", __LINE__, __FILE__, $sql);
		}

		$max_link_id = 0;
		if ( $row = $db->sql_fetchrowset($result) )
		{
			$max_link_id = $row[0]['max_id'] + 1;
		}
		
		$sql = "INSERT INTO " . LINKS_TABLE . " (link_id, link_url, link_text, link_set)
			VALUES ($max_link_id, '$link_url', '$link_text', $id)";
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't insert new link", "", __LINE__, __FILE__, $sql);
		}

		message_die(GENERAL_MESSAGE, "Изменения сохранены!<br /><br /><a href=\"" . append_sid("module_links.$phpEx") . "\">Вернуться</a>");

		break;

	/*case 'savenewset':
		
		$set_name = ( isset($HTTP_POST_VARS['set_name']) ) ? $HTTP_POST_VARS['set_name'] : $HTTP_GET_VARS['set_name'];
		
		if ($set_name == '')
		{
			message_die(MESSAGE, $lang['Fields_empty']);
		}

		$sql = "SELECT MAX(set_id) AS max_id FROM " . LINKS_SETS_TABLE;
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't find maximum links id", "", __LINE__, __FILE__, $sql);
		}

		$max_set_id = 0;
		if ( $row = $db->sql_fetchrowset($result) )
		{
			$max_set_id = $row[0]['max_id'] + 1;
		}
		
		$sql = "INSERT INTO " . LINKS_SETS_TABLE . " (set_id, set_name)
			VALUES ($max_set_id, '$set_name')";
		if( !$result = $db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Couldn't insert new link set", "", __LINE__, __FILE__, $sql);
		}

		message_die(GENERAL_MESSAGE, "Изменения сохранены!<br /><br /><a href=\"" . append_sid("module_links.$phpEx") . "\">Вернуться</a>");

		break;*/

	default:
		
		$template->set_filenames(array(
			"body" => "admin/links_list_body.tpl")
		);

		$s_hidden_fields = '<input type="hidden" name="type" value="set" />';
	
		$template->assign_vars(array(
			"L_ACTION" => $lang['Action'],			
			"L_DELETE" => $lang['Delete'],
			"L_EDIT" => $lang['Edit'],					
			"S_HIDDEN_FIELDS" => $s_hidden_fields,
			"S_LINKS_ACTION" => append_sid("module_links.$phpEx?mode=addset"))
		);
	
		for ( $i = 0; $i < count($links_set_data); $i++)
		{						
			$row_class = ( !($i % 2) ) ? 'row_hard' : 'row_easy';	
			$template->assign_block_vars("cat", array(				
				"ROW_CLASS" => $row_class,	
				"SET_NAME" => $links_set_data[$i]['set_name'],	
				"U_ADD" => append_sid("module_links.$phpEx?mode=addlink&amp;id=" . $links_set_data[$i]['set_id']),
				"U_EDIT" => append_sid("module_links.$phpEx?mode=editset&amp;id=" . $links_set_data[$i]['set_id']), 
				"U_DELETE" => append_sid("module_links.$phpEx?mode=deleteset&amp;id=" . $links_set_data[$i]['set_id']))
			);
	
			for ($j = 0; $j < count($links_data); $j++)
			{									
				if ( $links_data[$j]['link_set'] == $links_set_data[$i]['set_id'] )
				{
					$row_class = ( !($j % 2) ) ? 'row_hard' : 'row_easy';
					$template->assign_block_vars("cat.url", array(						
						"ROW_CLASS" => $row_class,				
						"URL" =>  $links_data[$j]['link_url'], 
						"NAME" => $links_data[$j]['link_text'],				
						"U_EDIT" => append_sid("module_links.$phpEx?mode=editlink&amp;id=" . $links_data[$j]['link_id']), 
						"U_DELETE" => append_sid("module_links.$phpEx?mode=deletelink&amp;id=" . $links_data[$j]['link_id']))
					);
				}
			}
		}
		$template->pparse("body");
}

include('./page_footer_admin.'.$phpEx);

?>