<?php

    include_once 'config.php';

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

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_services where service_id=".$_POST["service_id"];
      $conn->exec($sql);
      //echo "Success";
      header("Location: http://finstechnologies.com/rockypetghana/admin/ui/service_all.php");
      die();
}

if(isset($_POST['save_project']))
{
$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";
   }


$service = $_POST["service"];
$sub_title = $_POST["sub_title"];
$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){
        $details = nl2br(htmlentities($details, ENT_QUOTES, 'UTF-8'));
      $conn = new PDO("mysql:host=$dbhost;dbname=$db_name", $dbuser, $dbpass);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "insert into rockypet_services (service, subtitle, details, image_path) 
                values('$service','$sub_title','$details', '$filePath')";
      $conn->exec($sql);
      //echo "Success";
      header("Location: http://finstechnologies.com/rockypetghana/admin/ui/service_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/service_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);

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

    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/service_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_services set service='$service', subtitle='$sub_title', details='$details' where service_id=".$_POST['service_id'];
      }else{
         $sql = "update rockypet_services set service='$service', subtitle='$sub_title', details='$details', image_path='$filePath' where service_id=".$_POST['service_id']; 
      }
    //echo  $sql;       
      $conn->exec($sql);
      //echo "Success";
      header("Location: http://finstechnologies.com/rockypetghana/admin/ui/service_all.php");
      die();
    
    } catch(PDOException $e) {
      echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
}
}
   
    
?>