<?php

class fblogmanager {

    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 = 's.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 fblogmanager($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;
        $this->mcombo = (isset($_REQUEST['mdnm'])) ? $_REQUEST['mdnm'] : $this->mcombo;
        $this->tcombo = (isset($_REQUEST['tcmb'])) ? $_REQUEST['tcmb'] : $this->tcombo;

        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 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.'&mdnm='.$this->mcombo.'&tcmb='.$this->tcombo;
        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby.'&mdnm='.$this->mcombo.'&tcmb='.$this->tcombo;
        $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.'&mdnm='.$this->mcombo.'&tcmb='.$this->tcombo;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby.'&mdnm='.$this->mcombo.'&tcmb='.$this->tcombo;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs.'&mdnm='.$this->mcombo.'&tcmb='.$this->tcombo;
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal;
    }

   

    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 * from " . DB_PREFIX . $tablename . " where int_glcode = '" . $selectarray[$i] . "'";
            $result = $this->configObj->glDbObj->query($data);
            if ($tablename == 'sharelog') {
                if ($_REQUEST['chkids'] != '') {
                    $sql="SELECT l.var_title as Title,l.var_description as Description,m.var_modulename as Module,case when s.chr_type='F' then ('Facebook') else ('Twitter') END as Type, case when s.chr_type = 'F' then (select var_pagename from ecl_facebook where int_glcode = s.fk_facebook_twitter) else (select var_accountname from ecl_twitter where int_glcode = s.fk_facebook_twitter) END as Account_Type,s.dt_createdate as Date FROM ecl_sharelog s LEFT JOIN ecl_facebooklog as l ON l.fk_sharelogglcode=s.int_glcode LEFT JOIN ecl_modules as m ON s.fk_module=m.int_glcode WHERE s.chr_delete = 'N' and l.chr_delete = 'N' and m.chr_delete='N' and s.int_glcode IN($selectids);";
                    } else {
                        $sql="SELECT l.var_title as Title,l.var_description as Description,m.var_modulename as Module,case when s.chr_type='F' then ('Facebook') else ('Twitter') END as Type, case when s.chr_type = 'F' then (select var_pagename from ecl_facebook where int_glcode = s.fk_facebook_twitter) else (select var_accountname from ecl_twitter where int_glcode = s.fk_facebook_twitter) END as Account_Type,s.dt_createdate as Date FROM ecl_sharelog s LEFT JOIN ecl_facebooklog as l ON l.fk_sharelogglcode=s.int_glcode LEFT JOIN ecl_modules as m ON s.fk_module=m.int_glcode WHERE s.chr_delete = 'N' and l.chr_delete = 'N' and m.chr_delete='N'";
                }
            }

        }
        return $sql;
    }

    function load_fblogmanager() {

        $param['js'] = array(
            "jquery.ui.core.min.js",
            "jquery.ui.widget.min.js",
            "jquery.ui.position.min.js",
            "jquery.ui.autocomplete.min.js",
            "highslide-with-html.js"
        );

        $param['css'] = array(
            "jquery.ui.theme.css",
            "jquery.ui.autocomplete.css",
            "jquery.ui.tabs.css",
            "highslide.css",
            "jquery.ui.theme.css",
            "jquery.ui.button.css",
            "jquery.ui.dialog.css",
            "jquery.ui.core.css"
        );
//        $this->configObj->glLibraryObj->loadPPJSCSS($param);
        $this->pagename = 'index.php?module=fblogmanager'; // Attribute of Page Name
        $this->loadCount('CountRowsnews');
        $this->CountRowsnews();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $result = $this->selectall_fblogmanager();
        $this->configObj->glTplObj->set('result', $result);
        $this->Generateurl();
        $view = 'Manage Facebook Log Manager';
        $actionurl = 'index.php?module=fblogmanager';
        $searchArray = array("l.var_title" => "Title", "m.var_modulename" => "Module");
        $table = DB_PREFIX . 'fblogmanagerleads';
        $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('fblogmanager.tpl.php');
            exit;
        } else {

            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('fblogmanager.tpl.php');
        }
    }

    function CountRowsnews() {
        $this->intitialize();
        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and (l.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['mdnm'] != '' && $_REQUEST['mdnm'] != '0'){
            $whereclause .= " and s.fk_module = '".$_REQUEST['mdnm']."'";
        }
        if($_REQUEST['tcmb'] != '' && $_REQUEST['tcmb'] != '0'){
            $whereclause .= " and s.chr_type = '".$this->tcombo."'";
        }
         $sqlCountPages = "select count(1) as total FROM ecl_sharelog s
LEFT JOIN ecl_facebooklog as l ON l.fk_sharelogglcode=s.int_glcode
LEFT JOIN ecl_modules as m ON s.fk_module=m.int_glcode
WHERE s.chr_delete = 'N' and l.chr_delete = 'N' and m.chr_delete='N' $whereclause"; 
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_fblogmanager() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable('ecl_sharelog s
                                                LEFT JOIN ecl_facebooklog as l ON l.fk_sharelogglcode=s.int_glcode
                                                LEFT JOIN ecl_modules as m ON s.fk_module=m.int_glcode');
        $this->configObj->glDbObj->addColumn('l.var_name,l.var_ipaddress,l.var_title,l.var_description,l.fk_sharelogglcode,s.dt_createdate,s.fk_module,s.fk_facebook_twitter,m.int_glcode,s.int_glcode,s.fk_module,s.chr_type, case when s.chr_type = \'F\' then (select var_pagename from ecl_facebook where int_glcode = s.fk_facebook_twitter) else (select var_accountname from ecl_twitter where int_glcode = s.fk_facebook_twitter) END as Account_Type');
        $whereclause = "  s.chr_delete = 'N' and l.chr_delete = 'N' and m.chr_delete='N' ";

        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and (l.var_title like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%')" : " and $this->searchby  like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
        }
        if($_REQUEST['mdnm'] != '' && $_REQUEST['mdnm'] != '0'){
            $whereclause .= " and s.fk_module = '".$_REQUEST['mdnm']."'";
        }
        if ($this->filterby != '0' && $this->filterby != '') {
            $filterarray = explode('-', $this->filterby);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        if($_REQUEST['tcmb'] != '' && $_REQUEST['tcmb'] != '0'){
            $whereclause .= " and s.chr_type = '".$this->tcombo."'";
        }
       
        $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)) ? "  and {$this->configObj->glDbObj->addSlashes($this->searchbyVal)} like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' " : "$whereclause";

            if ($this->searchbyVal == '0') {
                $this->searchbyVal = "l.var_title";
            }
            if ($this->searchbyVal == 'l.var_title') {
                $this->searchbyVal = "l.var_title";
            }
             if ($this->filterby != '0' && $this->filterby != '') {
                $filterarray = explode('-', $this->filterby);
                $whereclauseauto = " and $filterarray[0] = '$filterarray[1]'";
            }
            if($_REQUEST['mdnm'] != '' && $_REQUEST['mdnm'] != '0'){
            $whereclause1 = " and s.fk_module = '".$_REQUEST['mdnm']."'";
        }
        if($_REQUEST['tcmb'] != '' && $_REQUEST['tcmb'] != '0'){
            $whereclause2 = " and s.chr_type = '".$this->tcombo."'";
        }
        $searchbyVal1 = "{$this->searchbyVal} like '%" . $this->searchtxt . "%' ";
             $autoSearchQry = "select m.var_modulename,s.fk_module,s.chr_type,l.int_glcode,f.int_glcode,s.int_glcode,t.int_glcode,m.int_glcode ,l.var_title," . $this->searchbyVal . " as AutoVal FROM " . DB_PREFIX . "facebook f ,".DB_PREFIX."twitter t ,".DB_PREFIX."facebooklog l ,".DB_PREFIX."sharelog s ,".DB_PREFIX."modules m where s.fk_module=m.int_glcode and l.fk_sharelogglcode=s.int_glcode and    $searchbyVal1 $whereclauseauto   $whereclause1 $whereclause2 group by " . $this->searchbyVal . " $orderby";
//            echo $autoSearchQry;
             //$autoSearchQry="select m.var_modulename,s.fk_module,s.chr_type,l.int_glcode,f.int_glcode,s.int_glcode,t.int_glcode,m.int_glcode ,l.var_title,l.var_title as AutoVal FROM ecl_facebook f, ecl_twitter t, ecl_facebooklog l, ecl_sharelog s, ecl_modules m where l.var_title like '%test%' and s.chr_delete = 'N' and l.chr_delete = 'N' and m.chr_delete='N' and (l.var_title like '%test%') group by l.var_title order by s.int_glcode desc";
             
             
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
          // $autoSearchQry = "select m.var_modulename,s.fk_module,s.chr_type,l.int_glcode,f.int_glcode,s.int_glcode,t.int_glcode,m.int_glcode ,l.var_title as AutoVal FROM " . DB_PREFIX . "facebook f ".DB_PREFIX."twitter t ".DB_PREFIX."facebooklog l ".DB_PREFIX."sharelog s ".DB_PREFIX."modules m   where $searchbyVal group by l.var_title $orderby";

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

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

            $sql = "Update " . DB_PREFIX . "fblogmanagerleads 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(96, DB_PREFIX . 'fblogmanagerleads', 'var_email', $deletearray[$i], 'D');
        }
        $this->load_fblogmanager();
        exit;
    }

    function generateParam($view = 'fblogmanager', $actionurl = 'index.php?module=fblogmanager', $searchArray = array("fblogmanager" => "fblogmanager"), $table = 'DB_PREFIX.fblogmanager', $position = 'top') {
        $filterPos = ($position == 'top') ? 'FT' : 'FB';
        if($position=='top')
        {
            $careertypecombo =  $this-> modulecombo();
            $careertypecombo1 =  $this-> typecombo();
        }    
           
        $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,
            ),
            'careertypecombo' => $careertypecombo,
            'careertypecombo1' => $careertypecombo1,
        );
    }
    function modulecombo() {
         $sqlSelect = "select m.*,s.fk_module from ecl_modules m,ecl_sharelog s where m.int_glcode=s.fk_module group by s.fk_module";
        $result = $this->configObj->glDbObj->query($sqlSelect);
        $modulecombo = "<div class=\"new-search-show-all\">
                                <select style=\"width:130px;  text-transform: capitalize;\" id=\"modulecombo\" style=\"width:200px\" tabindex=\"24\" style=\"width:125px;\" class=\"more-textarea\" name=\"modulecombo\"
                               onchange=\"SendGridBindRequest('" . $this->urlwithpara . "','gridbody','MDC');\">";
        $modulecombo.="<option value='0'> -- Sort By Module -- </option>";
        while ($row = $this->configObj->glDbObj->fetch_array($result)) {

            if ($row['int_glcode'] == $_REQUEST['mdnm']) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $modulecombo.="<option $selected value='" . $row['int_glcode'] . "'>" . $row['var_modulename'] . "</option>";
        }
        $modulecombo.='</select></div>';
       return $modulecombo;
    }

    function typecombo() {
        $typecombo = "<div class=\"new-search-show-all\">
                                <select style=\"width:130px;\" id=\"typecombo\" style=\"width:200px\" tabindex=\"24\" style=\"width:125px;\" class=\"more-textarea\" name=\"typecombo\"
                               onchange=\"SendGridBindRequest('" . $this->urlwithpara . "','gridbody','MDC1');\">";
        $typecombo.="<option value='0'> -- Sort By Type -- </option>";
            $selected=($_REQUEST['tcmb'] == 'F')?$selected = 'selected':$selected = '';
            $selected1=($_REQUEST['tcmb'] == 'T')?$selected1 = 'selected':$selected1 = '';
            $typecombo.="<option value='F' $selected>Facebook</option>";
            $typecombo.="<option value='T' $selected1>Twitter</option>";
        $typecombo.='</select></div>';
       return $typecombo;
    }
}

?>