<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 5.0.0 Beta 10
|| # ---------------------------------------------------------------- # ||
|| # Copyright 2000-2012 vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('CVS_REVISION', '$RCSfile$ - $Revision: 65106 $');

// #################### PRE-CACHE TEMPLATES AND DATA ######################
global $phrasegroups, $specialtemplates, $vbphrase, $vbulletin;
$phrasegroups = array();
$specialtemplates = array('bookmarksitecache');

// ########################## REQUIRE BACK-END ############################
require_once(dirname(__FILE__) . '/global.php');
require_once(DIR . '/includes/adminfunctions_bookmarksite.php');

// ############################# LOG ACTION ###############################

$vbulletin->input->clean_array_gpc('r', array('bookmarksiteid' => vB_Cleaner::TYPE_INT));

log_admin_action($vbulletin->GPC['bookmarksiteid'] != 0 ? "bookmark site id = " . $vbulletin->GPC['bookmarksiteid'] : '');

// ######################## CHECK ADMIN PERMISSIONS #######################
if (!can_administer('canadminsettings'))
{
	print_cp_no_permission();
}

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

print_cp_header($vbphrase['social_bookmarking_manager']);
$assertor = vB::getDbAssertor();

// default action
if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

// ########################################################################
// when we want to add a new site from the site list page we need change the action before the main 'socialbookmarks_setpost' handler
// we came here if somebody press the add button in the sitelist edit/save form
if (($_POST['do'] == 'socialbookmarks_setpost') AND $vbulletin->GPC['add'])
{
	$_POST['do'] = 'add';
}

// ########################################################################
// confirmed delete, we shall do the delete from the database
if ($_POST['do'] == 'kill')
{
	$vbulletin->input->clean_array_gpc('p', array('bookmarksiteid' => vB_Cleaner::TYPE_UINT));

	if ($vbulletin->GPC['bookmarksiteid'])
	{
		$bookmarksite = $assertor->assertQuery('vBForum:bookmarksite', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'bookmarksiteid' => $vbulletin->GPC['bookmarksiteid']));
		if ($bookmarksite AND $bookmarksite->valid())
		{
			$assertor->assertQuery('vBForum:bookmarksite', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, 'bookmarksiteid' => $vbulletin->GPC['bookmarksiteid']));

			// rebuild the cache
			build_bookmarksite_datastore();
			print_stop_message2('bookmark_site_deleted_successfully', 'bookmarksite');
		}
	}

	$_REQUEST['do'] = 'modify';
}

// ########################################################################
// delete handler - we want to delete one of the sites
if ($_REQUEST['do'] == 'delete')
{
	$vbulletin->input->clean_array_gpc('r', array('bookmarksiteid' => vB_Cleaner::TYPE_UINT));

	$bookmarksite = $assertor->assertQuery('vBForum:bookmarksite', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'bookmarksiteid' => $vbulletin->GPC['bookmarksiteid']));
	if ($vbulletin->GPC['bookmarksiteid'] AND $bookmarksite AND $bookmarksite->valid())
	{
		// display the delete confirmation page
		print_delete_confirmation('bookmarksite', $vbulletin->GPC['bookmarksiteid'], 'bookmarksite', 'kill');
	}
	else
	{
		$_REQUEST['do'] = 'modify';
	}
}

// ########################################################################
// update handler - we sent the site details form (add new or edit old one)
if ($_POST['do'] == 'update')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'bookmarksiteid' => vB_Cleaner::TYPE_UINT,
		'title' => vB_Cleaner::TYPE_NOHTML,
		'iconpath' => vB_Cleaner::TYPE_STR,
		'active' => vB_Cleaner::TYPE_BOOL,
		'displayorder' => vB_Cleaner::TYPE_UINT,
		'url' => vB_Cleaner::TYPE_STR,
		'utf8encode' => vB_Cleaner::TYPE_BOOL
	));

	$vbulletin->GPC['url'] = preg_replace('/&(?!(#[0-9]+|[a-z]+);)/U', '&amp;', $vbulletin->GPC['url']);

	if (!$vbulletin->GPC['title'] OR !$vbulletin->GPC['url'])
	{
		print_stop_message2('please_complete_required_fields');
	}

	$bookmarksite = $assertor->assertQuery('vBForum:bookmarksite', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'bookmarksiteid' => $vbulletin->GPC['bookmarksiteid']));
	if ($vbulletin->GPC['bookmarksiteid'] AND $bookmarksite AND $bookmarksite->valid())
	{
		$assertor->assertQuery('vBForum:bookmarksite',
			array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_UPDATE,
				'title' => $vbulletin->GPC['title'],
				'iconpath' => $vbulletin->GPC['iconpath'],
				'active' => $vbulletin->GPC['active'],
				'displayorder' => $vbulletin->GPC['displayorder'],
				'url' => $vbulletin->GPC['url'],
				'utf8encode' => $vbulletin->GPC['utf8encode'],
				vB_dB_Query::CONDITIONS_KEY => array(
					array('field' => 'bookmarksiteid','value' => $vbulletin->GPC['bookmarksiteid'], 'operator' => vB_dB_Query::OPERATOR_EQ)
				)
			)
		);
	}
	else
	{
		$res = $assertor->assertQuery('vBForum:bookmarksite',
			array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_INSERT,
				'title' => $vbulletin->GPC['title'],
				'iconpath' => $vbulletin->GPC['iconpath'],
				'active' => $vbulletin->GPC['active'],
				'displayorder' => $vbulletin->GPC['displayorder'],
				'url' => $vbulletin->GPC['url'],
				'utf8encode' => $vbulletin->GPC['utf8encode']
			)
		);
	}

	// rebuild the cache
	build_bookmarksite_datastore();
	print_stop_message2('bookmark_site_saved_successfully', 'bookmarksite');

	$_REQUEST['do'] = 'modify';
}

