<?php
/*
Plugin Name: hoozgot
Plugin URI: 
Description: the hoozgot plugin
Author: Thomas
Version: 0.1
Author URI:
*/
include('hoozgot.tablenames.php');

function installation(){
	include ('hoozgot.install.php');
}
function update_database(){
	echo '<br><div class="title">Future Update Database Function.</div>';
	include ('css.functions.php');
}
//register_activation_hook(__FILE__, 'install_tables');
add_action('admin_menu', 'hoozgot');


// Add menu page
function hoozgot() {
	$plugin_page = add_menu_page( 'Hoozgot', 'Hoozgot', 'level_10', 'hoozgot', 'add_item_page', 'none', 3 );
	add_submenu_page( 'hoozgot', 'Hoozgot', 'Hoozgot','level_10', 'hoozgot', 'add_item_page' );
	add_submenu_page( 'hoozgot', 'Hoozgot', 'Hoozgot','level_10', 'hoozgot', 'add_item_page' );
	add_submenu_page( 'hoozgot', 'Update Database', 'Update Database','level_10', 'update-gear-database', 'update_database' );
	add_submenu_page( 'hoozgot', 'Install', 'Install','level_10', 'install', 'installation' );
	add_action('admin_head-'.$plugin_page, 'hoozgot_head');
}

function hoozgot_head() {
echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') .'/wp-content/plugins/hoozgot/css/jquery.fileupload-ui.css" />' . "\n";
}

