<?php

session_start();
ob_start();
//the global file operation
$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)) {
    //the global file operation
    include("../public/impOpreation.php");
}
//Page
require_once('../models/dao/PagesDAO.class.php');
require_once('../models/dto/Page.class.php');
require_once('../models/mysql/PagesMySqlDAO.class.php');
require_once('../models/mysql/ext/PagesMySqlExtDAO.class.php');

// get the config file
include_once("../public/config.php");
include("../library/uploadImages.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/productCat/productCat.php");
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

/* ======================

  Controller Name :- productCatController

  OPERTATION in Controller

  1-display add form
  2- add in productCat tble
  4-display sucess or error
  5-display show and delete
  6-delete from productCat tbl
  7-display edit
  8- update productCat tbl
  ======================== */

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

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

//here goes the instances and general variables
//Productcat
$pages = new Page();
$pagesDAO = new PagesMySqlDAO();
$pagesEX = new PagesMySqlExtDAO();

$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 = $pagesDAO->queryByIsdel(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 = $pagesDAO->load($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() {
    global $pages;
    global $pagesDAO;

    $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'];



    $pages->title = $title;
    $pages->titleEn = $titleEn;
    $pages->contentOrExternalLink = $contentOrExternalLink;
    $pages->content = $content;
    $pages->contentEn = $contentEn;
    $pages->externalLink = $externalLink;
    $pages->openTarget = $openTarget;
    $pages->isActive = $isActive;

    $pages->sysDate = date("Y-m-d H:i:s");
    $pages->userid = $_SESSION['userid'];
    $pages->isdel = 0;

    $handle = new upload($_FILES['logo']);
    $image = uploadImages2($handle, '../upload/pages');
    $pages->image = $image;
    //print_r($productCat);
    $pageId = $pagesDAO->insert($pages);

    return $pageId;
}

function update() {
    global $pagesDAO;

    $id = $_POST['id'];
    $pages = $pagesDAO->load($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'];

    $pages->title = $title;
    $pages->titleEn = $titleEn;
    $pages->contentOrExternalLink = $contentOrExternalLink;
    $pages->content = $content;
    $pages->contentEn = $contentEn;
    $pages->externalLink = $externalLink;
    $pages->openTarget = $openTarget;
    $pages->isActive = $isActive;

    $pages->sysDate = date("Y-m-d H:i:s");
    $pages->userid = $_SESSION['userid'];
    $pages->isdel = 0;

    $handle = new upload($_FILES['logo']);
    $image = updateImagesWithoutresiz($handle, "oldlogo", '../upload/pages');
    $pages->image = $image;
    $pagesDAO->update($pages);
}

function deleteFinaly($id) {
    global $pagesDAO;
    $pages = $pagesDAO->load($id);
    unlink('../upload/pages/' . $pages->image);
    $delcatdata = $pagesDAO->delete($id);
}
