<?php

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

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

$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=product');
        }
        $configObj->glTplObj->set('productaddTab', true);
        if ($_REQUEST['h_save'] == 'T') {
            $productObj->varpostdata = $_REQUEST;

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

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

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


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

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


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

        break;
        
        case 'sharedata':

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


    case 'edit':

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

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

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


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

    case 'delete':
        if ($configObj->isAjaxSet) {
            $productObj->is_productused();
        }
        break;
    
    case 'updatepublish':
        $id = $_REQUEST['id'];
        $configObj->glLibraryObj->updatefieldinmultiple();
        echo 1;
        exit;     
   
    default:
        $configObj->glTplObj->set('productTab', true);
        $productObj->load_product();

        break;
}
?>
