<?php

class appointment {

    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 appointment($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 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 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_Address, opi.var_phone  as Phone, opi.var_message as Message, CASE opi.chr_replysent  When 'Y' THEN 'Yes' ELSE 'No' END AS Reply_Sent, 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_Address, opi.var_phone  as Phone, opi.var_message as Message, CASE opi.chr_replysent  When 'Y' THEN 'Yes' ELSE 'No' END AS Reply_Sent, 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 == 'appointmentleads') {
                if ($_REQUEST['chkids'] != '') {
                    $sql = "select opi.var_name as Name,CASE opi.var_email WHEN '' THEN 'N/A' ELSE opi.var_email END AS 'Email ID',opi.var_phone as Phone, DATE_FORMAT(opi.dt_appointment,'" . DEFAULT_DATEFORMAT . "') as 'Date of Appointment',time(opi.dt_appointment) as 'Time of Appointment',opi.var_service as Service,CASE opi.var_description WHEN '' THEN 'N/A' ELSE opi.var_description END AS 'Description', 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) order by int_glcode desc";
                } else {
                    
                    
//                    $sql = "select opi.var_name as Name,CASE opi.var_email WHEN '' THEN 'N/A' ELSE opi.var_email END AS 'Email ID',opi.var_phone as Phone#, opi.dt_appointment as 'Date of Appointment',opi.var_service as Services,CASE opi.var_description WHEN '' THEN 'N/A' ELSE opi.var_description END AS 'Description', 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' order by int_glcode desc";
                      $sql = "select opi.var_name as Name,CASE opi.var_email WHEN '' THEN 'N/A' ELSE opi.var_email END AS 'Email ID',opi.var_phone as 'Phone No',DATE_FORMAT(opi.dt_appointment,'" . DEFAULT_DATEFORMAT . "') as 'Date of Appointment',time(opi.dt_appointment) as 'Time of Appointment',opi.var_service as Service,CASE opi.var_description WHEN '' THEN 'N/A' ELSE opi.var_description END AS 'Description', 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' order by int_glcode desc";     
                    
                    
                }
            }
        }
        //echo $sql; exit;
        return $sql;
    }

    function load_appointmentleads() {

        $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);
//          echo "asd";exit;
        $this->pagename = 'index.php?module=appointment'; // Attribute of Page Name
        $this->loadCount('CountRowsnews');
        $this->CountRowsnews();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $result = $this->selectall_appointmentleads();
        $this->configObj->glTplObj->set('result', $result);
        $this->Generateurl();
        $view = 'Manage Appointment Leads';
        $actionurl = 'index.php?module=appointment';
        $searchArray = array("var_name" => "name");
        $table = DB_PREFIX . 'appointmentleads';
        $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('appointmentleads.tpl.php');
            exit;
        } else {

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

    function CountRowsnews() {
        $this->intitialize();
        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);
            $whereclause .=" and $filterarray[0] = '$filterarray[1]'";
        }
        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "appointmentleads where chr_delete = 'N' $whereclause";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_appointmentleads() {
        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'appointmentleads');
        $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_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);
            $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 '%" . addslashes($this->searchtxt) . "%' " : "$whereclause";

            $autoSearchQry = "select int_glcode ,var_name as AutoVal FROM " . DB_PREFIX . "appointmentleads where $searchbyVal group by var_email $orderby";

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

    function is_appointmentused($fk_modules) {

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

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

            $sql = "Update " . DB_PREFIX . "appointmentleads 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(101, DB_PREFIX . 'appointmentleads', 'var_name', $deletearray[$i], 'D');
        }
        $this->load_appointmentleads();
        exit;
    }

   

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

}

?>