<?php

class pagephoto {

    var $pagename = 'index.php?module=pagephoto';
    var $int_glcode;
    var $chr_read;
    var $chr_star;
    var $chr_publish;
    var $int_displayorder;
    var $chr_delete = 'Y';
    var $dt_createdate;
    var $dt_modifydate;
    var $oldint_displayorder;
    var $numofrows;
    var $numofpages;
    var $orderby = 'int_displayorder';
    var $ordertype = 'asc';
    var $searchby = '0';
    var $searchtxt;
    var $start = 1;
    var $pagesize = DEFAULT_PAGESIZE;
    var $pagenumber = '1';
    var $lastinsertid;
    var $urlwithpara = '';
    var $urlwithpoutsearch = '';
    var $urlwithoutsort = '';
    var $urlwithoutpaging = '';
    var $configObj;
    var $filterby = '0';
    var $urlwithoutfilter = '';
    var $display_limit_array = array(5, 10, 15, 30);
    var $varpostdata = array();
    var $thumb_width = TRADESHOW_THUMB_WIDTH;
    var $thumb_height = TRADESHOW_THUMB_HEIGHT;

    function pagephoto($configObj) {
        $this->sortvar = '<img alt="sorting" src="' . GLOBAL_ADMIN_IMAGES_PATH . 'arrow-up.png" style="vertical-align:middle;">';
        $this->configObj = $configObj;
    }

