<?
    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['btnaddarticle']))
    {
        if(!adminchecklogin('', '', '', $G_userid)){
            $redirect = ADMIN_DIR."auth/login.php";
            header("location:".$redirect);
            exit();
        }
        // echo "<pre>";                   
        // print_r($_POST);
        // die();
        $articleid          = intval($_POST['articleid']);
        $articletitle       = sanitize($_POST['articletitle'], SQL);
        $companyname        = sanitize($_POST['companyname'], SQL);
        $articleurl         = cleanURL($articletitle);
        $pagedesc           = sanitize($_POST['articledesc'], SQL);
        //$pagedesc = str_replace("'", "/", $_POST['pagedesc']);
        $articledesc        = trim($pagedesc);
        
        $articleshortdesc   = sanitize($_POST['articleshortdesc'], SQL+HTML);
        $articleimage       = $_POST['articleimage1'];
        
        
        $valid = "";
        $valid .= $Validation->check_blank("articletitle", $articletitle, "Article Title");       
        $valid .= $Validation->check_blank("companyname", $companyname, "Company Name");
        $valid .= $Validation->check_blank("articledesc", $articledesc, "Article Description");
        //$valid .= $Validation->check_blank("articleimagepath", $articleimage, "Article Image");
        $valid .= $Validation->check_blank("articleshortdesc", $articleshortdesc, "Article Short Description");
        
        if($valid){
            if($articleid > 0 && $articleid != 0 && $articleid != null && $articleid != ''){
                $redirect = ADMIN_DIR."add-exhibitor-articles.php?articleid=".$articleid;
                header("location:".$redirect);
                exit();
            }else{
                $redirect = ADMIN_DIR."add-exhibitor-articles.php";
                header("location:".$redirect);
                exit(); 
            }
            $redirect = ADMIN_DIR."add-exhibitor-articles.php";
            header("location:".$redirect);
            exit();    
        }else{
            /*
            $doc = new DOMDocument();
            $doc->loadHTML($articledesc);
            $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/', $articledesc);
//                if(strpos($src, 'http') == false) { 
//                    $docrootpath = str_replace('assets/', DOC_ROOT.'assets/', $src);
//                }else{
//                    $docrootpath = $src;
//                }
            }
            */
            //die();
            $articlearray['articletitle'] = trim($articletitle);
            $articlearray['companyname'] = trim($companyname);
            $articlearray['articledesc'] = $articledesc;
            $articlearray['articleurl'] = $articleurl;
            $articlearray['articleshortdesc'] = $articleshortdesc;
            $articlearray['articleimage'] = $articleimage;
            $articlearray['formlinks'] = $_POST['formlinks'];
            $articlearray['status'] = $_POST['status'];
            
            
            if($articleid > 0 && $articleid != 0 && $articleid != null && $articleid != '')
            {
                $where = "articleid = ".$articleid;
                
                 $articlearray['last_edited_by'] = $G_userid;
                $articlearray['last_edit_date'] = date("Y-m-d");
                
                $addarticle = $alltable->EditInfo($articlearray, $where, ARTICLES_TABLE);
                if($addarticle)
                {
                    $_SESSION['pageaddsuccess'] = "Article Updated Successfully";
                    $redirect = ADMIN_DIR."view-exhibitor-articles.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            else
            {
                
                $articlearray['entered_by'] = $G_userid;
                $articlearray['entry_date'] = date("Y-m-d");
                
                                        
                $addarticle = $alltable->AddInfo($articlearray, ARTICLES_TABLE);
                if($addarticle)
                {
                    $_SESSION['pageaddsuccess'] = "Article Added Successfully";
                    $redirect = ADMIN_DIR."view-exhibitor-articles.php";
                    header("location:".$redirect);
                    exit();    
                }
            }
            
            
        }
        
    }
    else if($G_userid > 0 && $G_userid != null && $G_userid != '')
    {
        if(isset($_GET['articleid']))
        {
            $articleid = intval($_GET['articleid']);
        
            $sql = "select * from ".ARTICLES_TABLE." where articleid = $articleid limit 0,1";
            $data = $alltable->getInfo('', $sql);
            $count = count($data);
            
            if($count > 0)
            {     
                $articletitle       = $data[0]['articletitle'];
                $companyname        = $data[0]['companyname'];
                $articledesc        = $data[0]['articledesc'];
                $articleimage       = $data[0]['articleimage'];
                $articleshortdesc   = $data[0]['articleshortdesc'];
                $formlinks          = $data[0]['formlinks'];
                $status             = $data[0]['status'];
            }
            else
            {
                $redirect = ADMIN_DIR."view-exhibitor-articles.php";
                header("location:".$redirect);
                exit();
            }
            
            $formtitle = "Edit Articles";       
        }
        else
        {
            $formtitle = "Add New Articles";
        }
        
        require_once "add-exhibitor-articles1.html";
        unset($_SESSION['articletitle']);
        unset($_SESSION['articledesc']);
        unset($_SESSION['pageaddsuccess']);
    }
    else
    {
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }   
?>