<?php
include('config/db.config.inc.php');
include('config/ini.config.php');
include('config/session.chk.inc.php');

$db = new SQL();

if($_REQUEST['deleteid'])
{
	$query  = "UPDATE " . DB_PREFIX . "HOME_BANNER SET `ACTIVE` = '0' ,`MODIFIED_BY` = '".$_SESSION['userId']."' WHERE `BANNER_ID` = '".$_REQUEST['deleteid']."'";
	$type   = 'update';
	$msg    = 'Banner Deleted';
}

$PICTURE_1 = $_FILES['BANNER_IMAGE']['name'];
$PICTURE_1_ARRAY = explode(".", $PICTURE_1);
$string     = strtolower(str_replace(' ', '', $_POST['BANNER_NAME']));
$NEWARRAY   = $string.'-'.date('dmY').rand().".".$PICTURE_1_ARRAY['2']."".$PICTURE_1_ARRAY['1'];
$TARGET     = "images/banner/";
$TARGET     = $TARGET.$NEWARRAY;
move_uploaded_file($_FILES['BANNER_IMAGE']['tmp_name'], $TARGET);

if($_REQUEST['deleteid'] =='' && $_POST['BANNER_NAME'] != '')
{
	if($_POST['BANNER_ID'] == '' && $_REQUEST['deleteid'] =='' )
    {
        $query  = "INSERT INTO " . DB_PREFIX . "HOME_BANNER SET
                        `BANNER_NAME` = '".addslashes($_POST['BANNER_NAME'])."',";

        if($PICTURE_1 != "")
        {
            $query .= "`BANNER_IMAGE` = '".$NEWARRAY."',";
        }

        $query .= "     
                        `CREATED_BY` = '".$_SESSION['userId']."', `MODIFIED_BY` = '".$_SESSION['userId']."',
                        `CREATED_DATE` = NOW(), `MODIFIED_DATE` = NOW()";
        $type   = 'insert';
        $msg    = 'New Banner Created';
	}
	else
    {
		$query = "UPDATE " . DB_PREFIX . "HOME_BANNER SET
				        `BANNER_NAME` = '".addslashes($_POST['BANNER_NAME'])."', ";

        if($PICTURE_1 != "")
        {
            $query .= "`BANNER_IMAGE` = '".$NEWARRAY."',";
        }

		$query .= "     
                        `MODIFIED_BY` = '".$_SESSION['userId']."', `MODIFIED_DATE` = NOW()
                        WHERE `BANNER_ID` = '".$_POST['BANNER_ID']."'";
        $type = 'update';
        $msg = 'Banner details updated!';
	}
}

$db->ExecuteQuery($query,$type);
header('location:view_home_page.php?success=1&msg='.$msg);