<?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: plan.php,v $ - $Revision: 1.15 $
// | $Date: 2004/08/03 04:28:44 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
require_once('../includes/functions_subscription.php');
cp_header(' &raquo; Plans Manager');
cp_nav('subplans');

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

// ############################################################################
// Remove plan
if ($_POST['cmd'] == 'kill') {
	$plan = getinfo('plan', $planid);

	$subscriptions = $DB_site->query("
		SELECT *
		FROM hive_subscription
		WHERE planid = $planid
	");
	$newusergroups = array();
	while ($subscription = $DB_site->fetch_array($subscriptions)) {
		$newusergroups[subscription_getnew_usergroup(array('userid' => $subscription['userid']), $subscription)][] = $subscription['userid'];
	}
	foreach ($newusergroups as $usergroupid => $userids) {
		$DB_site->query("
			UPDATE hive_user
			SET usergroupid = $usergroupid, subexpiry = 0
			WHERE userid IN (".implode(', ', $userids).")
		");
	}

	$DB_site->query("
		DELETE FROM hive_plan
		WHERE planid = $planid
	");
	$DB_site->query("
		DELETE FROM hive_subscription
		WHERE planid = $planid
	");
	$DB_site->query("
		DELETE FROM hive_payment
		WHERE planid = $planid
	");

	adminlog($planid, true);
	cp_redirect('The plan was successfully removed.', 'plan.php');
}

// ############################################################################
// Remove plan
if ($cmd == 'remove') {
	$plan = getinfo('plan', $planid);

	adminlog($planid);
	startform('plan.php', 'kill', 'Are you sure you want to remove this plan?');
	starttable('Remove plan "'.$plan['name'].'" (ID: '.$planid.')', '450');
	textrow('Are you <b>sure</b> you want to remove this plan? This procedure <b>cannot</b> be reversed! Users that are currently subscribed to this plan will be returned to their original user group.');
	hiddenfield('planid', $planid);
	endform('Remove Plan', '', 'Go Back');
	endtable();
}

// ############################################################################
// Create a new plan or update an existing one
if ($_POST['cmd'] == 'update') {
	if (empty($plan['name'])) {
		adminlog($planid, false);
		cp_error('The plan must have a name.');
	} elseif (empty($plan['description'])) {
		adminlog($planid, false);
		cp_error('The plan must have a description.');
	} elseif (empty($plan['processors'])) {
		adminlog($planid, false);
		cp_error('You must choose at least one processor.');
	} elseif (!is_array($plan['fromusergroups'])) {
		adminlog($planid, false);
		cp_error('You must select at least one usergroup that the user needs to belong to in order to subscribe to this plan.');
	} elseif (in_array($plan['usergroupid'], $plan['fromusergroups'])) {
		adminlog($planid, false);
		cp_error('You cannot select the same user group for both the destination group and one of the source group(s).');
	} else {
		$plan['fromusergroups'] = implode(',', $plan['fromusergroups']).',0';
		$plan['processors'] = implode(',', $plan['processors']);
		if ($plan_length_nolimit) {
			$plan['length'] = 0;
		}
		if ($plan_triallength_nolimit) {
			$plan['triallength'] = 0;
		}
		if ($planid == 0) {
			$DB_site->auto_query('plan', $plan);
			adminlog($planid, true);
			cp_redirect('The plan has been created.', 'plan.php');
		} else {
			$DB_site->auto_query('plan', $plan, "planid = $planid");
			adminlog($planid, true);
			cp_redirect('The plan has been updated.', 'plan.php');
		}
	}
}

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

	$plan = getinfo('plan', $planid, false, false);
	adminlog($planid);
	if ($plan === false) {
		$plan = array(
			'fromusergroups' => array(),
			'usergroupid' => 2,
			'processors' => array(),
			'cost' => 0,
			'length' => 1,
			'unit' => 'm',
			'canpayadvance' => 1,
			'reminder' => 1,
		);
		$planid = 0;
		starttable('Create New Plan');
	} else {
		$plan['processors'] = explode(',', $plan['processors']);
		$plan['fromusergroups'] = explode(',', $plan['fromusergroups']);
		starttable('Update Plan "'.$plan['name'].'" (ID: '.$planid.')');
	}

	$lengthunits = array('d' => 'Day(s)', 'w' => 'Week(s)', 'm' => 'Month(s)', 'y' => 'Year(s)');
	$unit = '';
	$unit2 = '';
	foreach ($lengthunits as $unitvalue => $unitname) {
		$unit .= '<option value="'.$unitvalue.'"';
		$unit2 .= '<option value="'.$unitvalue.'"';
		if ($plan['unit'] == $unitvalue) {
			$unit .= ' selected="selected"';
		}
		if ($plan['triallengthunit'] == $unitvalue) {
			$unit2 .= ' selected="selected"';
		}
		$unit .= '>'.$unitname.'</option>';
		$unit2 .= '>'.$unitname.'</option>';
	}

	$processors = array();
	foreach ($_processor_info as $proccode => $procinfo) {
		$processors["$proccode"] = $procinfo['name'];
	}
	$listsize = iif(count($processors) < 3, 3, count($processors));

	hiddenfield('planid', $planid);
	inputfield('Name of plan:', 'plan[name]', $plan['name']);
	$checkboxes = '';
	$theclass = getclass();
	foreach($_usergroups as $usergid => $usergtitle) {
		$checkboxes .= "<input type=\"checkbox\" name=\"plan[fromusergroups][]\" class=\"radio_$theclass\" id=\"fromusergroups_$usergid\" value=\"$usergid\"";
		if (is_array($plan['fromusergroups']) and in_array($usergid, $plan['fromusergroups'])) {
			$checkboxes .= ' checked="checked"';
		}
		$checkboxes .= " /> <label for=\"fromusergroups_$usergid\">$usergtitle</label><br />\n";
	}
	echo "	<tr class=\"$theclass\">\n";
	echo "		<td valign=\"top\">From user group(s):<br /><span class=\"cp_small\">Choose the user group(s) that users must belong to in order to subscribe to this plan.</span></td>\n";
	echo "		<td valign=\"top\">$checkboxes</td>\n";
	echo "	</tr>\n";
	getclass();
	tableselect('To user group:<br /><span class="cp_small">Choose the user group that subscribers will be moved to after signing up for this plan.</span>', 'plan[usergroupid]', 'usergroup', $plan['usergroupid']);
	textarea('Description of plan:<br /><span class="cp_small">This is the description users will see when deciding which plan to sign up for, so you should include information about what users get when they sign up. Payment details do not have to be described here as they are automatically shown. HTML can be used in this field. Please don\'t forget to add &lt;br /&gt; tags where line breaks should appear!</span>', 'plan[description]', $plan['description']);
	selectbox('Payment processors that can be used:', 'plan[processors][]', $processors, $plan['processors'], false, '', $listsize, ' multiple="multiple" style="width: 150px"');
	inputfield('Remind users to pay:<br /><span class="cp_small">Only applicable if the payment is recurring.</span>', 'plan[reminder]', $plan['reminder'], '5', ' days before expiry');
	
	if ($planid == 0) {
		emptyrow(2);
		endtable();
	} else {
		tablehead(array('<input class="button" type="submit" value="  Update Plan  " />&nbsp;<input class="button" type="reset" value="  Reset Fields  " />'), 2);
		endtable();
	}

	starttable('Trial Settings');
	radiofield('Offer free trial:', 'plan[trial]', $plan['trial']);
	limitfield('Trial lasts:<br /><span class="cp_small">Select the length of time that the free trial should last.</span>', 'plan[triallength]', $plan['triallength'], '15', 0, true, 'No trial', " <select name=\"plan[triallengthunit]\">$unit2</select>");

	if ($planid == 0) {
		emptyrow(2);
		endtable();
	} else {
		tablehead(array('<input class="button" type="submit" value="  Update Plan  " />&nbsp;<input class="button" type="reset" value="  Reset Fields  " />'), 2);
		endtable();
	}

	starttable('Payment Settings');
	inputfield('Payment amount:<br /><span class="cp_small">The cost of the subscription, in U.S. Dollars.</span>', 'plan[cost]', sprintf('%.2f', $plan['cost']));
	limitfield('Recur payment every:<br /><span class="cp_small">Choose whether or not the payment for this plan should recur, and if so when.</span>', 'plan[length]', $plan['length'], '15', 0, true, 'One-time payment', " <select name=\"plan[unit]\">$unit</select>");
	radiofield('Can pay in advance:<br /><span class="cp_small">Allow users to pay for more than one period in advance? Only applicable if the payment is recurring.</span>', 'plan[canpayadvance]', $plan['canpayadvance']);

	if ($planid == 0) {
		endform('Create Plan');
	} else {
		endform('Update Plan', 'Reset Fields');
	}
	endtable();
}

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

	$plans = $DB_site->query('
		SELECT plan.*, usergroup.title, COUNT(subscription.subscriptionid) AS subscriptions
		FROM hive_plan AS plan
		LEFT JOIN hive_usergroup AS usergroup USING (usergroupid)
		LEFT JOIN hive_subscription AS subscription ON (plan.planid = subscription.planid AND subscription.active = 1)
		GROUP BY plan.planid
	');
	starttable('Plans Manager');
	$cells = array(
		'ID',
		'Name',
		'Subscribers',
		'User Group',
		'Options',
	);
	tablehead($cells);
	if ($DB_site->num_rows($plans) < 1) {
		textrow('No plans found', count($cells), 1);
	} else {
		while ($plan = $DB_site->fetch_array($plans)) {
			$cells = array(
				$plan['planid'],
				$plan['name'],
				iif($plan['subscriptions'] > 0, "<a href=\"subscription.php?cmd=list&planid=$plan[planid]\">$plan[subscriptions] subscriber".iif($plan['subscriptions'] != 1, 's').'</a>', "$plan[subscriptions] users"),
				"<a href=\"usergroup.php?cmd=edit&usergroupid=$plan[usergroupid]\">$plan[title]</a>",
				makelink('edit', "plan.php?cmd=edit&planid=$plan[planid]") . '-' . makelink('remove', "plan.php?cmd=remove&planid=$plan[planid]"),
			);
			tablerow($cells);
		}
	}
	emptyrow(count($cells));
	endtable();

	button('', 'Create New Subscription Plan', "window.location='plan.php?cmd=edit'", true);

	starttable('Help', '450');
	textrow('Using <b>subscriptions</b> you can charge your users to upgrade to a user group with more or better features, such as increased mail storage space or the ability to check external POP3 accounts. You must first create the group that the user is to be put in after paying, which you can do from the <a href="usergroup.php?cmd=modify">User Group Manager</a>. Then create the plan by clicking the Create New Plan button above.');
	endtable();
}

cp_footer();
?>