<?
    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['btnaddvolume']))
    {
        if(!adminchecklogin('', '', '', $G_userid)){
            $redirect = ADMIN_DIR."auth/login.php";
            header("location:".$redirect);
            exit();
        }
        //echo "<pre>";
        //print_r($_POST);
        //die();
        $volumeid = intval(sanitize($_POST['volumeid'], SQL+HTML));
        $volumeno = sanitize($_POST['volumeno'], SQL+HTML);
        $volumetitle = sanitize($_POST['volumetitle'], SQL+HTML);
        
        $valid = "";
        $valid .= $Validation->check_blank("volumetitle", $volumetitle, "Volume Title");
        $valid .= $Validation->check_blank("volumeno", $volumeno, "Volume Description");
        
        if($valid){
            if($volumeid > 0 && $volumeid != 0 && $volumeid != null && $volumeid != ''){
                $redirect = ADMIN_DIR."add-volume.php?volumeid=".$volumeid;
                header("location:".$redirect);
                exit();
            }else{
                $redirect = ADMIN_DIR."add-volume.php";
                header("location:".$redirect);
                exit(); 
            }
            $redirect = ADMIN_DIR."add-volume.php";
            header("location:".$redirect);
            exit();    
        }else{
            
            //die();
            $volumearray['volumename'] = trim($volumetitle);
            $volumearray['volumeno'] = trim($volumeno);
            
            
            if($volumeid > 0 && $volumeid != 0 && $volumeid != null && $volumeid != '')
            {
                $where = "volumeid = ".$volumeid;
                $addvolume = $alltable->EditInfo($volumearray, $where, VOLUMES_TABLE);
                if($addvolume)
                {
                    $_SESSION['pageaddsuccess'] = "Volume Updated Successfully";
                    $redirect = ADMIN_DIR."view-volumes.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            else
            {
                $addvolume = $alltable->AddInfo($volumearray, VOLUMES_TABLE);
                if($addvolume)
                {
                    $_SESSION['pageaddsuccess'] = "Volume Added Successfully";
                    $redirect = ADMIN_DIR."view-volumes.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            
            
        }
        
    }
    else if($G_userid > 0 && $G_userid != null && $G_userid != '')
    {
        
        if(isset($_GET['volumeid']))
        {
            $volumeid = intval(sanitize($_GET['volumeid'], SQL+HTML));
        
            $sql = "select * from ".VOLUMES_TABLE." where volumeid = $volumeid limit 0,1";
            $data = $alltable->getInfo('', $sql);
            $count = count($data);
            
            if($count > 0)
            {
                $volumeid = $data[0]['volumeid'];
                $volumetitle = $data[0]['volumename'];
                $volumeno = $data[0]['volumeno'];
                
            }
            else
            {
                $redirect = ADMIN_DIR."view-volumes.php";
                header("location:".$redirect);
                exit();
            }
            
            $formtitle = "Edit Volume";
        }
        else
        {
            $formtitle = "Add New Volume";
        }
        
        require_once "add-volume1.html";
        unset($_SESSION['volumetitle']);
        unset($_SESSION['volumedesc']);
        unset($_SESSION['pageaddsuccess']);
    }
    else
    {
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }   
?>