<?php

class package {

    var $pagename = 'index.php?module=package';
    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 package($configObj) {
        $this->sortvar = '<img alt="sorting" src="' . GLOBAL_ADMIN_IMAGES_PATH . 'arrow-up.png" style="vertical-align:middle;">';
        $this->configObj = $configObj;
    }

    function intitialize() {

        $this->searchby = (isset($_REQUEST['searchby'])) ? $_REQUEST['searchby'] : $this->searchby;
        $this->searchtxt = (isset($_REQUEST['searchtxt'])) ? $_REQUEST['searchtxt'] : '';
        $this->searchtxtjs = (isset($_REQUEST['searchtxt'])) ? $_REQUEST['searchtxt'] : '';
        $_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->optval = (isset($_REQUEST['optval'])) ? $_REQUEST['optval'] : $this->optval;
        $this->filterby = (isset($_REQUEST['filterby'])) ? $_REQUEST['filterby'] : $this->filterby;
        $this->packagetype = (isset($_REQUEST['fk_packagetype'])) ? $_REQUEST['fk_packagetype'] : $thid->packagetype;



        if (isset($_REQUEST['sorting'])) {
            if ($this->ordertype == "asc") {
                $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->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 select_package($id) {
        $string = DEFAULT_DATEFORMAT;
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('select');
        $tablename = DB_PREFIX . "package";
        $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]'";
        }
        if ($_REQUEST['optval'] != '' && $_REQUEST['optval'] != 0) {
            $whereclause .= " and fk_packagetype=" . $_REQUEST['optval'] . "";
        }

        if ($_REQUEST['fk_packagetype'] != '') {
            $whereclause.=" and fk_packagetype=" . $this->packagetype;
        }

        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "package where 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 . "packagetype where int_glcode=" . $id;
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        return $result['var_title'];
    }

    /*
     * 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 getpackagecustomdata() {
        $sql = "SELECT var_title,var_description FROM " . DB_PREFIX . "package 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('packagedata', $this->getpackagecustomdata());
        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=84  order by dt_createdate desc limit 0,1";
        $rec = $this->configObj->glDbObj->query($sql);
        return $rec;
    }

    function getpackage_type_alias($pid) {
        $sql = "SELECT var_alias,var_logo FROM " . DB_PREFIX . "packagetype WHERE chr_delete='N' AND int_glcode='" . $pid . "'";
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        return $result;
    }

    function get_Post_data($id) {
        $sql = "SELECT * FROM " . DB_PREFIX . "package WHERE chr_delete='N' AND int_glcode='" . $id . "'";

        $result = $this->configObj->glDbObj->get_sql_row($sql);

        $plink = $this->getpackage_type_alias($result["fk_packagetype"]);

        $imagename = $result['var_image'];
        if ($imagename != '') {
            $corePath = "upimages/package/images/main/";
            $imagepath = '../' . $corePath;
            $imagename = "thumb_package_" . $imagename;
            $image = $imagepath . $imagename;
        } else {
            $corePath1 = "upimages/package/logo/main/";
            $imagepath = '../' . $corePath1;
            $imagename = "thumb_" . $plink["var_logo"];
            $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;
        }
        $paramArr['int_glcode'] = $id;
        if (SITE_SEO == 'Y') {
            $packagelink = SITE_PATH . $plink["var_alias"];
        } else {
            $packagelink = SITE_PATH . "packages" . $result["fk_packagetype"];
        }


        $paramArr['data']['name'] = $this->varpostdata['var_title'];
        $paramArr["data"]["actions"] = array("name" => "Sent from Netclues PowerPanel", "link" => "http://www.netclues.ky");
        $paramArr['data']['link'] = $packagelink;

//        $paramArr['data']['description'] = $this->varpostdata['var_short_description'];
        if ($this->varpostdata['var_short_description'] != '') {
            $paramArr['data']['description'] = $this->varpostdata['var_short_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["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];
                    $package_type_id = $this->get_package_type($trid);

                    $getimage = $this->getimage($package_type_id);

                    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 get_package_type($pid) {
        $sql = "select fk_packagetype from " . DB_PREFIX . "package where chr_delete='N' and chr_publish='Y' and int_glcode=$pid";
        $rec = $this->configObj->glDbObj->get_sql_row($sql);
        return $rec["fk_packagetype"];
    }

    function getimage($pid) {
        $sql = "select var_logo from " . DB_PREFIX . "packagetype where chr_delete='N' and chr_publish='Y' and int_glcode=$pid";
        $rec = $this->configObj->glDbObj->get_sql_row($sql);
        return $rec["var_logo"];
    }

    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" => "84"
        );

        $this->configObj->glLibraryObj->insertIntoDB(DB_PREFIX . "sharelog", $param);
        $this->insertfacebooklogdata();
    }

    function insertfacebooklogdata() {
//        $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']."')";
        $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_short_description']) . "','" . $_SERVER['REMOTE_ADDR'] . "','" . ADMIN_NAME . "')";

        $this->configObj->glDbObj->query($sql);
    }

    /*
     * Facebook and Twitter End
     */

    function selectall_package() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $tablename = DB_PREFIX . "package";
        $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 ($_REQUEST['optval'] != '' && $_REQUEST['optval'] != 0) {
            $whereclause .= " and fk_packagetype=" . $_REQUEST['optval'] . "";
        }
        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 ($_REQUEST['fk_packagetype'] != '') {
            $whereclause.=" and fk_packagetype=" . $this->packagetype;
        }
        $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->searchbyVal} like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' " : "$whereclause";
            if ($this->searchbyVal == '0') {
                $this->searchbyVal = "var_title";
            }
            if ($_REQUEST['optval'] != '' && $_REQUEST['optval'] != 0) {
                $whereclause1 = " and fk_packagetype=" . $_REQUEST['optval'] . "";
            }
            if ($this->searchbyVal == 'var_title') {
                $this->searchbyVal = "var_title";
            }
            if ($_REQUEST['fk_packagetype'] != '') {
                $whereclause1.=" and fk_packagetype=" . $this->packagetype;
            }
            if ($this->filterby != '0' && $this->filterby != '') {
                $filterarray = explode('-', $this->filterby);
                $whereclauseauto = " and $filterarray[0] = '$filterarray[1]'";
            }
            $searchbyVal1 = "{$this->searchbyVal} like '%" . $this->searchtxt . "%' and chr_delete = 'N'";
            $autoSearchQry = "select int_glcode ," . $this->searchbyVal . " as AutoVal FROM " . $tablename . " where  $searchbyVal $whereclause1 $whereclauseauto group by " . $this->searchbyVal . " $orderby";
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

