<?php

//defined('_VALID') or die('Restricted access');
include_once('class.productphotogallery.php');
$productphotogalleryObj = new productphotogallery($configObj);
$action = $_REQUEST['action'];
$productphotogalleryObj->intitialize($view);
$configObj->glTplObj->change_path($configObj->currentModulePath . 'html/templates/');
$configObj->glTplObj->set('configObj', $configObj);
$configObj->glTplObj->set('productphotogalleryObj', $productphotogalleryObj);

switch ($action) {
    case 'add':
        $productphotogalleryObj->varpostdata = $_REQUEST;
        $productphotogalleryObj->eid = $_REQUEST['eid'];
        if ($_FILES['var_image'] != '') {
            if ($productphotogalleryObj->insert_photo()) {
                $configObj->glLibraryObj->header_redirect('index.php?module=productphotogallery&fk_product='.$_REQUEST['fk_product'].'&msg=success');
            }
        } else {
            $configObj->glLibraryObj->header_redirect('index.php?module=productphotogallery&fk_product='.$_REQUEST['fk_product']);
            exit;
        }
        break;

    case 'edit':

        $configObj->glTplObj->set('addphotoTab', true);
        $productphotogalleryObj->pagename = 'index.php?module=productphotogallery'; // Attribute of Page Name
        $productphotogalleryObj->Generateurl();
        if ($_REQUEST['h_save'] == 'T') {
            $productphotogalleryObj->varpostdata = $_REQUEST;
            if ($productphotogalleryObj->update_photos()) {
                $configObj->glLibraryObj->header_redirect('index.php?module=productphotogallery&msg=edit');
            }
        }
        if ($_REQUEST['msg']) {
            if ($_REQUEST['msg'] == 'add') {
                $messagebox = $productphotogalleryObj->configObj->glLibraryObj->message("The record has been successfully saved.");
                $productphotogalleryObj->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $productphotogalleryObj->configObj->glLibraryObj->message("The record has been successfully edited and saved.");
                $productphotogalleryObj->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }
        $row = $productphotogalleryObj->select_photo($_REQUEST['eid']);
        $albumArry = array();
        if (!empty($row)) {
            $albumArry = $productphotogalleryObj->getAlbum();
        }
        $productphotogalleryObj->configObj->glTplObj->set('albumArr', $albumArry);
        $productphotogalleryObj->configObj->glTplObj->set('row', $row);
        $configObj->adminContentPanel = $configObj->glTplObj->process('productphotogallery_edit.tpl.php');
        break;

    case 'delete':
        if ($configObj->isAjaxSet) {
            $productphotogalleryObj->is_photoused($productphotogalleryObj->fk_module);
        }
        break;

    case 'delphoto':
       
        $albumID = $_REQUEST['fk_product'];
       $photoID = $_REQUEST['photoID'];
        $productphotogalleryObj->delete_image($photoID);
        ob_clean();
        echo "1";
        exit;
        break;
    
    case 'set_primary':
        
        $photoID = $_REQUEST['photoID'];
        $albumID = $_REQUEST['albumID'];
          if ($productphotogalleryObj->set_primary_photo($photoID, $albumID)) {
            echo "1";
        } else {
            echo "0";
        }
//        $productphotogalleryObj->set_primary_photo($photoID, $albumID);
//        echo "1";
        exit;
        break;
        
    case 'set_publish':
         $photoID = $_REQUEST['photoID'];
        $albumID = $_REQUEST['fk_product'];
        $publish = $_REQUEST['publish'];
        if ($productphotogalleryObj->set_publish($photoID,$publish)) {
            echo "1";
        } else {
            echo "0";
        }
        exit;
        break;

    case 'savealbumcaption':

        $productphotogalleryObj->varpostdata = $_REQUEST;
       
         $albumID = $_REQUEST['fk_product'];
          $photoID = $_REQUEST['photoID'];
        $photocaption = $_REQUEST['photocaption'];
        if ($productphotogalleryObj->savePhotoCaption($photoID,$photocaption)) {
            echo "1";
        } else {
            echo "0";
        }
        exit;
        break;

    case 'set_photo_displayorder':
        $productphotogalleryObj->varpostdata = $_REQUEST;
        $albumID = $productphotogalleryObj->varpostdata['fk_product'];
        $photointglcode = $productphotogalleryObj->varpostdata['photointglcode'];
        if ($productphotogalleryObj->set_photo_displayorder($albumID, $photointglcode)) {
            echo "1";
        } else {
            echo "0";
        }
        exit;
        break;

    case 'photo_grid':
        $albumID = $_REQUEST['fk_product'];
        $configObj->glTplObj->set('photosArr', $productphotogalleryObj->getPhotosByAlbum($albumID));
        echo $configObj->glTplObj->process('productphotos_listing.tpl.php');
        exit;
        break;

    default:
        
        $albumID = $_REQUEST['fk_product'];
       
        $configObj->glTplObj->set('mngPhotoTab', true);
        $configObj->glTplObj->set('photosArr', $productphotogalleryObj->getPhotosByAlbum($albumID));
        $productphotogalleryObj->Generateurl();
        $configObj->glTplObj->set('frmaction', $productphotogalleryObj->urlwithpara . '&action=add');
        $productphotogalleryObj->load_productphotogallery();

        break;
}
?>