<?
require_once "../include/config.php";
require_once GLOBAL_INCLUDE_PATH . "include/startup.php";


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

if (!adminchecklogin('', '', '', $G_userid)) {
    $redirect = ADMIN_DIR . "auth/login.php";
    header("location:" . $redirect);
    exit();
}
if (isset($_POST['btnaddpressreleases'])) {
    if (!adminchecklogin('', '', '', $G_userid)) {
        $redirect = ADMIN_DIR . "auth/login.php";
        header("location:" . $redirect);
        exit();
    }
    // echo "<pre>";
    // print_r($_POST);
    // die();
    $pressreleasesid = intval($_POST['pressreleasesid']);
    $pressreleasestitle = sanitize($_POST['pressreleasestitle'], SQL);

    $pressreleasesdesc = trim($_POST['pressreleasesdesc']);

    $pressreleasesdate = $_POST['pressreleasesdate'];


    $valid = "";
    $valid .= $Validation->check_blank("pressreleasestitle", $pressreleasestitle, "pressreleases Title");
    $valid .= $Validation->check_blank("pressreleasesdesc", $pressreleasesdesc, "pressreleases Description");
    $valid .= $Validation->check_blank("pressreleasesdate", $pressreleasesdate, "pressreleases Date");

    if ($valid) {
        if ($pressreleasesid > 0 && $pressreleasesid != 0 && $pressreleasesid != null && $pressreleasesid != '') {
            $redirect = ADMIN_DIR . "add-press-releases.php?pressreleasesid=" . $pressreleasesid;
            header("location:" . $redirect);
            exit();
        } else {
            $redirect = ADMIN_DIR . "add-press-releases.php";
            header("location:" . $redirect);
            exit();
        }
        $redirect = ADMIN_DIR . "add-press-releases.php";
        header("location:" . $redirect);
        exit();
    } else {
        $pressreleasesarray['pressreleasestitle'] = trim($pressreleasestitle);

        $pressreleasesarray['pressreleasesdesc'] = $pressreleasesdesc;

        if ($pressreleasesdate != '') {
            $pressreleasesarray['pressreleasesdate'] = date("Y-m-d", strtotime($pressreleasesdate));
        } else {
            $pressreleasesarray['pressreleasesdate'] = date("Y-m-d");
        }

        $pressreleasesarray['status'] = $_POST['status'];

        // print_r($pressreleasesarray);       

        if ($pressreleasesid > 0 && $pressreleasesid != 0 && $pressreleasesid != null && $pressreleasesid != '') {
            $where = "pressreleasesid = " . $pressreleasesid;
            $addpressreleases = $alltable->EditInfo($pressreleasesarray, $where, PRESSRELEAS_TABLE);
            if ($addpressreleases) {
                $redirect = ADMIN_DIR . "view-press-releases.php";
                echo '<script>
                        alert("Press Release Updated Successfully");
                        window.location.href = "' . $redirect . '";
                      </script>';
                exit();
            } 
        } else {
            $addpressreleases = $alltable->AddInfo($pressreleasesarray, PRESSRELEAS_TABLE);

            if ($addpressreleases) {
                $redirect = ADMIN_DIR . "view-press-releases.php";
                echo '<script>
                        alert("Press Release Added Successfully");
                        window.location.href = "' . $redirect . '";
                      </script>';
                exit();
            }
        }
    }
} else if ($G_userid > 0 && $G_userid != null && $G_userid != '') {
    if (isset($_GET['pressreleasesid'])) {
        $getpressreleasesid = intval($_GET['pressreleasesid']);

        $sql = "select * from " . PRESSRELEAS_TABLE . " where pressreleasesid = $getpressreleasesid limit 0,1";
        $data = $alltable->getInfo('', $sql);
        $count = count($data);

        if ($count > 0) {
            $pressreleasesid = $data[0]['pressreleasesid'];
            $pressreleasestitle = $data[0]['pressreleasestitle'];
            $pressreleasesdesc = $data[0]['pressreleasesdesc'];
            $status = $data[0]['status'];
            $pressreleasesdate = date("d-m-Y", strtotime($data[0]['pressreleasesdate']));
        } else {
            $redirect = ADMIN_DIR . "view-press-releases.php";
            header("location:" . $redirect);
            exit();
        }

        $formtitle = "Edit Press Release";
    } else {
        $formtitle = "Add Press Releases";
    }


    require_once "add-press-releases1.html";
    unset($_SESSION['pressreleasestitle']);
    unset($_SESSION['pressreleasesdesc']);
    unset($_SESSION['pageaddsuccess']);
} else {
    $redirect = ADMIN_DIR . "auth/login.php";
    header("location:" . $redirect);
    exit();
}
