<?php

class commonpdf {

    var $pagename = 'index.php?module=commonpdf';
    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_displayorder';
    var $ordertype = 'asc';
    var $searchby = '0';
    var $searchtxt;
    var $start = '0';
    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 commonpdf($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-down.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 oldintitialize($view) {
        $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-down.png\" style=\"vertical-align:middle;\">";
        }
        $this->numofrows = (isset($_REQUEST['numofrows'])) ? $_REQUEST['numofrows'] : $this->CountRows();
        $this->pagesize = (isset($_REQUEST['pagesize'])) ? $_REQUEST['pagesize'] : $this->pagesize;
        $this->pagenumber = (isset($_REQUEST['pagenumber'])) ? $_REQUEST['pagenumber'] : $this->pagenumber;
        $this->noofpages = intval($this->numofrows / $this->pagesize);
        $this->noofpages = (($this->numofrows % $this->pagesize) > 0) ? ($this->noofpages + 1) : ($this->noofpages);
        $this->start = ($this->pagenumber - 1 ) * $this->pagesize;
    }

    function select_commonpdf($id) {
        $string = DEFAULT_DATEFORMAT;
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $tablename = DB_PREFIX . "pdf_mst";
        $this->configObj->glDbObj->setTable($tablename);
        $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') ? " 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 . "pdf_mst where chr_delete = 'N'  $whereclause ";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_commonpdf() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $tablename = DB_PREFIX . "pdf_mst";
        $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 (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);
        }
//        echo $this->configObj->glDbObj->buildQuery();exit;
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

    // **********************
    // INSERT
    // **********************

