require_once 'header.php';
$controller = new ControllerPhoto();
$controllerRealEstate = new ControllerRealEstate();
$realestates = $controllerRealEstate->getRealEstates();
$extras = new Extras();
$realestate_id = $extras->decryptQuery1(KEY_SALT, $_SERVER['QUERY_STRING']);
$backUrl = $extras->encryptQuery1(KEY_SALT, 'realestate_id', $realestate_id, 'photo_realestate_view.php');
if (isset($_POST['url_upload'])) {
    $itm = new Photo();
    $itm->photo_url = trim($_POST['photo_url']);
    $itm->thumb_url = trim($_POST['thumb_url']);
    $itm->realestate_id = $realestate_id;
    $itm->created_at = time();
    $itm->updated_at = time();
    $controller->insertPhoto($itm);
    echo "<script type='text/javascript'>location.href='{$backUrl}';</script>";
}
if (isset($_POST['file_upload'])) {
    $count = count($_FILES["file"]["name"]);
    if (!empty($_FILES["file"]["name"][0]) && !empty($_FILES["file"]["name"][1])) {
        uploadFile($controller, $realestate_id);
    } else {
        echo "<script>alert('You must provide both Photo and Thumbnail file.');</script>";
    }
}
if ($realestate_id == null) {
    echo "<script type='text/javascript'>location.href='403.php';</script>";
}
?>
     $thumb_new_file_name = $desired_dir . "/" . "thumb_" . $id . "." . $extension;
     $thumb_new_file_name_path = $desired_dir_path . "/" . "thumb_" . $id . "." . $extension;
     move_uploaded_file($_FILES['thumb_file']['tmp_name'], $thumb_new_file_name_path);
     $itm->thumb_url = ROOT_URL . $thumb_new_file_name;
     $id = uniqid();
     $temp = explode(".", $_FILES["photo_file"]["name"]);
     $extension = end($temp);
     $photo_new_file_name = $desired_dir . "/" . "photo_" . $id . "." . $extension;
     $photo_new_file_name_path = $desired_dir_path . "/" . "photo_" . $id . "." . $extension;
     move_uploaded_file($_FILES['photo_file']['tmp_name'], $photo_new_file_name_path);
     $itm->photo_url = ROOT_URL . $photo_new_file_name;
     if ($photo != null) {
         $controllerPhoto->updatePhoto($itm);
         $photo_id = $itm->photo_id;
     } else {
         $controllerPhoto->insertPhoto($itm);
         $photo_id = $controllerPhoto->getLastInsertedPhotoId();
     }
     $itm = $controllerPhoto->getPhotoByPhotoId($photo_id);
     $json = "{\n                            \"photo_realestate_info\" : {\n                                          \"photo_id\" : \"{$itm->photo_id}\",\n                                          \"photo_url\" : \"{$itm->photo_url}\",\n                                          \"thumb_url\" : \"{$itm->thumb_url}\",\n                                          \"realestate_id\" : \"{$itm->realestate_id}\",\n                                          \"created_at\" : \"{$itm->created_at}\",\n                                          \"updated_at\" : \"{$itm->updated_at}\",\n                                          \"is_deleted\" : \"0\"\n                                          },\n                            \"status\" : {\n                                          \"status_code\" : \"-1\",\n                                          \"status_text\" : \"Success.\"\n                                        }\n                            }";
 } else {
     if ($photo != null) {
         $controllerPhoto->updatePhoto($itm);
         $photo_id = $itm->photo_id;
     } else {
         $controllerPhoto->insertPhoto($itm);
         $photo_id = $controllerPhoto->getLastInsertedPhotoId();
     }
     $itm = $controllerPhoto->getPhotoByPhotoId($photo_id);
     $json = "{\n                            \"photo_realestate_info\" : {\n                                          \"photo_id\" : \"{$itm->photo_id}\",\n                                          \"photo_url\" : \"{$itm->photo_url}\",\n                                          \"thumb_url\" : \"{$itm->thumb_url}\",\n                                          \"realestate_id\" : \"{$itm->realestate_id}\",\n                                          \"created_at\" : \"{$itm->created_at}\",\n                                          \"updated_at\" : \"{$itm->updated_at}\",\n                                          \"is_deleted\" : \"0\"\n                                          },\n                            \"status\" : {\n                                          \"status_code\" : \"-1\",\n                                          \"status_text\" : \"Success.\"\n                                        }\n                            }";
 }