<?php
include("../public/impOpreation.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');

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

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("associatedtagview/add.html");
    $smarty->display("footer.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $associatedtags = R::getAll('SELECT associatedtags.* FROM `associatedtags` WHERE conditions = 0');
    $smarty->assign('associatedtags', $associatedtags);
    $smarty->display("header.html");
    $smarty->display("associatedtagview/show.html");
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $id = filter_input(INPUT_GET, 'id');
    $edit = R::load('associatedtags', $id);
    $smarty->assign('edit', $edit);
    $smarty->display("header.html");
    $smarty->display("associatedtagview/edit.html");
    $smarty->display("footer.html");
}else if ($do == 'add') {
    $tagname = filter_input(INPUT_POST, 'tagname');
     try {
    $associatedtags = R::dispense('associatedtags');
    $associatedtags->today = $today;  
    $associatedtags->conditions = 0;        
    $associatedtags->userid = $userid;
    $associatedtags->tagname = $tagname;
    R::store($associatedtags);
        header("location:?do=sucess");
    } catch (Exception $e) {
        print_r($e);
        header("location:?do=error");
    }
}else if ($do == 'update') {
    $tagname = filter_input(INPUT_POST, 'tagname');
    $id = filter_input(INPUT_POST, 'id');
    try {
    $associatedtags = R::load('associatedtags',$id);
    $associatedtags->today = $today;          
    $associatedtags->userid = $userid;
    $associatedtags->tagname = $tagname;
    R::store($associatedtags);
        header("location:?do=sucess");
    } catch (Exception $e) {
        print_r($e);
        header("location:?do=error");
    }
}else if ($do == 'del') {
    $id = filter_input(INPUT_GET, 'id');
     try {
       $associatedtags = R::load('associatedtags',$id);
       $associatedtags->conditions = 1;    
       R::store($associatedtags);  
        header("location:?do=sucess");
    } catch (Exception $e) {
        print_r($e);
        header("location:?do=error");
    }
}else if ($do == 'getTags') {
    $name = $_GET['term']; //It could be product name or category name
    $limit = intval($_GET['page_limit']);

    $row_array = array();
    $return_arr = array();
    $resultData = R::getAll('SELECT * from associatedtags where conditions = 0 and tagname LIKE "%' . $name . '%" limit ' . $limit);
    foreach ($resultData as $value) {
        $row_array['id'] = $value['id'];
        $row_array['text'] = $value['tagname'];
        array_push($return_arr, $row_array);
    }
    //echo $return_arr;
    echo json_encode($return_arr);
} elseif ($do == "sucess") {
        $smarty->display("header.html");
        $smarty->display("succes.html");  
        $smarty->display("footer.html");   
} elseif ($do == "error") {
        $smarty->display("header.html");
        $smarty->display("error.html");
        $smarty->display("footer.html");
}





?>