<?php

class banner {

    var $pagename = 'index.php?module=banner'; // Attribute of Page Name
    var $int_glcode;   // KEY ATTR. WITH AUTOINCREMENT
    var $chr_read;
    var $chr_star;
    var $chr_publish;
    var $intdisplayorder;   // (normal Attribute)
    var $chr_delete = 'Y';   // (normal Attribute)
    var $dt_createdate;   // (normal Attribute)
    var $dt_modifydate;   // (normal Attribute)
    var $oldint_displayorder; // Attribute of Old Displayorder
    var $numofrows; // Attribute of Num Of Rows In Result
    var $numofpages; // Attribute of Num Of Pages In Result
    // var $orderby = 'int_displayorder' ; // Attribute of Deafult Order By
    ////var $orderby = 'dt_modifydate' ; // Attribute of Deafult Order By
    var $orderby = 'int_displayorder';
    var $ordertype = 'asc'; // Attribute of Deafult Order By
    var $searchby = '0'; // Attribute of Search By
    var $searchtxt; // Attribute of Search Text
    var $start = 1; // Attribute of Start For Paging
    var $pagesize = DEFAULT_PAGESIZE; // Attribute of Pagesize For Paging
    var $pagenumber = '1'; // Attribute of Page Number For Paging
    var $lastinsertid; // Attribute of Last Inserid
    var $urlwithpara = ''; // Attribute of URL With parameters
    var $urlwithpoutsearch = ''; // Attribute of URL With parameters without searh
    var $urlwithoutsort = ''; // Attribute of URL With parameters without sort
    var $urlwithoutpaging = ''; // Attribute of URL With parameters without paging
    var $configObj;
    var $thumb_width;
    var $thumb_height;
    var $var_main_file;
    // var $sortvar = '<img alt="sorting" src="'.GLOBAL_ADMIN_IMAGES_PATH.'/arrow-up.png" style="vertical-align:middle;">'; // This is for sort image
    var $filterby = '0';
    var $urlwithoutfilter = '';
    var $display_limit_array = array(5, 10, 15, 30);
    var $varpostdata = array();

