Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Module extends \yii\base\Module
public $tablePrefix = NULL;
public $enableRemote = FALSE;
public $remoteSettings = ['uri' => NULL, 'token' => NULL];
public $defaultLanguage = 'en';

public function init()
{
Expand Down
4 changes: 4 additions & 0 deletions commands/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public function actionCountries($fn = NULL)
public function actionCountryTranslations()
{
$fn = \Yii::getAlias('@vendor') . '/humanized/yii2-location/data/countries/countries.json';

// Check if file exist
if (!file_exists($fn)) throw new Exception('Country JSON file not exists.');

$json = file_get_contents($fn);
$object = json_decode($json);
foreach ($object as $record) {
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
],
"require": {
"yiisoft/yii2": ">=2.0.7",
"humanized/php-locale-helpers": "*"
"humanized/php-locale-helpers": "*",
"kartik-v/yii2-widgets": "*",
"humanized/yii2-translation" : "*",
"humanized/yii2-clihelpers" : "*"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 10 additions & 4 deletions migrations/m130524_201440_init.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace humanized\location\migrations;

use yii\db\Migration;

class m130524_201440_init extends Migration
Expand Down Expand Up @@ -38,7 +40,7 @@ public function safeUp()


$this->addPrimaryKey('pk_country_translation', 'country_translation', ['country_id', 'language_id']);
$this->addForeignKey('fk_country_tanslation_language', 'country_translation', 'language_id', 'language', 'code', 'CASCADE', 'CASCADE');
//$this->addForeignKey('fk_country_tanslation_language', 'country_translation', 'language_id', 'language', 'code', 'CASCADE', 'CASCADE');
$this->addForeignKey('fk_country_tanslation_country', 'country_translation', 'country_id', 'country', 'iso_2', 'CASCADE', 'CASCADE');

/*
Expand All @@ -52,14 +54,14 @@ public function safeUp()
'language_id' => $this->string(5)->notNull(), //The default language for fallback purposes
], $this->tableOptions);

$this->addForeignKey('fk_city_language', 'city', 'language_id', 'language', 'code', 'CASCADE', 'CASCADE');
//$this->addForeignKey('fk_city_language', 'city', 'language_id', 'language', 'code', 'CASCADE', 'CASCADE');
$this->createTable('city_translation', [
'language_id' => $this->string(2)->notNull(),
'city_id' => $this->integer()->notNull(), //ISO-2 Code is considered ID
'name' => $this->string(255)->notNull(),
], $this->tableOptions);
$this->addPrimaryKey('pk_city_translation', 'city_translation', ['city_id', 'language_id']);
$this->addForeignKey('fk_city_translation_language', 'city_translation', 'language_id', 'language', 'code', 'CASCADE', 'CASCADE');
//$this->addForeignKey('fk_city_translation_language', 'city_translation', 'language_id', 'language', 'code', 'CASCADE', 'CASCADE');
$this->addForeignKey('fk_city_translation_city', 'city_translation', 'city_id', 'city', 'id', 'CASCADE', 'CASCADE');


Expand All @@ -84,7 +86,11 @@ public function safeUp()

public function safeDown()
{
$this->dropTable('{{%user}}');
$this->dropTable('location');
$this->dropTable('city_translation');
$this->dropTable('city');
$this->dropTable('country_translation');
$this->dropTable('country');
}

}
8 changes: 5 additions & 3 deletions migrations/m160129_060019_nuts.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace humanized\location\migrations;

use yii\db\Migration;

class m160129_060019_nuts extends Migration
Expand Down Expand Up @@ -44,9 +46,9 @@ public function safeUp()

public function safeDown()
{
echo "m160129_060019_nuts cannot be reverted.\n";

return false;
$this->dropTable('nuts_location');
$this->dropTable('nuts_hierarchy');
$this->dropTable('nuts_code');
}

}
29 changes: 23 additions & 6 deletions models/location/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Country extends \yii\db\ActiveRecord
public $code;
public $common_name;
public $official_name;

protected static $_countries;

/**
* @inheritdoc
Expand Down Expand Up @@ -79,9 +81,12 @@ public function getLocations()

public static function available()
{
$searchModel = (new Country())->_query();
if (!isset(self::$_countries)) {
$searchModel = (new Country())->_query();
self::$_countries = $searchModel->asArray()->all();
}

return $searchModel->asArray()->all();
return self::$_countries;
}

public static function dropdown()
Expand All @@ -94,17 +99,29 @@ public static function enabled()
{

}

protected function getModuleByClassName($class) {
$modules = \Yii::$app->getModules();
foreach ($modules as $id => $module) {
if ($module['class'] == $class) {
return $id;
}
}
}

protected function _query()
{
$query = Country::find();
$currentLanguage = substr(Translation::current(), 0, 2);
$currentLanguage = substr(\Yii::$app->language, 0, 2);

$module = \Yii::$app->getModule($this->getModuleByClassName(\humanized\location\Module::className()));

$local = new Expression("'$currentLanguage'");
$fallbackLanguage = substr(Translation::fallback(), 0, 2);
$fallbackLanguage = substr($module->defaultLanguage, 0, 2);
$fallback = new Expression("'$fallbackLanguage'");

$query->leftJoin('country_translation default_label', "(`country`.`iso_2`=`default_label`.`country_id` AND $fallback=`default_label`.`language_id`)");
$query->leftJoin('country_translation localised_label', "(`country`.`iso_2`=`localised_label`.`country_id` AND $local =`localised_label`.`language_id`)");
$query->leftJoin('country_translation default_label', "(`country`.`iso_2`=`default_label`.`country_id` AND `default_label`.`language_id` = $fallback)");
$query->leftJoin('country_translation localised_label', "(`country`.`iso_2`=`localised_label`.`country_id` AND `localised_label`.`language_id` = $local)");
$query->select = [
'code' => 'iso_2',
'label' => 'CONCAT(IF(localised_label.common_name IS NULL, default_label.common_name,localised_label.common_name),\' (\',iso_2,\')\')',
Expand Down