<?php

/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@functions: Database querying utilities
@rowcount
@insert/update
@select
*******************************************************************************/

	//error_reporting(E_ALL); ini_set('display_errors', 1);
	ini_set('post_max_size', '256M');
	ini_set('max_input_time', '1000');
	ini_set('max_execution_time', '120');
	ini_set('memory_limit','1024M');

	$opts = json_decode($_POST['opts'], true);
	if (isset($_POST['data'])) $data = $_POST['data'];

	$file = @fopen('log/phplog.txt', 'a+');
	
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
		$driver = "mysql";
		$user_name = "root";
		$pass_word = "";
		$database = "projects";
		$server = "localhost";
	} 
	else {
		$driver = "pgsql";
		$user_name = "divas_user";
		$pass_word = "SvsiDivas_69134Q9";
		$database = "divas";
		$server = "127.0.0.1";
	}

	$db = new PDO("$driver:host=$server;dbname=$database", $user_name, $pass_word);

	switch ($opts['query']){
		case 'select_newid':

			$query = $db->prepare("SELECT MAX(id) FROM panels"); 
			$query->execute();
			$row = $query->fetch(PDO::FETCH_ASSOC);

			$maxid = intval($row['max']);
			
			if ($maxid < 10) $maxid = 10;
			else echo $maxid+1;

			break;
		case 'insert':
		
			$id = $opts['id'];
			
			if (isset($opts['name'])) $name = $opts['name'];
				else $name = '';
			if (isset($opts['datemodified'])) $datemodified = $opts['datemodified'];
				else $datemodified = '';
			if (isset($opts['buttonlib'])) $buttonlib = json_encode($opts['buttonlib']);
				else $buttonlib = '';
			if (isset($opts['templatelib'])) $templatelib = json_encode($opts['templatelib']);
				else $templatelib = '';
			
			$query = $db->prepare("SELECT * FROM panels WHERE id = :id");
			$query->execute(array(':id' => $id));
			
			if ($query->rowCount() < 1){
				$query = $db->prepare("INSERT INTO panels(id, data, name, datemodified, buttonlib, templatelib, servervars) VALUES (:id, :data, :name, :datemodified, :buttonlib, :templatelib, :servervars)");
				$query->execute(array(':data' => $data, ':id' => $id, ':name' => $name,':datemodified' => $datemodified, ':buttonlib' => $buttonlib, ':templatelib' => $templatelib, ':servervars' => "{}"));
			}
			else {
				$query = $db->prepare("UPDATE panels SET data = :data, name = :name, datemodified = :datemodified, buttonlib = :buttonlib, templatelib = :templatelib WHERE id = :id");
				$query->execute(array(':data' => $data, ':name' => $name, ':datemodified' => $datemodified, ':id' => $id, ':buttonlib' => $buttonlib, ':templatelib' => $templatelib));
			}

			break;
		case 'update_DateModified':
		
			$id = $opts['id'];
			
			$datemodified = $opts['datemodified'];
			
			$query = $db->prepare("UPDATE panels SET datemodified = :datemodified WHERE id = :id");
			$query->execute(array(':datemodified' => $datemodified, ':id' => $id));	

			break;
		case 'select':
		
			$id = $opts['id'];
			
			$query = $db->prepare("SELECT data FROM panels WHERE id = :id");
			$query->execute(array(':id' => $id));
			$row = $query->fetch(PDO::FETCH_ASSOC);
	
			echo $row['data'];

			break;
		case 'select_byname':
		
			$name = $opts['name'];
			
			$query = $db->prepare("SELECT data FROM panels WHERE name = :name");
			$query->execute(array(':name' => $name));
			$row = $query->fetch(PDO::FETCH_ASSOC);
	
			echo $row['data'];

			break;
		case 'select_buttonlibbyname':
		
			$name = $opts['name'];
			
			$query = $db->prepare("SELECT buttonlib FROM panels WHERE name = :name");
			$query->execute(array(':name' => $name));
			$row = $query->fetch(PDO::FETCH_ASSOC);
	
			echo $row['buttonlib'];

			break;
		case 'select_templatelibbyname':
		
			$name = $opts['name'];
			
			$query = $db->prepare("SELECT templatelib FROM panels WHERE name = :name");
			$query->execute(array(':name' => $name));
			$row = $query->fetch(PDO::FETCH_ASSOC);
	
			echo $row['templatelib'];

			break;
		case 'select_allbuttonlibs':
		
			$query = $db->prepare("SELECT buttonlib FROM panels");
			$query->execute();
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);
	
			if (count($rows) == 0) echo '';
			else echo json_encode($rows);

			break;
		case 'select_alltemplatelibs':
			
			$query = $db->prepare("SELECT templatelib FROM panels");
			$query->execute();
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);
	
			if (count($rows) == 0) echo '';
			else echo json_encode($rows);

			break;
		case 'select_recent':
		
			$num = $opts['num'];
	
			$query = $db->prepare("SELECT * FROM panels ORDER BY datemodified desc LIMIT 8");
			$query->execute();
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);

			if (isset($rows[$num][1])) echo $rows[$num][1];

			break;
		case 'select_recentname':
		
			$num = $opts['num'];
	
			$query = $db->prepare("SELECT name FROM panels ORDER BY datemodified desc LIMIT 8");
			$query->execute();
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);

			echo $rows[$num][0];

			break;
		case 'select_all':
	
			$query = $db->prepare("SELECT * FROM panels ORDER BY id desc");
			$query->execute();
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);
			
			if (count($rows) == 0) echo '';
			else echo json_encode($rows);

			break;
		case 'select_allnames':
		
			$query = $db->prepare("SELECT name FROM panels ORDER BY datemodified desc");
			$query->execute();
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);
			
			$output = array();
			
			if (count($rows) == 0) echo '[]';
			else {
				foreach ($rows as &$row){
					
					/*$puid = mt_rand();
					$projects = getProjectNames();
					
					$match = true;
					while ($match){
						$match = false;
						for ($i=0; $i<count($projects); $i++){
							if ($projects[$i]->puid == $puid){
								$puid = mt_rand();
								$match = true;
							}
						}
					}*/
					
					$obj = (object) array('name' => $row['name'], 'puid' => $row['name'], 'fromDB' => true);
					array_push($output, $obj);

				}
				echo json_encode($output);
			}

			break;
		case 'delete':
		
			$id = $opts['id'];
	
			$query = $db->prepare("DELETE FROM panels WHERE id = :id");
			$query->execute(array(':id' => $id));

			break;
		case 'delete_byname':
		
			$name = $opts['name'];
	
			$query = $db->prepare("DELETE FROM panels WHERE name = :name");
			$query->execute(array(':name' => $name));

			break;
		case 'check_name':
		
			$name = $opts['name'];
			
			$query = $db->prepare("SELECT * FROM panels WHERE name = :name");
			$query->execute(array(':name' => $name));
			$rows = $query->fetchAll(PDO::FETCH_ASSOC);
			
			if (count($rows) == 0) echo 0;
			else echo 1;

			break;
		case 'insert_vars':

			if (isset($opts['id'])) $id = $opts['id'];
				else $id = '-1';

			$query = $db->prepare("SELECT servervars FROM panels WHERE id = :id"); 
			$query->execute(array(':id' => $id));
			
			$row = $query->fetch();
			$oldjsondata = json_decode($row[0],true);
			$newjsondata = json_decode(json_decode($data,true));
			
			var_dump($newjsondata);
			
			foreach ($newjsondata as $key => $value){
				$oldjsondata[$key] = $value;
			}
			
			$servervars = json_encode($oldjsondata,true);

			$query = $db->prepare("UPDATE panels SET servervars = :servervars WHERE id = :id");
			$query->execute(array(':servervars' => $servervars, ':id' => $id));

			break;
		case 'select_vars':
		
			if (isset($opts['id'])) $id = (int)$opts['id'];
				else $id = -1;
			
			$query = $db->prepare("SELECT servervars FROM panels WHERE id = :id");
			$query->execute(array(':id' => $id));
			$row = $query->fetch();
						
			if ($query->rowCount() > 0){

				echo $row['servervars'];

			}

			break;
		default:
	}
	
	@fclose($file);

?>