<?php
include_once 'config.php';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(isset($_POST['delete_project'])){
    $conn = new PDO("mysql:host=$dbhost;dbname=$db_name", $dbuser, $dbpass);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "delete from rockypet_projects where project_id=".$_POST['project_id'];
                
      $conn->exec($sql);
      //echo "Success";
      header("Location: http://finstechnologies.com/rockypetghana/admin/ui/project_all.php");
        die();
}
if(isset($_POST['save_project']))
{

if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysqli_select_db($conn, "$db_name")or die("cannot select DB");

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login_result = ftp_login($ftp_conn, $ftp_user, $ftp_pass);

// check connection
if ((!$ftp_conn) || (!$login_result)) {
       echo "FTP connection has failed!";
       //echo "Attempted to connect to $ftp_server for user $ftp_user";
       exit;
   } else {
       //echo "Connected to $ftp_server, for user $ftp_user";
   }


$project = $_POST["project"];
$scope = $_POST["scope"];
$date = $_POST["sdate"];
$type = $_POST["type"];
$client = $_POST["client"];
$details = $_POST["details"];
$uploadDir = 'https://finstechnologies.com/rockypetghana/'.'uploads/'; 
$fileName = $_FILES['image']['name'];
$filePath = $uploadDir . $fileName;
$form_type = $_POST["form_type"];



if($form_type == "save"){
try {
    $destination_path = getcwd().DIRECTORY_SEPARATOR;
    $target_path = $destination_path .'../../../uploads/'. basename( $_FILES["image"]["name"]);
    @move_uploaded_file($_FILES['image']['tmp_name'], $target_path);

    $image_info = getimagesize($target_path);
    $image_width = $image_info[0];
    $image_height = $image_info[1];

    if($image_width == 640 && $image_height == 420){
      $conn = new PDO("mysql:host=$dbhost;dbname=$db_name", $dbuser, $dbpass);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $details = nl2br(htmlentities($details, ENT_QUOTES, 'UTF-8'));
      $sql = "insert into rockypet_projects (project, scope, date, type, details, image_path, client) 
                values('$project','$scope','$date','$type','$details', '$filePath', '$client')";
      $conn->exec($sql);
      //echo "Success";
      header("Location: http://finstechnologies.com/rockypetghana/admin/ui/project_all.php");
        die();
}else{
        echo '<script language="javascript">';
        echo 'alert("Image size not valid (should be 640w x 420h)")';
        echo '</script>';
        unlink($target_path);
        header("Location: http://finstechnologies.com/rockypetghana/admin/ui/project_all.php");
        die();
    }
    } catch(PDOException $e) {
      echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
}

if($form_type == "update"){
try {
    $destination_path = getcwd().DIRECTORY_SEPARATOR;
    $target_path = $destination_path .'../../../uploads/'. basename( $_FILES["image"]["name"]);
    @move_uploaded_file($_FILES['image']['tmp_name'], $target_path);


    if(strcmp($filePath,"https://finstechnologies.com/rockypetghana/uploads/") != 0){
        $image_info = getimagesize($target_path);
        $image_width = $image_info[0];
        $image_height = $image_info[1];
        if(($image_width == 640 && $image_height == 420)){
            
        }else{
            echo '<script language="javascript">';
            echo 'alert("Image size not valid (should be 640w x 420h)")';
            echo '</script>';
            unlink($target_path);
            header("Location: http://finstechnologies.com/rockypetghana/admin/ui/project_all.php");
            die();
        }
    }
      $conn = new PDO("mysql:host=$dbhost;dbname=$db_name", $dbuser, $dbpass);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql ="";
      $details = nl2br(htmlentities($details, ENT_QUOTES, 'UTF-8'));
      if(strcmp($filePath,"https://finstechnologies.com/rockypetghana/uploads/") == 0){
            $sql = "update rockypet_projects set project='$project', scope='$scope', date='$date', type='$type', details='$details', client='$client' where project_id=".$_POST['project_id'];
      }else{
          $sql = "update rockypet_projects set project='$project', scope='$scope', date='$date', type='$type', details='$details', image_path='$filePath', client='$client' where project_id=".$_POST['project_id'];
      }    
      $conn->exec($sql);
      //echo "Success";
      header("Location: http://finstechnologies.com/rockypetghana/admin/ui/project_all.php");
     die();
    
    } catch(PDOException $e) {
      echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
}

}
    
?>