// ########################################################################
if ($_REQUEST['do'] == 'add' OR $_REQUEST['do'] == 'edit')
{
	$vbulletin->input->clean_array_gpc('r', array('bookmarksiteid' => vB_Cleaner::TYPE_UINT));

	print_form_header('bookmarksite', 'update');
	print_column_style_code(array('width:35%', 'width:65%'));

	$bookmarksite = $assertor->getRow('vBForum:bookmarksite', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'bookmarksiteid' => $vbulletin->GPC['bookmarksiteid']));
	if ($_REQUEST['do'] == 'edit' AND $bookmarksite)
	{
		// edit existing
		print_table_header($vbphrase['edit_social_bookmarking_site'] . " <span class=\"normal\">$bookmarksite[title]</span>");
		construct_hidden_code('bookmarksiteid', $bookmarksite['bookmarksiteid']);
	}
	else
	{
		// add new
		$bookmarksite = $assertor->getRow('vBForum:getMaxDisplayOrder', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_STORED));
		$bookmarksite['displayorder'] += 10;
		$bookmarksite['url'] = 'http://';
		$bookmarksite['active'] = true;
		$bookmarksite['utf8encode'] = false;

		print_table_header($vbphrase['add_new_social_bookmarking_site']);
	}

	print_input_row($vbphrase['title'], 'title', $bookmarksite['title'], false, 50);
	print_input_row($vbphrase['icon'] . '<dfn>' . $vbphrase['icon_bookmarksite_help'] . '</dfn>', 'iconpath', $bookmarksite['iconpath'], true, 50);
	print_input_row($vbphrase['link_gcpglobal'] . '<dfn>' . $vbphrase['link_replacement_variables_help'] . '</dfn>', 'url', $bookmarksite['url'], true, 50);
	print_input_row($vbphrase['display_order'], 'displayorder', $bookmarksite['displayorder'], true, 2);
	print_yes_no_row($vbphrase['active_gcpglobal'], 'active', $bookmarksite['active']);
	print_yes_no_row($vbphrase['utf8encode_title'] . '<dfn>' . $vbphrase['utf8encode_title_help'] . '</dfn>', 'utf8encode', $bookmarksite['utf8encode']);
	print_submit_row();
}

// #############################################################################
// quick update of active and display order fields
if ($_POST['do'] == 'quickupdate')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'active'            => vB_Cleaner::TYPE_ARRAY_BOOL,
		'displayorder'      => vB_Cleaner::TYPE_ARRAY_UINT,
		'displayorderswap'  => vB_Cleaner::CONVERT_KEYS
	));

	// handle display order and active fields for update
	$changes = $assertor->assertQuery('vBForum:quickUpdateBookmarkSites',
		array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_METHOD,
			'active' => $vbulletin->GPC['active'],
			'displayorder' => $vbulletin->GPC['displayorder']
		)
	);

	// handle swapping
	if (!empty($vbulletin->GPC['displayorderswap']))
	{
		list($orig_bookmarksiteid, $swap_direction) = explode(',', $vbulletin->GPC['displayorderswap'][0]);

		if (isset($vbulletin->GPC['displayorder']["$orig_bookmarksiteid"]))
		{
			$bookmarksite_orig = array(
				'bookmarksiteid' => $orig_bookmarksiteid,
				'displayorder' => $vbulletin->GPC['displayorder']["$orig_bookmarksiteid"]
			);

			$changes = $assertor->assertQuery('vBForum:quickUpdateBookmarkSitesSwap',
				array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_METHOD,
					'swap_direction' => $swap_direction,
					'bookmarksite_orig' => $bookmarksite_orig
				)
			);
		}
	}

	//update the datastore bookmarksite cache
	if ($changes)
	{
		build_bookmarksite_datastore();
	}

	$_REQUEST['do'] = 'modify';
}

