<?php
    require_once "../include/config.php";
    require_once GLOBAL_INCLUDE_PATH."include/startup.php";
    
    $alltable = new AllTables();
    $Validation = new Validation();
      
    

    if(!adminchecklogin('', '', '', $G_userid)){
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }
    if(isset($_POST['btnaddpage']) && $_POST['form'] == ''){                   
        if(!adminchecklogin('', '', '', $G_userid)){   
            $redirect = ADMIN_DIR."auth/login.php";   
            header("location:".$redirect); 
            exit();        
        }      
        // echo "<pre>";
        // print_r($_POST);  
        // die();
        $pageid = intval($_POST['pageid']);
        $pagetitle = sanitize($_POST['pagetitle'], SQL);
        $metatitle = sanitize($_POST['metatitle'], SQL);
        $keywords = sanitize($_POST['meta-keywords'], SQL);
        $description = sanitize($_POST['meta-description'], SQL);
        //$pagedesc = sanitize($_POST['pagedesc'], SQL);
        //$pagedesc = str_replace("'", "/", $_POST['pagedesc']);
        $pagedesc = trim($_POST['pagedesc']);
            
        $valid = "";
        $valid .= $Validation->check_blank("pagetitle", $pagetitle, "Page Title");
        $valid .= $Validation->check_blank("pagedesc", $pagedesc, "Page Sescription");
        
        if($valid){
            if($pageid > 0 && $pageid != 0 && $pageid != null && $pageid != ''){
                $redirect = ADMIN_DIR."add-page.php?pageid=".$pageid;
                header("location:".$redirect);
                exit();
            }else{
                $redirect = ADMIN_DIR."add-page.php";
                header("location:".$redirect);
                exit(); 
            }
            $redirect = ADMIN_DIR."add-page.php";
            header("location:".$redirect);
            exit();    
        }else{
            
             $doc = new DOMDocument();
            $doc->loadHTML($pagedesc);
            $xml = simplexml_import_dom($doc);
            $images = $xml->xpath('//img');
            
            if(count($images) > 0)
            {
                foreach ($images as $img)
                {
                    //$src =  str_replace('\"', "", $pagedesc);
                    $docrootpath = str_replace('root/assets/', DOC_ROOT.'assets/', $pagedesc);
    //                if(strpos($src, 'http') == false) { 
    //                    $docrootpath = str_replace('assets/', DOC_ROOT.'assets/', $src);
    //                }else{
    //                    $docrootpath = $src;
    //                }
                }
            }
            else
            {
                $docrootpath = str_replace('root/assets/', DOC_ROOT.'assets/', $pagedesc);
                
               // $docrootpath=$pagedesc;
            }
            $finalpagedesc = str_replace('root/', DOC_ROOT,$docrootpath);
           
           //echo $docrootpath;
           //die();
            $pageurl = str_replace(array(' ', '-', '_'), "-", strtolower(trim($pagetitle)));
            $pagearray['pagetitle'] = trim($pagetitle);
            $pagearray['metatitle'] = trim($metatitle);
            $pagearray['metakeywords'] = trim($keywords);
            $pagearray['metadescription'] = trim($description);
            $pagearray['pagedesc'] = $finalpagedesc;  
            $pagearray['pageurl'] = $pageurl;
            $pagearray['isparentpage'] = $_POST['parentpage'];
            $pagearray['status'] = $_POST['status'];;
            $pagearray['parentpageid'] = $_POST['parentpageid'];
            
            
            //print_r($pagearray);
            //die();
            
            if ($pageid > 0 && $pageid != 0 && $pageid != null && $pageid != '') {
                $where = "pageid = " . $pageid;
                $addpage = $alltable->EditInfo($pagearray, $where, PAGE_TABLE);
                if ($addpage) {
                    $_SESSION['pageaddsuccess'] = "Page Updated Successfully";
                    echo "<script>
                            alert('Page Updated Successfully');
                            window.location.href = '".ADMIN_DIR."view-page.php?pageid=".$pageid."';
                          </script>";
                    exit();                
                }
            } else {
                $addpage = $alltable->AddInfo($pagearray, PAGE_TABLE);
                if ($addpage) {
                    $_SESSION['pageaddsuccess'] = "Page Added Successfully";
                    echo "<script>
                            alert('Page Added Successfully');
                            window.location.href = '".ADMIN_DIR."view-page.php';
                          </script>";
                    exit();    
                }
            }
            
            
            
              
        }
        
    }else if($G_userid > 0 && $G_userid != null && $G_userid != ''){
        if(isset($_GET['pageid'])){
            $pageid = intval($_GET['pageid']);
        
            $sql = "select * from ".PAGE_TABLE." where pageid = $pageid limit 0,1";
            $data = $alltable->getInfo('', $sql);
            $count = count($data);
            
            if($count > 0){   
                $metatitle = $data[0]['metatitle'];
                $metakeywords = $data[0]['metakeywords'];
                $metadescription = $data[0]['metadescription'];
                $pagetitle = $data[0]['pagetitle'];
                $pagedesc = $data[0]['pagedesc'];
                $status = $data[0]['status'];
                $parentpageid = $data[0]['parentpageid'];
                $isparentpage = $data[0]['isparentpage'];
                
                $parentpagelist = getSelectComboParentPage($parentpageid);
            }else{
                $redirect = ADMIN_DIR."view-page.php";
                header("location:".$redirect);
                exit();
            }
            
            $formtitle = "Edit Page";
        }else{
            $formtitle = "Add New Page";
            $parentpagelist = getSelectComboParentPage();
        }
        
        require_once "add-page.html";
        unset($_SESSION['pagetitle']);
        unset($_SESSION['pagedesc']);
        unset($_SESSION['pageaddsuccess']);
    }else{
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }   
?>    