Skip to content
Snippets Groups Projects
Commit 0429f45d authored by Camille Simiand's avatar Camille Simiand
Browse files

Add a capsule group

parent db6e9c65
Branches
No related tags found
2 merge requests!55demo version for Clarisse workshop,!51tuleap-130-add-a-capsule-into-custom-groups
Showing
with 239 additions and 3 deletions
......@@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220125084520 extends AbstractMigration
final class Version20220208141756 extends AbstractMigration
{
public function getDescription(): string
{
......
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220208145209 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE `group` (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE group_capsule (group_id INT NOT NULL, capsule_id INT NOT NULL, INDEX IDX_4468F58FFE54D947 (group_id), INDEX IDX_4468F58F714704E9 (capsule_id), PRIMARY KEY(group_id, capsule_id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE group_capsule ADD CONSTRAINT FK_4468F58FFE54D947 FOREIGN KEY (group_id) REFERENCES `group` (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE group_capsule ADD CONSTRAINT FK_4468F58F714704E9 FOREIGN KEY (capsule_id) REFERENCES capsule (id) ON DELETE CASCADE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE group_capsule DROP FOREIGN KEY FK_4468F58FFE54D947');
$this->addSql('DROP TABLE `group`');
$this->addSql('DROP TABLE group_capsule');
}
}
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
<?php
namespace App\Controller;
use App\Entity\Capsule;
use App\Entity\Group;
use App\Entity\User;
use App\Form\EditCapsuleGroupsFormType;
use App\Repository\CapsuleRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
class CapsuleGroupController extends AbstractController
{
private TranslatorInterface $translator;
private CapsuleRepository $capsule_repository;
private EntityManagerInterface $entity_manager;
public function __construct(
TranslatorInterface $translator,
CapsuleRepository $capsule_repository,
EntityManagerInterface $entity_manager
) {
$this->translator = $translator;
$this->capsule_repository = $capsule_repository;
$this->entity_manager = $entity_manager;
}
/**
* @Route("/capsule/{capsule_id}/groups/edit", name="edit_capsule_groups")
*/
public function edit(int $capsule_id, Request $request): Response
{
$current_user = $this->getUser();
if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout');
}
$form = $this->createForm(EditCapsuleGroupsFormType::class);
$form->handleRequest($request);
$capsule = $this->capsule_repository->findOneBy(['id' => $capsule_id]);
if (! $capsule instanceof Capsule) {
throw new \Exception("Capsule does not exist");
}
$groups = $capsule->getGroups();
if ($form->isSubmitted() && $form->isValid()) {
$group_name = $form->get('name')->getData();
$group = new Group();
$group->setName($group_name);
$this->entity_manager->persist($group);
$capsule->addGroup($group);
$this->entity_manager->persist($capsule);
$this->entity_manager->flush();
$this->addFlash(
'success',
$this->translator->trans(
'groups.add.success',
[
'group_name' => $group->getName(),
'capsule_name' => $capsule->getName()
]
)
);
return $this->redirectToRoute('edit_capsule_groups', [
'capsule_id' => $capsule_id
]);
}
return $this->render('capsule/groups/edit.html.twig', [
'editCapsuleGroupsForm' => $form->createView(),
'capsule_name' => $capsule->getName(),
'groups' => $groups
]);
}
}
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -90,9 +90,16 @@ class Capsule
*/
private Collection $editors;
/**
* @var Collection<Group>
* @ORM\ManyToMany(targetEntity=Group::class, mappedBy="capsules")
*/
private Collection $groups;
public function __construct()
{
$this->editors = new ArrayCollection();
$this->groups = new ArrayCollection();
}
public function getId(): int
......@@ -205,14 +212,13 @@ class Capsule
}
/**
* @return Collection<User> $editors
* @return Collection<User>
*/
public function getEditors(): Collection
{
return $this->editors;
}
public function getVideoPreviewImageLink(): ?string
{
$file_system = new Filesystem();
......@@ -231,4 +237,31 @@ class Capsule
return 'https://vumbnail.com/' . $video_id . '.jpg';
}
/**
* @return Collection|Group[]
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
$group->addCapsule($this);
}
return $this;
}
public function removeGroup(Group $group): self
{
if ($this->groups->removeElement($group)) {
$group->removeCapsule($this);
}
return $this;
}
}
<?php
namespace App\Entity;
use App\Repository\GroupRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass=GroupRepository::class)
* @ORM\Table(name="`group`")
* @UniqueEntity(fields={"name"}, message="group.name.unique")
*/
class Group
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=255)
*/
private string $name;
/**
* @var Collection<Capsule>
* @ORM\ManyToMany(targetEntity=Capsule::class, inversedBy="groups")
*/
private Collection $capsules;
public function __construct()
{
$this->capsules = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Capsule[]
*/
public function getCapsules(): Collection
{
return $this->capsules;
}
public function addCapsule(Capsule $capsule): self
{
if (!$this->capsules->contains($capsule)) {
$this->capsules[] = $capsule;
}
return $this;
}
public function removeCapsule(Capsule $capsule): self
{
$this->capsules->removeElement($capsule);
return $this;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment