<?php

class services {

    var $pagename = 'index.php?module=services';
    var $int_glcode;
    var $chr_read;
    var $chr_star;
    var $chr_publish;
    var $int_displayorder;
    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 = 1;
    var $pagesize = DEFAULT_PAGESIZE;
    var $pagenumber = '1';
    var $lastinsertid;
    var $urlwithpara = '';
    var $urlwithpoutsearch = '';
    var $urlwithoutsort = '';
    var $urlwithoutpaging = '';
    var $configObj;
    var $categorytype = '';
    var $filterby = '0';
    var $urlwithoutfilter = '';
    var $display_limit_array = array(5, 10, 15, 30);
    var $varpostdata = array();

    function services($configObj) {
        $this->checkAliasTable = array(
            DB_PREFIX . "services" => "var_alias",
        );
        $this->configObj = $configObj;
    }

    function get_fk_modules($module) {
        $sql = "SELECT int_glcode FROM " . DB_PREFIX . "modules WHERE var_modulename='$module' and chr_publish='Y' and chr_delete='N'";
        $rs = $this->configObj->glDbObj->get_sql_row($sql);
        $fk_modules = 0;
        $fk_modules = $rs['int_glcode'];
        return $fk_modules;
    }

    function intitialize($view) {

        $this->categorytype = (isset($_REQUEST['categorytype'])) ? $_REQUEST['categorytype'] : $categorytype;
        $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'])) ? $_REQUEST['searchtxt'] : '';
        $this->searchtxtjs = (isset($_REQUEST['searchtxt'])) ? $_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->categoryid = (isset($_REQUEST['categoryid'])) ? $_REQUEST['categoryid'] : $this->categoryid;

        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_services($id) {
        $dtformat = DEFAULT_DATEFORMAT;

        $this->configObj->glDbObj->resetfields(); // Reset the object
        $this->configObj->glDbObj->setQueryType('select');    // Set the Table
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'services');
        $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($view);

        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['categoryid'] != '' && $_REQUEST['categoryid'] != '0') {

            $whereclause .=" and fk_category = '" . $this->categoryid . "'";
        }
        if ($_REQUEST['id'] != '') {
            $whereclause1 .=" and fk_category='" . $_REQUEST['id'] . "'";
        } else {
            $whereclause1.='';
        }
        $sqlCountPages = "select count(1) as total from " . DB_PREFIX . "services where chr_delete = 'N' $whereclause  $whereclause1";
        $rs = $this->configObj->glDbObj->get_sql_row($sqlCountPages);
        $this->numofrows = $rs['total'];
        return $rs['total'];
    }

    function selectall_services() {

        $dtformat = DEFAULT_DATEFORMAT;
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('select');
        $tablename = DB_PREFIX . "services";
        $this->configObj->glDbObj->setTable($tablename);
        $this->configObj->glDbObj->addColumn("*");
        $whereclause = "chr_delete = 'N'";

        if ($_REQUEST['id'] != '') {
            $whereclause .= " and fk_category='" . $_REQUEST['id'] . "'";
        } else {
            $whereclause.='';
        }

        if ($_REQUEST['categoryid'] != '' && $_REQUEST['categoryid'] != '0') {

            $whereclause .=" and fk_category = '" . $this->categoryid . "'";
        }

        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]'";
        }


        $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 dt_modifydate desc';
            if (trim($this->searchtxt) != '') {
                $whereclause = ( $this->searchby != '0') ? " $this->searchbyVal like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%' " : "  $this->searchbyVal like '%" . $this->configObj->glDbObj->addSlashes($this->searchtxt) . "%'";
            }
            if ($this->filterby != '0' && $this->filterby != '') {
                $filterarray = explode('-', $this->filterby);
                $whereclause1 .=" and $filterarray[0] = '$filterarray[1]'";
            }
            if ($_REQUEST['categoryid'] != '' && $_REQUEST['categoryid'] != '0') {

                $whereclause .=" and fk_category = '" . $this->categoryid . "'";
            }

            $searchbyVal = (!empty($this->searchbyVal)) ? " $whereclause " : $whereclause;
            $autoSearchQry = "select  int_glcode ," . $this->searchbyVal . " as AutoVal FROM " . $tablename . " where $searchbyVal $whereclause1 and chr_delete='N' group by " . $this->searchbyVal . " $orderby";
            $this->configObj->glLibraryObj->GetAutoSearch($autoSearchQry);
        }
        $this->configObj->glDbObj->buildQuery();
        $rs = $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        return $rs;
    }

    function updatedisplay_order() {
        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'services');
        $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>=' . $this->varpostdata['int_displayorder']);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
    }

