<?php

defined('_VALID') or die(RESTRICTED_ACCESS);
include_once('class.pages.php');
$pagesObj = new pages($configObj);
$configObj->glTplObj->change_path($configObj->currentModulePath . 'html/templates/');
$configObj->glTplObj->set('pagesObj', $pagesObj);
$configObj->glTplObj->set('configObj', $configObj);

$paramMain = $configObj->AllmoduleAccess[$configObj->ModuleName];
$configObj->glTplObj->set('paramMain', $paramMain);
$url_string = ereg_replace("&ajax=Y", "", $_SERVER['QUERY_STRING']);
$back_url_string = base64_encode($url_string);
$configObj->glTplObj->set('back_url_string', $back_url_string);

$action = $_REQUEST['action'];

$fk_modules = $pagesObj->get_fk_modules($module);
$configObj->glTplObj->set('fk_modules', $fk_modules);

switch ($action) {
    case 'add':

        if ($paramMain['Add'] != 'Y') {
            $configObj->glLibraryObj->header_redirect('index.php?module=pages');
        }
        $pagesObj->varpostdata = $_REQUEST;
        $pagesObj->loadCount('CountRows');
        $pagesObj->configObj->glTplObj->set('paction', 'add');

        $configObj->glTplObj->set('pageAddTab', true);
         if ($_REQUEST['h_save'] == 'T') {
        if ($pagesObj->insert_pages()) {
            if ($_POST['btnsaveandc_x'] != '') {
                $configObj->glLibraryObj->header_redirect('index.php?module=pages&action=edit&msg=add&eid=' . $pagesObj->lastinsertid);
            } else if ($_POST['btnsaveande_x'] != '') {
                $configObj->glLibraryObj->header_redirect('index.php?module=pages&msg=add');
            } else if ($_POST['Image40_x'] != '') {   //exit;                            
                $configObj->glLibraryObj->header_redirect($pagesObj->urlwithpara . '&action=edit&preview=yes&msg=preview&paction=add&eid=' . $pagesObj->lastinsertid);
            }
        }
        
        } else {
            $configObj->glTplObj->set('imghelpdiv', $configObj->glLibraryObj->helpdivforimg(PAGE_THUMB_WIDTH, PAGE_THUMB_HEIGHT));

            $arrParam = array('name' => 'var_team[]', '', '', 'class' => 'add-new-user-textarea2');
            //    $configObj->glTplObj->set('teamrecords', $pagesObj->Bindteamhierarchy($arrParam));

            $configObj->glTplObj->set('pagetree', $pagesObj->Bindpageshierarchy('fk_pages', $row['fk_pages'], 'add-new-user-textarea2', '1'));
            $configObj->glTplObj->set('fk_modules', '1');
            $configObj->glTplObj->set('BottomSubmitButtons', $configObj->glLibraryObj->GetSubmitButtons($pagesObj->urlwithpara, 'bottom', '0'));
            if ($_REQUEST['msg'] == 'add') {
                $messagebox = $pagesObj->configObj->glLibraryObj->message("Congrats! The record has been successfully saved.");
                $pagesObj->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $pagesObj->configObj->glLibraryObj->message("Congrats! The record has been successfully edited and saved.");
                $pagesObj->configObj->glTplObj->set('messagebox', $messagebox);
            }
            $configObj->adminContentPanel = $configObj->glTplObj->process('addpages.tpl.php');
        }


        break;

    

    case 'orderupdate':
        $updateids = $_REQUEST['uids'];
        $updatevalues = $_REQUEST['uvals'];
        $updateoldvalues = $_REQUEST['uoldvals'];
        $upidsarray = explode(',', $updateids);
        $upvalsarray = explode(',', $updatevalues);
        $upoldvalsarray = explode(',', $updateoldvalues);
        $pagesObj->update_display_order($upidsarray, $upvalsarray, $upoldvalsarray);
        $pagesObj->loadpage();

        break;

    case 'edit':


        if (empty($_REQUEST['eid'])) {
            $configObj->glLibraryObj->header_redirect('index.php');
        }
        if ($paramMain['Edit'] != 'Y') {
            $configObj->glLibraryObj->header_redirect('index.php?module=pages');
        }
        $configObj->glTplObj->set('pageAddTab', true);
        $configObj->glTplObj->set('pagesEditTab', true);
        // $configObj->glLibraryObj->checkEditid($pagesObj->pagename, DB_PREFIX . "pages", "int_glcode");
        $configObj->glLibraryObj->checkchild($pagesObj->pagename, DB_PREFIX . "pages", "fk_pages", "");

        $pagesObj->loadCount('CountRows');


        if ($_REQUEST['h_save'] == 'T') {
            $pagesObj->varpostdata = $_REQUEST;

            if ($_POST['btnsaveandc_x'] != '') {
                $pagesObj->update_pages();
                $configObj->glLibraryObj->header_redirect($pagesObj->urlwithpara . '&action=' . $_REQUEST['action'] . '&eid=' . $_REQUEST['eid'] . '&msg=edit');
            } else if ($_POST['btnsaveande_x'] != '') {
                $pagesObj->update_pages();
                $configObj->glLibraryObj->header_redirect($pagesObj->urlwithpara . '&msg=edit');
            } else if ($_POST['Image40_x'] != '') {
                $pagesObj->insert_preview();
                $configObj->glLibraryObj->header_redirect('index.php?module=pages&action=edit&preview=yes&msg=preview&paction=' . $_REQUEST['paction'] . '&eid=' . $pagesObj->lastinsertid);
            }
        } else {
            if ($_REQUEST['paction'] == 'add') {
                $paction = 'add';
            } else {
                $paction = 'edit';
            }
            $pagesObj->configObj->glTplObj->set('paction', $paction);
            $configObj->glTplObj->set('frmaction', $pagesObj->urlwithpara . '&action=' . $_REQUEST['action'] . '&eid=' . $_REQUEST['eid']);
            $configObj->glTplObj->set('BottomSubmitButtons', $configObj->glLibraryObj->GetSubmitButtons($pagesObj->urlwithpara, 'bottom', '0'));
            $configObj->glTplObj->set('fk_modules', '1');

            $row = $pagesObj->select_pages($_REQUEST['eid']);
            $configObj->glTplObj->set('row', $row);

            $configObj->glTplObj->set('pagetree', $pagesObj->Bindpageshierarchy('fk_pages', $row['fk_pages'], 'add-new-user-textarea2', '4'));

            $category = explode(',', $row['fk_team']);
            $arrParam = array('seldata' => $category);
//          echo "<pre>";
//            print_r($arrParam);
            //$configObj->glTplObj->set('teamrecords', $pagesObj->Bindteamhierarchy($arrParam));

            $configObj->glTplObj->set('imghelpdiv', $configObj->glLibraryObj->helpdivforimg(PAGE_THUMB_WIDTH, PAGE_THUMB_HEIGHT));
            if ($_REQUEST['msg'] == 'add') {
                $messagebox = $pagesObj->configObj->glLibraryObj->message("Congrats! The record has been successfully saved.");
                $pagesObj->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $pagesObj->configObj->glLibraryObj->message("Congrats! The record has been successfully edited and saved.");
                $pagesObj->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'preview') {
                $messagebox = $pagesObj->configObj->glLibraryObj->message("This is a Preview Record!");
                $pagesObj->configObj->glTplObj->set('messagebox', $messagebox);
            }
            $configObj->adminContentPanel = $configObj->glTplObj->process('addpages.tpl.php');
        }

        break;

    case 'delphoto':
        $pagesObj->varpostdata = $_REQUEST;
        $file = $_REQUEST['file'];
        $sid = $_REQUEST['sid'];
        $pagesObj->delete_image($sid, $file);
        ob_clean();
        echo "1";
        exit;
        break;

    case 'showPageHits':

        if ($configObj->isAjaxSet) {
            echo $pagesObj->getPageHits($_REQUEST['id']);
            exit;
        }
        break;


    case 'delete' :
        if ($configObj->isAjaxSet) {
            $pagesObj->delete_page('1');
        }
        
       
        break;

    case 'addquickcat':
        if ($configObj->isAjaxSet) {

            $configObj->glTplObj->set('pagetree_quick', $pagesObj->Bindpageshierarchy('fk_pages_quick', $row['fk_pages'], 'add-new-user-textarea2', '4'));
            echo $configObj->glTplObj->process('quickAddCategory.tpl.php');
            exit;
        }


        break;


    case 'insertpagesquick':
        if ($configObj->isAjaxSet) {

            $ParentId = $_REQUEST['fk_pages_quick'];
            $CategoryName = $_REQUEST['var_category_quick'];

            $pagesObj->insertpagesquick($ParentId, $CategoryName);
        }
        break;



    case 'cmsplugin':

        if ($configObj->isAjaxSet) {


            $result = $pagesObj->selectall_commonpdf();
            $countdata = $pagesObj->countall_commonpdf();

            $pagesObj->configObj->glTplObj->set('result', $result);
            $pagesObj->configObj->glTplObj->set('countdata', $countdata);

            //$data = $pagesObj->selectall_cms();

            $pagesObj->configObj->glTplObj->set('data', $data);
            echo $pagesObj->configObj->glTplObj->process('cmsplugin.tpl.php');
            exit;
        }
        break;
    case 'cmspages':

        if ($configObj->isAjaxSet) {

            $data = $pagesObj->selectall_cms();
            $pagesObj->configObj->glTplObj->set('cmsdata', '1');
            $pagesObj->configObj->glTplObj->set('data', $data);
            $countdata = $pagesObj->countall_commonpdf();
            $pagesObj->configObj->glTplObj->set('countdata', $countdata);
            echo $pagesObj->configObj->glTplObj->process('cmspages.tpl.php');
            exit;
        }
        break;


    case 'realcrop':
        $eid = $_REQUEST['eid'];
        $pid = $_REQUEST['pageid'];
        if ($configObj->isAjaxSet) {
            if ($_REQUEST['cropaction'] == "crop") {
                $pagesObj->do_crop();
            }
            exit;
        } else {
            $thumb_width = '730';
            $thumb_height = '245';
            $tablename = DB_PREFIX . 'pages';
            $cropaction = $_REQUEST['cropaction'];
            $configObj->glTplObj->set('cropaction', $cropaction);
            $pagesObj->pagename = 'index.php?module=pages';
            $pagesObj->loadCount('CountRows');
            $configObj->glTplObj->set('backurl', $pagesObj->urlwithpara);
            $filename = $pagesObj->get_var_imagepath($eid, 'var_bannerimage', $tablename);
            $filename = 'thumb_' . $filename;
            $configObj->glTplObj->set('cropwidth', $thumb_width);
            $configObj->glTplObj->set('cropheight', $thumb_height);
            $configObj->glTplObj->set('pid', $pid);
            $configObj->glTplObj->set('cropitem', $_REQUEST['cropitem']);
            $configObj->glTplObj->set('filename', $filename);
            $cropstatus = $pagesObj->get_cropstatus($eid, 'chr_crop', $tablename);
            $configObj->glTplObj->set('cropstatus', $cropstatus);
        }
        $configObj->adminContentPanel = $configObj->glTplObj->process('realcrop.tpl.php');
        break;

    case 'updatepublish':
        if ($_REQUEST['flagconfirm'] == 'N') {
            $id = $_REQUEST['id'];
        } else {
            $id = $pagesObj->getpagesubchildid($_REQUEST['id']);
        }
        $pagesObj->configObj->glDbObj->query("update " . $_REQUEST['table'] . " set " . $_REQUEST['field'] . "='" . $_REQUEST['value'] . "' where int_glcode in(" . $id . ")");
        echo "$$$$$1$$$$$" . $id;
        exit;


    case 'issameAlias':

        $param = array(
            "tables" => $pagesObj->configObj->glLibraryObj->checkAliasTable,
            "alias" => $_REQUEST['alias']
        );

        if (!empty($_REQUEST['alias'])) {
            $param['table'] = DB_PREFIX . base64_decode($_REQUEST['table']);
            $param['alias'] = $_REQUEST['alias'];
            $param['eid'] = base64_decode($_REQUEST['eid']);
            $param['mode'] = $_REQUEST['mode'];
        }

        $same = $pagesObj->configObj->glLibraryObj->checkAlias($param);
        echo $same;
        exit();
        break;


    case 'updatepublish':
        if ($_REQUEST['flagconfirm'] == 'N') {
            $id = $_REQUEST['id'];
        } else {
            $id = $pagesObj->getpagesubchildid($_REQUEST['id']);
        }
        $pagesObj->configObj->glDbObj->query("update " . $_REQUEST['table'] . " set " . $_REQUEST['field'] . "='" . $_REQUEST['value'] . "' where int_glcode in(" . $id . ")");

        exit;


    default:
        $configObj->glTplObj->set('pageTab', true);
        $pagesObj->loadpage();
        break;
}
?>