<?php

session_start();
ob_start();
$do = isset($_GET['do']) ? $_GET['do'] : "";
$ajaxDoArr = array("add2SimpleReturn");
include("initiateStaticSessionCommingWithCurl.php");
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
    array_push($ajaxDoArr, $do);
}

if (!in_array($do, $ajaxDoArr)) {
    include("../public/impOpreation.php");
}



include_once("../public/config.php");
include("../library/newUplode/uploadfunctions.php");


require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');




//get the do the action
$do = $_GET['do'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");


if (!in_array($do, $ajaxDoArr)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

    } else {
        $smarty->display("header.html");
    }
}



$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

$today = date("Y-m-d");
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    $smarty->display("pagesview/add.html");
} elseif ($do == "add") {
    try {
        add();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        //echo $e;
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "show") {
    $showData = R::findAll('pages', 'isdel = 0');
    $smarty->assign('showData', $showData);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("pagesview/show.html");
    $smarty->assign("customCheck", 1);
} elseif ($do == "deleteFinaly") {
    try {
        $id = (int) $_GET['id'];
        deleteFinaly($id);

        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "edit") { //edit  productCat
    $id = $_GET['id'];
    $showData = R::load('pages', $id);
    $smarty->assign('showData', $showData);

    $smarty->display("pagesview/edit.html");
} elseif ($do == "update") { //edit update
    try {
        update();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}


$smarty->assign("customValidation", 1);

//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

    } else {
        $smarty->display("footer.html");
    }
}

/* ===============================
  function in this CONTROLLER
  ================================ */

// add in productCat tbl
function add() {
    $title = $_POST['title'];
    $titleEn = $_POST['titleEn'];
    $contentOrExternalLink = (int) $_POST['contentOrExternalLink'];
    if ($contentOrExternalLink == 0) {
        $content = $_POST['content'];
        $contentEn = $_POST['contentEn'];
    } else {
        $externalLink = $_POST['externalLink'];
        $openTarget = (int) $_POST['openTarget'];
    }
    $isActive = (int) $_POST['isActive'];
    $image = uploadnew('image', False, 0, 0, 'pages');
    
    $rdispense = R::dispense('pages');
    $rdispense->title = $title ;
    $rdispense->titleEn = $titleEn;
    $rdispense->contentOrExternalLink = 0;
    $rdispense->image = $image;
    $rdispense->content = $content;
    $rdispense->contentEn = $contentEn;
    $rdispense->externalLink = '';
    $rdispense->openTarget = 0;
    $rdispense->isActive = $isActive;
    $rdispense->sysDate = date("Y-m-d H:i:s");
    $rdispense->userid = $_SESSION['userid'];
    $rdispense->isdel = 0;
    $id = R::store($rdispense);
    return $id;
}

function update() {

    $id = $_POST['id'];
    $title = $_POST['title'];
    $titleEn = $_POST['titleEn'];
    $contentOrExternalLink = (int) $_POST['contentOrExternalLink'];
    if ($contentOrExternalLink == 0) {
        $content = $_POST['content'];
        $contentEn = $_POST['contentEn'];
    } else {
        $externalLink = $_POST['externalLink'];
        $openTarget = (int) $_POST['openTarget'];
    }
    $isActive = (int) $_POST['isActive'];
    $image = uploadupdate('image', 'imageurl', False, 0, 0, 'pages');
    
    $rupdate = R::load('pages', $id);
    $rupdate->title = $title ;
    $rupdate->titleEn = $titleEn;
    $rupdate->contentOrExternalLink = 0;
    $rupdate->image = $image;
    $rupdate->content = $content;
    $rupdate->contentEn = $contentEn;
    $rupdate->externalLink = '';
    $rupdate->openTarget = 0;
    $rupdate->isActive = $isActive;
    $rupdate->isdel = 0;
    $id = R::store($rupdate);
}

function deleteFinaly($id) {
    $rtrash = R::load('pages', $id);
    if (file_exists('../upload/pages/' . $rtrash->image)) {
        chmod('../upload/pages/' . $rtrash->image, 0777);
        unlink('../upload/pages/' . $rtrash->image);
    }
    R::trash($rtrash);
}
