<?
    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['btnaddpresscoverage']))
    {
        if(!adminchecklogin('', '', '', $G_userid)){
            $redirect = ADMIN_DIR."auth/login.php";
            header("location:".$redirect);
            exit();
        }
        //echo "<pre>";
        //print_r($_POST);
        //die();
        $presscoverageid = intval($_POST['presscoverageid']);
        $presscoveragetitle = sanitize($_POST['presscoveragetitle'], SQL);
        $presscoveragesubtitle = sanitize($_POST['presscoveragesubtitle'], SQL+HTML);
        
        $presscoverageurl    =   cleanURL($presscoveragetitle);
        //$pagedesc = sanitize($_POST['pagedesc'], SQL);
        //$pagedesc = str_replace("'", "/", $_POST['pagedesc']);
        $presscoveragedesc = trim($_POST['presscoveragedesc']);
        
        $presscoverageshortdesc = sanitize($_POST['presscoveragehortdesc'], SQL+HTML);
        $presscoverageimage = $_POST['presscoverageimage1'];
        $presscoveragedate = $_POST['presscoveragedate'];
        $presscoveragetimeing = $_POST['presscoveragetimeing'];
        $newscoveragebytitle = $_POST['newscoveragebytitle'];
        $imagesource = $_POST['imagesource'];
        $newscoveragebyimage =       $_POST['newscoveragebyimage1'];
        
        $valid = "";
        $valid .= $Validation->check_blank("presscoveragetitle", $presscoveragetitle, "presscoverage Title");
        $valid .= $Validation->check_blank("presscoveragedesc", $presscoveragedesc, "presscoverage Description");
        //$valid .= $Validation->check_blank("presscoverageimagepath", $presscoverageimage, "presscoverage Image");
        
        $valid .= $Validation->check_blank("newscoveragebytitle", $newscoveragebytitle, "newscoverageby Title");
        //$valid .= $Validation->check_blank("newscoveragebyimagepath", $newscoveragebyimage, "newscoverageby Image");
        //$valid .= $Validation->check_blank("blogshortdesc", $blogshortdesc, "Blog Short Description");
        
        if($valid){
            if($presscoverageid > 0 && $presscoverageid != 0 && $presscoverageid != null && $presscoverageid != ''){
                $redirect = ADMIN_DIR."add-presscoverage.php?presscoverageid=".$presscoverageid;
                header("location:".$redirect);
                exit();
            }else{
                $redirect = ADMIN_DIR."add-presscoverage.php";
                header("location:".$redirect);
                exit(); 
            }
            $redirect = ADMIN_DIR."add-presscoverage.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();
            $presscoveragearray['presscoveragetitle'] = trim($presscoveragetitle);
            $presscoveragearray['presscoveragesubtitle'] = trim($presscoveragesubtitle);

            $presscoveragearray['presscoveragedesc'] = $presscoveragedesc;
            $presscoveragearray['presscoverageurl'] = $presscoverageurl;
            $presscoveragearray['presscoverageshortdesc'] = $presscoverageshortdesc;
            $presscoveragearray['presscoverageimage'] = $presscoverageimage;
            
            $presscoveragearray['presscoverageby'] = trim($newscoveragebytitle);
            //$presscoveragearray['presscoverageby'] = $newscoveragebytitle;
            $presscoveragearray['presscoveragelogo'] = $newscoveragebyimage;
            $presscoveragearray['presscoverageimagelogosource'] = $imagesource;
            if($presscoveragedate !='')
            {
                $presscoveragearray['presscoveragedate'] = date("Y-m-d",strtotime($presscoveragedate)); 
            }
            else
            {
                 $presscoveragearray['presscoveragedate'] = date("Y-m-d");
            }
            
            $presscoveragearray['presscoveragetimeing'] = $presscoveragetimeing;
            $presscoveragearray['status'] = $_POST['status'];
            
           // print_r($presscoveragearray);
            
            if($presscoverageid > 0 && $presscoverageid != 0 && $presscoverageid != null && $presscoverageid != '')
            {
                $where = "presscoverageid = ".$presscoverageid;
                $addpresscoverage = $alltable->EditInfo($presscoveragearray, $where, PRESSCOVERAGE_TABLE);
                if($addpresscoverage)
                {
                    $_SESSION['pageaddsuccess'] = "Press Coverage Updated Successfully";
                    $redirect = ADMIN_DIR."view-presscoverage.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            else
            {
                $addpresscoverage = $alltable->AddInfo($presscoveragearray, PRESSCOVERAGE_TABLE);
                if($addpresscoverage)
                {
                    $_SESSION['pageaddsuccess'] = "Press Coverage Added Successfully";
                    $redirect = ADMIN_DIR."view-presscoverage.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            
            
        }
        
    }
    else if($G_userid > 0 && $G_userid != null && $G_userid != '')
    {
        if(isset($_GET['presscoverageid']))
        {
            $presscoverageid = intval($_GET['presscoverageid']);
        
            $sql = "select * from ".PRESSCOVERAGE_TABLE." where presscoverageid = $presscoverageid limit 0,1";
            $data = $alltable->getInfo('', $sql);
            $count = count($data);
            
            if($count > 0)
            {
                $presscoveragetitle = $data[0]['presscoveragetitle'];
                $presscoveragedesc = $data[0]['presscoveragedesc'];
                $presscoveragesubtitle = $data[0]['presscoveragesubtitle'];
                $presscoverageimage = $data[0]['presscoverageimage'];
                $presscoverageshortdesc = $data[0]['presscoverageshortdesc'];
                $status = $data[0]['status'];
                $presscoveragetimeing = $data[0]['presscoveragetimeing'];
                $presscoveragebyimage = $data[0]['presscoveragelogo'];
                $presscoverageby = $data[0]['presscoverageby'];
                $imagesource = $data[0]['presscoverageimagelogosource'];
                $presscoveragedate = date("d-m-Y",strtotime($data[0]['presscoveragedate']));
            }
            else
            {
                $redirect = ADMIN_DIR."view-presscoverage.php";
                header("location:".$redirect);
                exit();
            }
            
            $formtitle = "Edit Press Coverage";
        }
        else
        {
            $formtitle = "Add New Press Coverage";
        }
        
        require_once "add-presscoverage1.html";
        unset($_SESSION['presscoveragetitle']);
        unset($_SESSION['presscoveragedesc']);
        unset($_SESSION['pageaddsuccess']);
    }
    else
    {
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }   
?>