    function intitialize($view = '') {
        $this->searchby = (isset($_REQUEST['searchby'])) ? $_REQUEST['searchby'] : $this->searchby;
        $_REQUEST['searchtxt'] = ($_REQUEST['type'] == 'autosearch') ? $_REQUEST['term'] : $_REQUEST['searchtxt'];
        $this->searchbyVal = (isset($_REQUEST['searchbyVal'])) ? $_REQUEST['searchbyVal'] : $this->searchbyVal;
        $this->searchtxt = (isset($_REQUEST['searchtxt'])) ? $this->configObj->glLibraryObj->rem_special_chars($_REQUEST['searchtxt']) : '';
        $this->searchtxtjs = (isset($_REQUEST['searchtxt'])) ? $this->configObj->glLibraryObj->rem_special_chars_js($_REQUEST['searchtxt']) : '';

        $this->orderby = (isset($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : $this->orderby;
        $this->ordertype = (isset($_REQUEST['ordertype'])) ? $_REQUEST['ordertype'] : $this->ordertype;
        $this->filterby = (isset($_REQUEST['filterby'])) ? $_REQUEST['filterby'] : $this->filterby;

        if (isset($_REQUEST['sorting'])) {
            if ($this->ordertype == "asc") {   // This is for sort image
                $this->ordertype = "desc";
                $this->sortvar = "<img alt=\"sorting\" src=\"" . GLOBAL_ADMIN_IMAGES_PATH . "arrow-down.png\" style=\"vertical-align:middle;\">";
            } else if ($this->ordertype == "desc") {   // This is for sort image
                $this->ordertype = "asc";
                $this->sortvar = "<img alt=\"sorting\" src=\"" . GLOBAL_ADMIN_IMAGES_PATH . "arrow-up.png\" style=\"vertical-align:middle;\">";
            }
        }
        $this->pagesize = (isset($_REQUEST['pagesize'])) ? $_REQUEST['pagesize'] : $this->pagesize;
        $this->pagenumber = (isset($_REQUEST['pagenumber'])) ? $_REQUEST['pagenumber'] : $this->pagenumber;

        $this->start = ($this->pagenumber - 1 ) * $this->pagesize;
    }

    function CountRows() {
        $this->intitialize($view);
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and var_title like '%$this->searchtxt%'" : " and $this->searchby like '%$this->searchtxt%'";
        }

        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }

        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "pagephoto where chr_delete = 'N' and fk_recid=" . $_REQUEST['fk_recid'] . "  $whereclause ";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function CountAssignedPosition($sql) {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $rs = $this->configObj->glDbObj->get_sql_row($sql);
        return $rs['total'];
    }

    // **********************
    // Generate URL
    // **********************

    function Generateurl() {
        $this->urlwithpara = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&filterby=' . $this->filterby . '&fk_recid=' . $_REQUEST['fk_recid'] ;
        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby;
        $this->urlwithoutsort = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby . '&fk_recid=' . $_REQUEST['fk_recid'] ;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby . '&fk_recid=' . $_REQUEST['fk_recid'];
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&fk_recid=' . $_REQUEST['fk_recid'];
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal;
    }

    function org_generateParam_withoutfilter($view = 'Pages Banners', $actionurl = 'index.php?module=pagephoto&action=add', $searchArray = array("var_title" => "Username"), $table = 'DB_PREFIX.pagephoto', $position = 'top') {

        $filterPos = ($position == 'top') ? 'FT' : 'FB';
        return array('pageurl' => $this->pagename,
            'heading' => $view,
            'listImage' => 'add-new-user-icon.png',
            'tablename' => $table,
            'position' => $position,
            'actionImage' => 'add-new-button-blue.gif',
            'actionImageHover' => 'add-new-button-blue-hover.gif',
            'backbuttonurl' => 'index.php?module=pages',
            'actionUrl' => $actionurl,
            'search' => array('searchArray' => $searchArray,
                'searchBy' => $this->searchby,
                'searchText' => $this->searchtxt,
                'searchUrl' => $this->urlwithpoutsearch
            ),
            'paging' => array('pagenumber' => $this->pagenumber,
                'noofpages' => $this->noofpages,
                'numofrows' => $this->numofrows,
                'pagingurl' => $this->urlwithpara
            ),
            'display' => array('displayurl' => $this->urlwithoutpaging,
                'pagesize' => $this->pagesize,
                'limitarray' => $this->display_limit_array,
            )
        );
    }

    function update_display_order($ids, $values, $oldvalues) {

        $tablename = DB_PREFIX . "pagephoto";
        for ($i = 0; $i < count($ids); $i++) {
            // Updation Of the Display Order
            if ($values[$i] != $oldvalues[$i]) {
                $this->configObj->glDbObj->resetfields();
                $this->configObj->glDbObj->setQueryType('select');
                $this->configObj->glDbObj->setTable($tablename);
                $this->configObj->glDbObj->addColumn('count(1) as total');
                $this->configObj->glDbObj->setWhere("int_displayorder=" . $values[$i] . " and chr_delete = 'N' and fk_recid='" . $_REQUEST['fk_recid'] . "'");
                $rs = $this->configObj->glDbObj->get_sql_row($this->configObj->glDbObj->buildQuery());
                if ($rs['total'] > 0) {
                    if ($values[$i] > $oldvalues[$i]) {

                        $this->configObj->glDbObj->resetfields();
                        $this->configObj->glDbObj->setTable($tablename);
                        $this->configObj->glDbObj->setQueryType('update');
                        $this->configObj->glDbObj->addColumn('int_displayorder');
                        $this->configObj->glDbObj->UnsetQuotes();
                        $this->configObj->glDbObj->addValue('int_displayorder - 1');
                        $this->configObj->glDbObj->SetQuotes();
                        $this->configObj->glDbObj->setWhere("int_displayorder > " . $oldvalues[$i] . " and int_displayorder <= " . $values[$i] . "  and fk_recid='" . $_REQUEST['fk_recid'] . "'");
                        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
                        // echo $this->configObj->glDbObj->buildQuery();
                    } else {
                        $this->configObj->glDbObj->resetfields();
                        $this->configObj->glDbObj->setTable($tablename);
                        $this->configObj->glDbObj->setQueryType('update');
                        $this->configObj->glDbObj->addColumn('int_displayorder');
                        $this->configObj->glDbObj->UnsetQuotes();
                        $this->configObj->glDbObj->addValue('int_displayorder + 1');
                        $this->configObj->glDbObj->SetQuotes();
                        $this->configObj->glDbObj->setWhere("int_displayorder >= " . $values[$i] . " and  int_displayorder < " . $oldvalues[$i] . " and fk_recid='" . $_REQUEST['fk_recid'] . "' ");
                        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
                    }
                }
                $this->configObj->glDbObj->resetfields();
                $this->configObj->glDbObj->setTable($tablename);
                $this->configObj->glDbObj->setQueryType('update');
                $this->configObj->glDbObj->addColumn('int_displayorder');
                $this->configObj->glDbObj->addValue($values[$i]);
                $this->configObj->glDbObj->setWhere("int_glcode=" . $ids[$i] . "  and fk_recid='" . $_REQUEST['fk_recid'] . "' ");
                $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
            }
        }
    }

    function message($msg = '') {
        (string) $display_output = null;
        if ($msg != "") {
            $display_output .="<div class=\"dverrorcustom\"><div class=\"confirmation-div\"><strong>$msg</strong></div></div>";
        }
        return $display_output;
    }

    function get_fk_tml_modules($module) {
        $sql = "SELECT int_glcode from " . DB_PREFIX . "modules WHERE var_modulename='$module' and chr_publish='Y' and chr_delete='N'";
        $rs = $this->configObj->glDbObj->get_sql_row($sql);
        $fk_tml_modules = 0;
        $fk_tml_modules = $rs['int_glcode'];
        return $fk_tml_modules;
    }

    function is_photoused($fk_tml_modules) {
        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);
        $is_assigned = 0;
        $delcount = 0;
        for ($i = 0; $i < $totaldeletedrecords; $i++) {
            $sql = "Update " . DB_PREFIX . "pagephoto set chr_delete='Y',dt_modifydate=NOW(),var_ipaddress='" . $_SERVER['REMOTE_ADDR'] . "',var_adminuser='" . ADMIN_NAME . " 'where int_glcode='$deletearray[$i]'";
            $this->configObj->glDbObj->query($sql);
            $delcount++;
            $this->configObj->glLibraryObj->insertinlogmanager(25, DB_PREFIX . 'pagephoto', 'var_title', $deletearray[$i], 'D');
        }
        $this->load_photogallery();
    }

