<?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: StateController.php, v 1.0 2011/06/14 20:52:11
//
class StateController extends Controller {
    public function indexAction() {
        //$defaultCountry=1;
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $ObjCountry             = new CountryModel();
        $ObjState               = new StateModel();
        $countryListID          = $this->getRequestValue('country');
        $countryName            = $this->getRequestValue('countryName');
        $stateName              = $this->getRequestValue('city');
        $this->ObjView->assign("countryName", $countryName);
        $this->ObjView->assign("stateName", $stateName);
        if (isset($_POST['submitCountrySearch'])) {
            $countryID = $_POST['countries'];
            $this->setRedirect("state/index/country/$countryID/");
            $countryID      = $_POST['countries'];
            $stateName      = $this->encodeUrlString(trim($_POST['stateName']));
            $linkRedirect   = "state/index/country/$countryID/";
            if($stateName!='') {
                $linkRedirect .= 'city/'.$stateName.'/';
            }
            $this->setRedirect($linkRedirect);            
        }
        if (isset($_POST['submitBulkStatus'])) {
            $status    = $_POST['stateStatus'];
            $countryID = $_POST['countryID'];
            if ($status) {
                $rsStates = $ObjState->changeStateStatus($_POST, $status);
                $this->setMessage("language.msgStatus", 3);
                $this->setRedirect("state/index/country/$countryID/");
            }
        }
        if ($this->ObjForm->isValid('formState', 'submitState')) {
            $countryName = $_POST['hiddenStatesUnderCountry'];
            $countryID   = $_POST['countryID'];
            $rsStates    = $ObjState->saveStateDetails($languageIDs, $_POST);
            $this->setMessage("language.msgState", 3);
            $this->setRedirect();
        }
        $rsCountries    = $ObjCountry->listCountries($languageIDs, "", $limitFrom = "", $limitTo = "", 'ASC', $keyword = "", $stateCount = "No", $addressFormat = "No", $priorityType = "All");
        $defaultCountry = $rsCountries[0]['countryID'];
        $countryID      = ($countryListID) ? $countryListID : $defaultCountry;
        $link           = 'state/index/country/' . $countryID . '/';
        $page           = $this->getRequestValue('page');
        if ($page)
            $link .= "page/" . $page . "/";
        $languageIDs['Current'] = $languageIDs['Content'];
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $rsStates      = $ObjState->listStates($languageIDs, $countryID, '', '', '', $limitFrom, $limitTo,$stateName);
        $resPagesCount = $this->setPagesCount($key = 'page');
        $this->ObjView->assign("rsCountries", $rsCountries);
        $this->ObjView->assign("selCountry", $countryID);
        $this->ObjView->assign("rsStates", $rsStates);
        $this->ObjView->assign("link", $link);
        $this->ObjView->assign("cid", isset($rsStates[0]['countryID']) ? $rsStates[0]['countryID'] : '');
        $this->ObjView->assign("editPrivilege", $this->ObjCommon->hasPrivilege('state/edit/'));
        $this->ObjView->assign("deletePrivilege", $this->ObjCommon->hasPrivilege('state/destroy/'));
        if ($countryName) {
            $this->ObjView->assign("rsCountryName", $countryName);
            $this->ObjView->display('StatesUnderCountry.tpl');
        } else {
            $this->ObjView->display('State.tpl');
        }
    }
    public function editAction() {
        //$defaultCountry=1;
        $languageIDs            = $this->ObjView->languageIDs;
        $ObjState               = new StateModel();
        $stateID                = $this->getRequestValue('id');
        $countryID              = $this->getRequestValue('countryid');
        $languageIDs['Current'] = $languageIDs['Content'];
        $resStates              = $ObjState->getStateDetails($languageIDs, $stateID);
        if (isset($countryName))
            $this->ObjView->assign("countryName", $countryName);
        if ($this->ObjForm->isValid('formState', 'submitState')) {
            $_POST['countryID'] = $countryID;
            $_POST['stateID']   = $stateID;
            $rsStates           = $ObjState->saveStateDetails($languageIDs, $_POST);
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            $this->setMessage("language.msgState", 3);
            exit;
        }
        $this->ObjView->assign("countryID", $countryID);
        $this->ObjView->assign("stateID", $stateID);
        $this->ObjView->assignValues($resStates);
        $this->ObjView->display('StateEdit.tpl');
    }
    public function newAction() {
        //$defaultCountry=1;
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $ObjState               = new StateModel();
        $countryID              = $this->getRequestValue('countryid');
        if ($this->ObjForm->isValid('formState', 'submitState')) {
            $_POST['countryID'] = $countryID;
            $rsStates           = $ObjState->saveStateDetails($languageIDs, $_POST);
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            $this->setMessage("language.msgState", 3);
            exit;
        }
        $this->ObjView->assign("countryID", $countryID);
        //$this->ObjView->assign("stateID",$stateID);
        $this->ObjView->display('StateEdit.tpl');
    }
    // CHECK FOR STATE EXISTS
    public function ajaxStateExistAction() {
        $ObjState               = new StateModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $countryID              = $_POST['countryID'];
        $stateName              = isset($_POST['stateName']) ? trim($_POST['stateName']) : '';
        $stateShortName         = isset($_POST['stateShortName']) ? trim($_POST['stateShortName']) : '';
        $stateID                = $_POST['stateID'];
        $check                  = $ObjState->getStatesCount($languageIDs, $countryID, $stateName, $stateShortName, $stateID);
        return $check;
    }
    // For State Status Change
    public function ajaxstatusAction() {
        $ObjState                 = new StateModel();
        $_POST['stateBulkStatus'] = array(
            $_POST['sid']
        );
        $status                   = $_POST['newstatus'];
        $rsStates                 = $ObjState->changeStateStatus($_POST, $status);
        $this->setMessage("language.msgStatus", 3);
        if ($rsStates)
            echo 1;
    }
    //DELETE STATE 
    public function destroyAction() {
        $ObjState  = new StateModel();
        $stateID   = $this->getRequestValue('id');
        $page      = $this->getRequestValue('page');
        $countryID = $this->getRequestValue('country');
        $rsStates  = $ObjState->destroyStateDetails($stateID);
        $this->setMessage("language.msgStateDelete", 3);
        $link = "state/index/country/$countryID/";
        if ($page)
            $link .= "page/" . $page . "/";
        $this->setRedirect($link);
    }
}