// listing ok
function listNames($theTable, $theName){
	global $wpdb;
	$myrows = $wpdb->get_results( "SELECT id, $theName as name, checked FROM $theTable ORDER BY $theTable.$theName ASC" );
	foreach($myrows as $row){
		if(!empty($row)){
			if($row->checked > 0){ ?>
				<option value="<?php echo $row->id;?>" <?php if(isset($_POST[$row->name])){ echo 'selected="selected"';} ?>><?php echo $row->name;?></option><br>
				<?php //echo '<option value="'.$row->id.'">'.$row->name.'</option>';
			}
			else {
				echo '<option value="'.$row->id.'" class="error">'.$row->name.'</option>';
			}
		}
	}
}
function checkDuplicate($newItem, $existingItems, $tableName){
	global $wpdb;
	$myrows = $wpdb->get_results( "SELECT id, $existingItems as existingItems, ORDER BY $tableName.$existingItems ASC" );
	foreach($myrows as $row){
		if(!empty($row)){
			if($row->existingItems == $newItem){ 
				return false;
			}
			else {
				return true;
			}
		}
	}
}
function insertToTable($theTable, $theValue){
	echo $theTable.' | '. $theValue;
	$theTableSplitted = explode("-", $theTable);
	echo 'theTableSplitted: '.$theTableSplitted;
	$theCell = $theTableSplitted[1];
	echo 'the cell: '.$theCell;
}
function getTableContents($theTable,$theTarget){
	global $wpdb;
	$theTable = str_replace("'", "", $theTable);
	$myrows = $wpdb->get_results( "SELECT id, $theTarget FROM $theTable ORDER BY $theTable.$theTarget ASC" );
	$i=0;
	$theArray = '';
	foreach($myrows as $row){
			$theName = $row->$theTarget;
			$theArray .= '"'.$theName.'", ';
			$i++;
	}
	$theArray = substr($theArray, 0, -2);
	return $theArray;
}
function getArrayContent($theTable){
	global $wpdb;
	$i=0;
	$theArray = '';
	foreach($theTable as $tName){
			$theArray .= '"'.$tName.'", ';
	}
	$theArray = substr($theArray, 0, -2);
	return $theArray;
}
function checkIfDuplicate($toBchecked, $theTable){
	global $wpdb;
	// check
	$theName = substr($theTable,11,99).'_name';
	$myCheck = $wpdb->get_results( "SELECT * FROM $theTable WHERE $theName LIKE '$toBchecked' LIMIT 0 , 30" );
	
	foreach($myCheck as &$check){
		if($check->$theName == $toBchecked || $toBchecked == ''){
			return 'duplicate';
			break;
		}
	}
}
// Draw the menu page itself
function add_item_page() {
	global $current_user;
	get_currentuserinfo();
	include('upload.functions.php');
	wp_enqueue_script('jquery');
	global $table_names;
	global $errorMessage;
	global $wpdb;
	if($_POST){
		$creation_date= date('Y-m-d H:i:s').' GMT';
		echo 'creation_date = '.$creation_date."<br>";
		// arrays
		$table0Cells = $table1Cells = $table2Cells = $table3Cells = $table4Cells = $table5Cells = $table6Cells = array();
		$table0CellValues = $table1CellValues = $table2CellValues = $table3CellValues = $table4CellValues = $table5CellValues = $table6CellValues = array();
		$t0i = $t1i = $t2i = $t3i = $t4i = $t5i = $t6i = 0;
		//
		while (list($key, $value) = each($_POST)){
			if($value != '' && $value != __('Choose from list') && $value != __('> Other ?')){
				//
				$theTableSplitted = explode("-", $key);
				$destinationTable = $theTableSplitted[0];
				$destinationValue = $theTableSplitted[1];
				
				if($destinationTable == $table_names[0]){
					if(checkIfDuplicate($value, $table_names[0]) != 'duplicate' ){
						$table0Cells[$t0i] = $destinationValue;
						$table0CellValues[$t0i] = $value;
					}
				}
				if($destinationTable == $table_names[1]){
					if(checkIfDuplicate($value, $table_names[1]) != 'duplicate' ){
						$table1Cells[$t1i] = $destinationValue;
						$table1CellValues[$t1i] = $value;
					}
				}
				if($destinationTable == $table_names[2]){
					if(checkIfDuplicate($value, $table_names[2]) != 'duplicate' ){
						$table2Cells[$t2i] = $destinationValue;
						$table2CellValues[$t2i] = $value;
					}
				}
				if($destinationTable == $table_names[3]){
					if(checkIfDuplicate($value, $table_names[3]) != 'duplicate' ){
						$table3Cells[$t3i] = $destinationValue;
						$table3CellValues[$t3i] = $value;
					}
				}
				if($destinationTable == $table_names[4]){
					if(checkIfDuplicate($value, $table_names[4]) != 'duplicate' ){
						$table4Cells[$t4i] = $destinationValue;
						$table4CellValues[$t4i] = $value;
					}
				}
				if($destinationTable == $table_names[5]){
					if(checkIfDuplicate($value, $table_names[5]) != 'duplicate' ){
						$table5Cells[$t5i] = $destinationValue;
						$table5CellValues[$t5i] = $value;
					}
				}
				if($destinationTable == $table_names[6]){
					$table6Cells[$t6i] = $destinationValue;
					$table6CellValues[$t6i] = $value;
				}
				$t0i++; $t1i++; $t2i++; $t3i++; $t4i++; $t5i++; $t6i++;
			}
		}		
		// prepare the data
		for($i=0;$i < count($table_names); $i++){
			if( count(${table.$i.Cells}) > 1 ){
				echo "------------------------------------------------<br>";
				echo $table_names[$i]."<br>";
				foreach(${table.$i.Cells} as &${table.$i.Cell}){
				echo 'cell: '.${table.$i.Cell}.', ';
				}
				echo "<br>";	
				foreach(${table.$i.CellValues} as &${table.$i.CellValue}){
					echo ${table.$i.CellValue}.', ';
				}
				echo "<br>------------------------------------------------<br>";
				
				${table.$i.Cells} = array_values(${table.$i.Cells});
				${table.$i.CellValues} = array_values(${table.$i.CellValues});
				${theList.$i} = ${theColumns.$i} = '';
				
				while (list($key, $value) = each(${table.$i.Cells})) {
					${theColumns.$i} .= (string)$value;
					${theColumns.$i} .= ", ";
				}
				
				while (list($key, $value) = each(${table.$i.CellValues})) {
					if( is_numeric($value) ){
						${theList.$i} .= (string)$value;
					} else {
						${theList.$i} .= "'".(string)$value."'";
					}
					${theList.$i} .= ", ";
				}
				${theList.$i} = substr(${theList.$i}, 0, -2);
				${theColumns.$i} = substr(${theColumns.$i}, 0, -2);
				$wpdb->show_errors();
				$wpdb->query( $wpdb->prepare( "INSERT INTO $table_names[$i] (${theColumns.$i}) VALUES (${theList.$i});") );
			}
		}
	} 
	?>
<div class="wrap">
	<h2>ADD ITEM</h2>
	<!--#formElements-->
	<div id="formElements">
<!--begin form-->
<form method="post" action="" id="add-gear">    
<!--<?php echo $table_names[0];?>-->
	<div id="div-<?php echo $table_names[0];?>">
		<div class="title">TYPE<br />
			<select name="<?php echo $table_names[0];?>-type" id="select-<?php echo $table_names[0];?>">
			<option><?php _e('Choose from list'); ?></option>
			<?php listNames($table_names[0], 'type_name'); ?>
			<option id="other-<?php echo $table_names[0];?>"><?php _e('> Other ?'); ?></option>
			</select>
		</div>
		<div id="new-<?php echo $table_names[0];?>" style="display:none;">
			Add Type<br />
			<input type="text" name="<?php echo $table_names[0];?>-type_name" value="" class="newInput"/>
            <input type="hidden" name="<?php echo $table_names[0];?>-submittedByUser" value="" id="<?php echo $table_names[0];?>-submittedByUser"/>
            
			<span id="yes-<?php echo $table_names[0];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/yes.png" /></span>
			<span id="no-<?php echo $table_names[0];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/no.png" /></span>
		</div>
        <span id="error-<?php echo $table_names[0];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[0];?>-->

<!--<?php echo $table_names[1];?>-->
	<div id="div-<?php echo $table_names[1];?>">
		<div class="title">SUBTYPE<br />
			<select name="<?php echo $table_names[1];?>-subtype_id" id="select-<?php echo $table_names[1];?>">
			<option><?php _e('Choose from list'); ?></option>
			<?php listNames($table_names[1], 'subtype_name'); ?>
			<option id="other-<?php echo $table_names[1];?>"><?php _e('> Other ?'); ?></option>
			</select>
            <input name="<?php echo $table_names[1];?>-type_id" type="hidden" value="" id="<?php echo $table_names[1];?>-type_id"/>
		</div>
		<div id="new-<?php echo $table_names[1];?>" style="display:none;">
			Add Subtype<br />
			<input type="text" name="<?php echo $table_names[1];?>-subtype_name" value="" class="newInput"/>
            <input type="hidden" name="<?php echo $table_names[1];?>-submittedByUser" value="" id="<?php echo $table_names[1];?>-submittedByUser"/>
			<span id="yes-<?php echo $table_names[1];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/yes.png" /></span>
			<span id="no-<?php echo $table_names[1];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/no.png" /></span>
		</div>
        <span id="error-<?php echo $table_names[1];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[1];?>-->

<!--<?php echo $table_names[2];?>-->
    <div id="div-<?php echo $table_names[2];?>">
		<div class="title">BRAND<br />
			<select name="<?php echo $table_names[2];?>-brand_id" id="select-<?php echo $table_names[2];?>">
			<option><?php _e('Choose from list'); ?></option>
			<?php listNames($table_names[2],'brand_name'); ?>
			<option id="other-<?php echo $table_names[2];?>"><?php _e('> Other ?'); ?></option>
			</select>
            <input name="<?php echo $table_names[2];?>-type_id" type="hidden" value="" id="<?php echo $table_names[2];?>-type_id"/>
            <input name="<?php echo $table_names[2];?>-subtype_id" type="hidden" value="" id="<?php echo $table_names[2];?>-subtype_id"/>
		</div>
		<div id="new-<?php echo $table_names[2];?>" style="display:none;">
			Add Brand<br />
			<input type="text" name="<?php echo $table_names[2];?>-brand_name" value="" class="newInput"/>
            <input type="hidden" name="<?php echo $table_names[2];?>-submittedByUser" value="" id="<?php echo $table_names[2];?>-submittedByUser"/>
			<span id="yes-<?php echo $table_names[2];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/yes.png" /></span>
			<span id="no-<?php echo $table_names[2];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/no.png" /></span>
		</div>
        <span id="error-<?php echo $table_names[2];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[2];?>-->

<!--<?php echo $table_names[3];?>-->
    <div id="div-<?php echo $table_names[3];?>">
		<div class="title">MODEL<br />
			<select name="<?php echo $table_names[3];?>-model_id" id="select-<?php echo $table_names[3];?>">
			<option><?php _e('Choose from list'); ?></option>
			<?php listNames($table_names[3], 'model_name'); ?>
			<option id="other-<?php echo $table_names[3];?>"><?php _e('> Other ?'); ?></option>
			</select>
            <input name="<?php echo $table_names[3];?>-type_id" type="hidden" value="" id="<?php echo $table_names[3];?>-type_id"/>
            <input name="<?php echo $table_names[3];?>-subtype_id" type="hidden" value="" id="<?php echo $table_names[3];?>-subtype_id"/>
            <input name="<?php echo $table_names[3];?>-brand_id" type="hidden" value="" id="<?php echo $table_names[3];?>-brand_id"/>
		</div>
		<div id="new-<?php echo $table_names[3];?>" style="display:none;">
			Add Model<br />
			<input type="text" name="<?php echo $table_names[3];?>-model_name" value="" class="newInput"/>
            <input type="hidden" name="<?php echo $table_names[3];?>-submittedByUser" value="" id="<?php echo $table_names[3];?>-submittedByUser"/>
			<span id="yes-<?php echo $table_names[3];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/yes.png" /></span>
			<span id="no-<?php echo $table_names[3];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/no.png" /></span>
		</div>
        <span id="error-<?php echo $table_names[3];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[3];?>-->

<!--<?php echo $table_names[4];?>-->
    <div id="div-<?php echo $table_names[4];?>">
		<div class="title">YEAR<br />
			<select name="<?php echo $table_names[4];?>-year_id" id="select-<?php echo $table_names[4];?>">
			<option><?php _e('Choose from list'); ?></option>
			<?php listNames($table_names[4], 'year_name'); ?>
			<option id="other-<?php echo $table_names[4];?>"><?php _e('> Other ?'); ?></option>
			</select>
		</div>
		<div id="new-<?php echo $table_names[4];?>" style="display:none;">
			Add Year<br />
			<input type="text" name="<?php echo $table_names[4];?>-year_name" value="" class="newInput"/>
            <input type="hidden" name="<?php echo $table_names[4];?>-submittedByUser" value="" id="<?php echo $table_names[4];?>-submittedByUser"/>
			<span id="yes-<?php echo $table_names[4];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/yes.png" /></span>
			<span id="no-<?php echo $table_names[4];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/no.png" /></span>
		</div>
        <span id="error-<?php echo $table_names[4];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[4];?>-->

<!--<?php echo $table_names[5];?>-->
    <div id="div-<?php echo $table_names[5];?>">
		<div class="title">COLOR<br />
			<select name="<?php echo $table_names[5];?>-color_id" id="select-<?php echo $table_names[5];?>">
			<option><?php _e('Choose from list'); ?></option>
			<?php listNames($table_names[5], 'color_name'); ?>
			<option id="other-<?php echo $table_names[5];?>"><?php _e('> Other ?'); ?></option>
			</select>
            <input name="<?php echo $table_names[5];?>-type_id" type="hidden" value="" id="<?php echo $table_names[5];?>-type_id"/>
            <input name="<?php echo $table_names[5];?>-subtype_id" type="hidden" value="" id="<?php echo $table_names[5];?>-subtype_id"/>
            <input name="<?php echo $table_names[5];?>-brand_id" type="hidden" value="" id="<?php echo $table_names[5];?>-brand_id"/>
		</div>
		<div id="new-<?php echo $table_names[5];?>" style="display:none;">
			Add Color<br />
			<input type="text" name="<?php echo $table_names[5];?>-color_name" value="" class="newInput"/>
            <input type="hidden" name="<?php echo $table_names[5];?>-submittedByUser" value="" id="<?php echo $table_names[5];?>-submittedByUser"/>
			<span id="yes-<?php echo $table_names[5];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/yes.png" /></span>
			<span id="no-<?php echo $table_names[5];?>" style="display:none;"><img src="<?php echo admin_url(); ?>images/no.png" /></span>
		</div>
        <span id="error-<?php echo $table_names[5];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[5];?>-->

<!--<?php echo $table_names[6];?>-->
    <div id="div-<?php echo $table_names[6];?>">
		<div class="title">ITEM<br />
            <input type="hidden" name="<?php echo $table_names[6];?>-submittedByUser" value="" id="<?php echo $table_names[6];?>-submittedByUser"/>
            <input name="<?php echo $table_names[6];?>-type_id" type="hidden" value="" id="<?php echo $table_names[6];?>-type_id"/>
			<input name="<?php echo $table_names[6];?>-subtype_id" type="hidden" value="" id="<?php echo $table_names[6];?>-subtype_id"/>
			<input name="<?php echo $table_names[6];?>-brand_id" type="hidden" value="" id="<?php echo $table_names[6];?>-brand_id"/>
            <input name="<?php echo $table_names[6];?>-model_id" type="hidden" value="" id="<?php echo $table_names[6];?>-model_id"/>
            <input name="<?php echo $table_names[6];?>-year_id" type="hidden" value="" id="<?php echo $table_names[6];?>-year_id"/>
            <input name="<?php echo $table_names[6];?>-color_id" type="hidden" value="" id="<?php echo $table_names[6];?>-color_id"/>
            <textarea name="<?php echo $table_names[6];?>-specs" id="text-<?php echo $table_names[6];?>"></textarea><br />
			Serial Number: <br />
            <input name="<?php echo $table_names[6];?>-serial_number" type="text" value="" id="<?php echo $table_names[6];?>-serial_number"/>
		</div>
        <span id="error-<?php echo $table_names[6];?>" class="error"></span>
	</div>
<!--end <?php echo $table_names[6];?>-->

<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Add Gear') ?>" /></p></form>
	<?php
	include ('jQuery.functions.php');
	include ('css.functions.php');
	//}
}
?>