<?php

class contactuspage {

    var $pagename = 'index.php?module=contactuspage';
    var $int_glcode;
    var $chr_read;
    var $chr_star;
    var $chr_publish;
    var $intdisplayorder;
    var $chr_delete = 'Y';
    var $dt_createdate;
    var $dt_modifydate;
    var $oldint_displayorder;
    var $numofrows;
    var $numofpages;
    var $orderby = 'int_glcode';
    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();

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

    function intitialize() {
        $this->searchby = (isset($_REQUEST['searchby'])) ? $_REQUEST['searchby'] : $this->searchby;
        $this->searchtxt = (isset($_REQUEST['searchtxt'])) ? urldecode($_REQUEST['searchtxt']) : '';
        $this->searchtxtjs = (isset($_REQUEST['searchtxt'])) ? urlencode($_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;\">";
            }
        } else {
            $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 select_contactuspage($id) {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'contactuspage');
        $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 CountRows() {
        $this->intitialize();
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? "where and (var_title like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' or var_email 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 . "contactuspage $whereclause ";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_contactuspage() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'contactuspage');
        $this->configObj->glDbObj->addColumn('*');

        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? "where and (var_title like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' or var_email 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]'";
        }

        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

// **********************
// 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;
        $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;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs;
    }

    function org_generateParam($view = 'contactuspage', $actionurl = 'index.php?module=contactuspage&action=add', $searchArray = array("contacts" => "Contactus"), $table = 'DB_PREFIX.contacts', $position = 'top') {
        $filterPos = ($position == 'top') ? 'FT' : 'FB';
        if ($this->configObj->glSessionObj->value('chr_usertype') == 'A') {
            $filterstar = array('filterarray' => array('chr_read-Y' => 'Read Only',
                    'chr_read-N' => 'Unread Only',
                    'chr_star-Y' => 'Star Only',
                    'chr_star-N' => 'Unstar Only'
                ),
                'filterurl' => $this->urlwithoutfilter,
                'filterposition' => $filterPos,
                'filterby' => $this->filterby
            );
        } else {

            $filterstar = array();
        }
        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',
            'actionUrl' => $actionurl,
            '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 get_fk_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 update_contactuspage() {
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'contactuspage');
        $this->configObj->glDbObj->addColumn('var_name');
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('var_address');
        $this->configObj->glDbObj->addColumn('var_mail_address');
        $this->configObj->glDbObj->addColumn('var_phone');        
        $this->configObj->glDbObj->addColumn('var_cell');        
        $this->configObj->glDbObj->addColumn('var_email');
        $this->configObj->glDbObj->addColumn('var_fax');
        $this->configObj->glDbObj->addColumn('var_open_hour');

        // Set the Columns Value
        $this->configObj->glDbObj->addValue($this->varpostdata['var_name']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_title']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_address']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_mail_address']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_phone']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_cell']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_email']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_fax']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_open_hour']);
        $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('13', DB_PREFIX . 'contactuspage', 'var_title', $this->lastinsertid, 'U');

        return true;
    }

//start load page
    function load_contactuspage() {
        $this->pagename = 'index.php?module=contactuspage'; // Attribute of Page Name
        $this->countFunction = $this->CountRows();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $this->loadCount();
        $result = $this->selectall_contactuspage();
        $this->configObj->glTplObj->set('result', $result);
        $this->Generateurl();
        $view = 'Contact Us Page Details';
        $actionurl = 'index.php?module=contactuspage&action=add';
        $searchArray = array("var_name" => "Name", "var_email" => "Email Id");
        $table = DB_PREFIX . 'contacts';
        $parmas = $this->org_generateParam($view, $actionurl, $searchArray, $table);

        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas, 'no'));
        $this->configObj->glTplObj->set('pagingTop', $this->configObj->glLibraryObj->generatePagingPannel($parmas));
        $this->configObj->glTplObj->set('pagingBottom', $this->configObj->glLibraryObj->generatePagingPannel($this->org_generateParam($view, $actionurl, $searchArray, $table, 'bottom')));
        // 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']) {
            if ($_REQUEST['msg'] == 'add') {
                $messagebox = $this->configObj->glLibraryObj->message("The record is successfully saved");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $this->configObj->glLibraryObj->message("The record is successfully saved");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }
        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('contactuspage.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('contactuspage.tpl.php');
        }
    }

    function loadCount() {
        $this->intitialize();
        $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();
    }

}
?>