    function banner($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;
        $_REQUEST['searchtxt'] = ($_REQUEST['type'] == 'autosearch') ? $_REQUEST['term'] : $_REQUEST['searchtxt'];
        $this->searchbyVal = (isset($_REQUEST['searchbyVal'])) ? $_REQUEST['searchbyVal'] : $this->searchbyVal;

        $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_banner($id) {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'banner');
        $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 CountRowsbanner() {

        if (trim($this->searchtxt) != '') {
            $whereclause .= ( $this->searchby == '0') ? " and var_title like '%" . addslashes($this->searchtxt) . "%' " : " and $this->searchby  like '%" . 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 . "banner where chr_delete = 'N' $whereclause ";

        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);

        $this->numofrows = $rs['total'];

        return $rs['total'];
    }

    function selectall_banner() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $tablename = DB_PREFIX . "banner";
        $this->configObj->glDbObj->setTable($tablename);
        $this->configObj->glDbObj->addColumn('*');

        $whereclause = "chr_delete = 'N'";
        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .= " and $filterarray[0] = '$filterarray[1]'";
        }
        if ($this->accessories != '0' && $this->accessories != '') {
            $whereclause .= " and accessory_type='" . $this->accessories . "'";
        }
        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) . "%'";
        }
        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);

        if ($_REQUEST['type'] == 'autosearch') {
            $orderby = (isset($_REQUEST['orderby'])) ? 'order by ' . $this->orderby . ' ' . $this->ordertype : 'order by int_displayorder';

            $searchbyVal = (!empty($this->searchbyVal)) ? " chr_delete = 'N' and {$this->configObj->glDbObj->addSlashes($this->searchbyVal)} like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' " : "$whereclause";

            if ($this->searchbyVal == '0') {
                $this->searchbyVal = "var_title";
            }
            if ($this->searchbyVal == 'var_title') {
                $this->searchbyVal = "var_title";
            }

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

            $searchbyVal1 = "{$this->searchbyVal} like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' and chr_delete = 'N'";
            $autoSearchQry = "select int_glcode ," . $this->searchbyVal . " as AutoVal FROM " . $tablename . " where  $searchbyVal1 $whereclauseauto group by " . $this->searchbyVal . " $orderby";
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        //echo $this->configObj->glDbObj->buildQuery(); exit;
        return $rs;
    }

    function update_banner() {
//        echo "<pre>";
//        print_r($_REQUEST);exit;

        if ($this->varpostdata['int_displayorder'] != $this->varpostdata['old_displayorder']) {
            $this->update_display_order($this->varpostdata['eid'], $this->varpostdata['int_displayorder'], $this->varpostdata['old_displayorder']);
        }
        $sess = time();
        $this->thumb_width = BANNER_THUMB_WIDTH;

        $this->thumb_height = BANNER_THUMB_HEIGHT;

        if ($_FILES["Filedata"]["name"] != "") {

            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['Filedata']['name']);
            $pdf = basename($_FILES['Filedata']['name']);
            $pdffile = str_replace(' ', '-', $pdf);
            $pdfdata = explode('.', $pdffile);
            $vartitle = $pdfdata[0];
            $_FILES['Filedata']['name'] = $pdffile;
            $tmp = $_FILES['Filedata']['tmp_name'];
            $var_thumb = $this->upload_image('Filedata', $this->thumb_width, $this->thumb_height, 'thumb_', $sess, $this->thumb_width, $this->thumb_height, 'thumb_');
            $this->imagename = $var_thumb;

            $uploaddir = $this->configObj->currentModulePath . 'html/uploads/images/';
            $uploadfile = $uploaddir . $this->varpostdata['hidvar_image'];
            $uploadthumb = $uploaddir . "thumb_" . $this->varpostdata['hidvar_image'];

            if (file_exists($uploadfile)) {
                unlink($uploadfile);
            }
            if (file_exists($uploadthumb)) {
                unlink($uploadthumb);
            }
        } else {
            $this->imagename = $_REQUEST['hidvar_image'];
            $this->chr_validimage = $_REQUEST['hidchr_validthumb'];
        }

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'banner');

        // Set the Columns
        $this->configObj->glDbObj->addColumn('var_title');
//        $this->configObj->glDbObj->addColumn('banner_type');
        $this->configObj->glDbObj->addColumn('var_caption');
        $this->configObj->glDbObj->addColumn('fk_pageid');
         $this->configObj->glDbObj->addColumn('fk_pagetypeid');
        $this->configObj->glDbObj->addColumn('var_banner');
        $this->configObj->glDbObj->addColumn('var_link');
        $this->configObj->glDbObj->addColumn('int_displayorder');
        $this->configObj->glDbObj->addColumn('chr_publish');
        $this->configObj->glDbObj->addColumn('chr_read');
        $this->configObj->glDbObj->addColumn('chr_star');
        $this->configObj->glDbObj->addColumn('chr_delete');
        $this->configObj->glDbObj->addColumn('dt_createdate');
        $this->configObj->glDbObj->addColumn('dt_modifydate');
        $this->configObj->glDbObj->addColumn('var_imagepath');

        $fk_pages = $this->varpostdata['fk_pages'];
         $pagetypeid = $this->varpostdata['hidchr_pagetypeid'];

        $var_link = $this->varpostdata['var_link'];

        $fk_pages = ($this->varpostdata['link'] == "i") ? $fk_pages : "";
         $pagetypeid = ($this->varpostdata['link'] == "i") ? $pagetypeid : "";
        $var_link = ($this->varpostdata['link'] == "e") ? $var_link : "";

        $this->configObj->glDbObj->addValue(htmlspecialchars($this->varpostdata['var_title']));
