diff --git a/DataFixtures/ORM/LoadPostalCodes.php b/DataFixtures/ORM/LoadPostalCodes.php new file mode 100644 index 000000000..63d0de9ef --- /dev/null +++ b/DataFixtures/ORM/LoadPostalCodes.php @@ -0,0 +1,106 @@ +, + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace Chill\MainBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\AbstractFixture; +use Doctrine\Common\DataFixtures\OrderedFixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Chill\MainBundle\Entity\PostalCode; + +/** + * Description of LoadPostalCodes + * + * @author Julien Fastré + * @author Champs Libres + */ +class LoadPostalCodes extends AbstractFixture implements OrderedFixtureInterface +{ + public function getOrder() + { + return 50; + } + + public static $refs = array(); + + public function load(ObjectManager $manager) + { + $lines = str_getcsv(self::$codes, "\n"); + $belgium = $manager->getRepository('ChillMainBundle:Country') + ->findOneBy(array('countryCode' => 'BE')); + + foreach($lines as $line) { + $code = str_getcsv($line); + $c = new PostalCode(); + $c->setCountry($belgium) + ->setCode($code[0]) + ->setName(implode(' - ', array( + ucwords(strtolower($code[1])), strtoupper($code[2]), + ))); + $manager->persist($c); + $ref = 'postal_code_'.$code[0]; + + if (! $this->hasReference($ref)) { + $this->addReference($ref, $c); + self::$refs[] = $ref; + } + } + + $manager->flush(); + } + + private static $codes = <<id; + } + + /** + * Set streetAddress1 + * + * @param string $streetAddress1 + * + * @return Address + */ + public function setStreetAddress1($streetAddress1) + { + $this->streetAddress1 = $streetAddress1; + + return $this; + } + + /** + * Get streetAddress1 + * + * @return string + */ + public function getStreetAddress1() + { + return $this->streetAddress1; + } + + /** + * Set streetAddress2 + * + * @param string $streetAddress2 + * + * @return Address + */ + public function setStreetAddress2($streetAddress2) + { + $this->streetAddress2 = $streetAddress2; + + return $this; + } + + /** + * Get streetAddress2 + * + * @return string + */ + public function getStreetAddress2() + { + return $this->streetAddress2; + } + + /** + * Set postcode + * + * @param \Chill\MainBundle\Entity\PostalCode $postcode + * + * @return Address + */ + public function setPostcode(\Chill\MainBundle\Entity\PostalCode $postcode = null) + { + $this->postcode = $postcode; + + return $this; + } + + /** + * Get postcode + * + * @return \Chill\MainBundle\Entity\PostalCode + */ + public function getPostcode() + { + return $this->postcode; + } + + /** + * + * @return \DateTime + */ + public function getValidFrom() + { + return $this->validFrom; + } + + /** + * + * @param \DateTime $validFrom + * @return \Chill\MainBundle\Entity\Address + */ + public function setValidFrom(\DateTime $validFrom) + { + $this->validFrom = $validFrom; + return $this; + } + + +} + diff --git a/Entity/PostalCode.php b/Entity/PostalCode.php new file mode 100644 index 000000000..41143a6e6 --- /dev/null +++ b/Entity/PostalCode.php @@ -0,0 +1,113 @@ +id; + } + + /** + * Set name + * + * @param string $name + * + * @return PostalCode + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Set code + * + * @param string $code + * + * @return PostalCode + */ + public function setCode($code) + { + $this->code = $code; + + return $this; + } + + /** + * Get code + * + * @return string + */ + public function getCode() + { + return $this->code; + } + + /** + * Set country + * + * @param \Chill\MainBundle\Entity\Country $country + * + * @return PostalCode + */ + public function setCountry(\Chill\MainBundle\Entity\Country $country = null) + { + $this->country = $country; + + return $this; + } + + /** + * Get country + * + * @return \Chill\MainBundle\Entity\Country + */ + public function getCountry() + { + return $this->country; + } +} + diff --git a/Resources/config/doctrine/Address.orm.yml b/Resources/config/doctrine/Address.orm.yml new file mode 100644 index 000000000..5ecc8a627 --- /dev/null +++ b/Resources/config/doctrine/Address.orm.yml @@ -0,0 +1,22 @@ +Chill\MainBundle\Entity\Address: + type: entity + table: chill_main_address + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + streetAddress1: + type: string + length: 255 + streetAddress2: + type: string + length: 255 + validFrom: + type: date + manyToOne: + postcode: + targetEntity: Chill\MainBundle\Entity\PostalCode + lifecycleCallbacks: { } diff --git a/Resources/config/doctrine/PostalCode.orm.yml b/Resources/config/doctrine/PostalCode.orm.yml new file mode 100644 index 000000000..09381d465 --- /dev/null +++ b/Resources/config/doctrine/PostalCode.orm.yml @@ -0,0 +1,21 @@ +Chill\MainBundle\Entity\PostalCode: + type: entity + table: chill_main_postal_code + id: + id: + type: integer + id: true + generator: + strategy: AUTO + fields: + name: + type: string + length: 255 + column: label + code: + type: string + length: 100 + manyToOne: + country: + targetEntity: Chill\MainBundle\Entity\Country + lifecycleCallbacks: { } diff --git a/Resources/migrations/Version20160310122322.php b/Resources/migrations/Version20160310122322.php new file mode 100644 index 000000000..566dc7f79 --- /dev/null +++ b/Resources/migrations/Version20160310122322.php @@ -0,0 +1,66 @@ +abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('CREATE SEQUENCE chill_main_address_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE SEQUENCE chill_main_postal_code_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE chill_main_address (' + . 'id INT NOT NULL, ' + . 'postcode_id INT DEFAULT NULL, ' + . 'streetAddress1 VARCHAR(255) NOT NULL, ' + . 'streetAddress2 VARCHAR(255) NOT NULL, ' + . 'validFrom DATE NOT NULL, ' + . 'PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_165051F6EECBFDF1 ON chill_main_address ' + . '(postcode_id)'); + $this->addSql('CREATE TABLE chill_main_postal_code (' + . 'id INT NOT NULL, ' + . 'country_id INT DEFAULT NULL, ' + . 'label VARCHAR(255) NOT NULL, ' + . 'code VARCHAR(100) NOT NULL, ' + . 'PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_6CA145FAF92F3E70 ON chill_main_postal_code ' + . '(country_id)'); + $this->addSql('ALTER TABLE chill_main_address ADD CONSTRAINT ' + . 'FK_165051F6EECBFDF1 ' + . 'FOREIGN KEY (postcode_id) ' + . 'REFERENCES chill_main_postal_code (id) ' + . 'NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_main_postal_code ADD CONSTRAINT ' + . 'FK_6CA145FAF92F3E70 ' + . 'FOREIGN KEY (country_id) ' + . 'REFERENCES Country (id) ' + . 'NOT DEFERRABLE INITIALLY IMMEDIATE'); + + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('ALTER TABLE chill_main_address ' + . 'DROP CONSTRAINT FK_165051F6EECBFDF1'); + $this->addSql('DROP SEQUENCE chill_main_address_id_seq CASCADE'); + $this->addSql('DROP SEQUENCE chill_main_postal_code_id_seq CASCADE'); + $this->addSql('DROP TABLE chill_main_address'); + $this->addSql('DROP TABLE chill_main_postal_code'); + } +} diff --git a/Resources/views/Address/macro.html.twig b/Resources/views/Address/macro.html.twig new file mode 100644 index 000000000..3471f0152 --- /dev/null +++ b/Resources/views/Address/macro.html.twig @@ -0,0 +1,7 @@ +{%- macro _render(address) -%} + {% if address.streetAddress1 is not empty %}{{ address.streetAddress1 }}
{% endif %} + {% if address.streetAddress2 is not empty %}{{ address.streetAddress2 }}
{% endif %} + {{ address.postCode.code }} {{ address.postCode.name }}
+ {{ address.postCode.country.name|localize_translatable_string }}
+ {{ 'Since %date%'|trans( { '%date%' : address.validFrom|localizeddate('long', 'none') } ) }} +{%- endmacro -%}