From 289afcdd0cffea7418c5c9a06600c8e5df320ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 30 Jan 2020 23:04:24 +0100 Subject: [PATCH] add trigger on alt names and improve layout with alt names --- Resources/config/doctrine/Person.orm.yml | 4 +- .../migrations/Version20200130213446.php | 154 ++++++++++++++++++ Resources/views/layout.html.twig | 11 +- Templating/Entity/PersonRender.php | 7 +- 4 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 Resources/migrations/Version20200130213446.php diff --git a/Resources/config/doctrine/Person.orm.yml b/Resources/config/doctrine/Person.orm.yml index 29d981750..f4d80cdf2 100644 --- a/Resources/config/doctrine/Person.orm.yml +++ b/Resources/config/doctrine/Person.orm.yml @@ -52,8 +52,8 @@ Chill\PersonBundle\Entity\Person: nullable: true length: 40 fullnameCanonical: - type: string - length: 255 + type: text + nullable: true manyToOne: countryOfBirth: targetEntity: Chill\MainBundle\Entity\Country diff --git a/Resources/migrations/Version20200130213446.php b/Resources/migrations/Version20200130213446.php new file mode 100644 index 000000000..115694bfe --- /dev/null +++ b/Resources/migrations/Version20200130213446.php @@ -0,0 +1,154 @@ + OLD.fullnameCanonical + THEN + UPDATE chill_person_person + SET fullnameCanonical=fullname_canonicalized + WHERE id=NEW.id; + END IF; + RETURN NEW; +END; +$BODY$; +SQL; + + const CANONICALIZE_FULLNAME_ON_ALT_NAME_ALTER = <<<'SQL' +CREATE OR REPLACE FUNCTION public.canonicalize_fullname_on_alt_name_alter() + RETURNS trigger + LANGUAGE 'plpgsql' + COST 100 + VOLATILE NOT LEAKPROOF +AS $BODY$ +DECLARE + target_person_id INTEGER; + cur_person CURSOR(pid INTEGER) FOR SELECT firstname, lastname FROM chill_person_person WHERE id = pid; + person RECORD; + cur_alt_names CURSOR(pid INTEGER) FOR SELECT label FROM chill_person_alt_name WHERE person_id = pid; + alt_name RECORD; + fullname_canonicalized TEXT; +BEGIN + IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' + THEN target_person_id := NEW.person_id; + ELSE target_person_id := OLD.person_id; + END IF; + + OPEN cur_person(pid:=target_person_id); + FETCH cur_person INTO person; + fullname_canonicalized := LOWER(UNACCENT(CONCAT(person.firstname, ' ', person.lastname))); + -- loop over alt names + OPEN cur_alt_names(pid:=target_person_id); + LOOP + FETCH cur_alt_names INTO alt_name; + + EXIT WHEN NOT FOUND; + + fullname_canonicalized := CONCAT(fullname_canonicalized, ' ', + LOWER(UNACCENT(alt_name.label))); + END LOOP; + CLOSE cur_alt_names; + CLOSE cur_person; + + UPDATE chill_person_person + SET fullnameCanonical=fullname_canonicalized + WHERE id=target_person_id; + + RETURN NEW; +END; +$BODY$; +SQL; + + const CANONICALIZE_FULLNAME_ON_ALT_NAME_INSERT = <<addSql("ALTER TABLE chill_person_person ALTER fullnamecanonical TYPE TEXT;"); + $this->addSql("ALTER TABLE chill_person_person ALTER fullnamecanonical DROP DEFAULT;"); + // insert function and triggers + $this->addSql(self::CANONICALIZE_FULLNAME_ON_UPDATE); + $this->addSql(self::CANONICALIZE_FULLNAME_ON_ALT_NAME_ALTER); + $this->addSql(self::CANONICALIZE_FULLNAME_ON_ALT_NAME_INSERT); + $this->addSql(self::CANONICALIZE_FULLNAME_ON_ALT_NAME_DELETE); + $this->addSql(self::CANONICALIZE_FULLNAME_ON_ALT_NAME_UPDATE); + + } + + public function down(Schema $schema) : void + { + $this->addSql("DROP TRIGGER canonicalize_fullname_on_alt_name_update ON chill_person_alt_name;"); + $this->addSql("DROP TRIGGER canonicalize_fullname_on_alt_name_insert ON chill_person_alt_name;"); + $this->addSql("DROP TRIGGER canonicalize_fullname_on_alt_name_delete ON chill_person_alt_name;"); + $this->addSql("DROP FUNCTION canonicalize_fullname_on_alt_name_alter();"); + $this->addSql(<<<'SQL' + CREATE OR REPLACE FUNCTION canonicalize_fullname_on_update() RETURNS TRIGGER AS + $BODY$ + BEGIN + IF NEW.firstname <> OLD.firstname OR NEW.lastname <> OLD.lastname + THEN + UPDATE chill_person_person + SET fullnameCanonical=LOWER(UNACCENT(CONCAT(NEW.firstname, ' ', NEW.lastname))) + WHERE id=NEW.id; + END IF; + RETURN NEW; + END; + $BODY$ LANGUAGE PLPGSQL; +SQL + ); + } + +} diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 1992d5ae9..30028b54b 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -29,10 +29,7 @@
- {{ 'Last name'|trans|upper }} : {{ person.lastName|upper }} -
-
- {{ 'First name'|trans|upper}} : {{ person.firstName|upper }} + {{ 'Name'|trans|upper }} : {{ person|chill_entity_render_string }}
{{ 'File number'|trans|upper}} : {{ person.id|upper }} @@ -47,6 +44,12 @@ {% endif %} {% endspaceless %}">
+
+ {{ 'Phonenumber'|trans|upper }} : + {% set phone = person.mobilenumber|default(person.phonenumber) %} + {% if phone is not empty %}{{ phone|chill_format_phonenumber }}{% else %}{{ 'No data given'|trans }}{% endif %} +
+
diff --git a/Templating/Entity/PersonRender.php b/Templating/Entity/PersonRender.php index 98398d8d5..d1f554492 100644 --- a/Templating/Entity/PersonRender.php +++ b/Templating/Entity/PersonRender.php @@ -96,9 +96,10 @@ class PersonRender extends AbstractChillEntityRender $str .= ""; } } - if (!$isFirst) { - $str .= ")"; - } + } + + if (!$isFirst) { + $str .= ")"; } }