<?
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['btnaddblog'])) {
    if (!adminchecklogin('', '', '', $G_userid)) {
        $redirect = ADMIN_DIR . "auth/login.php";
        header("location:" . $redirect);
        exit();
    }                   
    // echo "<pre>";
    // print_r($_POST);
    // die();
    $blogid = intval($_POST['blogid']);
    $blogsite = sanitize($_POST['blogsite'], SQL + HTML);
    $blogtitle = sanitize($_POST['blogtitle'], SQL);        
    // $blogurl    =   cleanURL($blogurl);
    $blogdate = $_POST['blogdate'];

    //$pagedesc = sanitize($_POST['pagedesc'], SQL);
    //$pagedesc = str_replace("'", "/", $_POST['pagedesc']);
    $blogdesc = trim($_POST['blogdesc']);

    $blogimage = $_POST['blogimage1'];


    $valid = "";
    $valid .= $Validation->check_blank("blogtitle", $blogtitle, "Blog Title");
    $valid .= $Validation->check_blank("blogdesc", $blogdesc, "Blog Description");
    //$valid .= $Validation->check_blank("blogimagepath", $blogimage, "Blog Image");

    if ($valid) {
        if ($blogid > 0 && $blogid != 0 && $blogid != null && $blogid != '') {
            $redirect = ADMIN_DIR . "add-blog.php?blogid=" . $blogid;
            header("location:" . $redirect);
            exit();
        } else {
            $redirect = ADMIN_DIR . "add-blog.php";
            header("location:" . $redirect);
            exit();
        }
        $redirect = ADMIN_DIR . "add-blog.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();
        $blogarray['blogsite'] = trim($blogsite);
        if ($blogdate != '') {
            $blogarray['blogdate'] = date("Y-m-d", strtotime($blogdate));
        } else {
            $blogarray['blogdate'] = date("Y-m-d");
        }              
        $blogarray['blogtitle'] = trim($blogtitle);
        $blogarray['blogdesc'] = $blogdesc;
        // $blogarray['blogurl'] = $blogurl;
        $blogarray['blogimage'] = $blogimage;
        $blogarray['status'] = $_POST['status'];

           

        if ($blogid > 0 && $blogid != 0 && $blogid != null && $blogid != '') {
            $where = "blogid = " . $blogid;
            $addblog = $alltable->EditInfo($blogarray, $where, BLOG_TABLE);
            if ($addblog) {
                $redirect = ADMIN_DIR . "view-blog.php";

                echo '<script>
                alert("Blog  Updated Successfully");                  
                window.location.href = "' . $redirect . '";
              </script>';
       
                exit(); 
            }
        } else {
               $addblog = $alltable->AddInfo($blogarray, BLOG_TABLE);
            if ($addblog) {
                $redirect = ADMIN_DIR . "view-blog.php";
                echo '<script>
                alert("Blog Added Successfully");
                window.location.href = "' . $redirect . '";
              </script>';
                exit();
            }
        }
    }
} else if ($G_userid > 0 && $G_userid != null && $G_userid != '') {
    if (isset($_GET['blogid'])) {
        $blogid = intval($_GET['blogid']);

        $sql = "select * from " . BLOG_TABLE . " where blogid = $blogid limit 0,1";
        $data = $alltable->getInfo('', $sql);
        $count = count($data);

        if ($count > 0) {
            $blogsite = $data[0]['blogsite'];
            $blogtitle = $data[0]['blogtitle'];             
            
            $blogdate = $data[0]['blogdate'];
            $blogdesc = $data[0]['blogdesc'];
            $blogimage = $data[0]['blogimage'];                 
            $status = $data[0]['status'];
        } else {
            $redirect = ADMIN_DIR . "view-blog.php";
            header("location:" . $redirect);
            exit();
        }

        $formtitle = "Edit blog";
    } else {
        $formtitle = "Add New blog";
    }

    require_once "add-blog1.html";
    unset($_SESSION['blogtitle']);
    unset($_SESSION['blogdesc']);
    unset($_SESSION['pageaddsuccess']);
} else {
    $redirect = ADMIN_DIR . "auth/login.php";
    header("location:" . $redirect);
    exit();
}