    function insert_commonpdf() {
        
//        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "pdf_mst where chr_delete = 'N'";
//        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
//       if ($rs['total'] >= $this->varpostdata['int_displayorder']) {
//            $this->configObj->glLibraryObj->updatedisplay_order('pdf_mst', $this->varpostdata['int_displayorder']);
//            $int_displayorder = $this->varpostdata['int_displayorder'];
//        } else {
//            $int_displayorder = $rs['total'] + 1;
//        }
        $this->configObj->glLibraryObj->updatedisplay_order('pdf_mst', $this->varpostdata['int_displayorder']);
         
//$this->updatedisplay_order();
        if ($this->varpostdata['h_save'] == 'T') {
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_pdffile']['name']);
            $pdf = basename($_FILES['var_pdffile']['name']);
            if ($pdf != '') {
                $pdffile = preg_replace('/[\/:*?"<>| ]/', '-', $pdf);
            } else {
                $pdffile = $pdf;
            }

            if ($pdffile != "") {
                $uploaddir = '../upimages/commonpdf/';
                $sess = time();
                $pdf = basename($_FILES['var_pdffile']['name']);
                $pdffile = str_replace(' ', '-', $pdf);
                $new_pdfname = $sess . $pdffile;
                $file = $uploaddir . $new_pdfname;
                $tmp = $_FILES['var_pdffile']['tmp_name'];
                if (move_uploaded_file($tmp, $file)) {
                    $pdffile = $new_pdfname;
                } else {
                    echo "error " . $_FILES['var_pdffile']['error'] . " --- " . $tmp . " %%% " . $file;
                }
            }

            $this->int_glcode = ""; // clear key for autoincrement
            $this->configObj->glDbObj->resetfields();
            $tablename = DB_PREFIX . 'pdf_mst';
            $this->configObj->glDbObj->setTable($tablename);
            $this->configObj->glDbObj->setQueryType('insert');

            // Set the Columns
            $this->configObj->glDbObj->addColumn('var_title');
            $this->configObj->glDbObj->addColumn('var_alias');
            $this->configObj->glDbObj->addColumn('fk_categoryglcode');
            $this->configObj->glDbObj->addColumn('var_description');

            $this->configObj->glDbObj->addColumn('int_hits');
            $this->configObj->glDbObj->addColumn('int_displayorder');

            $this->configObj->glDbObj->addColumn('dt_createddate');
            $this->configObj->glDbObj->addColumn('dt_modifydate');
            $this->configObj->glDbObj->addColumn('chr_delete');
            $this->configObj->glDbObj->addColumn('chr_read');
            $this->configObj->glDbObj->addColumn('chr_star');
            $this->configObj->glDbObj->addColumn('chr_publish');
            //$this->configObj->glDbObj->addColumn('fk_countryglcode');
            $this->configObj->glDbObj->addColumn('var_pdffile');

            $this->configObj->glDbObj->addValue(($this->varpostdata['var_title']));
            $this->configObj->glDbObj->addValue($this->varpostdata['var_alias']);
            $this->configObj->glDbObj->addValue($this->varpostdata['fk_subcategory_mst']);
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description']));

            $this->configObj->glDbObj->addValue($this->varpostdata['int_hits']);
            $this->configObj->glDbObj->addValue($this->varpostdata['int_displayorder']);

            $this->configObj->glDbObj->addValue('NOW()');
            $this->configObj->glDbObj->addValue('NOW()');
            $this->configObj->glDbObj->addValue('N');
            $this->configObj->glDbObj->addValue('N');
            $this->configObj->glDbObj->addValue('N');
            $this->configObj->glDbObj->addValue($this->varpostdata['chr_publish']);
            $this->configObj->glDbObj->addValue($pdffile);

            $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());

            $this->lastinsertid = mysql_insert_id();
            
            $this->configObj->glLibraryObj->set_display_order_sequence($tablename);
            
            $this->configObj->glLibraryObj->insertinlogmanager(76, DB_PREFIX . 'pdf_mst', 'var_title', $this->lastinsertid, 'I');

            return true;
        }
    }
    function updatedisplay_order() {
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'pdf_mst');
        $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>=' . $this->varpostdata['int_displayorder']);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
    }
    
    
    
    // UPDATE
    function update_commonpdf() {
        if ($this->varpostdata['int_displayorder'] != $this->varpostdata['old_displayorder']) {
           $this->update_display_order($this->varpostdata['eid'], $this->varpostdata['int_displayorder'], $this->varpostdata['old_displayorder']);
            
        }
//        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "pdf_mst where chr_delete = 'N'";
//               $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
//               if ($rs['total'] >= $this->varpostdata['int_displayorder']) {
//                    $this->configObj->glLibraryObj->updatedisplay_order('pdf_mst', $this->varpostdata['int_displayorder']);
//                    $int_displayorder = $this->varpostdata['int_displayorder'];
//                } else {
//                    $int_displayorder = $rs['total'] + 1;
//                }
        $pdf = basename($_FILES['var_pdffile']['name']);
        if ($pdf != '') {
            $pdffile = str_replace(' ', '-', $pdf);
            $pdffile = preg_replace('/[\/:*?"<>| ]/', '-', $pdf);
        } else {
            $pdffile = $pdf;
        }

        if ($pdffile != "") {
            $uploaddir = '../upimages/commonpdf/';
            $sess = time();
            $new_pdfname = $sess . $pdffile;
            $file = $uploaddir . $new_pdfname;
            $tmp = $_FILES['var_pdffile']['tmp_name'];
            if (move_uploaded_file($tmp, $file)) {
                $pdffile = $new_pdfname;
                $old_pdffile = $_REQUEST['hidvar_pdffile'];
                if ($old_pdffile != '') {
                    $old_pdffile = $uploaddir . $old_pdffile;
                    if (file_exists($old_pdffile)) {
                        unlink($old_pdffile);
                    }
                }
            } else {
                echo "error " . $_FILES['var_pdffile']['error'] . " --- " . $tmp . " %%% " . $file;
            }
        } else {
            $pdffile = $_REQUEST['hidvar_pdffile'];
        }

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

        // Set the Columns
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('var_alias');
        $this->configObj->glDbObj->addColumn('fk_categoryglcode');
        $this->configObj->glDbObj->addColumn('var_description');
        $this->configObj->glDbObj->addColumn('int_hits');
        $this->configObj->glDbObj->addColumn('int_displayorder');
        $this->configObj->glDbObj->addColumn('dt_modifydate');
        $this->configObj->glDbObj->addColumn('chr_publish');
        $this->configObj->glDbObj->addColumn('chr_read');
        $this->configObj->glDbObj->addColumn('var_pdffile');

        // Set the Values
        $this->configObj->glDbObj->addValue(($this->varpostdata['var_title']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_alias']));
        $this->configObj->glDbObj->addValue($this->varpostdata['fk_subcategory_mst']);
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description']));
        $this->configObj->glDbObj->addValue($this->varpostdata['int_hits']);
        $this->configObj->glDbObj->addValue($this->varpostdata['int_displayorder']);
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue($this->varpostdata['chr_publish']);
        $this->configObj->glDbObj->addValue('Y');
        $this->configObj->glDbObj->addvalue($pdffile);
        $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->set_display_order_sequence($tablename);
        $this->configObj->glLibraryObj->insertinlogmanager(76, DB_PREFIX . 'pdf_mst', 'var_title', $this->lastinsertid, 'U');
       
        return true;
    }

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

    function Generateurl() {
        $this->hurlwithpara = $this->pagename . '&' . 'hpagesize=' . $this->pagesize . '&hnumofrows=' . $this->numofrows . '&horderby=' . $this->orderby . '&hordertype=' . $this->ordertype . '&hsearchby=' . $this->searchby . '&hsearchtxt=' . $this->searchtxtjs . '&hpagenumber=' . $this->pagenumber . '&hfilterby=' . $this->filterby . '&history=T';
        $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 = 'commonpdf', $actionurl = 'index.php?module=commonpdf&action=add', $searchArray = array("var_title" => "commonpdf"), $table = 'cuc_pdf_mst', $position = 'top') {
        $filterPos = ($position == 'top') ? 'FT' : 'FB';
        if ($this->configObj->glSessionObj->value('admin_id') == '1') {
            $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,
            'search' => array('searchArray' => $searchArray,
                'searchBy' => $this->searchby,
                'searchText' => $this->searchtxt,
                '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 set_display_order_sequence($tablename) {
//
//        $sql = 'select int_glcode from ' . $tablename . ' where chr_delete = "N" order by int_displayorder asc';
//        $query = $this->configObj->glDbObj->query($sql);
//        $total = $this->configObj->glDbObj->num_rows($query);
//
//        $i = 0;
//        if ($total > 0) {
//            while ($row = $this->configObj->glDbObj->fetch_array($query)){
//                $i++;
//                $ids[$i] = $row['int_glcode'];
//                $update_syntax .= " WHEN " . $row['int_glcode'] . " THEN $i ";
//            }
//        }
//
//        if ($total > 0) {
//            $sql = "UPDATE " . $tablename . " SET int_displayorder = (CASE int_glcode " . $update_syntax . " END) WHERE int_glcode BETWEEN " . min($ids) . " AND " . max($ids) . " and chr_delete = 'N'";
//            $this->configObj->glDbObj->query($sql);
//        }
//    }
    
//       function update_display_order($ids, $values, $oldvalues,  $table = 'pdf_mst') {
//        $tablename = "" . DB_PREFIX . $table;
//
//        for ($i = 0; $i < count($ids); $i++) {
//            // Updation Of the Display Order
//            if ($values[$i] != $oldvalues[$i]) {
//
//                $query = "SELECT count(1) as total FROM " . $tablename . " WHERE int_displayorder=" . $values[$i] . " and chr_delete = 'N'";
//                $rs = $this->configObj->glDbObj->get_sql_row($query);
//
//                if ($rs['total'] > 0) {
//                    if ($values[$i] > $oldvalues[$i]) {
//                        $updateSql = "UPDATE $tablename SET int_displayorder=int_displayorder - 1 WHERE int_displayorder > " . $oldvalues[$i] . " and  int_displayorder <= " . $values[$i] . " and chr_delete = 'N'";
//                        $this->configObj->glDbObj->query($updateSql);
//                    } else {
//                        $updateSql = "UPDATE $tablename SET int_displayorder=int_displayorder + 1 WHERE int_displayorder >= " . $values[$i] . " and  int_displayorder < " . $oldvalues[$i] . " and chr_delete = 'N'";
//                        $this->configObj->glDbObj->query($updateSql);
//                    }
//                }
//
//                $updateSql = "UPDATE $tablename SET int_displayorder=$values[$i] WHERE int_glcode=" . $ids[$i] . " and chr_delete = 'N'";
//                $this->configObj->glDbObj->query($updateSql);
//            }
//            // End of updation of Display Order
//        }
//        // End of updation of Display Order
//    }
//
//    /*     * ********** */
//
//    //Start for delete
//    /*     * ******* */
    
    
    
    function update_display_order($ids, $values, $oldvalues) {
        $tablename = "pdf_mst";

        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(DB_PREFIX . "pdf_mst");

                $this->configObj->glDbObj->addColumn('count(1) as total');

                $this->configObj->glDbObj->setWhere("int_displayorder=" . $values[$i] . " and chr_delete = 'N' $updatewhereclause");

                $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(DB_PREFIX . "pdf_mst");

                        $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] . '' . $updatewhereclause);

                        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
                    } else {
                        $this->configObj->glDbObj->resetfields();

                        $this->configObj->glDbObj->setTable(DB_PREFIX . "pdf_mst");

                        $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] . '' . $updatewhereclause);

                        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
                    }
                }
                $this->configObj->glDbObj->resetfields();

                $this->configObj->glDbObj->setTable(DB_PREFIX . "pdf_mst");

                $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] . '' . $updatewhereclause);

                $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
                //echo $this->configObj->glDbObj->buildQuery();
            }
        }
    }

    function delete_commonpdf() {
        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);
        $is_assigned = 0;
        $delcount = 0;
        $tablename = DB_PREFIX . 'pdf_mst';
        for ($i = 0; $i < $totaldeletedrecords; $i++) {

            $sql = "Update " . DB_PREFIX . "pdf_mst 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(76, $tablename, 'var_title', $deletearray[$i], 'D');
            
            
            
        }
        $this->load_commonpdf();
        exit;
    }

   

    function load_commonpdf() {

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

        $this->loadCount('CountRows');

        $this->configObj->glTplObj->set('numofrows', $this->CountRows());
        $result = $this->selectall_commonpdf();
        $this->configObj->glTplObj->set('result', $result);

        $this->Generateurl();
        $view = 'Manage Common Files ';
        $actionurl = 'index.php?module=commonpdf&action=add';
        $searchArray = array("var_title" => "Title");
        $table = DB_PREFIX . "pdf_mst";
        $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
        $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("Congrats! The record has been successfully saved.");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $this->configObj->glLibraryObj->message("Congrats! The record has been successfully edited.");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }

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

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

    function loadCount($function) {
        $this->intitialize();
        $this->numofrows = (isset($_REQUEST['numofrows'])) ? $_REQUEST['numofrows'] : $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();
    }

}

?>