<?php
/**
Plugin Name: Villa Portal - Babon Multimedia
Plugin URI: http://w...content-available-to-author-only...a.com/
Description: Villa Portal
Version: 0.5 - Beta
Author: GusDeCooL
Author URI: http://w...content-available-to-author-only...l.com/
License: GPL - Ga Pake Lama, langsung pake aja.
*/

define('VP_URLPATH', WP_PLUGIN_URL.'/'.plugin_basename( dirname(__FILE__) ).'/' );
define('VP_DIRPATH', WP_PLUGIN_DIR.'/'.plugin_basename( dirname(__FILE__) ).'/');

// Set Master Class
include VP_DIRPATH.'includes/inc.villaportal.php'; // Include Core Class
$path = array('url'=>VP_URLPATH, 'dir'=>VP_DIRPATH);
$linkpage = array('property'=>'?page=vp-property');
// $vp = new VillaPortal($path, $linkpage); // Set Core Class

add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
    //session_start();
    global $vp;    
    add_menu_page('Babon Villa Portal', 'Babon VP', 'manage_options', 'vp-property', 'pageProperty');
    add_submenu_page('vp-property', 'Property', 'Property', 'manage_options', 'vp-property', 'pageProperty');    
    add_submenu_page('vp-property', 'Country', 'Country', 'manage_options', 'vp-config-country', 'pageConfigLocation');
    add_submenu_page('vp-property', 'Facilities', 'Facilities', 'manage_options', 'vp-config-facility', 'pageConfigFacility');

	if( is_admin() ) global $plugin_page;
    if($plugin_page == 'vp-property' or $plugin_page == 'vp-config-country' or $plugin_page == 'vp-config-facility' and is_admin() ){
		$vp->registerCSS();
		$vp->registerScript();
    }
}

function pageProperty() {
    global $vp;
    $vp->urlAdmin = '/wp-admin/admin.php?page=vp-property';
    $show = $_GET['show'];
    $action = $_GET['action'];
    $propertyId = $_GET['id'];
    $vp->loadExtention( 'Property', array($propertyId) );

    switch ($show) {
	// &show=propertylist
	default:
	    $data['title'] = 'List Property';
	    $data['content'] = $vp->propertyList('propertylist.php', 'propertylist-wrap.php');

	    $vp->parser('wrap.php', $data);
	    break;

	// Do Action
	case 'action':
	    switch ($action) {
		default:
		    header("Location: $vp->urlAdmin");
		    break;
		case 'delete':
		    $vp->propertyDelete();
		    break;
		case 'update':
		    if ($_POST){
			if ($vp->propertyIdentifier){ // property exist do update
			    if(!$vp->propertyUpdate($_POST)) {
				die('Error Property Update');
			    }
			}else{
			    $vp->propertyInsert($_POST);
			}
		    }
		    break;
	    }
	    header('refresh:1;url='.$vp->linkpage['property']);
	    break;

	// &show=propertyedit
	case 'propertyedit':
	    if ($vp->propertyIdentifier) { // Mode Update
		$data['title'] = 'Edit Location';
	    } else { // Mode Insert
		$data['title'] = 'Add Location';
	    }

	    $data['content'] = $vp->propertyEdit('property-form.php');
	    $vp->parser('wrap.php', $data);
	    break;

	// Show Property with its room
	case 'property':
	    $data['title'] = 'Show Property';
	    $data['content'] = '';
	    if ($vp->propertyIdentifier) {
		$data['content'] .= $vp->getProperty('property.php');
	    } else {
		header("Location: $vp->urlAdmin");
	    }

	    $vp->parser('wrap.php', $data);
	    $vp->showRoom('roomlist.php', 'roomlist-wrap.php');
	    $vp->showFacility();	    
	    break;

	case 'roomaction':
	    include VP_DIRPATH.'includes/inc.room.php'; // Include Room Class
	    $url_property = $vp->urlAdmin.'&show=property&id='.$vp->propertyIdentifier;
	    $room = new Room($url_property);

	    switch($_GET['roomaction']){
		case 'create':
		    // Show create form
		    if ($room->identifier) {
			$data['title'] = 'Add Room';
		    }else{
			$data['title'] = 'Edit Room';
		    }

		    $data['content'] = $vp->parseChunk('room-form.php', $room->data);
		    $vp->parser('wrap.php', $data);
		    break;

		case 'doedit':
		    if ($_POST){
			$_POST['property_id'] = $vp->propertyIdentifier;
			if($room->identifier) { // Do Update
			    $vp->db->update('vm_room', $_POST, 'id='.$room->identifier);
			}else{ // Do Insert
			    $vp->db->insert('vm_room', $_POST);
			}
		    }
		    header('Location: '.$room->url_property);
		    break;

		case 'dodelete':
		    if($_GET['roomid'] != '' && $_GET['id'] != '') {
			$vp->room_delete($_GET['roomid']);
		    }
		    header('Location: '.$room->url_property);
		    break;
	    }
	break;

	case 'roomprice':
	    $vp->loadExtention('Room');
	    switch ($_GET['roomprice']) {
		default:
		    $data['title'] = 'Rate Setup';
		    $data['content'] = $vp->listRate();
		    $vp->parser('wrap.php', $data);
		    $_SESSION['linkBack'] = $_SERVER['REQUEST_URI'];			
		break;

		case 'update':
		    switch ($_POST['method']) {
			case 'date':
			    unset($_POST['method']);
			    $_SESSION['rateDate'] = $_POST['year'].'-'.$_POST['month'];				
			break;

			case 'rate':
			    unset($_POST['method']);
			    $vp->room->roomUpdate($_POST);
			break;
		    }
		    header('refresh:1;url='.$_SESSION['linkBack']);
		break;
	    }
	break;
    }
}

