<?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: CountryController.php, v 1.0 2011/06/14 20:52:11
//
class CountryController extends Controller {
    public function indexAction() {
        $ObjCountry              = new CountryModel();
        $languageIDs             = $this->ObjView->languageIDs;
        $languageIDs['Current']  = $languageIDs['Content'];
        $keyword                 = $this->getRequestValue("keyword");
        $resListCountryAlphabets = $ObjCountry->listCountryAlphabets($languageIDs);
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resListCountries = $ObjCountry->listCountries($languageIDs, "", $limitFrom, $limitTo, 'ASC', $keyword, $stateCount = "Yes", $addressFormat = "Yes");
        $resPagesCount    = $this->setPagesCount($key = 'page');
        if (isset($_POST['submitBulkStatus']) && count($_POST['countryBulkStatus']) != 0) {
            $resCountryStatus = $ObjCountry->changeCountryStatus($_POST);
            $this->setMessage("language.msgStatus", 3);
            $this->setRedirect();
        }
        $this->ObjView->assign('keyword', $keyword);
        $this->ObjView->assign('resListCountryAlphabets', $resListCountryAlphabets);
        $this->ObjView->assign('resListCountries', $resListCountries);
        $this->ObjView->assign("editPrivilege", $this->ObjCommon->hasPrivilege('country/edit/'));
        $this->ObjView->assign("stateprivilage", $this->ObjCommon->hasPrivilege('state/index/'));
        $this->ObjView->display('Country.tpl');
    }
    public function editAction() {
        $ObjCountry             = new CountryModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $countryID              = $this->getRequestValue("id");
        $resCountry             = $ObjCountry->getCountryDetails($countryID, $languageIDs);
        $resListAddresFormat    = $ObjCountry->listCountryAddressFormat();
        if ($this->ObjForm->isValid('formCountrySubmit', 'submitCountryEdit')) {
            $resSaveCountry = $ObjCountry->saveCountry($languageIDs, $_POST, $countryID);
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            $this->setMessage("language.msgCountry", 3);
            exit;
        }
        $this->ObjView->assign('contentlanguageID', $languageIDs['Current']);
        $this->ObjView->assign('countryID', $countryID);
        //$this->ObjView->assign('resListCountryAlphabets', $resListCountryAlphabets);
        $this->ObjView->assign('resListAddresFormat', $resListAddresFormat);
        $this->ObjView->assign($resCountry);
        $this->ObjView->display('CountryEdit.tpl');
    }
    //TO SET PRIORITY
    public function priorityAction() {
        $ObjCountry             = new CountryModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $limitFrom              = isset($limitFrom) ? $limitFrom : '';
        $limitTo                = isset($limitTo) ? $limitTo : '';
        $keyword                = isset($keyword) ? $keyword : '';
        $resListCountryLeft     = $ObjCountry->listCountries($languageIDs, "Yes", $limitFrom, $limitTo, 'ASC', $keyword, $stateCount = "No", $addressFormat = "No", $priorityType = "NotAssined");
        $resListCountryRight    = $ObjCountry->listCountries($languageIDs, "Yes", $limitFrom, $limitTo, 'ASC', $keyword, $stateCount = "No", $addressFormat = "No", $priorityType = "Assined");
        $this->ObjView->assign('resListCountryLeft', $resListCountryLeft);
        $this->ObjView->assign('resListCountryRight', $resListCountryRight);
        $this->ObjView->display('CountryPriority.tpl');
    }
    //END OF FUNCTION
    //TO SET PRIORITY  USING AJAX
    public function ajaxPriorityAction() {
        $ObjCountry         = new CountryModel();
        $priorityString     = $this->decodeSplChar($this->escape($_POST['ajax_request'][0], TRUE, FALSE));
        $resPriorityCountry = $ObjCountry->changeCountryPriority($priorityString);
    }
    //END OF FUNCTION
    //FUNCTION TO CHECK COUNTRY EXISTS
    public function ajaxCountryExistAction() {
        $ObjCountry       = new CountryModel();
        $languageIDs      = $this->ObjView->languageIDs;
        $countryID        = $_POST['countryID'];
        $countryName      = $_POST['countryName'];
        $countryIsoA2     = $_POST['countryIsoA2'];
        $countryIsoA3     = $_POST['countryIsoA3'];
        $countryIsoNumber = $_POST['countryIsoNumber'];
        $check            = $ObjCountry->getCountryCount($languageIDs, $countryID, $countryName, $countryIsoA2, $countryIsoA3, $countryIsoNumber);
        return $check;
    }
    //END OF FUNCTION
    public function ajaxstatusAction() {
        $ObjCountry                 = new CountryModel();
        $history                    = $this->getPreviousURL();
        $_POST['countryBulkStatus'] = array(
            $_POST['id']
        );
        $_POST['countryStatus']     = $_POST['newstatus'];
        $resCountryStatus           = $ObjCountry->changeCountryStatus($_POST);
        $this->setMessage("language.msgStatus", 3);
        if ($resCountryStatus)
            echo 1;
    }
}