<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: BusinessController.php, v 1.0 2011/06/14 20:52:11
//
class LocationController extends Controller {
    public function indexAction() {
        $ObjBusiness = new BusinessModel();
        $languageIDs = $this->ObjView->languageIDs;
        $column      = $this->getRequestValue('field');
        $order       = $this->getRequestValue('order');
        if (isset($_POST['businessLocationCheck'])) {
            if (sizeof($_POST['businessLocationCheck']) > 0) {
                $businessLocations = array();
                foreach ($_POST['businessLocationCheck'] as $businessLocID) {
                    array_push($businessLocations, $businessLocID);
                } //$_POST[ 'businessLocationCheck' ] as $businessLocID
                $POST['checkbusinessLocIDs'] = $businessLocations;
                $POST['changeStatus']        = $_POST['statusSelect'];
                $updateBusinessUser          = $ObjBusiness->changeBusinessLocationStatus($POST);
                $this->setMessage("language.lblStatusChanged", "success");
                $this->setRedirect('/location/');
            } //sizeof( $_POST[ 'businessLocationCheck' ] ) > 0
        } //$_POST[ 'businessLocationCheck' ]
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resBusinessLocationDetails = $ObjBusiness->listBusinessLocations($limitFrom, $limitTo, '', $this->ObjSession->getSession("BUSINESS_USER_ID"), $languageIDs, $column, $order);
        $resPagesCount              = $this->setPagesCount($key = 'page');
        $url                        = $this->getSettingValue('site_settings', 'global_http_url');
        $this->ObjView->assign("column", $column);
        $this->ObjView->assign("order", $order == 'ds' ? 'desc' : 'asc');
        $this->ObjView->assign("url", $url);
        $this->ObjView->assign("resBusinessLocationDetails", $resBusinessLocationDetails);
        $this->ObjView->assign("record", count($resBusinessLocationDetails));
        $this->ObjView->display('BusinessLocations.tpl');
    }
    //FUNCTION to add Business Locations
    public function newAction() {
        $ObjCountry  = new CountryModel();
        $ObjBusiness = new BusinessModel();
        $languageIDs = $this->ObjView->languageIDs;
        if (isset($_POST['submitBusinessLocation'])) {
            if ($_POST['stateSelectedType'] == 'state') {
                $_POST['locProvince'] = '-';
            }
        }
        if ($this->ObjForm->isValid('frmBusinessLocation', 'submitBusinessLocation')) {
            $_POST['businessID'] = $this->ObjSession->getSession("BUSINESS_USER_ID");
            $resBusinessLocation = $ObjBusiness->saveBusinessLocation($languageIDs, $_POST);
            $this->setMessage("language.lblLocationAdded", "success");
            $this->setRedirect('location/');
        } //$this->ObjForm->isValid( 'frmBusinessLocation', 'submitBusinessLocation' )
        $resListCountryOptions = $ObjCountry->listCountries($languageIDs, '');
        $this->ObjView->assign('resCountry', $resListCountryOptions);
          $resCountryCode= $ObjCountry->getAllCountryCode($languageIDs);
        $this->ObjView->assign('resCountryCode', $resCountryCode);
        $this->ObjView->assign("businessLocID", 0);
        if (isset($_POST['submitBusinessLocation'])) {
            if ($_POST['stateSelectedType'] == 'state') {
                $_POST['locProvince'] = '';
            }
        }
        //--------GOOGLE MAP-----------	
        $ObjCommon                 = new CommonModel();
        $settings                  = array();
        $settings['mapContainer']  = 'city_add_form_map';
        $settings['zoom']          = 14;
        $settings['latLng']        = array(
            '10.0151582',
            '76.3130259'
        );
        $settings['latLngTextBox'] = array(
            'locLatitude',
            'locLongitude'
        );
        $settings['address']       = array(
            'location' => 'locName',
            'address1' => 'locAddress1',
            'address2' => 'locAddress2',
            'city' => 'locCity',
            'state' => 'locStateID',
            'province' => 'locProvince',
            'country' => 'locCountryID'
        );
        $settings['changeTags']    = array(
            'locName',
            'locCity'
        );
        $settings['isclickable']   = 'true';
        $settings['title']         = "";
        $settings['info']          = "";
        $ObjCommon->addMarker($settings);
        $this->ObjView->assign("map", $ObjCommon->showmap());
        //--------END OF GOOGLE MAP----
        $this->ObjView->display('BusinessLocationAdd.tpl');
    }
    //END of FUNCTION locationAction
    //FUNCTION to edit & fetch the business location 
    public function editAction() {
        $ObjBusiness = new BusinessModel();
        $ObjCountry  = new CountryModel();
        $businessID  = $this->ObjSession->getSession("BUSINESS_USER_ID");
        $languageIDs = $this->ObjView->languageIDs;
        $resCountryCode= $ObjCountry->getAllCountryCode($languageIDs);
        $this->ObjView->assign('resCountryCode', $resCountryCode);
        if (isset($_POST['submitBusinessLocation'])) {
            if ($_POST['stateSelectedType'] == 'state') {
                $_POST['locProvince'] = '-';
            }
        }
        if ($this->ObjForm->isValid('frmBusinessLocation', 'submitBusinessLocation')) {
            $_POST['businessID']    = $businessID;
            $_POST['businessLocID'] = $this->getRequestValue('locid');
            $resBusinessLocation    = $ObjBusiness->saveBusinessLocation($languageIDs, $_POST);
            $this->setMessage("language.lblLocationUpdate", "success");
            $this->setRedirect('/location/');
        }
        $busUserLocation = $ObjBusiness->getBusinessLocationDetails('', $this->getRequestValue('locid'), $languageIDs, $businessID);
        if(count($busUserLocation)>0){
            $phone=explode("-",$busUserLocation['locPhone']);
            $userCountrycode=$phone[0];
            $busUserLocation['locPhone']=$phone[1];
        }
         $this->ObjView->assign("userCountrycode", isset($userCountrycode)?$userCountrycode:'');
        if (is_bool($busUserLocation) === true)
            $this->setRedirect('location/');
        else {
            $resListCountryOptions = $ObjCountry->listCountries($languageIDs, '');
            $this->ObjView->assignValues($busUserLocation);
            $this->ObjView->assign('locState', $busUserLocation['locStateID']);
            $this->ObjView->assign('stateID', $busUserLocation['locStateID']);
            $this->ObjView->assign('resBusinessUsers', isset($resBusinessUsers) ? $resBusinessUsers : '');
            $this->ObjView->assign('resCountry', $resListCountryOptions);
            if (isset($_POST['submitBusinessLocation'])) {
                if ($_POST['stateSelectedType'] == 'state') {
                    $_POST['locProvince'] = '';
                }
            }
            if (count($busUserLocation)) {
                //--------GOOGLE MAP-----------	
                $ObjCommon                 = new CommonModel();
                $settings                  = array();
                $settings['mapContainer']  = 'city_add_form_map';
                $settings['zoom']          = 14;
                $settings['latLng']        = array(
                    $busUserLocation['locLatitude'],
                    $busUserLocation['locLongitude']
                );
                $settings['latLngTextBox'] = array(
                    'locLatitude',
                    'locLongitude'
                );
                $settings['address']       = array(
                    'location' => 'locName',
                    'address1' => 'locAddress1',
                    'address2' => 'locAddress2',
                    'city' => 'locCity',
                    'state' => 'locStateID',
                    'province' => 'locProvince',
                    'country' => 'locCountryID'
                );
                $settings['changeTags']    = array(
                    'locName',
                    'locCity'
                );
                $settings['isclickable']   = 'true';
                $settings['title']         = "";
                $settings['info']          = "";
                $ObjCommon->addMarker($settings);
                $this->ObjView->assign("map", $ObjCommon->showmap());
                //--------END OF GOOGLE MAP----
                $this->ObjView->display('BusinessLocationAdd.tpl');
            } else
                $this->setRedirect('location/');
        }
    }
    //END of FUNCTION editLocationAction
    //FUNCTION to delete the Business Location
    public function destroyAction() {
        $ObjBusiness                 = new BusinessModel();
        $POST['checkbusinessLocIDs'] = array(
            $this->getRequestValue('locid')
        );
        $POST['changeStatus']        = 'Deleted';
        $delBusinessUser             = $ObjBusiness->changeBusinessLocationStatus($POST);
        $this->setMessage("language.lblLocationDelete", "success");
        $this->setRedirect('/location/');
    }
    //END of FUNCTION deleteBusinessLocationAction
    //FUNCTION to lsit the STATES with AJAX
    public function ajaxStateAction() {
        $objState    = new StateModel();
        $countryID   = $_POST['countryID'];
        $languageIDs = $this->ObjView->languageIDs;
        $stateID     = $_POST['stateID'];
        if ($countryID > 0) {
            $statesList = $objState->listStates($languageIDs, $countryID);
        } //$countryID > 0
        $this->ObjView->assign('stateID', $stateID);
        $this->ObjView->assign('statesList', isset($statesList) ? $statesList : '');
        $this->ObjView->display('CityState.tpl');
    }
    //END OF FUNCTION ajaxStateAction
    //FUNCTION to change the businessLocationStatus
    public function changeStatusAction() {
        $ObjBusiness                 = new BusinessModel();
        $POST['checkbusinessLocIDs'] = array(
            $_POST['locid']
        );
        $POST['changeStatus']        = $_POST['locStatus'];
        $updateBusinessUser          = $ObjBusiness->changeBusinessLocationStatus($POST);
    }
    //END of function businessLocationStatus
}