From a9f432451388ddbb662a5ef1823a534e25c31419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 16 Nov 2014 22:18:12 +0100 Subject: [PATCH] Adapt countries to add country code, create command to populate or update countries and translate countries to chill_main.available languages. Fix #317 and fix #316 The LoadCountries Data fixtures are also updated. Both Command and DataFixtures/ORM/LoadCountries use the same static function. [ci skip] --- Command/LoadCountriesCommand.php | 75 +++++++ DataFixtures/ORM/LoadCountries.php | 256 ++-------------------- Entity/Country.php | 46 +++- Resources/config/doctrine/Country.orm.yml | 6 +- 4 files changed, 134 insertions(+), 249 deletions(-) create mode 100644 Command/LoadCountriesCommand.php diff --git a/Command/LoadCountriesCommand.php b/Command/LoadCountriesCommand.php new file mode 100644 index 000000000..2062c5667 --- /dev/null +++ b/Command/LoadCountriesCommand.php @@ -0,0 +1,75 @@ +setName('chill:main:countries:populate') + ->setDescription('Load or update countries in db. This command does not delete existing countries, '. + 'but will update names according to available languages'); + } + + /* + * (non-PHPdoc) + * @see \Symfony\Component\Console\Command\Command::execute() + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $countries = static::prepareCountryList($this->getContainer()->getParameter('chill_main.available_languages')); + $em = $this->getContainer()->get('doctrine.orm.entity_manager'); + + foreach($countries as $country) { + $countryStored = $em->getRepository('ChillMainBundle:Country') + ->findOneBy(array('countryCode' => $country->getCountryCode())); + + if (NULL === $countryStored) { + $em->persist($country); + } else { + $countryStored->setName($country->getName()); + } + } + + $em->flush(); + } + + public static function prepareCountryList($languages) + { + $regionBundle = Intl::getRegionBundle(); + + foreach ($languages as $language) { + $countries[$language] = $regionBundle->getCountryNames($language); + } + + $countryEntities = array(); + + foreach ($countries[$languages[0]] as $countryCode => $name) { + $names = array(); + + foreach ($languages as $language) { + $names[$language] = $countries[$language][$countryCode]; + } + + $country = new \Chill\MainBundle\Entity\Country(); + $country->setName($names)->setCountryCode($countryCode); + $countryEntities[] = $country; + } + + return $countryEntities; + } +} diff --git a/DataFixtures/ORM/LoadCountries.php b/DataFixtures/ORM/LoadCountries.php index a096cec9b..a4e6da740 100644 --- a/DataFixtures/ORM/LoadCountries.php +++ b/DataFixtures/ORM/LoadCountries.php @@ -4,15 +4,28 @@ namespace Chill\MainBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Doctrine\Common\Persistence\ObjectManager; -use Acme\HelloBundle\Entity\UserGroup; +use Chill\MainBundle\Command\LoadCountriesCommand; /** * Load countries into database * * @author Julien Fastré */ -class LoadCountries extends AbstractFixture { +class LoadCountries extends AbstractFixture implements ContainerAwareInterface { + + /** + * + * @var ContainerInterface + */ + private $container; + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } public function getOrder() { return 1001; @@ -22,247 +35,14 @@ class LoadCountries extends AbstractFixture { echo "loading countries... \n"; - foreach ($this->countries as $code => $name) { - $country = new \Chill\MainBundle\Entity\Country(); - $country->setLabel(ucwords($name)); + $languages = $this->container->getParameter('chill_main.available_languages'); + + foreach (LoadCountriesCommand::prepareCountryList($languages) as $country){ $manager->persist($country); } $manager->flush(); } - - public $countries = array( - "ad" => "andorre", - "ae" => "emirats arabes unis", - "af" => "afghanistan", - "ag" => "antigua-et-barbuda", - "ai" => "anguilla", - "al" => "albanie", - "am" => "arménie", - "an" => "antilles", - "ao" => "angola", - "aq" => "antarctique", - "ar" => "argentine", - "as" => "samoa américaines", - "at" => "autriche", - "au" => "australie", - "aw" => "aruba", - "az" => "azerbaïdjan", - "ba" => "bosnie-herzégovine", - "bb" => "barbade", - "bd" => "bangladesh", - "be" => "belgique", - "bg" => "bulgarie", - "bh" => "bahreïn", - "bi" => "burundi", - "bj" => "bénin ", - "bm" => "bermudes", - "bn" => "brunei darussalam", - "bo" => "bolivie", - "br" => "brésil", - "bs" => "bahamas", - "bt" => "bhoutan", - "bw" => "botswana", - "by" => "biélorussie", - "bz" => "belize", - "kh"=>"cambodge", - "ca" => "canada", - "cc" => "iles cocos", - "cd" => "république démocratique du congo", - "cf" => "république centrafricaine", - "cg" => "congo", - "ch" => "suisse", - "ci" => "côte d'ivoire", - "ck" => "iles cook", - "cl" => "chili", - "cm" => "cameroun", - "cn" => "chine", - "co" => "colombie", - "cr" => "costa rica","cu" => "cuba", - "cv" => "cap-vert", - "cx" => "ile christmas", - "cy" => "chypre", - "cz" => "république tchèque", - "de" => "allemagne", - "dj" => "djibouti", - "dk" => "danemark", - "dm" => "dominique", - "do" => "république dominicaine", - "dz" => "algérie", - "ec" => "equateur", - "ee" => "estonie", - "eg" => "egypte", - "eh" => "sahara occidental", - "er" => "erythrée", - "es" => "espagne", - "et" => "ethiopie", - "fi" => "finlande", - "fj" => "fidji", - "fk" => "iles falklands", - "fm" => "micronésie", - "fo" => "ile feroe", - "fr" => "france", - "ga" => "gabon", - "gd" => "grenade", - "ge" => "géorgie", - "gf" => "guyane française", - "gh" => "ghana", - "gi" => "gibraltar", - "gl" => "groënland", - "gq"=>"guinée équatoriale", - "gm" => "gambie", - "gn" => "guinée", - "gp" => "guadeloupe", - "gr" => "grèce", - "gt" => "guatemala", - "gu" => "guam", - "gw" => "guinée-bissao", - "gy" => "guyane", - "hk" => "hong kong", - "hn" => "honduras", - "hr" => "croatie", - "ht" => "haïti", - "hu" => "hongrie", - "id" => "indonésie", - "ie" => "irlande", - "il" => "israël", - "in" => "inde", - "iq" => "iraq", - "ir" => "iran", - "is" => "islande", - "it" => "italie", - "jm" => "jamaïque", - "jo" => "jordanie", - "jp" => "japon", - "ke" => "kenya", - "kg" => "kirghistan", - "bf" => "burkina faso", - "ki" => "kiribati", - "km" => "république comorienne", - "kn" => "saint-christophe-et-niévès", - "kp" => "corée du nord", - "kr" => "corée du sud", - "kw" => "koweït", - "ky" => "iles caïmans", - "kz" => "kazakhstan", - "la" => "laos", - "lb" => "liban", - "lc" => "sainte-lucie", - "li" => "liechtenstein", - "lk" => "sri lanka", - "lr" => "libéria", - "ls" => "lesotho", - "lt" => "lituanie", - "lu" => "luxembourg", - "lv" => "lettonie", - "ly" => "libye", - "ma" => "maroc", - "mc" => "monaco", - "md" => "moldavie", - "mg" => "madagascar", - "ml"=>"mali", - "mh" => "marshall", - "mk" => "macédoine","mm"=>"myanmar", - "mq"=>"martinique", - "mn" => "mongolie", - "mo" => "makau", - "mp" => "ile mariana du nord", - "mr" => "mauritanie", - "ms" => "monteserrat", - "mu" => "maurice", - "mt"=>"malte", - "mv" => "maldives", - "mw" => "malawi", - "mx" => "mexique west", - "my" => "malaisie", - "mz" => "mozambique", - "na" => "namibie", - "nc" => "nouvelle-calédonie", - "ne" => "niger", - "nf" => "ile de norfolk", - "ng" => "nigeria", - "ni" => "nicaragua", - "nl" => "pays-bas", - "no" => "norvège", - "np" => "népal", - "nr" => "nauru", - "nu" => "niue", - "nz" => "nouvelle-zélande", - "om" => "oman", - "pa" => "panama", - "pe" => "pérou", - "pf" => "polynésie française", - "pg" => "papouasie - nouvelle guinée", - "ph" => "philippines", - "pk" => "pakistan", - "pl" => "pologne", - "pm" => "st. pierre and miquelon", - "pn" => "pitcairn", - "pr" => "porto rico", - "ps" => "palestine", - "pt" => "portugal", - "pw" => "palau", - "py" => "paraguay", - "qa" => "qatar", - "re" => "réunion", - "ro" => "roumanie", - "ru" => "fédération russe", - "rw" => "rwanda", - "sa" => "arabie saoudite", - "sb" => "iles salomon", - "sc" => "seychelles", - "sd" => "soudan", - "se" => "suède", - "sg" => "singapour", - "sh" => "saint hélène", - "si" => "slovénie", - "sk" => "slovaquie", - "sl" => "sierra leone", - "sm" => "saint-marin", - "sn" => "sénégal", - "so" => "somalie", - "sr" => "suriname", - "st" => "sao tomé-et-principe", - "sv" => "salvador", - "sy" => "syrie", - "sz" => "swaziland", - "tc" => "turks et caicos", - "td" => "république du tchad", - "tg" => "togo", - "th" => "thaïlande", - "tj" => "tchétchénie", - "tk" => "iles tokelau", - "tm" => "turkménistan", - "tn" => "tunisie", - "to" => "tonga", - "tp" => "timor-oriental", - "tr" => "turquie", - "tt" => "trinité-et-tobago", - "tv" => "tuvalu", - "tw" => "taiwan", - "tz" => "tanzanie", - "ua" => "ukraine", - "ug" => "ouganda", - "gb" => "royaume-uni", - "us" => "etats unis d'amérique", - "uy" => "uruguay", - "uz" => "ousbékistan", - "va" => "vatican", - "vc" => "saint-vincent-et-les grenadines", - "ve" => "vénézuela", - "vg" => "iles vierges américaines", - "vi" => "iles vierges britanniques ", - "vn" => "viêt-nam", - "vu" => "vanuatu", - "wf" => "wallis et futuna", - "ws" => "samoa occidentales", - "ye" => "yémen", - "yt" => "mayotte", - "yu" => "yougoslavie", - "za" => "afrique du sud", - "zm" => "zambie", - "zw" => "zimbabwe" - ); } diff --git a/Entity/Country.php b/Entity/Country.php index e1679e9a1..96e50b4e3 100644 --- a/Entity/Country.php +++ b/Entity/Country.php @@ -17,7 +17,13 @@ class Country /** * @var string */ - private $label; + private $name; + + /** + * + * @var string + */ + private $countryCode; /** @@ -31,29 +37,51 @@ class Country } /** - * Set label + * Set name * - * @param string $label + * @param string $name * @return Country */ - public function setLabel($label) + public function setName($name) { - $this->label = $label; + $this->name = $name; return $this; } /** - * Get label + * Get name * * @return string */ - public function getLabel() + public function getName() { - return $this->label; + return $this->name; } + + public function __toString() { - return $this->getLabel(); + return $this->getName(); } + + /** + * + * @return the string + */ + public function getCountryCode() + { + return $this->countryCode; + } + + /** + * + * @param string $countryCode + */ + public function setCountryCode($countryCode) + { + $this->countryCode = $countryCode; + return $this; + } + } diff --git a/Resources/config/doctrine/Country.orm.yml b/Resources/config/doctrine/Country.orm.yml index fd3c05207..2e7f7ee37 100644 --- a/Resources/config/doctrine/Country.orm.yml +++ b/Resources/config/doctrine/Country.orm.yml @@ -7,7 +7,9 @@ Chill\MainBundle\Entity\Country: id: true generator: strategy: AUTO - label: + name: + type: json_array + countryCode: type: string - length: 255 + length: 3 lifecycleCallbacks: { }