<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: cplink.php,v $ - $Revision: 1.16 $
// | $Date: 2004/08/03 04:28:44 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
cp_header(' &raquo; Quick Links');
cp_nav('cplinkmodify');

// ############################################################################
// Set the default cmd
default_var($cmd, 'modify');

// ############################################################################
// Remove quick link
if ($_POST['cmd'] == 'kill') {
	$cplink = getinfo('cplink', $cplinkid);

	$DB_site->query("
		DELETE FROM hive_cplink
		WHERE cplinkid = $cplinkid
	");

	adminlog($cplinkid, true);
	cp_redirect('The link has been removed.', 'cplink.php');
}

// ############################################################################
// Remove quick link
if ($cmd == 'remove') {
	$cplink = getinfo('cplink', $cplinkid);

	adminlog($cplinkid);
	startform('cplink.php', 'kill', 'Are you sure you want to remove this link?');
	starttable('Remove Quick Link "'.$cplink['title'].'" (ID: '.$cplinkid.')', '450');
	textrow('Are you <b>sure</b> you want to remove this link? This procedure <b>cannot</b> be reversed!');
	hiddenfield('cplinkid', $cplinkid);
	endform('Remove Quick Link', '', 'Go Back');
	endtable();
}

// ############################################################################
// Create a new cplink or update an existing one
if ($_POST['cmd'] == 'update') {
	if (empty($cplink['title'])) {
		adminlog($cplinkid, false);
		cp_error('The link must have a title.');
	} else {
		unset($cplink['sep']);
		if ($cplinkid == 0) {
			$DB_site->auto_query('cplink', $cplink);
			adminlog($cplinkid, true);
			cp_redirect('The link has been created.', 'cplink.php');
		} else {
			$DB_site->auto_query('cplink', $cplink, "cplinkid = $cplinkid");
			adminlog($cplinkid, true);
			cp_redirect('The link has been updated.', 'cplink.php');
		}
	}
}

// ############################################################################
// Create a new cplink or update an existing one
if ($cmd == 'edit') {
	startform('cplink.php', 'update', '', array('cplink_title' => 'title'));

	$cplink = getinfo('cplink', $cplinkid, false, false);
	adminlog($cplinkid);
	if ($cplink === false) {
		$cplink = array(
			'display' => $DB_site->get_field('SELECT MAX(display) FROM hive_cplink') + 1,
		);
		$cplinkid = 0;
		starttable('Create New Quick Link', '450');
	} else {
		starttable('Update Quick Link "'.$cplink['title'].'" (ID: '.$cplinkid.')', '450');
	}

	hiddenfield('cplinkid', $cplinkid);
	inputfield('Title:', 'cplink[title]', $cplink['title']);
	inputfield('Link to:', 'cplink[url]', $cplink['url']);
	radiofield('Open link in new window:', 'cplink[newwin]', $cplink['newwin']);
	inputfield('Display order:', 'cplink[display]', $cplink['display'], 4);

	if ($cplinkid == 0) {
		endform('Create Quick Link');
	} else {
		endform('Update Quick Link');
	}
	endtable();
}

// ############################################################################
// List the cplinks
if ($cmd == 'modify') {
	adminlog();

	$links = $DB_site->query('
		SELECT *
		FROM hive_cplink
		ORDER BY display
	');
	if ($DB_site->num_rows($links) > 0) {
		startform('cplink.php', 'display');
	}
	$cells = array(
		'Title',
		'Options'
	);
	if ($DB_site->num_rows($links) < 1) {
		starttable('Error', '450');
		tablehead($cells);
		textrow('No links found', count($cells), 1);
		emptyrow(count($cells));
	} else {
		starttable('Existing Quick Links', '450');
		tablehead($cells);
		while ($link = $DB_site->fetch_array($links)) {
			$cells = array(
				'<input type="text" name="displays['.$link['cplinkid'].']" value="'.$link['display'].'" class="bginput" size="2" /> '."<a href=\"$link[url]\" target=\"_blank\">$link[title]</a>",
				makelink('edit', "cplink.php?cmd=edit&cplinkid=$link[cplinkid]") . '-' . makelink('remove', "cplink.php?cmd=remove&cplinkid=$link[cplinkid]"),
			);
			tablerow($cells);
		}
		endform('Update Link Orders', '', '', '', count($cells));
	}
	endtable();

	$cplink = array(
		'display' => $DB_site->get_field('SELECT MAX(display) FROM hive_cplink') + 1,
	);
	startform('cplink.php', 'update', '', array('cplink_title' => 'title'));
	starttable('Create New Quick Link', '450');
	hiddenfield('cplinkid', $cplinkid);
	inputfield('Title:', 'cplink[title]', $cplink['title']);
	inputfield('Link to:', 'cplink[url]', $cplink['url']);
	radiofield('Open link in new window:', 'cplink[newwin]', $cplink['newwin']);
	inputfield('Display order:', 'cplink[display]', $cplink['display'], 4);
	endform('Create Quick Link');
	endtable();
}

// ############################################################################
// Update display orders
if ($_POST['cmd'] == 'display') {
	if (!is_array($displays)) {
		adminlog(0, false);
		cp_error('Invalid information specified.');
	} else {
		foreach ($displays as $cplinkid => $display) {
			$DB_site->query('
				UPDATE hive_cplink
				SET display = '.intval($display).'
				WHERE cplinkid = '.intval($cplinkid).'
			');	
			adminlog($cplinkid, true);
		}
		cp_redirect('The links have been updated.', 'cplink.php');
	}
}

cp_footer();
?>