<?php

class modcontrol {

    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 = 'm.int_glcode';
    var $ordertype = 'desc';
    var $searchby = '0';
    var $searchtxt;
    var $start = 1;
    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 modcontrol($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 select_module($id, $flag) {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        if ($flag == 'N') {

            $this->configObj->glDbObj->addColumn('chr_pro_modulename');
            $this->configObj->glDbObj->setTable(DB_PREFIX . 'modules');
        } else {
            $this->configObj->glDbObj->addColumn('var_pagename');
            $this->configObj->glDbObj->setTable(DB_PREFIX . 'pages');
        }
        $this->configObj->glDbObj->setWhere('int_glcode =' . $id);
        $this->configObj->glDbObj->buildQuery();
        $row = $this->configObj->glDbObj->get_sql_row($this->configObj->glDbObj->buildQuery());
        return $row;
    }

    function BindActivityTypecmb($eid, $flag) {

        if ($flag == 'N') {
            $returndata = '';
            $sql = "select imc.fk_useraction as accid,imu.int_glcode as optid,imu.var_action from " . DB_PREFIX . "useraction  imu left join " . DB_PREFIX . "module_useraction imc on imc.fk_useraction=imu.int_glcode and imc.chr_delete='N' and imc.chr_custom='N' and imc.fk_modules=$eid";
            $rs = $this->configObj->glDbObj->query($sql);
            $returndata.= '<div style="float:left;">';
            $returndata.='<select  id="activitytype" multiple="" style="width:380px;" size="5" name="activitytype[]">';
            while ($rw = $this->configObj->glDbObj->fetch_array($rs)) {
                $select = (($rw['accid'] == $rw['optid']) ? 'selected' : '');
                $returndata.="<option value=" . $rw['optid'] . "  " . $select . ">" . $rw['var_action'] . "</option>";
            }
            $returndata.='</select>';
        } else {
            $returndata = '';
            $sql = "select imc.fk_useraction as accid,imu.int_glcode as optid,imu.var_action from " . DB_PREFIX . "useraction  imu left join " . DB_PREFIX . "module_useraction imc on imc.fk_useraction=imu.int_glcode and imc.chr_delete='N' and imc.chr_custom='Y' and imc.fk_modules=$eid";
            $rs = $this->configObj->glDbObj->query($sql);
            $returndata.= '<div style="float:left;">';
            $returndata.='<select  id="activitytype" multiple="" style="width:380px;" size="5" name="activitytype[]">';
            while ($rw = $this->configObj->glDbObj->fetch_array($rs)) {
                $select = (($rw['accid'] == $rw['optid']) ? 'selected' : '');
                $returndata.="<option value=" . $rw['optid'] . "  " . $select . ">" . $rw['var_action'] . "</option>";
            }
            $returndata.='</select>';
        }
        return $returndata;
    }

    function CountRowsmodules() {
        $this->intitialize();
        $this->Generateurl();

        if (trim($this->searchtxt) != '') {
            $whereclause .= ($this->searchby == '0') ? " and var_name 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);
        }

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

    function selectall_modules() {

        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'modules');
        $this->configObj->glDbObj->addColumn("m.int_glcode,m.chr_pro_modulename, GROUP_CONCAT(iu.var_action) as action");
        $this->configObj->glDbObj->setTable("" . DB_PREFIX . "modules m left join " . DB_PREFIX . "module_useraction imu on imu.`fk_modules` = m.int_glcode and imu.chr_custom ='N' and imu.chr_delete='N'
                                        left join " . DB_PREFIX . "useraction iu on imu.`fk_useraction` = iu.int_glcode");
        $whereclause = "m.chr_delete = 'N' and  m.chr_publish='Y' and m.chr_powerpanel_module = 'Y' group by m.int_glcode ";

        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);
        //echo $this->configObj->glDbObj->buildQuery();//exit;
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

    function selectall_custom() {

        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->addColumn("m.int_glcode,m.var_pagename, GROUP_CONCAT(iu.var_action) as action");
        $this->configObj->glDbObj->setTable("" . DB_PREFIX . "pages m left join " . DB_PREFIX . "module_useraction imu on imu.`fk_modules` = m.int_glcode and imu.chr_custom='Y' and imu.chr_delete='N'
                                        left join " . DB_PREFIX . "useraction iu on imu.`fk_useraction` = iu.int_glcode");
        $whereclause = "m.chr_delete = 'N' and m.chr_publish='Y' and m.chr_custom='Y' group by m.int_glcode";
        $this->configObj->glDbObj->setWhere($whereclause);
        $this->configObj->glDbObj->setOrderBy($this->orderby . ' ' . $this->ordertype);
        $this->configObj->glDbObj->setLimit($this->start . ', ' . $this->pagesize);
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

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

    function Generateurl() {
        $custom = $_REQUEST['custom'];
        $this->urlwithpara = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&custom=' . $custom . '&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 . '&custom=' . $custom . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby;
        $this->urlwithoutsort = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&custom=' . $custom . '&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 . '&custom=' . $custom . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&custom=' . $custom . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs;
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&custom='.$custom.'&searchbyVal=" . $this->searchbyVal;
    }

    function generateParam($view = 'groups', $actionurl = 'index.php?module=groups&action=add', $searchArray = array("groups" => "groups"), $table = 'DB_PREFIX.groups', $position = 'top', $otheraction = '') {
        $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',
            'otheractionImage' => 'back-button-blue.gif',
            'actionUrl' => $actionurl,
            'otheractionUrl' => $otheraction,
            '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->configObj->glLibraryObj->PagingArray(),
            )
        );
    }

    function load_modules() {

        $this->pagename = 'index.php?module=modcontrol'; // Attribute of Page Name
        $this->loadCount('CountRowsmodules');
        $result = $this->selectall_modules();
        $this->configObj->glTplObj->set('result', $result);

        $cusresult = $this->selectall_custom();
        $this->configObj->glTplObj->set('cusresult', $cusresult);
        $view = 'Assign Module Actions ';
        $actionurl = 'index.php?module=modcontrol&action=edit';
        $searchArray = array("var_name" => "Name");
        $table = DB_PREFIX . 'groups';
        $parmas = $this->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->generateParam($view, $actionurl, $searchArray, $table, 'bottom')));
        $finalsortvar = 'setsortimg' . $this->orderby;
        $this->configObj->glTplObj->set($finalsortvar, $this->sortvar);
        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('access.tpl.php');
            exit;
        } else {

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

    function loadCount($function) {
        $this->intitialize();
        $this->numofrows = (isset($_REQUEST['numofrows'])) ? $_REQUEST['numofrows'] : $this->{$function}();
        $this->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 UpdateAssignAction() {
        $sql_update = "update " . DB_PREFIX . "module_useraction set chr_delete='Y'";
        $this->configObj->glDbObj->query($sql_update);
        $sql = "select GROUP_CONCAT(fk_modules,'-',fk_useraction,'-',chr_custom) as actionName,GROUP_CONCAT(int_glcode) as int_glcode from " . DB_PREFIX . "module_useraction";
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        $tempAction = explode(",", $result['actionName']);
        $update_id = explode(",", $result['int_glcode']);
        foreach ($tempAction as $key => $value) {
            $updatearrayids[$value] = $update_id[$key];
        }
        foreach ($_REQUEST['chkaccess'] as $access) {

            if (in_array($access, $tempAction) && !empty($tempAction)) {
                $Update_ids .= $updatearrayids[$access] . ",";
            } else {
                list($fk_module, $fk_action, $chr_custon) = explode("-", $access);
                $insert_values .= "('" . $fk_module . "','" . $fk_action . "','" . $chr_custon . "','N',NOW()),";
            }
        }
        $Update_ids = substr($Update_ids, 0, -1);
        $insert_values = substr($insert_values, 0, -1);
        if (!empty($Update_ids)) {
            $sql_updates = "update " . DB_PREFIX . "module_useraction set chr_delete='N' where int_glcode IN($Update_ids)";
            $this->configObj->glDbObj->query($sql_updates);
            $return = "true";
        }
        if (!empty($insert_values)) {
            $sql_insert = "insert into " . DB_PREFIX . "module_useraction(fk_modules,fk_useraction,chr_custom,chr_delete,dt_modifydate) values $insert_values";
            $this->configObj->glDbObj->query($sql_insert);
            $return = "true";
        }
        return $return;
    }

    function selectAllassignAction() {
        $sql = "select fk_modules,fk_useraction,chr_custom from " . DB_PREFIX . "module_useraction where chr_delete='N'";
        $result = $this->configObj->glDbObj->query($sql);
        while ($row = $this->configObj->glDbObj->fetch_array($result)) {
            $actionmodule[$row[fk_modules]][$row[fk_useraction]][$row['chr_custom']] = $row['fk_modules'] . '-' . $row['fk_useraction'] . '-' . $row['chr_custom'];
        }
        return $actionmodule;
    }

    function getAllAction() {
        $sql = "select GROUP_CONCAT(int_glcode) as action,GROUP_CONCAT(var_action) as actionName from " . DB_PREFIX . "useraction where chr_delete='N'";
        $result = $this->configObj->glDbObj->get_sql_row($sql);
        $tempAction = explode(",", $result['action']);
        $tempActionName = explode(",", $result['actionName']);
        for ($i = 0; $i < count($tempAction); $i++) {
            $action[$i]['actionId'] = $tempAction[$i];
            $action[$i]['actionName'] = $tempActionName[$i];
        }
        return $action;
    }

    function getAllmodule() {
        $sql = "select int_glcode as moduleId,var_modulename as moduleName,chr_pro_modulename, 'N' as chrcustom,var_modulename as orderfield from " . DB_PREFIX . "modules where chr_powerpanel_module='Y' and chr_delete='N' and chr_publish='Y' and int_glcode NOT IN(8,11) union select int_glcode as moduleId,var_pagename as moduleName,var_pagename as chr_pro_modulename,'Y' as chrcustom,int_glcode as orderfield from " . DB_PREFIX . "pages where chr_custom='Y' and chr_delete='N' and chr_publish='Y' and chr_previewstatus='N' ORDER BY chrcustom,orderfield";

        $result = $this->configObj->glDbObj->query($sql);
        $module = array();
        while ($row = $this->configObj->glDbObj->fetch_array($result)) {
            $temp['moduleId'] = $row['moduleId'];
            $temp['moduleName'] = $row['moduleName'];
            $temp['chr_pro_modulename'] = $row['chr_pro_modulename'];
            $temp['chrcustom'] = $row['chrcustom'];

            array_push($module, $temp);
        }
        return $module;
    }

}
?>



