add address and postal code

This commit is contained in:
Julien Fastré 2016-03-10 17:57:30 +01:00
parent 6ae1b8334e
commit 7679786288
7 changed files with 476 additions and 0 deletions

View File

@ -0,0 +1,106 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2016, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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é <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
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 = <<<EOF
1000,BRUXELLES,BRUXELLES,Bruxelles
1020,Laeken,BRUXELLES,Bruxelles
1030,SCHAERBEEK,SCHAERBEEK,Bruxelles
1040,ETTERBEEK,ETTERBEEK,Bruxelles
1050,IXELLES,IXELLES,Bruxelles
1060,SAINT-GILLES,SAINT-GILLES,Bruxelles
1070,ANDERLECHT,ANDERLECHT,Bruxelles
1080,MOLENBEEK-SAINT-JEAN,MOLENBEEK-SAINT-JEAN,Bruxelles
1081,KOEKELBERG,KOEKELBERG,Bruxelles
1082,BERCHEM-SAINTE-AGATHE,BERCHEM-SAINTE-AGATHE,Bruxelles
1083,GANSHOREN,GANSHOREN,Bruxelles
1090,JETTE,JETTE,Bruxelles
1120,Neder-Over-Heembeek,BRUXELLES,Bruxelles
1130,Haren,BRUXELLES,Bruxelles
1140,EVERE,EVERE,Bruxelles
1150,WOLUWE-SAINT-PIERRE,WOLUWE-SAINT-PIERRE,Bruxelles
1160,AUDERGHEM,AUDERGHEM,Bruxelles
1170,WATERMAEL-BOITSFORT,WATERMAEL-BOITSFORT,Bruxelles
1180,UCCLE,UCCLE,Bruxelles
1190,FOREST,FOREST,Bruxelles
1200,WOLUWE-SAINT-LAMBERT,WOLUWE-SAINT-LAMBERT,Bruxelles
1210,SAINT-JOSSE-TEN-NOODE,SAINT-JOSSE-TEN-NOODE,Bruxelles
1300,Limal,WAVRE,Brabant-Wallon
1300,WAVRE,WAVRE,Brabant-Wallon
1301,Bierges,WAVRE,Brabant-Wallon
1310,LA HULPE,LA HULPE,Brabant-Wallon
1315,Glimes,INCOURT,Brabant-Wallon
1315,INCOURT,INCOURT,Brabant-Wallon
1315,Opprebais,INCOURT,Brabant-Wallon
1315,Piètrebais,INCOURT,Brabant-Wallon
1315,Roux-Miroir,INCOURT,Brabant-Wallon
1320,BEAUVECHAIN,BEAUVECHAIN,Brabant-Wallon
EOF;
}

141
Entity/Address.php Normal file
View File

@ -0,0 +1,141 @@
<?php
namespace Chill\MainBundle\Entity;
/**
* Address
*/
class Address
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $streetAddress1;
/**
* @var string
*/
private $streetAddress2;
/**
* @var \Chill\MainBundle\Entity\PostalCode
*/
private $postcode;
/**
*
* @var \DateTime
*/
private $validFrom;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->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;
}
}

113
Entity/PostalCode.php Normal file
View File

@ -0,0 +1,113 @@
<?php
namespace Chill\MainBundle\Entity;
/**
* PostalCode
*/
class PostalCode
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $code;
/**
* @var \Chill\MainBundle\Entity\Country
*/
private $country;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->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;
}
}

View File

@ -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: { }

View File

@ -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: { }

View File

@ -0,0 +1,66 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Add postal code and addresses
*/
class Version20160310122322 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->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');
}
}

View File

@ -0,0 +1,7 @@
{%- macro _render(address) -%}
{% if address.streetAddress1 is not empty %}<span class="street street1">{{ address.streetAddress1 }}</span><br/>{% endif %}
{% if address.streetAddress2 is not empty %}<span class="street street2">{{ address.streetAddress2 }}</span><br/>{% endif %}
<span class="postalCode"><span class="code">{{ address.postCode.code }}</span> <span class="name">{{ address.postCode.name }}</span></span><br/>
<span class="country">{{ address.postCode.country.name|localize_translatable_string }}</span><br/>
<span class="address_since">{{ 'Since %date%'|trans( { '%date%' : address.validFrom|localizeddate('long', 'none') } ) }}</span>
{%- endmacro -%}