<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/config.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "taken") 
    $timeTaken = $paramValue;  
  else if ($paramName == "selectid") 
    $pf_id = $paramValue;  
}

$dal = new DAL();

$ph_id = hexdec(uniqid());

$sql = "INSERT INTO photo (id, time_taken) VALUES ($ph_id, '$timeTaken') ";
$result = $dal->query($sql);       

$sql = "SELECT refno " .
       "FROM parcelfop PF,parcel PA, project PR " .
       "WHERE PF.id = $pf_id AND PA.id = PF.parcel_id " .
       "AND PR.id = PA.project_id LIMIT 1 ";
$refno = $dal->query($sql)->refno;       

$path = PHOTO_PATH . $refno ;
$fileName = $refno . "_" . str_pad($ph_id, 8, '0', STR_PAD_LEFT);
$filePath = $path . "/" . $fileName;

$sql = "UPDATE photo SET filename='$filePath' WHERE id = $ph_id ";
$result = $dal->query($sql);       

$sfp_id = hexdec(uniqid());

$sql = "INSERT INTO spatialunitfophoto (id, photo_id, spatialunitfop_id) " .
         "VALUES ($sfp_id, $ph_id, (SELECT id FROM spatialunitfop WHERE parcelfop_id = $pf_id)) "; 
$result = $dal->query($sql);
    
echo json_encode(array($ph_id, $fileName, $refno, $path, $timeTaken));

?>

