<?php

class product {

    var $pagename = 'index.php?module=product'; // Attribute of Page Name
    var $int_glcode;   // KEY ATTR. WITH AUTOINCREMENT
    var $chr_read;
    var $chr_star;
    var $chr_publish;
    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 $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 $categorytype = '';
    var $filterby = '0';
    var $urlwithoutfilter = '';
    var $display_limit_array = array(5, 10, 15, 30);
    var $varpostdata = array();

    function product($configObj) {
        $this->checkAliasTable = array(
            DB_PREFIX . "product" => "var_alias",);
       // $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;
        $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'])) ? $_REQUEST['searchtxt'] : '';
        $this->searchtxtjs = (isset($_REQUEST['searchtxt'])) ? $_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 select_product($id) {
        $dtformat = DEFAULT_DATEFORMAT;
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'product');
        $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($view);
        if (trim($this->searchtxt) != '') {
            $whereclause .= ( $this->searchby == '0') ? " and p.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 p.$filterarray[0] = '$filterarray[1]'";
        }
        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "product p  where p.chr_delete = 'N' $whereclause ";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];

        return $rs['total'];
    }
    
    function catnews($id) {
        $sql = "select var_title from " . DB_PREFIX . "product where int_glcode=" . $id;
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        return $result['var_title'];
    }

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

     /*
     * Facebook and Twitter Start
     */
    
    function getfacabookaccount() {
        $sql = "select * from " . DB_PREFIX . "facebook where chr_delete='N' and chr_publish='Y'";
        $rec = $this->configObj->glDbObj->query($sql);
        return $rec;
    }

    function gettwitteraccount() {
        $sql = "select * from " . DB_PREFIX . "twitter where chr_delete='N' and chr_publish='Y'";
        $rec = $this->configObj->glDbObj->query($sql);
        return $rec;
    }
    
    function getproductcustomdata()
    {
        $sql = "SELECT var_title,var_description FROM " . DB_PREFIX . "product WHERE chr_delete='N' AND int_glcode='" . $_REQUEST['eid'] . "'";
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        return $result;
    }

    function genrateshareOption() {

        $this->configObj->glTplObj->set('twitterresult', $this->gettwitteraccount());
        $this->configObj->glTplObj->set('result', $this->getfacabookaccount());
        $this->configObj->glTplObj->set('productdata', $this->getproductcustomdata());
        return $this->configObj->glTplObj->process('shareoption.tpl.php');

    }

    function getlogdetail($fb_tw_id, $articleid, $type = "F") {
       $sql = "select *,DATE_FORMAT(dt_createdate ,'" . DEFAULT_DATEFORMAT . "') as dt_createdate from " . DB_PREFIX . "sharelog where chr_type ='{$type}' and fk_facebook_twitter ={$fb_tw_id}  and fk_article={$articleid} and fk_module=85  order by dt_createdate desc limit 0,1";
        $rec = $this->configObj->glDbObj->query($sql);
        return $rec;
    }
    
    function getproductalias($alias) {
        $sql = "SELECT var_alias FROM " . DB_PREFIX . "product WHERE chr_delete='N' AND int_glcode='" . $alias . "'";
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        return $result["var_alias"];
    }
    
     function get_Post_data($id) {
         
        $sql = "SELECT * FROM " . DB_PREFIX . "productphotogallery WHERE chr_delete='N' AND fk_product='" . $id . "' and chr_defaultimage='Y'";

        $result = $this->configObj->glDbObj->get_sql_row($sql);
        
        $product_alias=$this->getproductalias($result["fk_product"]);
  
        $imagename = $result['var_image'];
        $corePath="upimages/productphotogallery/images/thumb/";
        $imagepath = '../'.$corePath;
        $imagename="thumb_" . $imagename;
        
        $image = $imagepath . $imagename;
        
        $imagelink=$corePath.$imagename;
        //echo !file_exists($image);

        if ($imagename != "") {
            $image = (!file_exists($image)) ? "" : $image;
        } else {
            $image = "";
        }
        //echo $image;
        if (!empty($image)) {
            $image = SITE_PATH . $imagelink;
            $paramArr['data']['picture'] = $image;
        }
      if (SITE_SEO == 'Y') {
            $product_link = SITE_PATH . $product_alias;
        } else {
            $product_link = SITE_PATH . "product/detail/" . $id;
        }
        $paramArr['int_glcode'] = $id;
        $paramArr['data']['name'] = $this->varpostdata['var_title'];
        $paramArr["data"]["actions"] = array("name" => "Sent from Netclues PowerPanel", "link" => "http://www.netclues.ky");
        $paramArr['data']['link'] = $product_link;
//        $paramArr['data']['description'] = $this->varpostdata['var_short_description'];
        if ($this->varpostdata['var_description'] !='')
        {
        $paramArr['data']['description'] = $this->varpostdata['var_description'];
        }
        // $paramArr['description'] = "hello";

        return $paramArr;
    }
    
    function shareonfacebook() {
        $status = 0;

        if (!empty($this->varpostdata['fb']) || !empty($this->varpostdata['tw'])) {
            $eidArray = explode(",", $this->varpostdata['eid']);
           
            foreach ($eidArray as $eidvalue) {
                $param = $this->get_Post_data($eidvalue);
                if (!empty($this->varpostdata['fb'])) {

                    foreach ($this->varpostdata['fb'] as $value) {
                        // print_r($value);
                        $pageDetail = $this->getfbpagedetail($value);
                        
                        $param["APP_ID"] = $pageDetail['var_application_id'];
                        $param["APP_SECRET"] = $pageDetail['var_seceret_key'];
                        $param["PAGE_ID"] = $pageDetail['var_page_id'];
                        $param['data']["access_token"] = $pageDetail['var_oauth_token'];

                        if ($this->configObj->glLibraryObj->shareOnFacebook($param) == 1) {
                            $status = 1;
                            $this->addfbsharelog($pageDetail, $param, "F");
                        }
                    }
                }
                if (!empty($this->varpostdata['tw'])) {
                    $eidArray = explode(",", $this->varpostdata['eid']);

                    $trid = $eidArray[0];
//                    $albumalias = $this->get_album_alias($trid);
                    foreach ($this->varpostdata['tw'] as $value) {
                        $pageDetail = $this->gettwpagedetail($value);

                        $param['CONSUMER_KEY'] = $pageDetail['var_consumer_key'];
                        $param['CONSUMER_SECRET'] = $pageDetail['var_consumer_secret'];
                        $param['OUATH_TOKEN'] = $pageDetail['var_ouath_token'];
                        $param['OUATH_TOKEN_SECRET'] = $pageDetail['var_ouath_token_secret'];
                        $param['status'] = $param['name'];
//                        $param['media'] = $param['picture'];
                        $titledata = strlen($param['data']['name']) > 50 ? substr($param['data']['name'], 0, 50) . "..." : $param['data']['name'];
                        $data = strlen($param['data']['description']) > 70 ? substr($param['data']['description'], 0, 70) . "..." : $param['data']['description'];
                        $param['data']['name'] = $titledata;
                        $param['data']['name'].="\n" . $data;


                        $param['data']['name'].="\n" . SITE_PATH ;
//                        $param['data']['name'].=$param['media'];
                        if ($this->configObj->glLibraryObj->shareOnTwitter_news($param) == 1) {
                            $status = 1;
                            $this->addfbsharelog($pageDetail, $param, "T");
                        }
                    }
                }
            }
        }
        //print_r($_REQUEST);
        return $status;
    }
    
    function getfbpagedetail($id) {
        $sql = "select * from " . DB_PREFIX . "facebook where chr_delete='N' and chr_publish='Y' and int_glcode={$id}";
        $rec = $this->configObj->glDbObj->get_sql_row($sql);
        return $rec;
    }

    function gettwpagedetail($id) {
        $sql = "select * from " . DB_PREFIX . "twitter where chr_delete='N' and chr_publish='Y' and int_glcode={$id}";
        $rec = $this->configObj->glDbObj->get_sql_row($sql);
        return $rec;
    }

    function addfbsharelog($pageParam, $articleParam,$type) {
        $param = array(
            "fk_facebook_twitter" => $pageParam['int_glcode'],
            "fk_article" => $articleParam['int_glcode'],
            "chr_type" => $type,
            "dt_createdate" => "now()",
            "dt_modifydate" => "now()",
            "fk_module" => "85"
        );

        $this->configObj->glLibraryObj->insertIntoDB(DB_PREFIX . "sharelog", $param);
        $this->insertfacebooklogdata();
    }
    
    function insertfacebooklogdata()
    {
        $sql="insert into " . DB_PREFIX . "facebooklog (fk_sharelogglcode,var_title,var_description,var_ipaddress,var_name) values ('". mysql_insert_id()."','".mysql_real_escape_string($this->varpostdata['var_title'])."','".mysql_real_escape_string($this->varpostdata['var_description']). "','".$_SERVER['REMOTE_ADDR']."','".ADMIN_NAME."')";

//        $sql="insert into " . DB_PREFIX . "facebooklog (fk_sharelogglcode,var_title,var_description) values ('".  mysql_insert_id()."','".$this->varpostdata['var_title']."','".$this->varpostdata['var_short_description']."')";
        $this->configObj->glDbObj->query($sql);
    }
    
   /*
    * Facebook and Twitter End
    */
    
    function selectall_product() {
        $dtformat = DEFAULT_DATEFORMAT;
        $dtformat = "%d/%m/%Y";
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $tablename = DB_PREFIX . "product p";
        $this->configObj->glDbObj->setTable($tablename);
        $this->configObj->glDbObj->addColumn("*");
        $whereclause = "p.chr_delete = 'N' ";
        if (trim($this->searchtxt) != '') {
            $whereclause .= ( $this->searchby == '0') ? " and p.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 p.$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);

        if ($_REQUEST['type'] == 'autosearch') {
            $orderby = (isset($_REQUEST['orderby'])) ? 'order by p.' . $this->orderby . ' ' . $this->ordertype : 'order by p.dt_modifydate';
            $searchbyVal = (!empty($this->searchbyVal)) ? " $whereclause " : $whereclause;
            $autoSearchQry = "select p.int_glcode ," . $this->searchbyVal . " as AutoVal FROM " . $tablename . " where $searchbyVal group by " . $this->searchbyVal . " $orderby";
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }

        $this->configObj->glDbObj->buildQuery();
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

    function insert_product() {
        $this->configObj->glLibraryObj->updatedisplay_order('product', $this->varpostdata['int_displayorder']);
        
        $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_pdffile']['name']);
        $pdf = basename($_FILES['var_pdffile']['name']);
        $pdffile = str_replace(' ', '-', $pdf);
        $pdfdata = explode('.', $pdffile);
        $vartitle = $pdfdata[0];
        $pdffile = str_replace(' ', '-', $pdf);
        $pdfdata1 = explode('.', $pdffile);
        $p = count($pdfdata1);
        unset($pdfdata1[$p - 1]);
        $filename = implode('.', $pdfdata1);
        if ($pdf != "") {
            $uploaddir = '../upimages/product/pdf/';

            $sess = time();
            $pdf = basename($_FILES['var_pdffile']['name']);
            $pdffile = str_replace(' ', '-', $pdf);
            $pdffile = str_replace('%20', '-', $pdf);
            $new_pdfname = $sess . $pdffile;
            //echo $uploaddir ;exit;
            $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->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('insert');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'product');
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('var_alias');
         
         $this->configObj->glDbObj->addColumn('var_price');
         $this->configObj->glDbObj->addColumn('var_pdffile');
        
        $this->configObj->glDbObj->addColumn('var_homepagecaption');
        $this->configObj->glDbObj->addColumn('var_homepagetitle');
        $this->configObj->glDbObj->addColumn('var_homepagedescription'); 
         
         
         $this->configObj->glDbObj->addColumn('var_description');
        $this->configObj->glDbObj->addColumn('var_home_page_description');
        $this->configObj->glDbObj->addColumn('var_metatitle');
        $this->configObj->glDbObj->addColumn('var_metakeyword');
        $this->configObj->glDbObj->addColumn('var_metadescription');
        $this->configObj->glDbObj->addColumn('int_displayorder');
        $this->configObj->glDbObj->addColumn('chr_publish');
        $this->configObj->glDbObj->addColumn('chr_delete');
        $this->configObj->glDbObj->addColumn('dt_createdate');
        $this->configObj->glDbObj->addColumn('dt_modifydate');
        $this->configObj->glDbObj->addColumn('chr_read');
        // 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['var_price']);
        $this->configObj->glDbObj->addValue($pdffile);
        
        $this->configObj->glDbObj->addValue($this->varpostdata['var_homepagecaption']);
              $this->configObj->glDbObj->addValue($this->varpostdata['var_homepagetitle']);
                 $this->configObj->glDbObj->addValue($this->varpostdata['var_homepagedescription']);
        
        if($this->varpostdata['var_desc'] == 'Y') {
            $this->configObj->glDbObj->addValue($this->varpostdata['var_description']);
        } else {
            $this->configObj->glDbObj->addValue('');
        }
        if($this->varpostdata['var_h_desc'] == 'Y') { 
            $this->configObj->glDbObj->addValue($this->varpostdata['var_home_page_description']);
        } else {
            $this->configObj->glDbObj->addValue('');
        }
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_metatitle']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_metakeyword']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_metadescription']));
        $this->configObj->glDbObj->addValue($this->varpostdata['int_displayorder']);
        $this->configObj->glDbObj->addValue($this->varpostdata['chr_publish']);
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->lastinsertid = mysql_insert_id();
        $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'product');
        $this->configObj->glLibraryObj->insertinlogmanager('85', DB_PREFIX . 'product', 'var_title', $this->lastinsertid, 'I');
        return true;
    }

    // UPDATE
    function update_product() {

         if ($this->varpostdata['int_displayorder'] != $this->varpostdata['old_displayorder']) {
            $this->update_display_order($this->varpostdata['eid'], $this->varpostdata['int_displayorder'], $this->varpostdata['old_displayorder']);
        }
        
        $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_pdffile']['name']);
        $pdf = basename($_FILES['var_pdffile']['name']);

        if ($pdf != "") {

            $pdffile = str_replace(' ', '-', $pdf);
            $pdfdata = explode('.', $pdffile);
            $vartitle = $pdfdata[0];
            $pdfdata1 = explode('.', $pdffile);
            $p = count($pdfdata1);
            unset($pdfdata1[$p - 1]);
            $filename = implode('.', $pdfdata1);

            $uploaddir = '../upimages/product/pdf/';
            $sess = time();
            $pdf = basename($_FILES['var_pdffile']['name']);
            $pdffile = str_replace(' ', '-', $pdf);
            $new_pdfname = $sess . $pdffile;
            //echo $uploaddir ;exit;

            $file = $uploaddir . $new_pdfname;
            $tmp = $_FILES['var_pdffile']['tmp_name'];
            if (move_uploaded_file($tmp, $file)) {
                //echo 'successful';exit;
                $pdffile = $new_pdfname;
            } else {
                echo "error " . $_FILES['var_pdffile']['error'] . " --- " . $tmp . " %%% " . $file;
            }
        } else {
            $pdffile = $_REQUEST['hidvar_pdffile'];
            $filename = $_REQUEST['hidvar_filename'];
        }


        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'product');
        // Set the Columns
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('var_alias');
       
        $this->configObj->glDbObj->addColumn('var_price');
        $this->configObj->glDbObj->addColumn('var_pdffile');
           $this->configObj->glDbObj->addColumn('var_homepagecaption');
        $this->configObj->glDbObj->addColumn('var_homepagetitle');
        $this->configObj->glDbObj->addColumn('var_homepagedescription'); 
         
        $this->configObj->glDbObj->addColumn('var_description');
        $this->configObj->glDbObj->addColumn('var_home_page_description');
        $this->configObj->glDbObj->addColumn('var_metatitle');
        $this->configObj->glDbObj->addColumn('var_metakeyword');
        $this->configObj->glDbObj->addColumn('var_metadescription');
        $this->configObj->glDbObj->addColumn('int_displayorder');
        $this->configObj->glDbObj->addColumn('chr_publish');
        $this->configObj->glDbObj->addColumn('chr_read');
        $this->configObj->glDbObj->addColumn('chr_delete');
        $this->configObj->glDbObj->addColumn('dt_modifydate');
        // 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['var_price']);
          $this->configObj->glDbObj->addValue($pdffile);
          
          
               $this->configObj->glDbObj->addValue($this->varpostdata['var_homepagecaption']);
              $this->configObj->glDbObj->addValue($this->varpostdata['var_homepagetitle']);
                 $this->configObj->glDbObj->addValue($this->varpostdata['var_homepagedescription']);
          
        if($this->varpostdata['var_desc'] == 'Y') {
            $this->configObj->glDbObj->addValue($this->varpostdata['var_description']);
        }
        else {
            $this->configObj->glDbObj->addValue('');
        }
        if($this->varpostdata['var_h_desc'] == 'Y') {  
            $this->configObj->glDbObj->addValue($this->varpostdata['var_home_page_description']);
        } else {
            $this->configObj->glDbObj->addValue('');
        }
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_metatitle']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_metakeyword']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_metadescription']));
        $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('NOW()');

        $this->configObj->glDbObj->setWhere('int_glcode=' . $this->varpostdata['eid']);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'product');
        $this->configObj->glLibraryObj->insertinlogmanager('85', DB_PREFIX . 'product', 'var_title', $this->varpostdata['eid'], 'U');
        return true;
    }

    // Generate URL


    function update_display_order($ids, $values, $oldvalues) {
        $tablename = "product";

        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 . "product");

                $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 . "product");

                        $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 . "product");

                        $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 . "product");

                $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 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 . '&categorytype=' . $this->categorytype;
        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby . '&categorytype=' . $_REQUEST['categorytype'];
        $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 . '&categorytype=' . $this->categorytype;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby . '&categorytype=' . $this->categorytype;
        $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' . $homepara;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&categorytype=' . $this->categorytype;
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal;
    }

    function org_generateParam($view = 'product', $actionurl = 'index.php?module=product&action=add', $searchArray = array("var_title" => "Title"), $table = 'DB_PREFIX.product', $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->searchtxtjs,
                '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 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 is_productused() {
        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);
        $is_assigned = 0;
        $delcount = 0;
        for ($i = 0; $i < $totaldeletedrecords; $i++) {
            $sql = "Update " . DB_PREFIX . "product 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('85', DB_PREFIX . 'product', 'var_title', $deletearray[$i], 'D');
        }
        $this->load_product();
    }

    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 count_photos($id) {
         $sql_count = "select count(0) as total from ecl_productphotogallery where fk_product=" . $id . " and chr_delete='N' and chr_publish='Y'";
        $rs_total = $this->configObj->glDbObj->get_sql_row($sql_count);
        $total = $rs_total['total'];
        return $total;
    }

    function load_product() {
        if ($_REQUEST['task'] == 'orderupdate') {
            $updateids = $_REQUEST['uids'];
            $updatevalues = $_REQUEST['uvals'];
            $updateoldvalues = $_REQUEST['uoldvals'];
            $upidsarray = explode(',', $updateids);
            $upvalsarray = explode(',', $updatevalues);
            $upoldvalsarray = explode(',', $updateoldvalues);
            $this->update_display_order($upidsarray, $upvalsarray, $upoldvalsarray,'product');
            $tablename = "" . DB_PREFIX . 'product';
            $this->configObj->glLibraryObj->set_display_order_sequence($tablename);
        }
        $this->pagename = 'index.php?module=product'; // Attribute of Page Name
        $this->countFunction = $this->CountRows();

        $this->loadCount();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $result = $this->selectall_product();
        $this->configObj->glTplObj->set('result', $result);

        $this->Generateurl();

        $view = 'Manage Products';

        if ($_REQUEST['productcategory'] != '') {
            $cat = "&productcategory=" . $_REQUEST['productcategory'];
        }
        $actionurl = 'index.php?module=product&action=add' . $cat;
        $searchArray = array("p.var_title" => "Product Title");

        $table = DB_PREFIX . 'product';
        $parmas = $this->org_generateParam($view, $actionurl, $searchArray, $table);

        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas, 'yes'));
        $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')));
        $tmpsetsortvar = 'setsortimg' . $this->orderby;
        $$finalsortvar = $tmpsetsortvar;
        $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);
            }
        }
        $notemessagebox = $this->configObj->glLibraryObj->notemessage("Note : ", "Adding products are incremental, once you add a new product it will display as first product and previous products will move down in the order.");
        $this->configObj->glTplObj->set('notemessagebox', $notemessagebox);
        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('product.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('product.tpl.php');
        }
    }
}
?>