<?php

defined('_VALID') or die('Restricted access');
include_once('class.news.php');
$newsObj = new news($configObj);
$action = $_REQUEST['action'];
$newsObj->intitialize($view);

$configObj->glTplObj->change_path($configObj->currentModulePath . 'html/templates/');
$configObj->glTplObj->set('configObj', $configObj);
$configObj->glTplObj->set('newsObj', $newsObj);

$paramMain = $configObj->AllmoduleAccess[$configObj->ModuleName];
$configObj->glTplObj->set('paramMain', $paramMain);

switch ($action) {

    case 'add':
        if ($paramMain['Add'] != 'Y') {
            $configObj->glLibraryObj->header_redirect('index.php?module=news');
        }
        $configObj->glTplObj->set('newsaddTab', true);
        if ($_REQUEST['h_save'] == 'T') {
            $newsObj->varpostdata = $_REQUEST;

            if ($newsObj->insert_news()) {
                if ($_POST['btnsaveandc_x'] != '') {
                    $eid = $newsObj->lastinsertid;

                    $configObj->glLibraryObj->header_redirect('index.php?module=news&action=edit&msg=add&eid=' . $eid);
                } else if ($_POST['btnsaveande_x'] != '') {
                    $configObj->glLibraryObj->header_redirect('index.php?module=news&msg=add');
                } else if ($_POST['btnsaveandnew_x'] != '') {
                    $configObj->glLibraryObj->header_redirect('index.php?module=news&action=add&msg=add');
                } else if ($_POST['btnsaveandcopy_x'] != '') {

                    $configObj->glLibraryObj->header_redirect('index.php?module=news&action=edit&msg=add&mode=copy&eid=' . $newsObj->lastinsertid);
                }
            }
        } else {


            if ($_REQUEST['msg'] == 'add') {
                $messagebox = $newsObj->configObj->glLibraryObj->message("The data has been successfully saved.");
                $newsObj->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $newsObj->configObj->glLibraryObj->message("The data has been successfully edited and saved.");
                $newsObj->configObj->glTplObj->set('messagebox', $messagebox);
            }

            $newsaction = $newsObj->pagename . '&action=' . $action;
            $configObj->glTplObj->set('newsaction', $newsaction);


            $newsObj->Generateurl();
            $configObj->glTplObj->set('row', $row);
            $configObj->glTplObj->set('BottomSubmitButtons', $configObj->glLibraryObj->GetSubmitButtons($newsObj->urlwithpara, 'bottom'));
            $configObj->adminContentPanel = $configObj->glTplObj->process('news_add.tpl.php');
        }

        break;

    case 'sharedata':

        if ($_REQUEST['h_save'] == 'T') {
            $newsObj->varpostdata = $_REQUEST;
            echo $newsObj->shareonfacebook();
        } else {
            echo $newsObj->genrateshareOption();
        }
        exit;
        break;


    case 'edit':

        if ($paramMain['Edit'] != 'Y') {
            $configObj->glLibraryObj->header_redirect('index.php?module=news');
        }
        $configObj->glTplObj->set('newsaddTab', true);
        $newsObj->Generateurl();
        $configObj->glTplObj->set('BottomSubmitButtons', $configObj->glLibraryObj->GetSubmitButtons($newsObj->urlwithpara, 'bottom'));
        if ($_REQUEST['h_save'] == 'T') {
            $newsObj->varpostdata = $_REQUEST;
            if ($newsObj->update_news()) {
                if ($_POST['btnsaveandc_x'] != '') {
                    $configObj->glLibraryObj->header_redirect($newsObj->urlwithpara . '&action=' . $_REQUEST['action'] . '&eid=' . $_REQUEST['eid'] . '&msg=edit');
                } else if ($_POST['btnsaveande_x'] != '') {
                    $configObj->glLibraryObj->header_redirect($newsObj->urlwithpara . '&msg=edit');
                } else if ($_POST['btnsaveandnew_x'] != '') {

                    $configObj->glLibraryObj->header_redirect($newsObj->urlwithpara . '&action=add&msg=edit');
                } else if ($_POST['btnsaveandcopy_x'] != '') {
                    $configObj->glLibraryObj->header_redirect($newsObj->urlwithpara . '&action=edit&msg=edit&mode=copy&eid=' . $_REQUEST['eid']);
                }
            }
        } else {
            if ($_REQUEST['msg']) {
                if ($_REQUEST['msg'] == 'add') {
                    $messagebox = $newsObj->configObj->glLibraryObj->message("The record has been successfully saved.");
                    $newsObj->configObj->glTplObj->set('messagebox', $messagebox);
                } else if ($_REQUEST['msg'] == 'edit') {
                    $messagebox = $newsObj->configObj->glLibraryObj->message("The record has been successfully edited and saved.");
                    $newsObj->configObj->glTplObj->set('messagebox', $messagebox);
                }
            }
            if ($_REQUEST['mode'] == 'copy') {
                $action = 'add';
                $configObj->glTplObj->set('action', $action);
            } else {
                $action = $_REQUEST['action'];
            }
            $newsaction = $newsObj->urlwithpara . '&action=' . $action . '&eid=' . $_REQUEST['eid'];
            $configObj->glTplObj->set('newsaction', $newsaction);

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


            $configObj->adminContentPanel = $configObj->glTplObj->process('news_add.tpl.php');
        }
        break;

    case 'delete':

        if ($configObj->isAjaxSet) {
            $newsObj->is_newsused();
        }
        break;
        
    case 'deletedocument':
        $id = $_GET['sid'];
        $file = $_GET['file'];
        $newsObj->deletedocument($id,$file);
        ob_clean();
        break;
    
    case 'updatepublish':
        $id = $_REQUEST['id'];
        $configObj->glLibraryObj->updatefieldinmultiple();
        echo 1;
        exit;

   
    default:
        $configObj->glTplObj->set('newsTab', true);
        $newsObj->load_news();

        break;
}
?>