//        $this->configObj->glDbObj->addValue(htmlspecialchars($this->varpostdata['banner_type']));
        $this->configObj->glDbObj->addValue($this->varpostdata['var_caption']);
        $this->configObj->glDbObj->addValue($fk_pages);
        $this->configObj->glDbObj->addValue($pagetypeid);
        $this->configObj->glDbObj->addValue($_REQUEST['var_banner']);
        $this->configObj->glDbObj->addValue($var_link);
        $this->configObj->glDbObj->addValue($this->varpostdata['int_displayorder']);
        $this->configObj->glDbObj->addValue($this->varpostdata['chr_publish']);

        $this->configObj->glDbObj->addValue('Y');
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('NOW()');


        $this->configObj->glDbObj->addValue($this->imagename);
        $this->configObj->glDbObj->setWhere('int_glcode=' . $this->varpostdata['eid']);
        // echo $this->configObj->glDbObj->buildQuery();exit;
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        /* Start for Update Histroy */
        $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'banner');
        $this->configObj->glLibraryObj->insertinlogmanager(3, DB_PREFIX . 'banner', 'var_title', $this->varpostdata['eid'], 'U');
        return true;
    }

    function Bindpageshierarchy($name, $selected_id, $class = 'listbox') {
        if ($this->configObj->glSessionObj->value('admin_id') == '1') {
            
        } else {
            $dipnopar = "disabled";
        }
        $sql = "SELECT int_glcode AS id, var_pagename AS name, fk_pages,var_pagetype FROM " . DB_PREFIX . "pages
                WHERE chr_publish = 'Y'
                AND chr_delete = 'N' AND  chr_menu_display='Y' and chr_previewstatus='N' and chr_mainparent='N'
                ORDER BY int_displayorder";
        $rs = $this->configObj->glDbObj->query($sql);
        $children = array();
        $pitems = array();
        while ($row = $this->configObj->glDbObj->fetch_array($rs)) {
            $pitems[] = $row;
        }
        if ($pitems) {
            foreach ($pitems as $p) {
                $pt = $p['fk_pages'];
                $list = @$children[$pt] ? $children[$pt] : array();
                array_push($list, $p);
                $children[$pt] = $list;
            }
        }
        $list = $this->treerecurse(0, '&nbsp;&nbsp;&nbsp;', array(), $children, 10, 0, 0);

        $display_output = '<select name="' . $name . '" id="' . $name . '" ' . $classname . ' size="10" style="width: 55%;float:left;">';
        $display_output .= "";
        $temp1 = "";
        $temp = "";
        foreach ($list as $item) {
            if ($item['id'] == $_REQUEST['eid'] || $item['fk_pages'] == $_REQUEST['eid']) {
                $disabled = " disabled='disabled' ";
                $temp1 = $item['id'];
            } else if ($item['fk_pages'] == $temp || $item['fk_pages'] == $temp1 || $tempfk == $item['fk_pages']) {
                $disabled = " disabled='disabled' ";
                $temp = $item['id'];
                $tempfk = $item['fk_pages'];
            } else {
                $disabled = "";
            }
            if (isset($item['id']) && !empty($item['id']) && $item['id'] == '11') {
                $display_output .= "<option data-pagetypeid='0' value=" . $item['id'] . " " . (($item['id'] == $selected_id) ? 'selected' : '') . " " . $disabled . ">" . $item['treename'] . "</option>";
                $special = '';
                $specialsql = "select int_glcode,var_title from " . DB_PREFIX . "special where chr_delete = 'N' AND chr_publish = 'Y' AND if(dt_expiredate !='0000-00-00',CURDATE() between dt_startdate AND dt_expiredate, now()>=dt_startdate) order by int_displayorder asc, dt_startdate desc";
                $specialsrs = $this->configObj->glDbObj->query($specialsql);
                while ($specialrow = $this->configObj->glDbObj->fetch_array($specialsrs)) {
                    $display_output .= "<option data-pagetypeid='" . $item['var_pagetype'] . "' value='" . $specialrow['int_glcode'] . "' " . (($specialrow['int_glcode'] == $selected_id) ? 'selected' : '') . ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|_ " . $specialrow['var_title'] . "</option>";
                }
            } else {
                $display_output .= "<option data-pagetypeid='0' value=" . $item['id'] . " " . (($item['id'] == $selected_id) ? 'selected' : '') . " " . $disabled . ">" . $item['treename'] . "</option>";
            }
        }
        return $display_output;
    }

    function treerecurse($id, $indent, $list, $children, $maxlevel = PAGE_HIERARCHY_LEVEL, $level = 0, $type = 1) {
        if ($children[$id] && $level <= $maxlevel) {
            foreach ($children[$id] as $c) {
                $id = $c['id'];
                if ($type) {
                    $pre = '<sup>|_</sup>&nbsp;';
                    $spacer = '.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                } else {
                    $pre = '|_ ';
                    $spacer = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                if ($c['fk_pages'] == 0) {
                    $txt = $c['name'];
                } else {
                    $txt = $pre . $c['name'];
                }
                $pt = $c['fk_pages'];
                $list[$id] = $c;
                $list[$id]['treename'] = "$indent$txt";
                $list[$id]['children'] = count($children[$id]);
                $list = $this->treerecurse($id, $indent . $spacer, $list, $children, $maxlevel, $level + 1, $type);
            }
        }
        return $list;
    }

    function insert_banner() {

        $this->configObj->glLibraryObj->updatedisplay_order('banner', $this->varpostdata['int_displayorder']);
        $sess = time();
        if ($this->varpostdata['h_save'] == 'T') {
            //for remove space
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['Filedata']['name']);
            $pdf = basename($_FILES['Filedata']['name']);
            $pdffile = str_replace(' ', '-', $pdf);
            $pdfdata = explode('.', $pdffile);
            $vartitle = $pdfdata[0];
            $_FILES['Filedata']['name'] = $pdffile;
            $this->thumb_width = BANNER_THUMB_WIDTH;
            $this->thumb_height = BANNER_THUMB_HEIGHT;
            $thumb_width = BANNER_THUMB_WIDTH;
            $thumb_hight = BANNER_THUMB_HEIGHT;
            $var_thumb = $this->upload_image('Filedata', $thumb_width, $thumb_hight, 'thumb_', $sess, $thumb_width, $thumb_hight, 'thumb_');
            $this->imagename = $var_thumb;
            $this->int_glcode = ""; // clear key for autoincrement
            $this->configObj->glDbObj->resetfields();
            $this->configObj->glDbObj->setTable(DB_PREFIX . 'banner');
            $this->configObj->glDbObj->setQueryType('insert');
            // Set the Columns
            $this->configObj->glDbObj->addColumn('var_title');
//        $this->configObj->glDbObj->addColumn('banner_type');
            $this->configObj->glDbObj->addColumn('var_caption');
            $this->configObj->glDbObj->addColumn('fk_pageid');
            $this->configObj->glDbObj->addColumn('fk_pagetypeid');
            $this->configObj->glDbObj->addColumn('var_banner');
            $this->configObj->glDbObj->addColumn('var_link');
            $this->configObj->glDbObj->addColumn('int_displayorder');
            $this->configObj->glDbObj->addColumn('chr_publish');
            $this->configObj->glDbObj->addColumn('chr_read');
            $this->configObj->glDbObj->addColumn('chr_star');
            $this->configObj->glDbObj->addColumn('chr_delete');
            $this->configObj->glDbObj->addColumn('dt_createdate');
            $this->configObj->glDbObj->addColumn('dt_modifydate');
            $this->configObj->glDbObj->addColumn('var_imagepath');

            $fk_pages = $this->varpostdata['fk_pages'];
            $pagetypeid = $this->varpostdata['hidchr_pagetypeid'];

            $var_link = $this->varpostdata['var_link'];

            $fk_pages = ($this->varpostdata['link'] == "i") ? $fk_pages : "";
            $pagetypeid = ($this->varpostdata['link'] == "i") ? $pagetypeid : "";
            $var_link = ($this->varpostdata['link'] == "e") ? $var_link : "";

            $this->configObj->glDbObj->addValue(htmlspecialchars($this->varpostdata['var_title']));
//        $this->configObj->glDbObj->addValue(htmlspecialchars($this->varpostdata['banner_type']));
            $this->configObj->glDbObj->addValue($this->varpostdata['var_caption']);
            $this->configObj->glDbObj->addValue($fk_pages);
             $this->configObj->glDbObj->addValue($pagetypeid);
            $this->configObj->glDbObj->addValue($_REQUEST['var_banner']);
            $this->configObj->glDbObj->addValue($var_link);
            $this->configObj->glDbObj->addValue($this->varpostdata['int_displayorder']);
            $this->configObj->glDbObj->addValue($this->varpostdata['chr_publish']);

            $this->configObj->glDbObj->addValue('Y');
            $this->configObj->glDbObj->addValue('N');
            $this->configObj->glDbObj->addValue('N');
            $this->configObj->glDbObj->addValue('NOW()');
            $this->configObj->glDbObj->addValue('NOW()');


            $this->configObj->glDbObj->addValue($this->imagename);

//            echo $this->configObj->glDbObj->buildQuery();exit;
            $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
            $this->lastinsertid = mysql_insert_id();

            $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'banner');
            $this->configObj->glLibraryObj->insertinlogmanager(3, DB_PREFIX . 'banner', 'var_title', $this->lastinsertid, 'I');
            return true;
        }
    }

    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;

        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal;
    }

    function org_generateParam($view = 'banner', $actionurl = '', $searchArray = array(""), $table = '', $position = 'top') {
        $filterPos = ($position == 'top') ? 'FT' : 'FB';
        $prefix = ($view == 'Services') ? 'ser.' : '';

        if ($this->configObj->glSessionObj->value('admin_id') == '1') {
            $filterstar = array('filterarray' => array($prefix . 'chr_read-Y' => 'Read Only',
                    $prefix . 'chr_read-N' => 'Unread Only',
                    $prefix . 'chr_star-Y' => 'Star Only',
                    $prefix . 'chr_star-N' => 'Unstar Only',
                    $prefix . 'chr_publish-Y' => 'Publish Only',
                    $prefix . 'chr_publish-N' => 'Unpublish 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',
            'backbuttonurl' => 'index.php?module=pages',
            'actionImageHover' => 'add-new-button-blue-hover.gif',
            'actionUrl' => $actionurl,
//            'search' => array('searchArray' => $searchArray,
//                'searchBy' => $this->searchby,
//                'searchText' => $this->searchtxt,
//                //'searchUrl'    => $this->urlwithpoutsearch
//                'searchUrl' => $this->urlwithpoutsearch
//            ),
            'filter' => $filterstar,
            '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 . "banner";

        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'");
                $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] . '');
                        $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] . '');
                        $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] . '');
                $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
            }
        }
        // End of updation of Display Order
    }

    function WriteXMLFile() {
        $sql = "select * from " . DB_PREFIX . "banner where  chr_publish = 'Y' and chr_delete = 'N' order by int_displayorder desc";
        //echo $sql;exit;
        $rs = $this->configObj->glDbObj->query($sql);

        $xml = "<?xml version='1.0' encoding='UTF-8'?>
                   <banner>";
        while ($row = $this->configObj->glDbObj->fetch_array($rs)) {

            if ($row['chr_cropphoto'] == 'Y') {
                $image = "cr_thumb-" . $row['var_imagepath'];
            } else {

                $image = "thumb-" . $row['var_imagepath'];
            }


            if ($row['fk_pageid'] != '') {

                $url = 'index.php?module=banner&pageid=' . $row['fk_pageid'];
                //echo  $url;exit;
            } else {
                $url = $row['var_link'];
                //echo  $url;exit;
            }

            $xml .= '<banner>';
            $xml .= '<id>' . $row['int_glcode'] . '</id>';

            $xml .= '<title>' . $row['var_title'] . '</title>';
            $xml .= '<imagepath>' . SITE_PATH . '/powerpanel/modules/banner/html/uploads/images/' . $image . '</imagepath>';
            $xml .= '<link>' . $url . '</link>';
            $xml .= '<target>_self</target>';

            //echo $xml;exit;
            $xml .= '</banner>';
        }



        $xml .= "</banner>";

        $xml_filename = "../banner.xml";
        $ourFileName = $xml_filename;
        $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
        fwrite($ourFileHandle, $xml);
        fclose($ourFileHandle);
    }

    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 delete_banner($fk_modules) {

        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);

        for ($i = 0; $i < $totaldeletedrecords; $i++) {
            $sql = "Update " . DB_PREFIX . "banner 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);
            $this->configObj->glLibraryObj->insertinlogmanager($fk_modules, DB_PREFIX . 'banner', 'var_title', $deletearray[$i], 'D');
        }

        //echo $this->CountRowsFillout();exit;
        $this->configObj->glTplObj->set('numofrows', $this->CountRowsbanner());
        if (DISPLAY_XMLBANNER_FLAG == 'Y') {
            $this->WriteXMLFile();
        }
        //$this->configObj->glLibraryObj->insertinlogmanager(19, DB_PREFIX . 'banner', 'var_title', $this->lastinsertid, 'D');

        $this->load_banner();
        exit;
    }

    /*     * ********************* */

    //start load page

    /*     * ********************* */

    function load_banner() {
        $this->searchtxt = $_REQUEST['searchtxt'];

        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, 'banner');
            $tablename = "" . DB_PREFIX . 'banner';
            $this->configObj->glLibraryObj->set_display_order_sequence($tablename);
        }


        $this->pagename = 'index.php?module=banner';  // Attribute of Page Name
        $this->countFunction = $this->CountRowsbanner();
        $this->loadCount('CountRowsbanner');

        $this->Generateurl();


        $this->configObj->glTplObj->set('numofrows', $this->CountRowsbanner());

        $result = $this->selectall_banner();

        $this->configObj->glTplObj->set('result', $result);

        $view = 'Manage Banners (Home)';

        $actionurl = 'index.php?module=banner&action=add';

        $searchArray = array("var_title" => "Title");

        $table = DB_PREFIX . 'banner';

        $parmas = $this->org_generateParam($view, $actionurl, $searchArray, $table);

        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas));

        $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

        $new_string = str_replace("[^A-Za-z0-9_]", "", $this->orderby);
        $tmpsetsortvar = 'setsortimg' . $new_string;

        $$finalsortvar = $tmpsetsortvar;

        $this->configObj->glTplObj->set($$finalsortvar, $this->sortvar);

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


        $this->configObj->glTplObj->set($$finalsortvar, $this->sortvar);

        if ($_REQUEST['msg']) {
            if ($_REQUEST['msg'] == 'add') {
                $messagebox = $this->configObj->glLibraryObj->message("The record has been successfully saved.");

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

                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }

        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('banner.tpl.php');

            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('banner.tpl.php');
        }
    }

    function loadCount($neworderby = '') {

        ///argument $neworderby will be used only in case of view if orderby field is different then default

        $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();
    }

    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/banner/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);
        //print_r($image_info);exit;
        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;
        }

        // $src = imagecreatefromjpeg($uploadedfile);
        $src = @$img_create($uploadedfile);
        // Capture the original size of the uploaded image
        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);
        // this line actually does the image resizing, copying from the original // image into the $tmp image
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        // now write the resized image to disk.
        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;exit;

        if ($large_width > 0) {


            $returnthumbmsg = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width, false, false, $target1, false, null, $thumb_width, $thumb_height);
        }

        if ($returnthumbmsg == 'INVALID') {
            $this->chr_validthumb = 'N';
            $this->chr_validthumbdetail = 'N';
        } else {
            $this->chr_validthumb = 'Y';
            $this->chr_validthumbdetail = 'Y';
        }
        //chmod($uploaddir.$imagename,0777);
        //echo $imagename;exit;
        return $imagename;
    }

}

?>