function pageConfigLocation() {
    global $vp;
    $mode = $_GET['mode'];

    switch ($mode) {
	case 'savelocation':
	    $vp->saveLocation('?page=vp-config-country');
	    break;

	case 'deletelocation':
	    $vp->deleteLocation($_GET['id'], '?page=vp-config-country');
	    break;

	case 'addlocation':
	    ?>
	    <div class="wrap">
		<div class="icon32" id="icon-tools"><br></div>
		<h2>Add Location</h2>
		<?php $vp->formAddLocation('?page=vp-config-country&mode=savelocation'); ?>
	    </div>
	    <?php
	    break;

	default:
	    ?>
	    <div class="wrap">
		<div class="icon32" id="icon-tools"><br></div>
		<h2>Location Manager</h2>
		<a class="button-primary" href="?page=vp-config-country&mode=addlocation&do=add">Add Country</a>
		<?php $vp->listLocation(); ?>
	    </div>
	    <?php
	break;
    }

    print($return);
}

function pageConfigFacility() {
    global $vp;
    $vp->urlAdmin = '/wp-admin/admin.php?page=vp-config-facility';
    $table_name = 'vm_facility';
    DBAPI::$debug = false;
    switch ($_POST['action']) {
	case 'insert':
	    unset( $_POST['action'] );
	    $insertData = $_POST['insert'];
	    if ( $vp->db->countRow($table_name, null, "name='$insertData'") == 0 ) { // This data is not in database
		$data = array('name'=>$insertData);
		$vp->db->insert($table_name, $data);
	    }
	break;
	
	case 'update':
	    unset( $_POST['action'] );
	    foreach ($_POST['update'] as $key=>$value) {		
		$data['name'] = $value['name'];
		$vp->db->update($table_name, $data, 'id='.$key);
	    }
	break;
    
	default:
	break;
    }
    
    if ($_GET['action'] == 'delete' && $_GET['id'] != '' ) {
	$vp->db->delete($table_name, 'id='.$_GET['id']);
	header('Location: '.$vp->urlAdmin);
    }
    ?>
    <div class="wrap">
	<div class="icon32" id="icon-tools"><br></div>
	<h2>Facility Manager</h2>
	<?php $vp->configListFacility(); ?>	
	
	<form action="" method="post">
	    <input type="hidden" name="action" value="insert" />
	    <p>
		<input type="text" name="insert" value="" />
	    </p>	    
	    <p>
		<input type="submit" value="insert" class="button-primary" />
	    </p>
	</form>	
    </div>
    <?php
}
?>