<?php
class tables_in
{
  private $way_to_files = '/home/last_php_task13.hostfl.ru/public_html';
  private $dbname='last_php_task13';
  private $host='127.0.0.1';
  private $password='iVubC3J5';
  private $user='last_php_task13';

public function create_table($id)
{
	$a=0;
        try 
      {
          $db = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->password);
          $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $db->exec("set names utf8");
      		//echo('database connected ');
      }
    catch(PDOException $e) 
    	{
      		echo $e->getMessage();
      		return;
    	}
  
  $user = $db->prepare("SELECT * FROM `tasks` WHERE `id`=:id");
  $user->execute(array('id'=>$id));
  $r=$user->fetchAll();
  	return($r);
}


public function check_values($mas)
{
  if(isset($mas['date_start']) and isset($mas['time_start']) and isset($mas['date_end']) and isset($mas['time_end']))
  { 
    $mas['date_time_start']=$mas['date_start'].' '.$mas['time_start'].':00';
  	$mas['date_time_end']=$mas['date_end'].' '.$mas['time_end'].':00';
  	$mas['long']=1;
  }
  else
    return(1);
  //echo(preg_match("/^[0-9]{4}(\.|\-|\:|)(0[1-9]|1[012])(\.|\-|\:|)(0[1-9]|1[0-9]|2[0-9]|3[01]) ([01][0-9]|2[0-3])\:([0-5][0-9])\:([0-5][0-9])$/", $mas['date_time_start']));
	if(isset($mas['theme']) and isset($mas['type']) and isset($mas['place']) and isset($mas['comment']))
	{
		
		if($mas['theme']!='' and $mas['type']!='' and $mas['place']!='' and $mas['date_time_start']!='' and $mas['date_time_end']!='' and $mas['long']!='')
		{
			if (preg_match("/^[0-9]{4}(\.|\-|\:|)(0[1-9]|1[012])(\.|\-|\:|)(0[1-9]|1[0-9]|2[0-9]|3[01]) ([01][0-9]|2[0-3])\:([0-5][0-9])\:([0-5][0-9])$/", $mas['date_time_start'])
				and preg_match("/^[0-9]{4}(\.|\-|\:|)(0[1-9]|1[012])(\.|\-|\:|)(0[1-9]|1[0-9]|2[0-9]|3[01]) ([01][0-9]|2[0-3])\:([0-5][0-9])\:([0-5][0-9])$/", $mas['date_time_end']))
			{
				try 
				  {
					  $db = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->password);
					  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					  $db->exec("set names utf8");
						//echo('database connected ');
				  }
				catch(PDOException $e) 
				{
					echo $e->getMessage();
					return;
				}

				$ch = $db->prepare("SELECT `id` FROM `tasks` WHERE `date_time_start`<=:dt and `date_time_end`>=:dt");
				$ch->execute(array('dt'=>$mas['date_time_start']));
				$ch1=$ch->fetchAll();
				if(isset($ch1) and count($ch1)==0)
				{
					return(0);
				}
				else
					return(4);
			}
			else
				return(3);
		}
		else
			return(2);
	}
	else
		return(1);
}

public function add_task($mas)
{
	try 
	{
		  $db = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->password);
		  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		  $db->exec("set names utf8");
			//echo('database connected ');
	}
	catch(PDOException $e) 
	{
		echo $e->getMessage();
		return;
	}
  $mas['date_time_start']=$mas['date_start'].' '.$mas['time_start'].':00';
  $mas['date_time_end']=$mas['date_end'].' '.$mas['time_end'].':00';
  //var_dump($_POST);
  
	$add = $db->prepare("INSERT INTO `tasks` (`theme`, `type`, `place`,`date_time_start`,`date_time_end`,`comment`,`status`) VALUES(:theme, :type, :place, :dts, :dte, :comment, 'undone')");
	$add->execute(array('theme'=>$mas['theme'],
                      'type'=>$mas['type'],
                      'place'=>$mas['place'],
                      'dts'=>$mas['date_time_start'],
                      'dte'=>$mas['date_time_end'],
                      'comment'=>$mas['comment']
                     ));				
                     
  
  
  
  
  
  
 // $add = $db->prepare("INSERT INTO `tasks` (`theme`) VALUES(:theme);");
	//$add->execute(array('theme'=>$mas['theme']));
}
  
  
public function save_edits($mas, $id)
{
  try 
	{
		  $db = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->password);
		  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		  $db->exec("set names utf8");
			//echo('database connected ');
	}
	catch(PDOException $e) 
	{
		echo $e->getMessage();
		return;
	}
  if(isset($mas['delete']) and $mas['delete']==='yes')
  {
    $save=$db->prepare("DELETE FROM `tasks` WHERE `id`=:id");
    $save->execute(array('id'=>$id));
    return;
  }
  $mas['date_time_start']=$mas['date_start'].' '.$mas['time_start'].':00';
  $mas['date_time_end']=$mas['date_end'].' '.$mas['time_end'].':00';
  $mas['long']=1;
  echo($mas['status']);
  $save=$db->prepare("UPDATE `tasks` SET  `theme`=:theme, `type`=:type, `place`=:place, `date_time_start`=:dts,`date_time_end`=:dte,`comment`=:comment, `status`=:status WHERE `id`=:id");
  $save->execute(array('theme'=>$mas['theme'], 
                       'type'=>$mas['type'], 
                       'place'=>$mas['place'],
                       'dts'=>$mas['date_time_start'],
                       'dte'=>$mas['date_time_end'],
                     	 'comment'=>$mas['comment'],
                       'status'=>$mas['status'],
                       'id'=>$id
                ));
}
  /*
  CREATE TABLE `tasks` (
  `id` int(10) AUTO_INCREMENT NOT NULL,
  `theme` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  `place` varchar(255) DEFAULT NULL,
  `date_time_start` datetime DEFAULT NULL,
  `date_time_end` datetime DEFAULT NULL,
  `comment` varchar(255) DEFAULT NULL,
  `status` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
}
?>