//    function upload_image($filefield, $thumb_width, $thumb_height, $prefix, $sess, $large_width, $large_height, $prefix2 = '') {
//        $file_photo = basename($_FILES[$filefield]['name']);
//
//        $imagename = $sess . $file_photo;
//        $uploaddir = '../upimages/services/images/';
//        $file_photo = $sess . basename($_FILES[$filefield]['name']);
//        $file = $uploaddir . $file_photo;
//
//        $tmp = $_FILES[$filefield]['tmp_name'];
//        $uploadedfile = $_FILES[$filefield]['tmp_name'];
//
//        $image_info = getimagesize($uploadedfile);
//
//        switch ($image_info['mime']) {
//            case 'image/gif':
//                $img_create = 'ImageCreateFromGIF';
//                break;
//            case 'image/jpeg':
//                $img_create = 'ImageCreateFromJPEG';
//                break;
//            case 'image/png':
//                $img_create = 'ImageCreateFromPNG';
//                break;
//            case 'image/bmp':
//                $img_create = 'ImageCreateFromBMP';
//                break;
//        }
//
//        $photoexe = explode('.', $imagename);
//        $photoexename = $photoexe[count($photoexe) - 1];
//        if ($photoexename == 'bmp') {
//            $src = $this->configObj->glLibraryObj->imagecreatefrombmp($uploadedfile);
//        } else {
//            
//            $src = @$img_create($uploadedfile);
//             if ($image_info['mime'] == "image/X-PNG" || $image_info['mime']  == "image/PNG" || $image_info['mime']  == "image/png" || $image_info['mime']  == "image/x-png") {
//                $src = imagecreatefrompng($uploadedfile);
//            }
//        }
//        list($width, $height) = getimagesize($uploadedfile);
//
//        $newwidth = $width;
//        $newheight = $height;
//
//        $tmp = imagecreatetruecolor($newwidth, $newheight);
//       if ($image_info['mime'] == "image/gif" || $image_info['mime'] == "image/png") {
//            $background = imagecolorallocatealpha($tmp, 255, 255, 255,127);
//            imagefill($tmp, 0, 0, $background);
//           
//        }
//        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//         if ($image_info['mime'] == "image/X-PNG" || $image_info['mime'] == "image/PNG" || $image_info['mime'] == "image/png" || $image_info['mime'] == "image/x-png") {
//                imagepng($tmp, $file);
//            }
// else {
//        // now write the resized image to disk.
//        imagejpeg($tmp, $file, 90);
// }
//        imagedestroy($src);
//        imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request  // has completed.
//
//        $source1 = $uploaddir . $file_photo;
//
//        $thumbfilename = $prefix . $sess . $file_photo;
//
//        $target1 = $uploaddir . $prefix . $file_photo;
//
//        $target2 = $uploaddir . $prefix2 . $file_photo;
//
//        if ($large_width > 0) {
//            $returnthumbmsg1 = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width, false, false, $target1, false, null, $thumb_width, $thumb_height);
//        }
//        if ($large_width > 0) {
//            $returnthumbmsg2 = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width, false, false, $target2, false, null, $large_width, $large_height);
//        }
//        if ($returnthumbmsg1 == 'INVALID' || $returnthumbmsg2 == 'INVALID') {
//            $this->chr_validthumb = 'N';
//            $this->chr_validthumbdetail = 'N';
//        } else {
//            $this->chr_validthumb = 'Y';
//            $this->chr_validthumbdetail = 'Y';
//        }
//        return $imagename;
//    }
//    

    function upload_image($filefield, $thumb_width, $thumb_height, $prefix, $sess, $thumb_width2, $thumb_height2, $prefix2 = '', $path) {
//         echo $thumb_width;
//         echo $thumb_height;exit;
        $file_photo = basename($_FILES[$filefield]['name']);
        $imagename = $sess . $file_photo;

        $uploaddir = $path;
//        $uploaddir = '../upimages/services/images/';
        $file_photo = $sess . basename($_FILES[$filefield]['name']);
        $file = $uploaddir . $file_photo;
        $tmp = $_FILES[$filefield]['tmp_name'];

        $uploadedfile = $_FILES[$filefield]['tmp_name'];

        // Create an Image from it so we can do the resize
        $image_info = getimagesize($uploadedfile);

        switch ($image_info['mime']) {
            case 'image/gif':
                $img_create = 'ImageCreateFromGIF';
                break;
            case 'image/jpeg':
                $img_create = 'ImageCreateFromJPEG';
                break;
            case 'image/png':
                $img_create = 'ImageCreateFromPNG';
                break;
            case 'image/bmp':
                $img_create = 'ImageCreateFromBMP';
                break;
        }

        $photoexe = explode('.', $imagename);
        $photoexename = $photoexe[count($photoexe) - 1];

        if ($photoexename == 'bmp') {

            $src = $this->configObj->glLibraryObj->imagecreatefrombmp($uploadedfile);
        } else {

            $src = @$img_create($uploadedfile);
        }

        list($width, $height) = getimagesize($uploadedfile);

        if ($width >= height && $width >= 1000) {
            $newwidth = 950;
            $newheight = intval(($newwidth * $height) / $width);
        } else if ($height >= width && $height >= 750) {
            $newheight = 700;
            $newwidth = intval(($newheight * $width) / $height);
        } else {
            $newwidth = $width;
            $newheight = $height;
        }


        $tmp = imagecreatetruecolor($newwidth, $newheight);
        
        $background_color = imagecolorallocate($tmp, 255, 255, 255);
        imagefill($tmp, 0, 0, $background_color);
        
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        imagejpeg($tmp, $file, 90);

        imagedestroy($src);
        imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request  // has completed.

        $source1 = $uploaddir . $file_photo;

        //$sess = time();
        $thumbfilename = $prefix . $sess . $file_photo;
        $target1 = $uploaddir . $prefix . $file_photo;
        $target2 = $uploaddir . $prefix2 . $file_photo;
        $thumbstatus = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width, false, false, $target1, false, null, $thumb_width, $thumb_height);
        if ($thumbstatus == 'invalid filesize') {
            $errstatus.=":" . "invalid1";
        }
        if ($thumb_width2 > 0) {
            $thumbstatus = $this->configObj->glLibraryObj->generate_thumb($source1, $thumb_width2, false, false, $target2, false, null, $thumb_width2, $thumb_height2);

            if ($thumbstatus == 'invalid filesize') {
                $errstatus.=":" . "invalid2";
            }
        }
        if ($returnthumbmsg1 == 'INVALID' || $returnthumbmsg2 == 'INVALID') {
            $this->chr_validthumb = 'N';
        } else {
            $this->chr_validthumb = 'Y';
        }
        return $imagename;
    }

    function insert_services() {


        $this->configObj->glLibraryObj->updatedisplay_order('services', $this->varpostdata['int_displayorder']);
        if ($_FILES['var_image']['name'] != '') {
            $sess = time();
            //for removing space in name
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image']['name']);
            $photo = basename($_FILES['var_image']['name']);
            $photofile = str_replace(' ', '-', $photo);
            $phototitle = explode('.', $photofile);
            $var_title = $phototitle[0];
            $_FILES['var_image']['name'] = $photofile;

            $image_width = SERVICES_THUMB_WIDTH1;
            $image_height = SERVICES_THUMB_HEIGHT1;
            $path = '../upimages/services/images/';
            $var_thumb = $this->upload_image('var_image', $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_', $path);

            $this->imagename = $var_thumb;
        }

        if ($_FILES['var_image_logo']['name'] != '') {

            //for removing space in name
            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image_logo']['name']);
            $photo = basename($_FILES['var_image_logo']['name']);
            $photofile = str_replace(' ', '-', $photo);
            $phototitle = explode('.', $photofile);
            $var_title = $phototitle[0];
            $_FILES['var_image_logo']['name'] = $photofile;

            $image_width = SERVICES_THUMB_WIDTH1;
            $image_height = SERVICES_THUMB_HEIGHT1;
            $path = '../upimages/services/logo/';
            $var_thumb = $this->upload_image('var_image_logo', $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_', $path);

            $this->imagename1 = $var_thumb;
        }

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('insert');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'services');
        // Set the Columns
        $this->configObj->glDbObj->addColumn('fk_category');
        $this->configObj->glDbObj->addColumn('var_title');
//        $this->configObj->glDbObj->addColumn('var_alias');
        $this->configObj->glDbObj->addColumn('var_image_logo');
        $this->configObj->glDbObj->addColumn('var_image');
        $this->configObj->glDbObj->addColumn('text_fulltext');
        $this->configObj->glDbObj->addColumn('body_desc');
        $this->configObj->glDbObj->addColumn('var_description_body');
        $this->configObj->glDbObj->addColumn('facial_desc');
        $this->configObj->glDbObj->addColumn('var_description_facial');
        $this->configObj->glDbObj->addColumn('peels_desc');
        $this->configObj->glDbObj->addColumn('var_description_peels');
        $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('chr_publish');
        $this->configObj->glDbObj->addColumn('chr_delete');
        $this->configObj->glDbObj->addColumn('dt_createdate');
        $this->configObj->glDbObj->addColumn('dt_modifydate');
        $this->configObj->glDbObj->addColumn('chr_read');

        $this->configObj->glDbObj->addValue(trim($this->varpostdata['fk_category']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_title']));
//        $this->configObj->glDbObj->addValue($this->varpostdata['var_alias']);
        $this->configObj->glDbObj->addValue($this->imagename1);
        $this->configObj->glDbObj->addValue($this->imagename);
        if ($this->varpostdata['var_desc'] == 'Y') {
            $this->configObj->glDbObj->addValue($this->varpostdata['text_fulltext']);
        } else {
            $this->configObj->glDbObj->addValue('');
        }
        $d = ($this->varpostdata['body_desc'] == "Y") ? 'Y' : 'N';
        $this->configObj->glDbObj->addValue($d);

        if ($this->varpostdata['body_desc'] == "Y") {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description_body']));
        } else {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['']));
        }

        $d1 = ($this->varpostdata['facial_desc'] == "Y") ? 'Y' : 'N';
        $this->configObj->glDbObj->addValue($d1);

        if ($this->varpostdata['facial_desc'] == "Y") {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description_facial']));
        } else {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['']));
        }

        $d2 = ($this->varpostdata['peels_desc'] == "Y") ? 'Y' : 'N';
        $this->configObj->glDbObj->addValue($d2);

        if ($this->varpostdata['peels_desc'] == "Y") {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description_peels']));
        } else {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['']));
        }
        $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($this->varpostdata['chr_publish']);
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('N');

