<?
    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['btnaddnews']))
    {
        if(!adminchecklogin('', '', '', $G_userid)){
            $redirect = ADMIN_DIR."auth/login.php";
            header("location:".$redirect);
            exit();
        }
        //echo "<pre>";
        //print_r($_POST);
        //die();
        $newsid = intval($_POST['newsid']);
        $newstitle = sanitize($_POST['newstitle'], SQL);
        $newssubtitle = sanitize($_POST['newssubtitle'], SQL+HTML);
        
        $newsurl    =   cleanURL($newstitle);
        //$pagedesc = sanitize($_POST['pagedesc'], SQL);
        //$pagedesc = str_replace("'", "/", $_POST['pagedesc']);
        $newsdesc = trim($_POST['newsdesc']);
        
        $newsshortdesc = sanitize($_POST['newshortdesc'], SQL+HTML);
        $newsimage = sanitize($_POST['newsimage1'],SQL+HTML);
        $newsdate = $_POST['newsdate'];
        $newstimeing = $_POST['newstimeing'];
        $newsvolumearray = explode('|',$_POST['cmbvolume']);
        $newsvolume = intval($newsvolumearray[0]);
        $newsorderinvolume = intval($_POST['newsorderinvolume']);
        $newsvolumelabel = $newsvolumearray[1];
        
        $valid = "";
        $valid .= $Validation->check_blank("newstitle", $newstitle, "News Title");
        $valid .= $Validation->check_blank("newsdesc", $newsdesc, "News Description");
        $valid .= $Validation->check_blank("newsimagepath", $newsimage, "News Image");
        
        //$valid .= $Validation->check_blank("blogshortdesc", $blogshortdesc, "Blog Short Description");
        
        if($valid){
            if($newsid > 0 && $newsid != 0 && $newsid != null && $newsid != ''){
                $redirect = ADMIN_DIR."add-news.php?newsid=".$newsid;
                header("location:".$redirect);
                exit();
            }else{
                $redirect = ADMIN_DIR."add-news.php";
                header("location:".$redirect);
                exit(); 
            }
            $redirect = ADMIN_DIR."add-news.php";
            header("location:".$redirect);
            exit();    
        }else{
            /*
            $doc = new DOMDocument();
            $doc->loadHTML($blogdesc);
            $xml = simplexml_import_dom($doc);
            $images = $xml->xpath('//img');
            
            foreach ($images as $img)
            {
                //$src =  str_replace('\"', "", $pagedesc);
                $docrootpath = str_replace('root/assets/', DOC_ROOT.'assets/', $blogdesc);
//                if(strpos($src, 'http') == false) { 
//                    $docrootpath = str_replace('assets/', DOC_ROOT.'assets/', $src);
//                }else{
//                    $docrootpath = $src;
//                }
            }
            */
            //die();
            $newsarray['newstitle'] = trim($newstitle);
            $newsarray['newssubtitle'] = trim($newssubtitle);

            $newsarray['newsdesc'] = $newsdesc;
            $newsarray['newsurl'] = $newsurl;
            $newsarray['newsshortdesc'] = $newsshortdesc;
            $newsarray['newsimage'] = $newsimage;
            $newsarray['newsvolume'] = $newsvolume;
            $newsarray['newsorderinvolume'] = $newsorderinvolume;
            $newsarray['newsvolumelabel'] = $newsvolumelabel;
            
            if($newsdate !='')
            {
                $newsarray['newsdate'] = date("Y-m-d",strtotime($newsdate));
            }
            else
            {
                 $newsarray['newsdate'] = date("Y-m-d");
            }
            
            $newsarray['newstimeing'] = $newstimeing;
            $newsarray['status'] = $_POST['status'];
            
            
            if($newsid > 0 && $newsid != 0 && $newsid != null && $newsid != '')
            {
                $where = "newsid = ".$newsid;
                $addnews = $alltable->EditInfo($newsarray, $where, NEWS_TABLE);
                if($addnews)
                {
                    $_SESSION['pageaddsuccess'] = "News Updated Successfully";
                    $redirect = ADMIN_DIR."view-news.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            else
            {
                $addnews = $alltable->AddInfo($newsarray, NEWS_TABLE);
                if($addnews)
                {
                    $_SESSION['pageaddsuccess'] = "News Added Successfully";
                    $redirect = ADMIN_DIR."view-news.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            
            
        }
        
    }
    else if($G_userid > 0 && $G_userid != null && $G_userid != '')
    {
        
        if(isset($_GET['newsid']))
        {
            $newsid = intval($_GET['newsid']);
        
            $sql = "select * from ".NEWS_TABLE." where newsid = $newsid limit 0,1";
            $data = $alltable->getInfo('', $sql);
            $count = count($data);
            
            if($count > 0)
            {
                $newstitle = $data[0]['newstitle'];
                $newsdesc = $data[0]['newsdesc'];
                $newssubtitle = $data[0]['newssubtitle'];
                $newsimage = $data[0]['newsimage'];
                $newsshortdesc = $data[0]['newsshortdesc'];
                $status = $data[0]['status'];
                $newstimeing = $data[0]['newstimeing'];
                $newsdate = date("d-m-Y",strtotime($data[0]['newsdate']));
                $newsvolume = $data[0]['newsvolume'];
                $newsorderinvolume = $data[0]['newsorderinvolume'];
                
            }
            else
            {
                $redirect = ADMIN_DIR."view-news.php";
                header("location:".$redirect);
                exit();
            }
            
            $formtitle = "Edit News";
        }
        else
        {
            $formtitle = "Add New News";
        }
        $volumedropdownoptions = getVolumeCombo($newsvolume);
        require_once "add-news1.html";
        unset($_SESSION['newstitle']);
        unset($_SESSION['newsdesc']);
        unset($_SESSION['pageaddsuccess']);
    }
    else
    {
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }   
?>