Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Mon, Feb 10, 10:22 PM

in-portal

Index: branches/5.2.x/core/units/country_states/country_state_eh.php
===================================================================
--- branches/5.2.x/core/units/country_states/country_state_eh.php (revision 14819)
+++ branches/5.2.x/core/units/country_states/country_state_eh.php (revision 14820)
@@ -1,117 +1,146 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class CountryStateEventHandler extends kDBEventHandler {
+ function mapPermissions()
+ {
+ parent::mapPermissions();
+
+ $permissions = Array (
+ 'OnGetStatesJSON' => Array ('self' => true),
+ );
+
+ $this->permMapping = array_merge($this->permMapping, $permissions);
+ }
+
/**
* Applies edit picker filters
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
if (($event->Special == 'selected') || ($event->Special == 'available')) {
$edit_picker_helper =& $this->Application->recallObject('EditPickerHelper');
/* @var $edit_picker_helper EditPickerHelper */
$edit_picker_helper->applyFilter($event, 'Countries');
// only countries
$object->addFilter('type_filter', '%1$s.Type = ' . DESTINATION_TYPE_COUNTRY);
}
}
/**
* Makes sure, that state country is always specified
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$this->_setRequired($event);
}
/**
* Makes sure, that state country is always specified
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$this->_setRequired($event);
}
/**
* Makes sure, that state country is always specified
*
* @param kEvent $event
*/
function _setRequired(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$field_options = $object->GetFieldOptions('IsoCode');
if ($object->GetDBField('Type') == DESTINATION_TYPE_STATE) {
$object->setRequired('StateCountryId');
$field_options['unique'] = Array ('Type', 'StateCountryId');
}
else {
$object->setRequired('StateCountryId', false);
$field_options['unique'] = Array ('Type');
}
$object->SetFieldOptions('IsoCode', $field_options);
}
/**
* Don't allow to delete countries, that have states
*
* @param kEvent $event
*/
function OnBeforeItemDelete(&$event)
{
parent::OnBeforeItemDelete($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
/* @var $cs_helper kCountryStatesHelper */
if ($cs_helper->CountryHasStates( $object->GetDBField('IsoCode') )) {
$event->status = kEvent::erFAIL;
return ;
}
}
+
+ /**
+ * Returns given country states in JSON format
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnGetStatesJSON(kEvent &$event)
+ {
+ $event->status = kEvent::erSTOP;
+
+ $cs_helper =& $this->Application->recallObject('CountryStatesHelper');
+ /* @var $cs_helper kCountryStatesHelper */
+
+ $states = $cs_helper->getStates( (string)$this->Application->GetVar('country_iso') );
+ echo json_encode($states);
+ }
}
Index: branches/5.2.x/core/units/helpers/country_states_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/country_states_helper.php (revision 14819)
+++ branches/5.2.x/core/units/helpers/country_states_helper.php (revision 14820)
@@ -1,205 +1,223 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kCountryStatesHelper extends kHelper
{
/**
* Returns countries, that have states
*
* @return Array
*/
function getCountriesWithStates()
{
static $cache = null;
if (!isset($cache)) {
$table_name = $this->Application->getUnitOption('country-state', 'TableName');
$sql = 'SELECT DISTINCT cname.IsoCode, cid.StateCountryId
FROM ' . $table_name . ' cid
JOIN ' . $table_name . ' cname ON cname.CountryStateId = cid.StateCountryId
WHERE cid.StateCountryId IS NOT NULL';
$cache = $this->Conn->GetCol($sql, 'StateCountryId');
}
return $cache;
}
/**
* Checks, that country with given 3symbol ISO code has states
*
* @param string $country_code
* @return bool
*/
function CountryHasStates($country_code)
{
return $country_code ? in_array($country_code, $this->getCountriesWithStates()) : false;
}
/**
* Prepares states dropdown based on country selected
*
* @param kEvent $event
* @param string $state_field
* @param string $country_field
*/
function PopulateStates(&$event, $state_field, $country_field)
{
static $cache = Array ();
$object =& $event->getObject();
/* @var $object kDBItem */
$country_iso = $object->GetDBField($country_field);
if (!$country_iso) {
return ;
}
if (!array_key_exists($country_iso, $cache)) {
- $country_id = $this->getCountryStateId($country_iso, DESTINATION_TYPE_COUNTRY);
+ $states = $this->getStates($country_iso);
- if (!$country_id) {
- return ;
+ if ( !$states ) {
+ return;
}
- // don't use GetVar('m_lang') since it's always equals to default language on editing form in admin
- $current_language = $this->Application->Phrases->LanguageId;
- $primary_language = $this->Application->GetDefaultLanguageId();
-
- $sql = 'SELECT IF(l' . $current_language . '_Name = "", l' . $primary_language . '_Name, l' . $current_language . '_Name) AS Name, IsoCode
- FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
- WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = ' . $country_id . ')
- ORDER BY Name ASC';
- $cache[$country_iso] = $this->Conn->GetCol($sql, 'IsoCode');
+ $cache[$country_iso] = $states;
}
$field_options = $object->GetFieldOptions($state_field);
$field_options['options'] = $cache[$country_iso];
$field_options['options'][''] = '';
$object->SetFieldOptions($state_field, $field_options);
}
/**
+ * Returns list of given country states
+ *
+ * @param string $country_iso
+ * @return Array
+ */
+ public function getStates($country_iso)
+ {
+ $country_id = $this->getCountryStateId($country_iso, DESTINATION_TYPE_COUNTRY);
+
+ if ( !$country_id ) {
+ return Array ();
+ }
+
+ // don't use GetVar('m_lang') since it's always equals to default language on editing form in admin
+ $current_language = $this->Application->Phrases->LanguageId;
+ $primary_language = $this->Application->GetDefaultLanguageId();
+
+ $sql = 'SELECT IF(l' . $current_language . '_Name = "", l' . $primary_language . '_Name, l' . $current_language . '_Name) AS Name, IsoCode
+ FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
+ WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = ' . $country_id . ')
+ ORDER BY Name ASC';
+
+ return $this->Conn->GetCol($sql, 'IsoCode');
+ }
+
+ /**
* Returns valid state ISO code for state name and country code passed
*
* @param string $state_name
* @param string $country_iso
* @return string
*/
function getStateIso($state_name, $country_iso)
{
if (!$this->CountryHasStates($country_iso)) {
return $state_name;
}
$table_name = $this->Application->getUnitOption('country-state', 'TableName');
$country_id = $this->getCountryStateId($country_iso, DESTINATION_TYPE_COUNTRY);
// don't use GetVar('m_lang') since it's always equals to default language on editing form in admin
$current_language = $this->Application->Phrases->LanguageId;
$primary_language = $this->Application->GetDefaultLanguageId();
$sql = 'SELECT IsoCode
FROM ' . $table_name . '
WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = %1$s) AND
(
(IsoCode = %2$s) OR (UPPER(l%3$s_Name) = %2$s) OR (UPPER(l%4$s_Name) = %2$s)
)';
$state_name = trim( mb_strtoupper($state_name) );
$sql = sprintf($sql, $country_id, $this->Conn->qstr($state_name), $current_language, $primary_language);
return $this->Conn->GetOne($sql);
}
/**
* Checks, that entered state matches entered country
*
* @param kEvent $event
* @param string $state_field
* @param string $country_field
* @param bool $auto_required
* @return void
*/
function CheckStateField(&$event, $state_field, $country_field, $auto_required = true)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$country_iso = $object->GetDBField($country_field);
if ( $auto_required ) {
$object->setRequired($state_field, $this->CountryHasStates($country_iso));
}
$state = $object->GetDBField($state_field);
if ( $country_iso && $state ) {
$state_iso = $this->getStateIso($state, $country_iso);
if ( $state_iso !== false ) {
// replace state name with it's ISO code
$object->SetDBField($state_field, $state_iso);
}
else {
// state not found by name -> report error
$object->SetError($state_field, 'invalid_state', 'la_invalid_state');
}
}
}
/**
* Returns country/state id based on given iso code and it's type
*
* @param string $iso_code
* @param int $type
* @return int
*/
function getCountryStateId($iso_code, $type)
{
$sql = 'SELECT ' . $this->Application->getUnitOption('country-state', 'IDField') . '
FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
WHERE (Type = ' . $type . ') AND (IsoCode = ' . $this->Conn->qstr($iso_code) . ')';
return (int)$this->Conn->GetOne($sql);
}
/**
* Returns 3 symbols ISO code from 2 symbols ISO code or otherwise, when $from_short parameter is used
*
* @param string $iso_code
* @param bool $from_short
* @return string
*/
function getCountryIso($iso_code, $from_short = false)
{
if ($from_short) {
$sql = 'SELECT IsoCode
FROM ' . TABLE_PREFIX . 'CountryStates
WHERE ShortIsoCode = ' . $this->Conn->qstr($iso_code) . ' AND `Type` = ' . DESTINATION_TYPE_COUNTRY;
}
else {
$sql = 'SELECT ShortIsoCode
FROM ' . TABLE_PREFIX . 'CountryStates
WHERE IsoCode = ' . $this->Conn->qstr($iso_code) . ' AND `Type` = ' . DESTINATION_TYPE_COUNTRY;
}
return $this->Conn->GetOne($sql);
}
}
\ No newline at end of file

Event Timeline