<?

/*
**	Filename:	edit_attractions.php
**	Author:		Troy Lutton for Trendwest 
**	Date:		16/12/2005
**	Purpose:	To edit the attractions
*/

/////////////////////////////////////////////////////////////////////////////////////

// GET THE LOCAL CONFIG FILE
require_once("config.inc.php");

// AUTHENTICATE THIS USERS DETAILS
require_once("authentication.class.php");

// RUN AN AUTHORIZATION CHECK (SEND ACCESS LEVEL OF THIS PAGE)
$authorization = new Authentication(GENERAL_ACCESS);

// GET THE MENU BUILDER CLASS
require_once("menu_builder.class.php");

// GET THE MENU ARRAY OBJECT
require_once("menu.inc.php");

// GET THE TEMPLATING CLASS
require_once("template.class.php");

// GENERATE THE ADMIN MENU - (NULL IS NO SYSTEMS)
$menu_builder = new Admin_Menu_Builder($my_menu, $_SESSION['reg_sys'], $_SESSION['priv_level']);

// GET THE ADMIN MENU
$admin_menu = $menu_builder->get_menu();

// INSTANTIATE THE TEMPLATE CLASS WITH A TEMPLATE
$template = new Template_Builder((ADMIN_TEMPLATES_DIR . "admin_template.html"));

/////////////////////////////////////////////////////////////////////////////////////


// get the signatures class
require_once("attractions.class.php");

// instantiate the class
$attracs = new Attraction_Manager(DB_USERNAME, DB_PASSWORD, DB_SERVER, PROPERTIES_DB);


// DECLARE VARS
$display = NULL;
$action = NULL;
$content = NULL;
$outcome_msg = NULL;
$jscript_1 = RESORTS_JSCRIPT_FILE . RESORTS_STYLE;
$jscript_2 = NULL;
$images_url = RESORT_IMAGES_URL;

$attraction_id = NULL;
$attraction_imgs_url = ATTRACTION_IMGS_URL;
$attraction_relations = NULL;


// CATCH GETVARS
if (isset($_GET['action'])){$action = $_GET['action'];}
if (isset($_GET['display'])){$display = $_GET['display'];}
if (isset($_GET['property_id'])){$property_id = $_GET['property_id'];}
if (isset($_GET['attraction_id'])){$attraction_id = $_GET['attraction_id'];}

if (isset($_POST['action'])){$action = $_POST['action'];}
if (isset($_POST['display'])){$display = $_POST['display'];}
if (isset($_POST['property_id'])){$property_id = $_POST['property_id'];}
if (isset($_POST['attraction_id'])){$attraction_id = $_POST['attraction_id'];}
if (isset($_POST['attraction_relations'])){$attraction_relations = $_POST['attraction_relations'];}

// SWITCH ON ACTION
switch($action)
{
	// update the associaions
	case "update_associations":
	{
		// show any messages
		$attracs->update_property_associations($property_id, $attraction_relations);

		// show any output
		$outcome_msg = $attracs->get_messages();

		break;
	}

	// add a new attraction
	case "add_attraction":
	{		
		// add the property
		$attracs->add_attraction(ATTRACTION_IMGS_DIR, $_FILES, $_POST);

		// show any output
		$outcome_msg = $attracs->get_messages();

		break;
	}

	// remove a attraction
	case "remove_attraction":
	{		
		// add the property
		$attracs->remove_attraction(ATTRACTION_IMGS_DIR, $attraction_id);

		// show any output
		$outcome_msg = $attracs->get_messages();

		break;
	}

	// remove a attraction
	case "update_attraction":
	{		
		
		// add the property
		$attracs->update_attraction(ATTRACTION_IMGS_DIR, $_FILES, $_POST);

		// show any output
		$outcome_msg = $attracs->get_messages();

		break;
	}
}