//       echo $this->configObj->glDbObj->buildQuery();exit;
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->lastinsertid = mysql_insert_id();
        $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'services');
        $this->configObj->glLibraryObj->insertinlogmanager(90, DB_PREFIX . 'services', 'var_title', $this->lastinsertid, 'I');
        return true;
    }

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

        if ($_FILES['var_image']['name'] != '') {

            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image']['name']);
            $photo = basename($_FILES['var_image']['name']);
            $photofile = str_replace(' ', '-', $photo);
            $phototitle = explode('.', $photofile);
            $var_title = $phototitle[0];
            $_FILES['var_image']['name'] = $photofile;

            $image_width = SERVICES_THUMB_WIDTH1;
            $image_height = SERVICES_THUMB_HEIGHT1;
            $path = '../upimages/services/images/';
            $var_thumb = $this->upload_image('var_image', $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_', $path);

            $this->imagename = $var_thumb;
        } else {
            $this->imagename = $_REQUEST['hidvar_image1'];
        }

        if ($_FILES['var_image_logo']['name'] != '') {

            $file_extension = $this->configObj->glLibraryObj->file_extension($_FILES['var_image_logo']['name']);
            $photo = basename($_FILES['var_image_logo']['name']);
            $photofile = str_replace(' ', '-', $photo);
            $phototitle = explode('.', $photofile);
            $var_title = $phototitle[0];
            $_FILES['var_image_logo']['name'] = $photofile;

            $image_width = SERVICES_THUMB_WIDTH1;
            $image_height = SERVICES_THUMB_HEIGHT1;
            $path = '../upimages/services/logo/';
            $var_thumb = $this->upload_image('var_image_logo', $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_', $path);

            $this->imagename1 = $var_thumb;
        } else {
            $this->imagename1 = $this->varpostdata['hidvar_image'];
        }

        $this->configObj->glDbObj->resetfields();
        $this->configObj->glDbObj->setQueryType('update');
        $this->configObj->glDbObj->setTable(DB_PREFIX . 'services');
        // Set the Columns
        $this->configObj->glDbObj->addColumn('fk_category');
        $this->configObj->glDbObj->addColumn('var_title');
//        $this->configObj->glDbObj->addColumn('var_alias');
        $this->configObj->glDbObj->addColumn('var_image_logo');
        $this->configObj->glDbObj->addColumn('var_image');
        $this->configObj->glDbObj->addColumn('text_fulltext');
        $this->configObj->glDbObj->addColumn('body_desc');
        $this->configObj->glDbObj->addColumn('var_description_body');
        $this->configObj->glDbObj->addColumn('facial_desc');
        $this->configObj->glDbObj->addColumn('var_description_facial');
        $this->configObj->glDbObj->addColumn('peels_desc');
        $this->configObj->glDbObj->addColumn('var_description_peels');
        $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('chr_publish');
        $this->configObj->glDbObj->addColumn('chr_delete');
        $this->configObj->glDbObj->addColumn('dt_createdate');
        $this->configObj->glDbObj->addColumn('dt_modifydate');
        $this->configObj->glDbObj->addColumn('chr_read');

        $this->configObj->glDbObj->addValue(trim($this->varpostdata['fk_category']));
        $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_title']));
//        $this->configObj->glDbObj->addValue($this->varpostdata['var_alias']);
        $this->configObj->glDbObj->addValue($this->imagename1);
        $this->configObj->glDbObj->addValue($this->imagename);
        if ($this->varpostdata['var_desc'] == 'Y') {
            $this->configObj->glDbObj->addValue($this->varpostdata['text_fulltext']);
        } else {
            $this->configObj->glDbObj->addValue('');
        }
        $d = ($this->varpostdata['body_desc'] == "Y") ? 'Y' : 'N';
        $this->configObj->glDbObj->addValue($d);

        if ($this->varpostdata['body_desc'] == "Y") {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description_body']));
        } else {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['']));
        }

        $d1 = ($this->varpostdata['facial_desc'] == "Y") ? 'Y' : 'N';
        $this->configObj->glDbObj->addValue($d1);

        if ($this->varpostdata['facial_desc'] == "Y") {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description_facial']));
        } else {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['']));
        }

        $d2 = ($this->varpostdata['peels_desc'] == "Y") ? 'Y' : 'N';
        $this->configObj->glDbObj->addValue($d2);

        if ($this->varpostdata['peels_desc'] == "Y") {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['var_description_peels']));
        } else {
            $this->configObj->glDbObj->addValue(trim($this->varpostdata['']));
        }
        $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($this->varpostdata['chr_publish']);
        $this->configObj->glDbObj->addValue('N');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('NOW()');
        $this->configObj->glDbObj->addValue('Y');

        $this->configObj->glDbObj->setWhere('int_glcode=' . $this->varpostdata['eid']);
        $this->configObj->glDbObj->query($this->configObj->glDbObj->buildQuery());
        $this->configObj->glLibraryObj->set_display_order_sequence(DB_PREFIX . 'services');
        $this->configObj->glLibraryObj->insertinlogmanager(90, DB_PREFIX . 'services', 'var_title', $this->varpostdata['eid'], 'U');
        return true;
    }

    function Generateurl() {
        if ($_REQUEST['id'] != '') {
            $id = "&id=" . $_REQUEST['id'] . "";
        } else {
            $id = '';
        }
        $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 . '&categorytype=' . $this->categorytype . '&optval=' . $this->optval . '&brandtype=' . $this->brandtype . '&datetype=' . $this->datetype . '&categoryid=' . $this->categoryid . $id;
        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby . '&categorytype=' . $_REQUEST['categorytype'] . '&datetype=' . $this->datetype . '&categoryid=' . $this->categoryid . $id;
        $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 . '&categorytype=' . $this->categorytype . '&optval=' . $this->optval . '&brandtype=' . $this->brandtype . '&datetype=' . $this->datetype . '&categoryid=' . $this->categoryid . $id;
        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby . '&categorytype=' . $this->categorytype . '&optval=' . $this->optval . '&brandtype=' . $this->brandtype . '&datetype=' . $this->datetype . '&categoryid=' . $this->categoryid . $id;
        $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 . '&history=T' . $homepara . '&datetype=' . $this->datetype . '&categoryid=' . $this->categoryid . $id;
        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&categorytype=' . $this->categorytype . '&optval=' . $this->optval . '&brandtype=' . $this->brandtype . '&datetype=' . $this->datetype . '&categoryid=' . $this->categoryid . $id;
        $this->configObj->AutoSearchUrl = $this->urlwithpara . "&type=autosearch&searchbyVal=" . $this->searchbyVal . '&optval=' . $this->optval . '&brandtype=' . $this->brandtype . '&result=' . $result_date . '&categoryid=' . $this->categoryid . $id;
    }

    function org_generateParam($view = 'services', $actionurl = 'index.php?module=services&action=add', $searchArray = array("var_title" => "Name"), $table = 'DB_PREFIX.services', $position = 'top') {
        $filterPos = ($position == 'top') ? 'FT' : 'FB';
        if ($position == 'top' && $_REQUEST['id'] == '') {
            $catcombo = $this->search_categorycombo();
        }
        if ($_REQUEST['id'] != '') {
            $packageurldata = 'index.php?module=service_category';
        } 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,
            'fronturl' => SITE_PATH . 'index.php?module=ourstaff',
            '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->searchtxtjs,
                //'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,
            ),
            'datetype' => $datetype,
            'topservices' => $topservices,
            'scatcombo' => $catcombo,
        );
    }

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

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

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

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

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

                $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());
            }
        }
    }

    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 is_servicesused() {
        $deleteids = $_REQUEST['dids'];
        $deletearray = explode(',', $deleteids);
        $totaldeletedrecords = count($deletearray);
        $is_assigned = 0;
        $delcount = 0;
        for ($i = 0; $i < $totaldeletedrecords; $i++) {
            $sql = "Update " . DB_PREFIX . "services 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(90, DB_PREFIX . 'services', 'var_title', $deletearray[$i], 'D');
        }
        $this->load_services();
    }

    function loadCount() {
        $this->numofrows = $this->countFunction;
        $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 getbrandname($brandtype) {
        $sel = "select brand from " . DB_PREFIX . "brand_mst where c_id=" . $brandtype . "";
        $result = $this->configObj->glDbObj->get_sql_row($sel);
        $brandname = $result['brand'];
        return $brandname;
    }

    function load_services() {
        if ($_REQUEST['task'] == 'orderupdate') {
            $updateids = $_REQUEST['uids'];
            $updatevalues = $_REQUEST['uvals'];
            $updateoldvalues = $_REQUEST['uoldvals'];
            $upidsarray = explode(',', $updateids);
            $upvalsarray = explode(',', $updatevalues);
            $upoldvalsarray = explode(',', $updateoldvalues);
            $this->update_display_order($upidsarray, $upvalsarray, $upoldvalsarray, 'services');
            $tablename = "" . DB_PREFIX . 'services';
            $this->configObj->glLibraryObj->set_display_order_sequence($tablename);
        }
        if ($_REQUEST['id'] != '') {
            $id = "&id=" . $_REQUEST['id'] . "";
        } else {
            $id = '';
        }
        $this->configObj->glTplObj->set('servicesTab', true);
        $this->pagename = 'index.php?module=services'; // Attribute of Page Name
        $this->countFunction = $this->CountRows();
        $this->loadCount();
        $this->configObj->glTplObj->set('numofrows', $this->numofrows);
        $result = $this->selectall_services();
        $this->configObj->glTplObj->set('result', $result);
        $this->Generateurl();
        if (isset($_REQUEST['id'])) {
            $temp = $_REQUEST['id'];
            $temp1 = " - " . $this->displaycategory($temp);
        }
        $view = 'Manage Services' . $temp1;
        $actionurl = 'index.php?module=services&action=add' . $id;
        $searchArray = array("var_title" => "Name");

        $table = DB_PREFIX . 'services';
        $parmas = $this->org_generateParam($view, $actionurl, $searchArray, $table);
        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas, 'yes'));
        $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 has been successfully saved.");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            } else if ($_REQUEST['msg'] == 'edit') {
                $messagebox = $this->configObj->glLibraryObj->message("The record has been successfully edited and saved.");
                $this->configObj->glTplObj->set('messagebox', $messagebox);
            }
        }
        $notemessagebox = $this->configObj->glLibraryObj->notemessage("Note : ", "Adding services are incremental, once you add a new services it will display as first record and previous services will move down in the order.");
        $this->configObj->glTplObj->set('notemessagebox', $notemessagebox);

        if ($this->configObj->isAjaxSet) {
            echo $this->configObj->glTplObj->process('services.tpl.php');
            exit;
        } else {
            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('services.tpl.php');
        }
    }

    function combo($id = '') {
        $query = "select * from " . DB_PREFIX . "service_category ORDER BY int_displayorder asc";
        $res = $this->configObj->glDbObj->query($query);
        $sel = "<select id='fk_category' name='fk_category' style=\";width:362px\">";
        $sel .= "<option value='0'>Select Services Categories</option>";
        while ($row = $this->configObj->glDbObj->fetch_array($res)) {
            if ($row['int_glcode'] == $id) {
                $selected = "selected";
            } else if ($row['int_glcode'] == $_REQUEST['id']) {
                $selected = "selected";
            } else {
                $selected = '';
            }
            $sel.='<option value="' . $row['int_glcode'] . '" ' . $selected . '>' . $row['var_title'] . '</option>';
        }
        $sel .="</select>";
        return $sel;
    }

    function displaycategory($id) {
        $query = "select var_title from " . DB_PREFIX . "service_category where int_glcode=" . $id;
        $res = $this->configObj->glDbObj->get_sql_row($query);
        return $res['var_title'];
    }

    function search_categorycombo() {
        $query = "select * from " . DB_PREFIX . "service_category where chr_delete='N' ORDER BY int_displayorder asc";
        $res = $this->configObj->glDbObj->query($query);
        $sel = "<select id='var_title1' name='var_title1' style=\"float:left;margin-top: 3px;\" onchange=\"SendGridBindRequest('" . $this->urlwithpara . "','gridbody','search_category_services');\">";
        $sel .= "<option value='0'>Select Services Categories</option>";
        while ($row = $this->configObj->glDbObj->fetch_array($res)) {
            if ($row['int_glcode'] == $this->categoryid) {
                $selected = "selected";
            } else {
                $selected = '';
            }
            $sel.='<option value="' . $row['int_glcode'] . '" ' . $selected . '>' . $row['var_title'] . '</option>';
        }
        $sel .="</select>";
        return $sel;
    }

}

?>