    function Bindcategorieshierarchy($ids = '') {

        $sqlSelect = "select int_glcode,var_title from " . DB_PREFIX . "packagetype where chr_publish='Y' and chr_delete='N' order by int_displayorder asc";
        $result = $this->configObj->glDbObj->query($sqlSelect);
        $output_combo = "<select id='var_packagetype' name='var_packagetype' style='width:350px;' onchange=\"SendGridBindRequest2('" . $this->urlwithpara . "','Changebrand','producttype');\">";
        $output_combo.="<option value='0'> Select Package Type </option>";
        while ($row = $this->configObj->glDbObj->fetch_array($result)) {
            if ($_REQUEST['fk_packagetype'] != "") {
                $selected = ($_REQUEST['fk_packagetype'] == $row['int_glcode']) ? 'selected="selected" ' : '';
            } else {
                $selected = (($ids == $row['int_glcode']) ? 'selected' : '');
            }
            $output_combo.="<option value=" . $row['int_glcode'] . "  $selected>" . $this->configObj->glLibraryObj->add_special_chars($row['var_title']) . "</option>";
        }
        $output_combo.="</select>";
        return $output_combo;
    }

    function insert_package() {
        $this->configObj->glLibraryObj->updatedisplay_order('package', $this->varpostdata['int_displayorder']);
        if ($this->varpostdata['h_save'] == 'T') {
            $this->thumb_width = FEATURED_CATEGORY_BANNER_WIDTH;
            $this->thumb_height = FEATURED_CATEGORY_BANNER_HEIGHT;

            $this->thumb_width1 = FEATURED_SEARCH_CATEGORY_BANNER_WIDTH;
            $this->thumb_height1 = FEATURED_SEARCH_CATEGORY_BANNER_HEIGHT;
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image']['name']);

            if ($_FILES["var_image"]["name"] != "") {
                $maindir = '../upimages/package/images/main/';
                $thumbdir = '../upimages/package/images/main/';

                $var_main_file = $this->configObj->glLibraryObj->generate_image('var_image', $maindir); // Main Upload and Resize
                $this->imagename = $var_main_file;
                if (isset($var_main_file)) {
                    if (empty($this->varpostdata['var_var_title'])) {
                        $temp = explode(".", $_FILES["var_image"]["name"]);
                        $this->varpostdata['var_var_title'] = $temp[0];
                    }

                    $file_photo = basename($var_main_file);
                    $file = $maindir . $var_main_file;
                    $tmp = $_FILES['var_image']['tmp_name'];
                    $uploadedfile = $maindir . $file_photo;
                    $var_thumb = $this->configObj->glLibraryObj->create_image($this->thumb_width, $this->thumb_height, 'thumb_package_', $this->thumb_width, $this->thumb_height, $uploadedfile, '', $thumbdir);
                    $var_thumb = $this->configObj->glLibraryObj->create_image($this->thumb_width1, $this->thumb_height1, 'thumb_src_image_', $this->thumb_width1, $this->thumb_height1, $uploadedfile, '', $thumbdir);
                }
            } else {
                $this->imagename = $_REQUEST['hidvar_image'];
            }

            $this->int_glcode = ""; // clear key for autoincrement
            $this->configObj->glDbObj->resetfields();
            $tablename = DB_PREFIX . 'package';
            $this->configObj->glDbObj->setTable($tablename);
            $this->configObj->glDbObj->setQueryType('insert');
            // Set the Columns
            $this->configObj->glDbObj->addColumn('fk_packagetype');
            $this->configObj->glDbObj->addColumn('var_title');
            $this->configObj->glDbObj->addColumn('var_alias');
            $this->configObj->glDbObj->addColumn('var_description');
            $this->configObj->glDbObj->addColumn('var_image');
            $this->configObj->glDbObj->addColumn('var_price');
            $this->configObj->glDbObj->addColumn('var_short_description');
//            $this->configObj->glDbObj->addColumn('dt_startdate');
//            $this->configObj->glDbObj->addColumn('dt_expiredate_no');
//            $this->configObj->glDbObj->addColumn('dt_expiredate');
//            $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('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->addValue($this->varpostdata['var_packagetype']);
            $this->configObj->glDbObj->addValue($this->varpostdata['var_title']);
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_alias']));

            $this->configObj->glDbObj->addValue($this->varpostdata['var_description']);
            $this->configObj->glDbObj->addValue($var_main_file);
            $this->configObj->glDbObj->addValue($this->varpostdata['var_price']);
            $this->configObj->glDbObj->addValue($this->varpostdata['var_short_description']);
//            $this->configObj->glDbObj->addValue($this->configObj->glLibraryObj->date_convert($this->varpostdata['dt_startdate']));
//        $p = ($this->varpostdata['dt_expiredate_no'] == "Y") ? 'Y' : 'N';
//        $this->configObj->glDbObj->addValue($p);
//        $this->configObj->glDbObj->addValue($this->configObj->glLibraryObj->date_convert($this->varpostdata['dt_expirydate']));
//            $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('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->query($this->configObj->glDbObj->buildQuery());
            $this->lastinsertid = mysql_insert_id();
            // echo  $this->configObj->glDbObj->buildQuery();
            $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'package');
            $this->configObj->glLibraryObj->insertinlogmanager(84, DB_PREFIX . 'package', 'var_title', $this->lastinsertid, 'I');
            return true;
        }
    }

    // **********************
    // UPDATE
    // **********************

    function update_package() {

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

        $this->thumb_width = FEATURED_CATEGORY_BANNER_WIDTH;
        $this->thumb_height = FEATURED_CATEGORY_BANNER_HEIGHT;

        $this->thumb_width1 = FEATURED_SEARCH_CATEGORY_BANNER_WIDTH;
        $this->thumb_height1 = FEATURED_SEARCH_CATEGORY_BANNER_HEIGHT;
        $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image']['name']);

        if ($_FILES["var_image"]["name"] != "") {
            $maindir = '../upimages/package/images/main/';
            $thumbdir = '../upimages/package/images/main/';

            $var_main_file = $this->configObj->glLibraryObj->generate_image('var_image', $maindir); // Main Upload and Resize
            $this->imagename = $var_main_file;
            if (isset($var_main_file)) {
                if (empty($this->varpostdata['var_var_title'])) {
                    $temp = explode(".", $_FILES["var_image"]["name"]);
                    $this->varpostdata['var_var_title'] = $temp[0];
                }

                $file_photo = basename($var_main_file);
                $file = $maindir . $var_main_file;
                $tmp = $_FILES['var_image']['tmp_name'];
                $uploadedfile = $maindir . $file_photo;
                $var_thumb = $this->configObj->glLibraryObj->create_image($this->thumb_width, $this->thumb_height, 'thumb_package_', $this->thumb_width, $this->thumb_height, $uploadedfile, '', $thumbdir);
                $var_thumb = $this->configObj->glLibraryObj->create_image($this->thumb_width1, $this->thumb_height1, 'thumb_src_image_', $this->thumb_width1, $this->thumb_height1, $uploadedfile, '', $thumbdir);
            }
        } else {
            $this->imagename = $_REQUEST['hidvar_image'];
        }


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

        // Set the Columns
        $this->configObj->glDbObj->addColumn('fk_packagetype');
        $this->configObj->glDbObj->addColumn('var_title');
        $this->configObj->glDbObj->addColumn('var_alias');
        $this->configObj->glDbObj->addColumn('var_description');
        $this->configObj->glDbObj->addColumn('var_image');
        $this->configObj->glDbObj->addColumn('var_price');
        $this->configObj->glDbObj->addColumn('var_short_description');
//            $this->configObj->glDbObj->addColumn('dt_startdate');
//        $this->configObj->glDbObj->addColumn('dt_expiredate_no');
//        $this->configObj->glDbObj->addColumn('dt_expiredate');
//            $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('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->addValue($this->varpostdata['var_packagetype']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_title']);
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_alias']));
        $this->configObj->glDbObj->addValue($this->varpostdata['var_description']);
        $this->configObj->glDbObj->addValue($this->imagename);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_price']);
        $this->configObj->glDbObj->addValue($this->varpostdata['var_short_description']);
//             $this->configObj->glDbObj->addValue($this->configObj->glLibraryObj->date_convert($this->varpostdata['dt_startdate']));
//        $p = ($this->varpostdata['dt_expiredate_no'] == "Y") ? 'Y' : 'N';
//        $this->configObj->glDbObj->addValue($p);
//        if ($p == 'Y') {
//            $this->configObj->glDbObj->addValue($this->configObj->glLibraryObj->date_convert($this->varpostdata['0000-00-00']));
//        } else {
//            $this->configObj->glDbObj->addValue($this->configObj->glLibraryObj->date_convert($this->varpostdata['dt_expirydate']));
//        }
//            $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('NOW()');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue('Y');
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue($this->varpostdata['chr_publish']);
        $this->configObj->glDbObj->setWhere('int_glcode=' . $this->varpostdata['eid']);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
//        echo $this->configObj->glDbObj->buildQuery(); exit;
        $this->lastinsertid = $this->varpostdata['eid'];
        $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'package');
        $this->configObj->glLibraryObj->insertinlogmanager(84, DB_PREFIX . 'package', 'var_title', $this->lastinsertid, 'U');
        return true;
    }

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

    function Generateurl() {
        $this->urlwithpara = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&filterby=' . $this->filterby . '&optval=' . $this->optval . '&fk_packagetype=' . $this->packagetype;
        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby . '&optval=' . $this->optval . '&fk_packagetype=' . $this->packagetype;
        $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 . '&optval=' . $this->optval . '&fk_packagetype=' . $this->packagetype;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby . '&optval=' . $this->optval . '&fk_packagetype=' . $this->packagetype;
        $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 . '&optval=' . $this->optval . '&history=T' . $homepara . '&fk_packagetype=' . $this->packagetype;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&optval=' . $this->optval . '&fk_packagetype=' . $this->packagetype;
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal . '&filterby=' . $this->filterby . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&optval=' . $this->optval;
    }

    function org_generateParam($view = 'package', $actionurl = 'index.php?module=package&action=add', $searchArray = array("name" => "package"), $table = 'iwn_package', $position = 'top') {
        $filterPos = ($position == 'top') ? 'FT' : 'FB';

        if ($position == 'top' && (!isset($_REQUEST['fk_packagetype']) || $_REQUEST['ajax'] == 'Y')) {
            $sqlSelect = "select * from " . DB_PREFIX . "packagetype where chr_delete='N'  and chr_publish='Y' ";
            $result = $this->configObj->glDbObj->query($sqlSelect);
            $cmbpackage = "<div class=\"new-search-show-all\"><select id=\"var_packagetype\"  tabindex=\"24\" style=\"width:200px;\" class=\"more-textarea\" name=\"var_packagetype\"
                               onchange=\"SendGridBindRequest('" . $this->urlwithpara . "','gridbody','packagetype');\">";
            $cmbpackage.="<option value='0'>Select Package Type</option>";
            while ($row = $this->configObj->glDbObj->fetch_array($result)) {
                if ($row['int_glcode'] == $_REQUEST['optval']) {
                    $selected = 'selected';
                } else {
                    $selected = '';
                }
                $cmbpackage.="<option $selected value=" . $row['int_glcode'] . ">" . $row['var_title'] . "</option>";
            }
            $cmbpackage.='</select></div>';
        }
        if ($_REQUEST['fk_packagetype'] != '') {
            $packageurldata = 'index.php?module=packagetype';
        } else {
            $packageurldata = '';
        }
        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',
            'backbuttonurldata' => $packageurldata,
            '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,
            ),
            'cmbpackage' => $cmbpackage
        );
    }

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

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

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

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

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

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

    function get_fk_pages_modules_positions($eid) {
        $sql = "SELECT fk_pages_modules_positions from " . DB_PREFIX . "package_pages WHERE int_glcode = $eid";
        $rs = $this->configObj->glDbObj->get_sql_row($sql);
        $fk_pages_modules_positions = $rs['fk_pages_modules_positions'];
        return $fk_pages_modules_positions;
    }

    function delete_package() {
        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);
        $is_assigned = 0;
        $delcount = 0;
        $tablename = DB_PREFIX . 'package';
        for ($i = 0; $i < $totaldeletedrecords; $i++) {
            $sql = "Update " . DB_PREFIX . "package 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(84, DB_PREFIX . 'package', 'var_title', $deletearray[$i], 'D');
        }
        $this->load_package();
        exit;
    }

    function getpackage($id) {
        $sql = "select * from ecl_packagetype where chr_delete='N' and int_glcode='" . $id . "'";
        $rec = $this->configObj->glDbObj->get_sql_row($sql);
        if ($rec['var_title'] != '') {
            return ' - ' . $rec['var_title'];
        }
    }

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

        $this->loadCount('CountRows');

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

        $this->Generateurl();
        if (isset($_REQUEST['fk_packagetype'])) {
            $temp = $_REQUEST['fk_packagetype'];
            $temp1 = $this->getpackage($temp);
        }
        $view = 'Manage Packages' . $temp1;
        $actionurl = 'index.php?module=package&action=add';
        $searchArray = array("var_title" => "Title");
        $table = DB_PREFIX . "package";
        $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 and saved.");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }

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

        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('package.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('package.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();
    }

}

?>