    function loadCount() {
        $this->numofrows = $this->countFunction;
        $this->noofpages = intval($this->numofrows / $this->pagesize);
        $this->noofpages = (($this->numofrows % $this->pagesize) > 0) ? ($this->noofpages + 1) : ($this->noofpages);
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $this->Generateurl();
    }

    function load_photogallery() {
         if ($_REQUEST['task'] == 'orderupdate') {
            $updateids = $_REQUEST['uids'];
            $updatevalues = $_REQUEST['uvals'];
            $updateoldvalues = $_REQUEST['uoldvals'];
            $upidsarray = explode(',', $updateids);
            $upvalsarray = explode(',', $updatevalues);
            $upoldvalsarray = explode(',', $updateoldvalues); //Old Display Order Array
            $this->update_display_order($upidsarray, $upvalsarray, $upoldvalsarray, 'pagephoto');
            $tablename = "" . DB_PREFIX . 'pagephoto';
            $this->configObj->glLibraryObj->set_display_order_sequence($tablename);
        }
        $this->pagename = 'index.php?module=pagephoto&fk_recid=' . $_REQUEST['fk_recid'];
        
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
      $this->loadCount_photo('CountPhotogalleryRows');
       
        $result = $this->selectall_photogallery();
        $this->configObj->glTplObj->set('result', $result);

        $pagename = $this->getpagename($_REQUEST['fk_recid']);
        $view = 'Manage Banner Gallery (' . ucfirst($pagename) . ')';
        $actionurl = 'index.php?module=pagephoto&action=add&fk_recid=' . $_REQUEST['fk_recid'];
        $table = DB_PREFIX . 'pagephoto';
        $parmas = $this->org_generateParam_withoutfilter($view, $actionurl, '', $table);
//      echo $_REQUEST['fk_recid'];
       if($this->numofrows >= 1 && $_REQUEST['fk_recid'] != 1)
       {
           $status ='no';
          
       }  else {
           
           $status='Yes';
       }
        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas,$status));
        $this->configObj->glTplObj->set('pagingTop', $this->configObj->glLibraryObj->generatePagingPannel($parmas));
        $this->configObj->glTplObj->set('pagingBottom', $this->configObj->glLibraryObj->generatePagingPannel($this->org_generateParam_withoutfilter($view, $actionurl, $searchArray, $table, 'bottom')));

        //For Note
        $notemessagebox = $this->configObj->glLibraryObj->notemessage("Note : ", "Adding banners are incremental, once you add a new banners it will display as first banner and previous banners will move down in the order.");
        $this->configObj->glTplObj->set('notemessagebox', $notemessagebox);

        // Set the sorting image
        $tmpsetsortvar = 'setsortimg' . $this->orderby;
        $$finalsortvar = $tmpsetsortvar;
        $this->configObj->glTplObj->set($$finalsortvar, $this->sortvar);

        // End of Set the sorting image
        if ($_REQUEST['msg'] == 'add') {
            $messagebox = $this->configObj->glLibraryObj->message("The record has been successfully saved.");
            $this->configObj->glTplObj->set('messagebox', $messagebox);
        }
        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('pagephoto.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('pagephoto.tpl.php');
        }
    }

    function selectall_photogallery() {
        $dtformat = DEFAULT_DATEFORMAT;
        if ($_REQUEST['ajax'] != 'Y' || $_REQUEST['action'] == 'makeDefalutImage') {
            $this->orderby = 'int_displayorder';
        }
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'pagephoto');
        $this->configObj->glDbObj->addColumn("*");
        $orderby = (isset($_REQUEST['orderby'])) ? 'order by ' . $this->orderby . ' ' . $this->ordertype : 'order by int_displayorder';
        $whereclause = "chr_delete = 'N' and fk_recid=" . $_REQUEST['fk_recid'] . " ";
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and var_title like \"%" . mysql_real_escape_string($this->searchtxt) . "%\" " : " and $this->searchby like \"%" . mysql_real_escape_string($this->searchtxt) . "%\"";
        }
        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        if ($_REQUEST['type'] == 'autosearch') {
            $orderby = (isset($_REQUEST['orderby'])) ? 'order by ' . $this->orderby . ' ' . $this->ordertype : 'order by int_displayorder';
            $searchbyVal = (!empty($this->searchbyVal)) ? " and {$this->searchbyVal} like '%" . $this->searchtxt . "%' " : "";
            $autoSearchQry = "select int_glcode ,var_title AutoVal FROM " . DB_PREFIX . "pagephoto where $whereclause $searchbyVal group by var_title $orderby";
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);
        $this->configObj->glDbObj->buildQuery();
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());

        return $rs;
    }

    function loadCount_photo($function) {
        $this->intitialize();
        $this->{$function}();
        $this->noofpages = intval($this->numofrows / $this->pagesize);
        $this->noofpages = (($this->numofrows % $this->pagesize) > 0) ? ($this->noofpages + 1) : ($this->noofpages);
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $this->Generateurl();
    }

    function CountPhotogalleryRows() {
        $this->intitialize();

        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and var_title like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'" : " and $this->searchby like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
        }

        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);

            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "pagephoto where chr_delete = 'N' $whereclause and 	fk_recid='" . $_REQUEST['fk_recid'] . "' ";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function updatedisplay_order($tablename, $intdisplay) {
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . $tablename);
        $this->configObj->glDbObj->addColumn('int_displayorder');
        $this->configObj->glDbObj->UnsetQuotes();
        $this->configObj->glDbObj->addValue('int_displayorder + 1');
        $this->configObj->glDbObj->SetQuotes();
        $this->configObj->glDbObj->setWhere("int_displayorder >=" . $intdisplay . " and chr_delete = 'N' and fk_recid='" . $_REQUEST['fk_recid'] . "' ");
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return true;
    }

    function insert_photo() {

        if ($this->varpostdata['int_displayorder'] != '' && $this->varpostdata['int_displayorder'] != '0') {
            $intdiplayorder = $this->varpostdata['int_displayorder'];
        } else {
            $intdiplayorder = 1;
        }
        $this->updatedisplay_order('pagephoto', $intdiplayorder);
        $sess = time();

        if ($_FILES['var_image']['name'] != '') {

            //for removing space in name
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image']['name']);
            $photo = basename($_FILES['var_image']['name']);
            $photofile = str_replace(' ', '-', $photo);
            $phototitle = explode('.', $photofile);
            $var_title = $phototitle[0];
            $_FILES['var_image']['name'] = $photofile;
            if ($_REQUEST['fk_recid'] == 1) {
                $image_width = HOME_BANNER_WIDTH;
                $image_height = HOME_BANNER_HEIGHT;
            } else {
                $image_width = INNERBANNER_WIDTH;
                $image_height = INNERBANNER_HEIGHT;
            }
            $var_thumb = $this->upload_image('var_image', $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_');
            $this->imagename = $var_thumb;
        }
        if ($_REQUEST['var_title'] != '') {
            $pagename = $_REQUEST['var_title'];
        } else {
            $pagename = $var_title;
        }

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('insert');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'pagephoto');

        // Set the Columns
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('fk_recid');
        //$this->configObj->glDbObj->addColumn('fk_modid');
        $this->configObj->glDbObj->addColumn('var_image');
        $this->configObj->glDbObj->addColumn('var_cap_title');
        $this->configObj->glDbObj->addColumn('var_cap_description');
        $this->configObj->glDbObj->addColumn('chr_validthumb');
        $this->configObj->glDbObj->addColumn('chr_delete');
        $this->configObj->glDbObj->addColumn('dt_createdate');
        $this->configObj->glDbObj->addColumn('dt_modifydate');

        // Set the Values
        $this->configObj->glDbObj->addValue($pagename);
        $this->configObj->glDbObj->addValue($_REQUEST['fk_recid']);
        $this->configObj->glDbObj->addValue($this->imagename);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_cap_title']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_cap_description']);
        $this->configObj->glDbObj->addValue('Y');
        $this->configObj->glDbObj->addValue('N');

        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());

        $this->lastinsertid = mysql_insert_id();
         $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'pagephoto');
        $this->configObj->glLibraryObj->insertinlogmanager(25, DB_PREFIX . 'pagephoto', 'var_title', $this->lastinsertid, 'I');
        return true;
    }

    function update_photos() {
        $sess = time();
        if (isset($this->varpostdata['chr_defaultimage'])) {
            $default = 'Y';
            $id = $_REQUEST['eid'];
            $this->make_default_image($id, $_REQUEST['fk_pages']);
        } else {
            $default = 'N';
        }

        if ($_FILES['var_image']['name'] != '') {

            //for removing space in name
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image']['name']);
            $pdf = basename($_FILES['var_image']['name']);
            $pdffile = str_replace(' ', '-', $pdf);
            $pdfdata = explode('.', $pdffile);
            $var_title = $pdfdata[0];

            $_FILES['var_image']['name'] = $pdffile;
            $this->imagename = $_FILES['var_image']['name'];
            if ($_REQUEST['fk_recid'] == 1) {
                $image_width = HOME_BANNER_WIDTH;
                $image_height = HOME_BANNER_HEIGHT;
            } else {
                $image_width = INNERBANNER_WIDTH;
                $image_height = INNERBANNER_HEIGHT;
            }

            $var_thumb = $this->upload_image('var_image', $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_');
            $this->imagename = $var_thumb;
            $uploaddir = '../upimages/pagephoto/images/';
            $uploadfile = $uploaddir . $this->varpostdata['hidvar_photo1'];
            $uploadthumb = $uploaddir . "thumb_" . $this->varpostdata['hidvar_photo1'];
            $uploadthumb1 = $uploaddir . "thumb_small" . $this->varpostdata['hidvar_photo1'];

            if (file_exists($uploadfile)) {
                unlink($uploadfile);
            }
            if (file_exists($uploadthumb)) {
                unlink($uploadthumb);
            }
        } else {

            $this->imagename = $this->varpostdata['hidvar_photo'];
            $this->chr_validimage = $this->varpostdata['hidchr_validthumb'];
        }

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'pagephoto');
        // Set the Columns
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('var_image');
        $this->configObj->glDbObj->addColumn('var_cap_title');
        $this->configObj->glDbObj->addColumn('var_cap_description');
        // $this->configObj->glDbObj->addColumn('chr_crop');
        $this->configObj->glDbObj->addColumn('chr_validthumb');
        $this->configObj->glDbObj->addColumn('chr_defaultimage');
        $this->configObj->glDbObj->addColumn('dt_modifydate');

        $this->configObj->glDbObj->addValue($this->varpostdata['var_title']);
        $this->configObj->glDbObj->addValue($this->imagename);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_cap_title']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_cap_description']);
        $this->configObj->glDbObj->addValue('Y');

        $this->configObj->glDbObj->addValue($default);
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->setWhere('int_glcode=' . $this->varpostdata['eid']);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->lastinsertid = $this->varpostdata['eid'];
        $this->configObj->glLibraryObj->insertinlogmanager(25, DB_PREFIX . 'pagephoto', 'var_title', $this->varpostdata['eid'], 'U');
        return true;
    }

    function get_var_imagepath($eid, $fieldname, $tablename) {
        $sql = "select " . $fieldname . " from " . $tablename . " where int_glcode=$eid AND chr_delete='N'";
        $rs = $this->configObj->glDbObj->get_sql_row($sql);
        $var_imagepath = $rs[$fieldname];
        return $var_imagepath;
    }

    function update_photogallery_title($fk_sec_modules, $id) {
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'pagephoto');
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addValue($_REQUEST['title']);
        $this->configObj->glDbObj->setWhere('int_glcode=' . $id);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->load_photogallery();
    }

    function select_photo($id) {

        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'pagephoto');
        $this->configObj->glDbObj->addColumn('*');
        $this->configObj->glDbObj->setWhere('int_glcode =' . $id);
        $row = $this->configObj->glDbObj->get_sql_row($this->configObj->glDbObj->buildQuery());
        return $row;
    }

    function make_default_image($id, $fk_recid) {
        $sql = "update " . DB_PREFIX . "pagephoto set chr_defaultimage='Y' where int_glcode='" . $id . "' and fk_recid='" . $fk_recid . "'";
        $this->configObj->glDbObj->query($sql);
        $sql = "update " . DB_PREFIX . "pagephoto set chr_defaultimage='N' where int_glcode != '" . $id . "' and fk_recid='" . $fk_recid . "'";
        $this->configObj->glDbObj->query($sql);
        return true;
    }

    function upload_image($filefield, $thumb_width, $thumb_height, $prefix, $sess, $large_width, $large_height, $prefix2 = '') {
        $file_photo = basename($_FILES[$filefield]['name']);

        $imagename = $sess . $file_photo;
        $uploaddir = '../upimages/pagephoto/images/';
        $file_photo = $sess . basename($_FILES[$filefield]['name']);
        $file = $uploaddir . $file_photo;

        $tmp = $_FILES[$filefield]['tmp_name'];
        $uploadedfile = $_FILES[$filefield]['tmp_name'];

        // Create an Image from it so we can do the resize
        $image_info = getimagesize($uploadedfile);

        switch ($image_info['mime']) {
            case 'image/gif':
                $img_create = 'ImageCreateFromGIF';
                break;
            case 'image/jpeg':
                $img_create = 'ImageCreateFromJPEG';
                break;
            case 'image/png':
                $img_create = 'ImageCreateFromPNG';
                break;
            case 'image/bmp':
                $img_create = 'ImageCreateFromBMP';
                break;
        }
        $photoexe = explode('.', $imagename);
        $photoexename = $photoexe[count($photoexe) - 1];

        if ($photoexename == 'bmp') {

            $src = $this->configObj->glLibraryObj->imagecreatefrombmp($uploadedfile);
        } else {
            $src = @$img_create($uploadedfile);
        }
        list($width, $height) = getimagesize($uploadedfile);

        $newwidth = $width;
        $newheight = $height;

        $tmp = imagecreatetruecolor($newwidth, $newheight);
        
        $background_color = imagecolorallocate($tmp, 255, 255, 255);
        imagefill($tmp, 0, 0, $background_color);

        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($tmp, $file, 90);

        imagedestroy($src);
        imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request  // has completed.

        $source1 = $uploaddir . $file_photo;

        //$sess = time();
        $thumbfilename = $prefix . $sess . $file_photo;

        $target1 = $uploaddir . $prefix . $file_photo;

        $target2 = $uploaddir . $prefix2 . $file_photo;

        if ($large_width > 0) {


            $returnthumbmsg1 = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width, false, false, $target1, false, null, $thumb_width, $thumb_height);
        }
        if ($large_width > 0) {
            $returnthumbmsg2 = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width, false, false, $target2, false, null, $large_width, $large_height);
        }
        if ($returnthumbmsg1 == 'INVALID' || $returnthumbmsg2 == 'INVALID') {
            $this->chr_validthumb = 'N';
            $this->chr_validthumbdetail = 'N';
        } else {
            $this->chr_validthumb = 'Y';
            $this->chr_validthumbdetail = 'Y';
        }
        return $imagename;
    }

    function getpagename($id) {
        if ($modulename == 'activity') {
            $field = 'var_name';
        } else if ($modulename == 'menu' || $modulename == 'menuitem') {
            $field = 'var_name';
        } else {
            $field = 'var_pagename';
        }

        $sqlpages = "SELECT " . $field . " from " . DB_PREFIX ."pages where chr_delete='N' and int_glcode=" . $id . "";
        $row = $this->configObj->glDbObj->get_sql_row($sqlpages);
        return $row[$field];
    }
   

}
?>

