Merge branch 'notifications' into 'master'

Notifications

See merge request Chill-Projet/chill-bundles!81
This commit is contained in:
Marc Ducobu 2021-06-16 12:58:57 +00:00
commit 1b9d8fab61
6 changed files with 380 additions and 1 deletions

View File

@ -138,11 +138,15 @@ class LoadActivity extends AbstractFixture implements OrderedFixtureInterface, C
foreach($persons as $person) {
$activityNbr = rand(0,3);
$ref = 'activity_'.$person->getFullnameCanonical();
for($i = 0; $i < $activityNbr; $i ++) {
print "Creating an activity type for : ".$person."\n";
print "Creating an activity type for : ".$person." (ref: ".$ref.") \n";
$activity = $this->newRandomActivity($person);
$manager->persist($activity);
}
$this->setReference($ref, $activity);
}
$manager->flush();
}

View File

@ -0,0 +1,39 @@
<?php
namespace Chill\ActivityBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Intl\Intl;
use Chill\MainBundle\Entity\Notification;
use Chill\ActivityBundle\Entity\Activity;
use Chill\MainBundle\DataFixtures\ORM\LoadAbstractNotificationsTrait;
/**
* Load notififications into database
*/
class LoadActivityNotifications extends AbstractFixture implements OrderedFixtureInterface
{
use LoadAbstractNotificationsTrait;
public function getOrder() {
return 16500;
}
public $notifs = [
[
'message' => 'Hello !',
'entityClass' => Activity::class,
'entityRef' => 'activity_gerard depardieu',
'sender' => 'center a_social',
'addressees' => [
'center a_administrative',
'center a_direction',
'multi_center'
],
]
];
}

View File

@ -0,0 +1,41 @@
<?php
namespace Chill\MainBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Intl\Intl;
use Chill\MainBundle\Entity\Notification;
/**
* Load notififications into database
*/
trait LoadAbstractNotificationsTrait
{
public function load(ObjectManager $manager)
{
foreach ($this->notifs as $notif) {
$entityId = $this->getReference($notif['entityRef'])->getId();
print('Adding notification for '.$notif['entityClass'].'(entity id:'.$entityId.")\n");
$newNotif = (new Notification())
->setMessage($notif['message'])
->setSender($this->getReference($notif['sender']))
->setRelatedEntityClass($notif['entityClass'])
->setRelatedEntityId($entityId)
->setDate(new \DateTimeImmutable('now'))
->setRead([])
;
foreach ($notif['addressees'] as $addressee) {
$newNotif->addAddressee($this->getReference($addressee));
}
$manager->persist($newNotif);
$manager->flush();
}
}
}

View File

@ -0,0 +1,187 @@
<?php
/*
* Copyright (C) 2021 Champs-Libres <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\Entity;
use Chill\MainBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(
* name="chill_main_notification",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"relatedEntityClass", "relatedEntityId"})
* }
* )
*/
class Notification
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="text")
*/
private string $message;
/**
* @ORM\Column(type="datetime_immutable")
*/
private \DateTimeImmutable $date;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
*/
private User $sender;
/**
* @ORM\ManyToMany(targetEntity=User::class)
* @ORM\JoinTable(name="chill_main_notification_addresses_user")
*/
private Collection $addressees;
/**
* @ORM\Column(type="string", length=255)
*/
private string $relatedEntityClass;
/**
* @ORM\Column(type="integer")
*/
private int $relatedEntityId;
/**
* @ORM\Column(type="json")
*/
private array $read;
public function __construct()
{
$this->addressees = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(string $message): self
{
$this->message = $message;
return $this;
}
public function getDate(): ?\DateTimeImmutable
{
return $this->date;
}
public function setDate(\DateTimeImmutable $date): self
{
$this->date = $date;
return $this;
}
public function getSender(): ?User
{
return $this->sender;
}
public function setSender(?User $sender): self
{
$this->sender = $sender;
return $this;
}
/**
* @return Collection|User[]
*/
public function getAddressees(): Collection
{
return $this->addressees;
}
public function addAddressee(User $addressee): self
{
if (!$this->addressees->contains($addressee)) {
$this->addressees[] = $addressee;
}
return $this;
}
public function removeAddressee(User $addressee): self
{
$this->addressees->removeElement($addressee);
return $this;
}
public function getRelatedEntityClass(): ?string
{
return $this->relatedEntityClass;
}
public function setRelatedEntityClass(string $relatedEntityClass): self
{
$this->relatedEntityClass = $relatedEntityClass;
return $this;
}
public function getRelatedEntityId(): ?int
{
return $this->relatedEntityId;
}
public function setRelatedEntityId(int $relatedEntityId): self
{
$this->relatedEntityId = $relatedEntityId;
return $this;
}
public function getRead(): array
{
return $this->read;
}
public function setRead(array $read): self
{
$this->read = $read;
return $this;
}
}

View File

@ -0,0 +1,66 @@
<?php
/*
* Copyright (C) 2021 Champs-Libres <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\Repository;
use Chill\MainBundle\Entity\Notification;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
final class NotificationRepository implements ObjectRepository
{
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(Notification::class);
}
public function find($id, $lockMode = null, $lockVersion = null): ?Notification
{
return $this->repository->find($id, $lockMode, $lockVersion);
}
public function findOneBy(array $criteria, array $orderBy = null): ?Notification
{
return $this->repository->findOneBy($criteria, $orderBy);
}
/**
* @return Notification[]
*/
public function findAll(): array
{
return $this->repository->findAll();
}
/**
* @return Notification[]
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function getClassName() {
return Notification::class;
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add table for ChillMain/Notification
*/
final class Version20210610140248 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add table for ChillMain/Notification';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE chill_main_notification_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE chill_main_notification (id INT NOT NULL, sender_id INT NOT NULL, message TEXT NOT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, relatedEntityClass VARCHAR(255) NOT NULL, relatedEntityId INT NOT NULL, read JSONB NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_5BDC8067F624B39D ON chill_main_notification (sender_id)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_5BDC8067567988B4440F6072 ON chill_main_notification (relatedEntityClass, relatedEntityId)');
$this->addSql('COMMENT ON COLUMN chill_main_notification.date IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('CREATE TABLE chill_main_notification_addresses_user (notification_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(notification_id, user_id))');
$this->addSql('CREATE INDEX IDX_E52C5D2BEF1A9D84 ON chill_main_notification_addresses_user (notification_id)');
$this->addSql('CREATE INDEX IDX_E52C5D2BA76ED395 ON chill_main_notification_addresses_user (user_id)');
$this->addSql('ALTER TABLE chill_main_notification ADD CONSTRAINT FK_5BDC8067F624B39D FOREIGN KEY (sender_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_main_notification_addresses_user ADD CONSTRAINT FK_E52C5D2BEF1A9D84 FOREIGN KEY (notification_id) REFERENCES chill_main_notification (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_main_notification_addresses_user ADD CONSTRAINT FK_E52C5D2BA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_main_notification_addresses_user DROP CONSTRAINT FK_E52C5D2BEF1A9D84');
$this->addSql('DROP SEQUENCE chill_main_notification_id_seq CASCADE');
$this->addSql('DROP TABLE chill_main_notification');
$this->addSql('DROP TABLE chill_main_notification_addresses_user');
}
}