// switch on the display
switch ($display)
{
	// edit a resorts attractions
	case "add_attraction":
	{
		// get the list of property facilities
		$attraction_list = $attracs->get_attraction_list();

		// see if there are any entries
		if (is_array($attraction_list) && sizeof($attraction_list) > 0)
		{
			// format the facilties
			$attracs_content = $attracs->format_attractions_list($display, $attraction_id, $images_url, $attraction_imgs_url, $attraction_list);
		}
		else
		{
			$attracs_content = "<p><li>There are currently no attractions</li></p>";
		}

		// show any output
		$outcome_msg = $attracs->get_messages();

		// add a title
		$content .= "<h4>Editing Attraction List</h4>";		

		// show the upload table
		$content .= "
		<table cellpadding='5' cellspacing='0' class='admin_tbl' width='480'>
			<tr>
				<th class='tbl_hdr' colspan='2'>Manage Attractions</th>
			</tr>
			<tr>
				<td>
				<fieldset>
				<legend>Add Attraction</legend>


					<form method='post' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data' class='no_break'>
						<table cellpadding='5' cellspacing='0' width='100%'>
							<tr>
								<td>Attraction Name</td><td><input type='text' name='attraction_name' size='50' class='prop_txt_fld_lrg'></td>
								<td rowspan='3'><input type='image' src='{$images_url}disk_large.gif' border='0' width='24' height='24' class='no_border' title='Save'></td>
							</tr>
							<tr>
								<td>Attraction Desc</td><td><textarea name='attraction_descript' rows='3' class='prop_txt_fld_lrg'></textarea></td>
							</tr>
							<tr>
								<td>Attraction Image</td><td><input type='file' name='attraction_img' size='50' class='prop_txt_fld_lrg'></td>
							</tr>
						</table>
						<input type='hidden' name='action' value='add_attraction'>
					</form>
				</legend>
				</td>
			</tr>
			<tr>
				<td>$attracs_content</td>
			</tr>				
		</table>";

		break;
	}

	// add associations for a property
	case "edit_associations":
	{
		// add a title
		$content .= "<h4>Editing Attraction & Property Associations</h4>";

		// get the property name
		require_once("properties.class.php");

		// instantiate the class
		$props = new Property_Manager(DB_USERNAME, DB_PASSWORD, DB_SERVER, PROPERTIES_DB);

		// get it
		$property_name = $props->get_property_name($property_id);
		
		// get the [roperty name and display it
		$content .= "<p><li>Currently editing the property associations for '<strong>$property_name</strong>'</li></p>";

		// get the details for this property
		$associations = $attracs->get_association_relations($property_id);
		
		// format the list
		$content .= $attracs->format_associations($images_url, $property_id, 2, $associations);

		// show any output
		$outcome_msg = $attracs->get_messages();

		break;
	}

	// show the list
	default:
	{
		// get the list of property facilities
		$attraction_list = $attracs->get_attraction_list();

		// see if there are any entries
		if (is_array($attraction_list) && sizeof($attraction_list) > 0)
		{
			// format the facilties
			$attracs_content = $attracs->format_attractions_list($display, $attraction_id, $images_url, $attraction_imgs_url, $attraction_list);
		}
		else
		{
			$attracs_content = "<p><li>There are currently no attractions</li></p>";
		}

		// show any output
		$outcome_msg = $attracs->get_messages();

		// add a title
		$content .= "<h4>Editing Attraction List</h4>";	

		// show the upload table
		$content .= "
		<table cellpadding='5' cellspacing='0' class='admin_tbl' width='480'>
			<tr>
				<th class='tbl_hdr' colspan='2'>Manage Attractions</th>
			</tr>
			<tr>
				<td>
				<fieldset>
				<legend>Add Attraction</legend>";

		// load the Quickform class
		ini_set("include_path", PEAR_INCLUDES_DIR);
		require_once 'HTML/QuickForm.php';

		// THE FORM
		// create the form
		$form = new HTML_QuickForm('addAttraction', 'POST', null, null, null, true);

		// Add some elements to the form
		$form->addElement('text', 'attraction_name', 'Name:', array('class'=>'prop_txt_fld_lrg'));
		$form->addElement('textarea', 'attraction_descript', 'Description:', array('class'=>'prop_txt_fld_lrg'));
		$form->addElement('file', 'attraction_img', 'Image:', array('class'=>'prop_txt_fld_lrg'));
		$form->addElement('hidden', 'action', 'add_attraction');
		
		// Add submit button
		$form->addElement('image', 'save', $images_url.'disk_large.gif', array('class'=>'no_border'));

		// Add validation rules
		$form->addRule('attraction_name', 'a name must be entered', 'required', null, 'client');
		$form->addRule('attraction_descript', 'a description must be entered', 'required', null, 'client');
		//$form->addRule('attraction_img', 'a file must be selected', 'required', null, 'client');

		// start the output buffer handler
		ob_start();

		//display the form
		$form->display();

		// get the file output that was generated
		$content .= ob_get_contents();  

		// clean the buffer
		ob_end_clean();



		$content .= "		</legend>
				</td>
			</tr>
			<tr>
				<td>$attracs_content</td>
			</tr>				
		</table>";

		break;
	}
}

/////////////////////////////////////////////////////////////////////////////////////

// CHECK OUR OUTPUT MESSAGES
if (!is_null($outcome_msg))
{
	$outcome_msg = "<script language=\"Javascript1.2\">window.alert(\"$outcome_msg\");</script>";
}

// ADD MULTIPLE COMPONENTS TO THE TEMPALTE
$template->add_multiple_components(array('ADMIN_MENU' => $admin_menu, 'SCRIPTS_1' => $jscript_1, 'SCRIPTS_2' => $jscript_2, 'CONTENT' => $content, 'USERNAME' => $_SESSION['account_username'], 'OUTCOME_MSG' => $outcome_msg));

// SHOW THE POPULATED TEMPALTE
echo $template->get_template();

/////////////////////////////////////////////////////////////////////////////////////

?>