From 5bd8b22569555d73aa2e0aa54b7a9cebf79c94be Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Thu, 2 Jul 2015 11:33:16 +0200 Subject: [PATCH] Activity Reasons, Activity Reasons Category, Activity Types in Translatable strings --- Entity/ActivityReason.php | 35 +++++++--- Entity/ActivityReasonCategory.php | 37 +++++++--- Entity/ActivityType.php | 23 +++++-- Form/ActivityReasonCategoryType.php | 2 +- Form/ActivityReasonType.php | 4 +- Form/ActivityTypeType.php | 2 +- .../TranslatableActivityReasonCategory.php | 68 +++++++++++++++++++ .../config/doctrine/ActivityReason.orm.yml | 5 +- .../doctrine/ActivityReasonCategory.orm.yml | 5 +- .../config/doctrine/ActivityType.orm.yml | 3 +- Resources/config/routing/activitytype.yml | 2 +- Resources/config/services.yml | 9 ++- .../migrations/Version20150702093317.php | 64 +++++++++++++++++ .../views/ActivityReason/index.html.twig | 4 +- Resources/views/ActivityReason/show.html.twig | 4 +- .../ActivityReasonCategory/index.html.twig | 4 +- .../ActivityReasonCategory/show.html.twig | 4 +- Resources/views/ActivityType/index.html.twig | 2 +- Resources/views/ActivityType/show.html.twig | 2 +- 19 files changed, 228 insertions(+), 51 deletions(-) create mode 100644 Form/Type/TranslatableActivityReasonCategory.php create mode 100644 Resources/migrations/Version20150702093317.php diff --git a/Entity/ActivityReason.php b/Entity/ActivityReason.php index 10f9243a6..8ad072039 100644 --- a/Entity/ActivityReason.php +++ b/Entity/ActivityReason.php @@ -33,9 +33,9 @@ class ActivityReason private $id; /** - * @var string + * @var array */ - private $label; + private $name; /** * @var ActivityReasonCategory @@ -59,29 +59,42 @@ class ActivityReason } /** - * Set label + * Set name * - * @param string $label + * @param array $name * * @return ActivityReason */ - public function setLabel($label) + public function setName($name) { - $this->label = $label; + $this->name = $name; return $this; } /** - * Get label + * Get name * - * @return string + * @return array | string */ - public function getLabel() + public function getName($locale = null) { - return $this->label; + if ($locale) { + if (isset($this->name[$locale])) { + return $this->name[$locale]; + } else { + foreach ($this->name as $name) { + if (!empty($name)) { + return $name; + } + } + } + return ''; + } else { + return $this->name; + } } - + /** * Set category * diff --git a/Entity/ActivityReasonCategory.php b/Entity/ActivityReasonCategory.php index c7af694c5..22da06d74 100644 --- a/Entity/ActivityReasonCategory.php +++ b/Entity/ActivityReasonCategory.php @@ -33,12 +33,18 @@ class ActivityReasonCategory /** * @var string */ - private $label; + private $name; /** * @var boolean */ private $active; + + + public function __toString() + { + return 'blop'; + } /** @@ -52,27 +58,40 @@ class ActivityReasonCategory } /** - * Set label + * Set name * - * @param string $label + * @param array $name * * @return ActivityReasonCategory */ - public function setLabel($label) + public function setName($name) { - $this->label = $label; + $this->name = $name; return $this; } /** - * Get label + * Get name * - * @return string + * @return array */ - public function getLabel() + public function getName($locale = null) { - return $this->label; + if ($locale) { + if (isset($this->name[$locale])) { + return $this->name[$locale]; + } else { + foreach ($this->name as $name) { + if (!empty($name)) { + return $name; + } + } + } + return ''; + } else { + return $this->name; + } } /** diff --git a/Entity/ActivityType.php b/Entity/ActivityType.php index 0fadc77a0..2af64f861 100644 --- a/Entity/ActivityType.php +++ b/Entity/ActivityType.php @@ -31,7 +31,7 @@ class ActivityType private $id; /** - * @var string + * @var array */ private $name; @@ -49,7 +49,7 @@ class ActivityType /** * Set name * - * @param string $name + * @param array $name * * @return ActivityType */ @@ -63,11 +63,24 @@ class ActivityType /** * Get name * - * @return string + * @return array | string */ - public function getName() + public function getName($locale = null) { - return $this->name; + if ($locale) { + if (isset($this->name[$locale])) { + return $this->name[$locale]; + } else { + foreach ($this->name as $name) { + if (!empty($name)) { + return $name; + } + } + } + return ''; + } else { + return $this->name; + } } } diff --git a/Form/ActivityReasonCategoryType.php b/Form/ActivityReasonCategoryType.php index 4b21f3e3c..3e05011e0 100644 --- a/Form/ActivityReasonCategoryType.php +++ b/Form/ActivityReasonCategoryType.php @@ -15,7 +15,7 @@ class ActivityReasonCategoryType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('label') + ->add('name', 'translatable_string') ->add('active') ; } diff --git a/Form/ActivityReasonType.php b/Form/ActivityReasonType.php index 43704fe03..fd3939e29 100644 --- a/Form/ActivityReasonType.php +++ b/Form/ActivityReasonType.php @@ -15,9 +15,9 @@ class ActivityReasonType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('label') + ->add('name', 'translatable_string') ->add('active') - ->add('category') + ->add('category', 'translatable_activity_reason_category') ; } diff --git a/Form/ActivityTypeType.php b/Form/ActivityTypeType.php index cbc5d5afa..278c23a9a 100644 --- a/Form/ActivityTypeType.php +++ b/Form/ActivityTypeType.php @@ -15,7 +15,7 @@ class ActivityTypeType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('name') + ->add('name', 'translatable_string') ; } diff --git a/Form/Type/TranslatableActivityReasonCategory.php b/Form/Type/TranslatableActivityReasonCategory.php new file mode 100644 index 000000000..83495f274 --- /dev/null +++ b/Form/Type/TranslatableActivityReasonCategory.php @@ -0,0 +1,68 @@ +, + * + * 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\ActivityBundle\Form\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * Description of TranslatableActivityReasonCategory + * + * @author Champs-Libres Coop + */ + + +class TranslatableActivityReasonCategory extends AbstractType +{ + /** + * @var RequestStack + */ + private $requestStack; + + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + + public function getName() + { + return 'translatable_activity_reason_category'; + } + + public function getParent() + { + return 'entity'; + } + + public function configureOptions(OptionsResolver $resolver) + { + $locale = $this->requestStack->getCurrentRequest()->getLocale(); + $resolver->setDefaults( + array( + 'class' => 'ChillActivityBundle:ActivityReasonCategory', + 'property' => 'name['.$locale.']' + ) + ); + } +} diff --git a/Resources/config/doctrine/ActivityReason.orm.yml b/Resources/config/doctrine/ActivityReason.orm.yml index 00cfdb03f..86a4542cc 100644 --- a/Resources/config/doctrine/ActivityReason.orm.yml +++ b/Resources/config/doctrine/ActivityReason.orm.yml @@ -8,9 +8,8 @@ Chill\ActivityBundle\Entity\ActivityReason: generator: strategy: AUTO fields: - label: - type: string - length: 255 + name: + type: json_array active: type: boolean manyToOne: diff --git a/Resources/config/doctrine/ActivityReasonCategory.orm.yml b/Resources/config/doctrine/ActivityReasonCategory.orm.yml index 96e0b667d..830129740 100644 --- a/Resources/config/doctrine/ActivityReasonCategory.orm.yml +++ b/Resources/config/doctrine/ActivityReasonCategory.orm.yml @@ -8,9 +8,8 @@ Chill\ActivityBundle\Entity\ActivityReasonCategory: generator: strategy: AUTO fields: - label: - type: string - length: 255 + name: + type: json_array active: type: boolean lifecycleCallbacks: { } diff --git a/Resources/config/doctrine/ActivityType.orm.yml b/Resources/config/doctrine/ActivityType.orm.yml index d6691e25b..fb3878343 100644 --- a/Resources/config/doctrine/ActivityType.orm.yml +++ b/Resources/config/doctrine/ActivityType.orm.yml @@ -9,6 +9,5 @@ Chill\ActivityBundle\Entity\ActivityType: strategy: AUTO fields: name: - type: string - length: 255 + type: json_array lifecycleCallbacks: { } diff --git a/Resources/config/routing/activitytype.yml b/Resources/config/routing/activitytype.yml index 942b202fe..621be8bdc 100644 --- a/Resources/config/routing/activitytype.yml +++ b/Resources/config/routing/activitytype.yml @@ -1,4 +1,4 @@ -activitytype: +chill_activity_activitytype: path: /{_locale}/admin/activitytype/ defaults: { _controller: "ChillActivityBundle:ActivityType:index" } options: diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 7a089a4b2..2b4b8c630 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,4 +1,7 @@ services: -# chill_activity.example: -# class: Chill\ActivityBundle\Example -# arguments: [@service_id, "plain_value", %parameter%] + chill.activity.form.type.translatableactivityreasoncategory: + class: Chill\ActivityBundle\Form\Type\TranslatableActivityReasonCategory + arguments: + - "@request_stack" + tags: + - { name: form.type, alias: translatable_activity_reason_category } \ No newline at end of file diff --git a/Resources/migrations/Version20150702093317.php b/Resources/migrations/Version20150702093317.php new file mode 100644 index 000000000..6f65e63ef --- /dev/null +++ b/Resources/migrations/Version20150702093317.php @@ -0,0 +1,64 @@ +, + * + * 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 Application\Migrations; + +use Doctrine\DBAL\Migrations\AbstractMigration; +use Doctrine\DBAL\Schema\Schema; + +/** + * Auto-generated Migration: Please modify to your needs! + */ +class Version20150702093317 extends AbstractMigration +{ + /** + * @param Schema $schema + */ + public function up(Schema $schema) + { + // this up() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('ALTER TABLE ActivityReasonCategory DROP COLUMN label;'); + $this->addSql('ALTER TABLE ActivityReasonCategory ADD COLUMN name JSON;'); + $this->addSql('ALTER TABLE ActivityReason DROP COLUMN label;'); + $this->addSql('ALTER TABLE ActivityReason ADD COLUMN name JSON;'); + $this->addSql('ALTER TABLE ActivityType DROP COLUMN name;'); + $this->addSql('ALTER TABLE ActivityType ADD COLUMN name JSON;'); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('ALTER TABLE ActivityReasonCategory DROP COLUMN name;'); + $this->addSql('ALTER TABLE ActivityReasonCategory ADD COLUMN label VARCHAR(255) NOT NULL;'); + $this->addSql('ALTER TABLE ActivityReason DROP COLUMN name;'); + $this->addSql('ALTER TABLE ActivityReason ADD COLUMN label VARCHAR(255) NOT NULL;'); + $this->addSql('ALTER TABLE ActivityType DROP COLUMN name;'); + $this->addSql('ALTER TABLE ActivityType ADD COLUMN name VARCHAR(255) NOT NULL;'); + } +} \ No newline at end of file diff --git a/Resources/views/ActivityReason/index.html.twig b/Resources/views/ActivityReason/index.html.twig index 3108a589f..97915def3 100644 --- a/Resources/views/ActivityReason/index.html.twig +++ b/Resources/views/ActivityReason/index.html.twig @@ -23,7 +23,7 @@ Id - Label + Name Active Actions @@ -32,7 +32,7 @@ {% for entity in entities %} {{ entity.id }} - {{ entity.label }} + {{ entity.name|localize_translatable_string }} {{ entity.active }}
    diff --git a/Resources/views/ActivityReason/show.html.twig b/Resources/views/ActivityReason/show.html.twig index 0055d90f8..9aea54df2 100644 --- a/Resources/views/ActivityReason/show.html.twig +++ b/Resources/views/ActivityReason/show.html.twig @@ -26,8 +26,8 @@ {{ entity.id }} - Label - {{ entity.label }} + Name + {{ entity.name|localize_translatable_string }} Active diff --git a/Resources/views/ActivityReasonCategory/index.html.twig b/Resources/views/ActivityReasonCategory/index.html.twig index 3b8333cf3..b33b5a1d2 100644 --- a/Resources/views/ActivityReasonCategory/index.html.twig +++ b/Resources/views/ActivityReasonCategory/index.html.twig @@ -23,7 +23,7 @@ Id - Label + Name Active Actions @@ -32,7 +32,7 @@ {% for entity in entities %} {{ entity.id }} - {{ entity.label }} + {{ entity.name|localize_translatable_string }} {{ entity.active }}
      diff --git a/Resources/views/ActivityReasonCategory/show.html.twig b/Resources/views/ActivityReasonCategory/show.html.twig index 98242281b..617543a91 100644 --- a/Resources/views/ActivityReasonCategory/show.html.twig +++ b/Resources/views/ActivityReasonCategory/show.html.twig @@ -26,8 +26,8 @@ {{ entity.id }} - Label - {{ entity.label }} + Name + {{ entity.name|localize_translatable_string }} Active diff --git a/Resources/views/ActivityType/index.html.twig b/Resources/views/ActivityType/index.html.twig index 9cf952b77..68b3556e1 100644 --- a/Resources/views/ActivityType/index.html.twig +++ b/Resources/views/ActivityType/index.html.twig @@ -31,7 +31,7 @@ {% for entity in entities %} {{ entity.id }} - {{ entity.name }} + {{ entity.name|localize_translatable_string }}
      • diff --git a/Resources/views/ActivityType/show.html.twig b/Resources/views/ActivityType/show.html.twig index 89198cc88..6899df806 100644 --- a/Resources/views/ActivityType/show.html.twig +++ b/Resources/views/ActivityType/show.html.twig @@ -27,7 +27,7 @@ Name - {{ entity.name }} + {{ entity.name|localize_translatable_string }}