<?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");
}

//Slider
require_once('../models/dao/SliderDAO.class.php');
require_once('../models/dto/Slider.class.php');
require_once('../models/mysql/SliderMySqlDAO.class.php');
require_once('../models/mysql/ext/SliderMySqlExtDAO.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
$slider = new Slider();
$sliderDAO = new SliderMySqlDAO();
$sliderEX = new SliderMySqlExtDAO();

$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("sliderview/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 = $sliderDAO->queryByIsdel(0);
    $smarty->assign('showData', $showData);


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

    $smarty->display("sliderview/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 = $sliderDAO->load($id);
    $smarty->assign('showData', $showData);

    $smarty->display("sliderview/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 $slider;
    global $sliderDAO;

    $title = $_POST['title'];
    $titleEn = $_POST['titleEn'];
    $titleUrl = $_POST['titleUrl'];
    $openTarget = $_POST['openTarget'];
    $mediaType = $_POST['mediaType'];
    $mediaUrl = $_POST['mediaUrl'];
    $content = $_POST['content'];
    $contentEn = $_POST['contentEn'];
    $position = $_POST['position'];
    $isActive = $_POST['isActive'];


    $slider->title = $title;
    $slider->titleEn = $titleEn;
    $slider->titleUrl = $titleUrl;
    $slider->openTarget = $openTarget;
    $slider->mediaType = $mediaType;
    $slider->mediaUrl = $mediaUrl;
    $slider->description = $content;
    $slider->descriptionEn = $contentEn;
    $slider->position = $position;
    $slider->isActive = $isActive;

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

    $sliderId = $sliderDAO->insert($slider);

    return $sliderId;
}

function update() {
    global $slider;
    global $sliderDAO;

    $id = $_POST['id'];
    $slider = $sliderDAO->load($id);

    $title = $_POST['title'];
    $titleEn = $_POST['titleEn'];
    $titleUrl = $_POST['titleUrl'];
    $openTarget = $_POST['openTarget'];
    $mediaType = $_POST['mediaType'];
    $mediaUrl = $_POST['mediaUrl'];
    $content = $_POST['content'];
    $contentEn = $_POST['contentEn'];
    $position = $_POST['position'];
    $isActive = $_POST['isActive'];


    $slider->title = $title;
    $slider->titleEn = $titleEn;
    $slider->titleUrl = $titleUrl;
    $slider->openTarget = $openTarget;
    $slider->mediaType = $mediaType;
    $slider->mediaUrl = $mediaUrl;
    $slider->description = $content;
    $slider->descriptionEn = $contentEn;
    $slider->position = $position;
    $slider->isActive = $isActive;

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

    $sliderDAO->update($slider);
}

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