<?
require_once "../include/config.php";
require_once GLOBAL_INCLUDE_PATH . "include/startup.php";
//  ini_set('display_errors',1);
//error_reporting(E_ALL);

$alltable = new AllTables();
$Validation = new Validation();

if (!adminchecklogin('', '', '', $G_userid)) {
    $redirect = ADMIN_DIR . "auth/login.php";
    header("location:" . $redirect);
    exit();
}
if (isset($_POST['btnyoutube'])) {
    if (!adminchecklogin('', '', '', $G_userid)) {
        $redirect = ADMIN_DIR . "auth/login.php";
        header("location:" . $redirect);
        exit();
    }
    // echo "<pre>";
    // print_r($_POST);
    // die();
    $youtubeid  = intval($_POST['youtubeid']);
    $displayorder = sanitize($_POST['displayorder'], SQL + HTML);
    $youtubelink = sanitize($_POST['youtubelink'], SQL);
    $status = intval($_POST['status']);




    $valid = "";
    $valid .= $Validation->check_blank("displayorder", $displayorder, "Display Order");
    $valid .= $Validation->check_blank("youtubelink", $youtubelink, "youTube link");
    //$valid .= $Validation->check_blank("Linkimagepath", $Linkimage, "Link Image");

    if ($valid) {
        // echo "1";
        // die();
        if ($youtubeid  > 0 && $youtubeid  != 0 && $youtubeid  != null && $youtubeid  != '') {
            $redirect = ADMIN_DIR . "add-youtube-videos.php?youtubeid =" . $youtubeid;
            header("location:" . $redirect);
            exit();
        } else {
            $redirect = ADMIN_DIR . "add-youtube-videos.php";
            header("location:" . $redirect);
            exit();
        }
        $redirect = ADMIN_DIR . "add-youtube-videos.php";
        header("location:" . $redirect);
        exit();
    } else {
        $youTubearray['displayorder'] = trim($displayorder);
        $youTubearray['youtubelink'] = $youtubelink;
        $youTubearray['status'] = $status;
  
      
       
   
        if ($youtubeid  > 0 && $youtubeid  != 0 && $youtubeid  != null && $youtubeid  != '') {
            $where = "youtubeid  = " . $youtubeid;
           $addlinks = $alltable->EditInfo($youTubearray, $where, YOUTUBE_TABLE);
            if ($addlinks) {
                $redirect = ADMIN_DIR . "view-youtube-videos.php";

                echo '<script>
                alert("youTube Link Updated Successfully");                  
                window.location.href = "' . $redirect . '";    
              </script>';

                exit();
            }
        } else {
            $addlinks = $alltable->AddInfo($youTubearray, YOUTUBE_TABLE);
            if ($addlinks) {
                $redirect = ADMIN_DIR . "view-youtube-videos.php";  
                echo '<script>
                alert("YouTube link Added Successfully");
                window.location.href = "' . $redirect . '";
              </script>';     
                exit();
            }
        }
    }   
} else if ($G_userid > 0 && $G_userid != null && $G_userid != '') {
    if (isset($_GET['youtubeid'])) {
        $youtubeid  = intval($_GET['youtubeid']);
        

        $sql = "select * from " . YOUTUBE_TABLE . " where youtubeid  = $youtubeid  limit 0,1";
        $data = $alltable->getInfo('', $sql);
        // echo"<pre>";
        // print_r($data);
        $count = count($data);   

        if ($count > 0) {
            $displayorder = $data[0]['displayorder'];
            $youtubelink = $data[0]['youtubelink'];

            $status = $data[0]['status'];
        } else {
            $redirect = ADMIN_DIR . "view-youtube-videos.php";
            header("location:" . $redirect);
            exit();
        }   

        $formtitle = "Edit Link";
    } else {
        $formtitle = "Add New Link";
    }   

    require_once "add-youtube-videos1.html";
    unset($_SESSION['Linktitle']);
    unset($_SESSION['Linkdesc']);
    unset($_SESSION['pageaddsuccess']);
} else {
    $redirect = ADMIN_DIR . "auth/login.php";
    header("location:" . $redirect);
    exit();
}