// ########################################################################
// we want to display the bookmark list - this is the default action
if ($_REQUEST['do'] == 'modify')
{
	// display the form and table header
	print_form_header('bookmarksite', 'quickupdate');
	print_table_header($vbphrase['social_bookmarking_manager'], 3);

	$bookmarksites_results = $assertor->assertQuery('vBForum:bookmarksite',
		array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT),
		array('field' => array('displayorder', 'title'), 'direction' => array(vB_dB_Query::SORT_ASC, vB_dB_Query::SORT_ASC))
	);
	$bookmarksite_count = 0;

	if ($bookmarksites_results AND $bookmarksites_results->valid())
	{
		foreach ($bookmarksites_results AS $current)
		{
			$bookmarksite_count += 1;
		}
	}

	if ($bookmarksite_count)
	{
		print_description_row('<label><input type="checkbox" id="allbox" checked="checked" />' . $vbphrase['toggle_active_status_for_all'] . '</label><input type="image" value="" src="../' . $vbulletin->options['cleargifurl'] . '" name="normalsubmit" />', false, 3, 'thead checkbox-in-thead');
		print_column_style_code(array('width:20%; white-space:nowrap', 'width:60%', "width:20%; white-space:nowrap; text-align:" . vB_Template_Runtime::fetchStyleVar('right')));
		//while ($bookmarksite = $vbulletin->db->fetch_array($bookmarksites_result))
		foreach ($bookmarksites_results AS $bookmarksite)
		{
			print_cells_row(array(
				'<label class="smallfont"><input type="checkbox" name="active[' . $bookmarksite['bookmarksiteid'] . ']" value="1"' . ($bookmarksite['active'] ? ' checked="checked"' : '') . ' />' . $vbphrase['active_gcpglobal'] . '</label> &nbsp; ' .
				'<input type="image" src="' . $vbulletin->options['bburl'] . '/cpstyles/' . $vbulletin->options['cpstylefolder'] . '/move_down.gif" name="displayorderswap[' . $bookmarksite['bookmarksiteid'] . ',higher]" />' .
				'<input type="text" name="displayorder[' . $bookmarksite['bookmarksiteid'] . ']" value="' . $bookmarksite['displayorder'] . '" class="bginput" size="4" title="' . $vbphrase['display_order'] . '" style="text-align:' . vB_Template_Runtime::fetchStyleVar('right') . '" />' .
				'<input type="image" src="' . $vbulletin->options['bburl'] . '/cpstyles/' . $vbulletin->options['cpstylefolder'] . '/move_up.gif" name="displayorderswap[' . $bookmarksite['bookmarksiteid'] . ',lower]" />',

				'<a href="bookmarksite.php?' . vB::getCurrentSession()->get('sessionurl') . 'do=edit&amp;bookmarksiteid=' . $bookmarksite['bookmarksiteid'] . '" title="' . $vbphrase['edit'] . '">' . $bookmarksite['title'] . '</a>',

				construct_link_code($vbphrase['edit'], 'bookmarksite.php?' . vB::getCurrentSession()->get('sessionurl') . 'do=edit&amp;bookmarksiteid=' . $bookmarksite['bookmarksiteid']) .
				construct_link_code($vbphrase['delete'], 'bookmarksite.php?' . vB::getCurrentSession()->get('sessionurl') . 'do=delete&amp;bookmarksiteid=' . $bookmarksite['bookmarksiteid'])
			), false, '', -1);
		}
	}

	echo '<tr>
		<td class="tfoot">' .
		($bookmarksite_count ? '<input type="submit" class="button" accesskey="s" value="' . $vbphrase['save'] . '" /> <input type="reset" class="button" accesskey="r" value="' . $vbphrase['reset'] . '" />' : '&nbsp;') .
		'</td>
		<td class="tfoot" align="' . vB_Template_Runtime::fetchStyleVar('right') . '" colspan="2"><input type="button" class="button" value="' . $vbphrase['add_new_social_bookmarking_site'] . '" onclick="window.location=\'bookmarksite.php?' . vB::getCurrentSession()->get('sessionurl') . 'do=add\';" /></td>
	</tr>';
	print_table_footer();

	?>
	<script type="text/javascript">
	<!--
	function toggle_all_active(e)
	{
		for (var i = 0; i < this.form.elements.length; i++)
		{
			if (this.form.elements[i].type == "checkbox" && this.form.elements[i].name.substr(0, 6) == "active")
			{
				this.form.elements[i].checked = this.checked;
			}
		}
	}

	YAHOO.util.Event.on("allbox", "click", toggle_all_active);
	//-->
	</script>
	<?php
}

print_cp_footer();

/*======================================================================*\
|| ####################################################################
|| # 
|| # CVS: $RCSfile$ - $Revision: 65106 $
|| ####################################################################
\*======================================================================*/
?>
