<?php

class inquiry {

    var $int_glcode;
    var $chr_read;
    var $chr_star;
    var $chr_publish;
    var $intdisplayorder;
    var $dt_createdate;
    var $dt_modifydate;
    var $oldint_displayorder;
    var $numofrows;
    var $numofpages;
    var $orderby = 'int_glcode';
    var $ordertype = 'desc';
    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 inquiry($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;\">";
            }
        }
        $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 CountRows() {
        $this->intitialize();
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and (var_name like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' or var_email like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%')" : " and $this->searchby like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
        }
        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }

        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "contactdetails where chr_delete = 'N' $whereclause ";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_contact() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'contactdetails');
        $this->configObj->glDbObj->addColumn('*,DATE_FORMAT(dt_createddate,"' . DEFAULT_DATEFORMAT . '") as Date,time(dt_createddate) as Time');
        $whereclause = "chr_delete = 'N'";
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and (var_name like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' or var_email like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%')" : " and $this->searchby  like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
        }
        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);

        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_name";
            }
            if ($this->searchbyVal == 'var_name') {
                $this->searchbyVal = "var_name";
            }
            if ($this->filterby != '0' && $this->filterby != '') {
                $filterarray = explode('-', $this->filterby);
                $whereclausenew .=" and $filterarray[0] = '$filterarray[1]'";
            }
            $autoSearchQry = "select int_glcode , {$this->searchbyVal} as AutoVal FROM " . DB_PREFIX . "contactdetails where $searchbyVal $whereclausenew group by {$this->searchbyVal} $orderby";
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }


    function Generateurl() {

        $this->urlwithpara = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&filterby=' . $this->filterby;
        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby;
        $this->urlwithoutsort = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs;
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal;
    }

    function org_generateParam($view = '', $actionurl = 'index.php?module=contactusms&gaction=add', $searchArray = array("var_name" => "Name"), $table = 'DB_PREFIX.contactdetails', $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 is_contactused($fk_modules) {

        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);
        for ($i = 0; $i < $totaldeletedrecords; $i++) {

            $sql = "Update " . DB_PREFIX . "contactdetails set chr_delete='Y', dt_modifydate=NOW(),var_ipaddress='" . $_SERVER['REMOTE_ADDR'] . "',var_adminuser='" . ADMIN_NAME . "'  where int_glcode='$deletearray[$i]'";
            $this->configObj->glDbObj->query($sql);
            $this->configObj->glLibraryObj->insertinlogmanager('78', DB_PREFIX . 'contactdetails', 'var_name', $deletearray[$i], 'D');
        }
        $this->load_contactinquiry();
        exit;
    }

    function formemail($id) {

        $imgurl = SITE_PATH.GLOBAL_THEME_PATH.'images/.jpg';
        $this->configObj->setEmailConfig();

        $subject = "Administrator reply of contact us inquiry";
        

        $usersql = "select *,DATE_FORMAT(dt_createddate,'%D %M') as Date from " . DB_PREFIX . "contactdetails where int_glcode = " . $id . "";
        $usrarry = $this->configObj->glDbObj->get_sql_row($usersql);
        $Feedback = "<b>With reference to your email of ".$usrarry['Date'].", Administrator has replied to your inquiry.</b>";
        $this->configObj->glDbObj->query("update  " . DB_PREFIX . "contactdetails set chr_read='Y' where int_glcode='" . $id . "'");
        $msg=$this->configObj->glLibraryObj->makeSafe($this->varpostdata['var_message']);
        $this->configObj->glLibraryObj->settings();
        
       $usrarryphone=($usrarry['var_phone']!='') ? $usrarry['var_phone']:'N/A';
        
        $body = '<tr><td width="10"></td><td align="left" ><strong>Name : </strong>' . $usrarry['var_name'] . ' </td></tr>
		<tr><td width="10"></td><td align="left" ><strong>Email : </strong>' . $usrarry['var_email'] . ' </td></tr>
                <tr><td width="10"></td><td align="left" ><strong>Phone : </strong>' .$usrarryphone . ' </td></tr>

                <tr><td width="10"></td><td align="left" ><strong>Your Message : </strong>' . $usrarry['var_message']  . ' </td></tr>
                <tr><td width="10"></td><td align="left" ><strong>Administrator Reply : </strong>' . $msg . ' </td></tr>';
        

        $html_message = file_get_contents(GLOBAL_ADMIN_HTML_PATH . '/mailtemplates/contactus_admin.html');
        $html_message = str_replace("@Feedback", $Feedback, $html_message);
        $html_message = str_replace("@Admin", $usrarry['var_name'] , $html_message);
        $html_message = str_replace("@DETAIL_DATA", $body, $html_message);
        $html_message = str_replace("@SITE_URL_FOR_MAIL_LINKS", SITE_PATH, $html_message);
        $html_message = str_replace("@SITE_NAME_IN_WORDS", SITE_NAME, $html_message);
        $html_message = str_replace("@MAIL_TEXT_AFTER_BEST_REGARDS", SITE_NAME, $html_message);
        $html_message = str_replace("@LOGO_PATH", GLOBAL_EMAIL_LOGO, $html_message);
        $html_message = str_replace("@YEAR", date('Y'), $html_message);
        $html_message = str_replace("@RegardMessage", REGARD_MESSAGE, $html_message);
        $html_message = str_replace("@MAILBOTTOM_LINK", MAILBOTTOM_TEXT, $html_message);

//        echo $html_message; exit;

        $addarr = $usrarry['var_email'];

        $this->configObj->glMailObj->AddAddress($addarr);
        $this->configObj->glMailObj->Subject = $subject;
        $this->configObj->glMailObj->Body = $html_message;
        if (!$this->configObj->glLibraryObj->sendmail()) {

            $message = "Error in mail send";
        } else {
            $message = mysql_real_escape_string($html_message);
            $fk_emailtype = '3';

            $insertSql = "insert into " . DB_PREFIX . "emails (fk_emailtype,var_from,txt_to,txt_subject,txt_body,dt_createdate,dt_modifydate) Values ('" . $fk_emailtype . "','" . MAIL_FROM . "','" . $usrarry['var_email'] . "','" . $subject . "','" . $message . "',now(),now()) ";
            $exec = $this->configObj->glDbObj->query($insertSql);
            $message = "Your Mail sent Successfully. We will get you back soon..";
        }
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'contactdetails');
        $this->configObj->glDbObj->setQueryType('update');
        // Set the Columns

        $this->configObj->glDbObj->addColumn('chr_replysent');
        $this->configObj->glDbObj->addValue('Y');
        $this->configObj->glDbObj->setWhere('int_glcode=' . $id);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'contactusmsg_reply');
        $this->configObj->glDbObj->setQueryType('insert');

        $this->configObj->glDbObj->addColumn('fk_contactdetail');
        $this->configObj->glDbObj->addColumn('var_adminreply');
        $this->configObj->glDbObj->addColumn('dtcreateddate');
        $this->configObj->glDbObj->addColumn('var_modulename');
        $this->configObj->glDbObj->addValue($id);
        $this->configObj->glDbObj->addValue($msg);
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('contactdetails');
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());

        return $message;
    }

  

    function load_contactinquiry() {
        $this->pagename = 'index.php?module=contactusmsg'; // Attribute of Page Name
        $this->loadCount('CountRows');
        $this->CountRows();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $result = $this->selectall_contact();
        $this->configObj->glTplObj->set('result', $result);
        $this->Generateurl();
        $view = 'Manage Contact Us Leads';
        $actionurl = 'index.php?module=contactusmsg&action=add';
        $searchArray = array("var_name" => "Name", "var_email" => "Email Id"); //,"var_email"=>"Email Id"
        $table = DB_PREFIX . 'contactdetails';
        $parmas = $this->org_generateParam($view, $actionurl, $searchArray, $table);

        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas, 'no'));
        $this->configObj->glTplObj->set('pagingTop', $this->configObj->glLibraryObj->generatePagingPannel($parmas));
        $this->configObj->glTplObj->set('pagingBottom', $this->configObj->glLibraryObj->generatePagingPannel($this->org_generateParam($view, $actionurl, $searchArray, $table, 'bottom')));

        $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 is successfully saved");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $this->configObj->glLibraryObj->message("The record is successfully saved");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }
        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('inquiry.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('inquiry.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();
    }

    function gettabledataData($tablename) {
        $selectids = $_REQUEST['chkids'];
        $selectarray = explode(',', $selectids);
        $totaldeletedrecords = count($selectarray);
        $exportarry = array();
        for ($i = 0; $i < $totaldeletedrecords; $i++) {
            $data = "select var_name from " . DB_PREFIX . $tablename . " where int_glcode = '" . $selectarray[$i] . "'";
            $result = $this->configObj->glDbObj->query($data);
            if ($tablename == 'contactdetails') {
                if ($_REQUEST['chkids'] != '') {
                    $sql = "select opi.var_name as Name,opi.var_email as Email,opi.var_message as Message,opi.var_phone  as Phone,DATE_FORMAT(opi.dt_createddate,'" . DEFAULT_DATEFORMAT . "') as Date,time(opi.dt_createddate) as Time from " . DB_PREFIX . $tablename . " as opi where opi.chr_delete = 'N' and int_glcode IN($selectids)";
                } else {
                    $sql = "select opi.var_name as Name,opi.var_email as Email,opi.var_message as Message,opi.var_phone as Phone,DATE_FORMAT(opi.dt_createddate,'" . DEFAULT_DATEFORMAT . "') as Date,time(opi.dt_createddate) as Time from " . DB_PREFIX . $tablename . " as opi where opi.chr_delete = 'N' ";
                }
            }

            if ($tablename == 'newslatterleads') {
                if ($_REQUEST['chkids'] != '') {
                    $sql = "select opi.var_email as Email,DATE_FORMAT(opi.dt_createdate,'" . DEFAULT_DATEFORMAT . "') as Date,time(opi.dt_createdate) as Time from " . DB_PREFIX . $tablename . " as opi where opi.chr_delete = 'N' and int_glcode IN($selectids)";
                } else {
                    $sql = "select opi.var_email as Email,DATE_FORMAT(opi.dt_createdate,'" . DEFAULT_DATEFORMAT . "') as Date,time(opi.dt_createdate) as Time from " . DB_PREFIX . $tablename . " as opi where opi.chr_delete = 'N'  ";
                }
            }
        }
        return $sql;
    }


    function load_newslatterleads() {
        $this->pagename = 'index.php?module=contactusmsg&view=newslatter'; // Attribute of Page Name
        $this->loadCount('CountRowsnews');
        $this->CountRowsnews();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $result = $this->selectall_newslatterleads();
        $this->configObj->glTplObj->set('result', $result);
        $this->Generateurl();
        $view = 'Manage Newsletter Leads';
        $actionurl = 'index.php?module=contactusmsg&view=newslatter';
        $searchArray = array("var_email" => "Email"); //,"var_email"=>"Email Id"
        $table = DB_PREFIX . 'newslatterleads';
        $parmas = $this->generateParam($view, $actionurl, $searchArray, $table);
        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas, 'no'));
        $this->configObj->glTplObj->set('pagingTop', $this->configObj->glLibraryObj->generatePagingPannel($parmas));
        $this->configObj->glTplObj->set('pagingBottom', $this->configObj->glLibraryObj->generatePagingPannel($this->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'] == 'edit') {
                $messagebox = $this->configObj->glLibraryObj->message("The record is successfully saved");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }
        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('newslatterleads.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('newslatterleads.tpl.php');
        }
    }

    function CountRowsnews() {
        $this->intitialize();
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and (var_email like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%')" : " and $this->searchby like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
        }

        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "newslatterleads where chr_delete = 'N' $whereclause";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_newslatterleads() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'newslatterleads');
        $this->configObj->glDbObj->addColumn('*,DATE_FORMAT(dt_createdate,"' . DEFAULT_DATEFORMAT . '") as Date,time(dt_createdate) as Time');
        $whereclause = "chr_delete = 'N'";

        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and (var_email like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%')" : " and $this->searchby  like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
        }

        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);

        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->searchtxt . "%' " : "$whereclause";

            $autoSearchQry = "select int_glcode ,var_email as AutoVal FROM " . DB_PREFIX . "newslatterleads where $searchbyVal group by var_email $orderby";
            // echo $autoSearchQry;
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

    function is_newslatterused($fk_modules) {

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

        for ($i = 0; $i < $totaldeletedrecords; $i++) {

            $sql = "Update " . DB_PREFIX . "newslatterleads set chr_delete='Y', dt_modifydate=NOW(),var_ipaddress='" . $_SERVER['REMOTE_ADDR'] . "',var_adminuser='" . ADMIN_NAME . "'  where int_glcode='$deletearray[$i]'";
            $this->configObj->glDbObj->query($sql);
            $this->configObj->glLibraryObj->insertinlogmanager(46, DB_PREFIX . 'newslatterleads', 'var_email', $deletearray[$i], 'D');
        }
        $this->load_newslatterleads();
        exit;
    }

    function formemailnews($id) {
        $this->configObj->setEmailConfig();
        $subject = "Newsletter Admin Reply";
        
        $usersql = "select *,DATE_FORMAT(dt_createdate,'%D %M') as Date from " . DB_PREFIX . "newslatterleads where int_glcode = " . $id . "";
        $usrarry = $this->configObj->glDbObj->get_sql_row($usersql);
        $Feedback = "<b>With reference to your email of ".$usrarry['Date'].", Administrator has replied to your newsletter inquiry.</b>";
        $updatesql = "update " . DB_PREFIX . "newslatterleads set chr_read='Y' where int_glcode = " . $id . "";
        $uprarry1 = $this->configObj->glDbObj->get_sql_row($updatesql);
        $msg = $this->configObj->glLibraryObj->makeSafe($this->varpostdata['var_message']);
        $body = '<tr><td width="10"></td><td align="left" ><strong>Email : </strong>' . $usrarry['var_email'] . ' </td></tr>
                                <tr><td width="10"></td><td align="left" ><strong>Administrator\'s Message : </strong>' . $msg . ' </td></tr>';
        $this->configObj->glLibraryObj->settings();
        $html_message = file_get_contents(GLOBAL_ADMIN_HTML_PATH . '/mailtemplates/contactus_admin.html');

        $html_message = str_replace("@Feedback", $Feedback, $html_message);
        $html_message = str_replace("@Admin", 'Subscriber', $html_message);
        $html_message = str_replace("@DETAIL_DATA", $body, $html_message);
        $html_message = str_replace("@SITE_URL_FOR_MAIL_LINKS", SITE_PATH, $html_message);
        $html_message = str_replace("@SITE_NAME_IN_WORDS", SITE_NAME, $html_message);
        $html_message = str_replace("@MAIL_TEXT_AFTER_BEST_REGARDS", SITE_NAME, $html_message);
        $html_message = str_replace("@LOGO_PATH", GLOBAL_EMAIL_LOGO, $html_message);
        $html_message = str_replace("@RegardMessage", REGARD_MESSAGE, $html_message);
        $html_message = str_replace("@YEAR", date('Y'), $html_message);
        $html_message = str_replace("@MAILBOTTOM_LINK", MAILBOTTOM_TEXT, $html_message);

        $addarr = $usrarry['var_email'];
        $this->configObj->glMailObj->AddAddress($addarr);
        $this->configObj->glMailObj->Subject = $subject;
        $this->configObj->glMailObj->Body = $html_message;
        if (!$this->configObj->glLibraryObj->sendmail()) {

            $message = "Error in mail send";
        } else {
            $message = mysql_real_escape_string($html_message);
            $fk_emailtype = '3';

            $insertSql = "insert into " . DB_PREFIX . "emails (fk_emailtype,var_from,txt_to,txt_subject,txt_body,dt_createdate,dt_modifydate) Values ('5','" . MAIL_FROM . "','" . $usrarry['var_email'] . "','" . $subject . "','" . $message . "',now(),now()) ";
            $exec = $this->configObj->glDbObj->query($insertSql);
            $message = "Your Mail sent Successfully. We will get you back soon..";
        }

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'newslatterleads');
        $this->configObj->glDbObj->setQueryType('update');
        // Set the Columns

        $this->configObj->glDbObj->addColumn('chr_replysent');
        $this->configObj->glDbObj->addValue('Y');
        $this->configObj->glDbObj->setWhere('int_glcode=' . $id);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'newsletter_reply');
        $this->configObj->glDbObj->setQueryType('insert');

        $this->configObj->glDbObj->addColumn('fk_contactdetail');
        $this->configObj->glDbObj->addColumn('var_adminreply');
        $this->configObj->glDbObj->addColumn('dtcreateddate');
        $this->configObj->glDbObj->addColumn('var_modulename');

        $this->configObj->glDbObj->addValue($id);
        $this->configObj->glDbObj->addValue($msg);
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('contactdetails');

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

    function generateParam($view = 'newslatter', $actionurl = 'index.php?module=contactusmsg&view=newslatter&action=add', $searchArray = array("newsletter" => "newsletter"), $table = 'DB_PREFIX.newsletter', $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
                '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,
            )
        );
    }

}

?>