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

Merge branch 'tuleap-130-add-a-capsule-into-custom-groups' into 'demo-202202'

tuleap-130-add-a-capsule-into-custom-groups

See merge request !51
parents 0774f3ed 3683fb86
Branches
Tags
2 merge requests!55demo version for Clarisse workshop,!51tuleap-130-add-a-capsule-into-custom-groups
Showing
with 342 additions and 3 deletions
...@@ -131,6 +131,7 @@ button[type=submit]{ ...@@ -131,6 +131,7 @@ button[type=submit]{
.button-cancel { .button-cancel {
border: none; border: none;
border-radius: 3px; border-radius: 3px;
background-color: #5c636a;
} }
.button-cancel:hover { .button-cancel:hover {
......
...@@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; ...@@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/** /**
* Auto-generated Migration: Please modify to your needs! * Auto-generated Migration: Please modify to your needs!
*/ */
final class Version20220125084520 extends AbstractMigration final class Version20220208141756 extends AbstractMigration
{ {
public function getDescription(): string 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\CreateCapsuleGroupsFormType;
use App\Form\RemoveGroupFormType;
use App\Form\SelectCapsuleGroupsFormType;
use App\Repository\CapsuleRepository;
use App\Repository\GroupRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
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;
private GroupRepository $group_repository;
public function __construct(
TranslatorInterface $translator,
CapsuleRepository $capsule_repository,
EntityManagerInterface $entity_manager,
GroupRepository $group_repository
) {
$this->translator = $translator;
$this->capsule_repository = $capsule_repository;
$this->entity_manager = $entity_manager;
$this->group_repository = $group_repository;
}
/**
* @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');
}
$capsule = $this->capsule_repository->findOneBy(['id' => $capsule_id]);
if (! $capsule instanceof Capsule) {
throw new \Exception("Capsule does not exist");
}
$groups = $capsule->getGroups();
$editor_groups_not_added = $this->getEditorGroupsNotAdded($current_user, $groups);
$form = $this->createForm(SelectCapsuleGroupsFormType::class, ['groups' => $editor_groups_not_added]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$groups = $this->addGroups($form, $capsule);
$groups_names = [];
foreach ($groups as $group) {
$groups_names[] = $group->getName();
}
$this->addFlash(
'success',
$this->translator->trans(
'groups.add.success',
[
'groups_names' => implode(",", $groups_names),
'capsule_name' => $capsule->getName()
]
)
);
return $this->redirectToRoute('edit_capsule_groups', [
'capsule_id' => $capsule_id
]);
}
return $this->render('capsule/groups/edit.html.twig', [
'selectCapsuleGroupsForm' => $form->createView(),
'capsule' => $capsule,
'groups' => $groups,
'existing_groups' => $editor_groups_not_added
]);
}
/**
* @Route("capsule/{capsule_id}/groups/create", name="create_group")
*/
public function create(int $capsule_id, Request $request): Response
{
$current_user = $this->getUser();
if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout');
}
$capsule = $this->capsule_repository->findOneBy(['id' => $capsule_id]);
if (! $capsule instanceof Capsule) {
throw new \Exception("Capsule does not exist");
}
$form = $this->createForm(CreateCapsuleGroupsFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$group = $this->createAGroup($form, $capsule);
$this->addFlash(
'success',
$this->translator->trans(
'groups.create.success',
[
'group_name' => $group->getName()
]
)
);
return $this->redirectToRoute('edit_capsule_groups', [
'capsule_id' => $capsule_id
]);
}
return $this->render('capsule/groups/create.html.twig', [
'createCapsuleGroupsForm' => $form->createView(),
'capsule' => $capsule
]);
}
/**
* @Route("/capsule/{capsule_id}/groups/{group_id}/remove", name="remove_group")
*/
public function remove(
int $capsule_id,
int $group_id,
Request $request
): Response {
$capsule = $this->capsule_repository->findOneBy(['id' => $capsule_id]);
if (! $capsule instanceof Capsule) {
throw new \Exception('Capsule not found');
}
$group = $this->group_repository->findOneBy(['id' => $group_id]);
if (! $group instanceof Group) {
throw new \Exception('Group not found');
}
$current_user = $this->getUser();
if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout');
}
$form = $this->createForm(RemoveGroupFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$capsule->removeGroup($group);
$this->entity_manager->persist($capsule);
$this->entity_manager->flush();
$this->addFlash(
'success',
$this->translator->trans(
'groups.remove.success',
[
'group_name' => $group->getName(),
'capsule_name' => $capsule->getName()
]
)
);
return $this->redirectToRoute('edit_capsule_groups', [
'capsule_id' => $capsule->getId()
]);
}
return $this->render('capsule/groups/remove.html.twig', [
'removeGroupForm' => $form->createView(),
'group_name' => $group->getName(),
'capsule_id' => $capsule_id
]);
}
/**
* @return array<Group>
*/
private function addGroups(FormInterface $form, Capsule $capsule): array
{
$groups_data = $form->getData()['name'];
$groups = [];
foreach ($groups_data as $group_data) {
$group_db = $this->group_repository->findOneBy(['id' => $group_data->getId()]);
if (! $group_db instanceof Group) {
throw new \Exception('Group not found');
}
$groups[] = $group_db;
$capsule->addGroup($group_db);
$this->entity_manager->persist($capsule);
}
$this->entity_manager->flush();
return $groups;
}
private function createAGroup(FormInterface $form, Capsule $capsule): Group
{
$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();
return $group;
}
/**
* @return array<Group>
*/
private function getExistingGroupsForCurrentEditor(User $current_user): array
{
$editor_capsules = $current_user->getCapsules();
$existing_groups_for_current_editor = [];
foreach ($editor_capsules as $editor_capsule) {
$existing_groups_for_current_editor = array_merge(
$existing_groups_for_current_editor,
$editor_capsule->getGroups()->toArray()
);
}
return $existing_groups_for_current_editor;
}
/**
* @param Collection<Group> $groups
* @return array<Group>
*/
private function getEditorGroupsNotAdded(User $current_user, Collection $groups): array
{
$editor_existing_groups = $this->getExistingGroupsForCurrentEditor($current_user);
return array_udiff(
$editor_existing_groups,
$groups->toArray(),
function ($obj_a, $obj_b) {
return $obj_a->getId() - $obj_b->getId();
}
);
}
}
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 ...@@ -90,9 +90,16 @@ class Capsule
*/ */
private Collection $editors; private Collection $editors;
/**
* @var Collection<Group>
* @ORM\ManyToMany(targetEntity=Group::class, mappedBy="capsules")
*/
private Collection $groups;
public function __construct() public function __construct()
{ {
$this->editors = new ArrayCollection(); $this->editors = new ArrayCollection();
$this->groups = new ArrayCollection();
} }
public function getId(): int public function getId(): int
...@@ -205,14 +212,13 @@ class Capsule ...@@ -205,14 +212,13 @@ class Capsule
} }
/** /**
* @return Collection<User> $editors * @return Collection<User>
*/ */
public function getEditors(): Collection public function getEditors(): Collection
{ {
return $this->editors; return $this->editors;
} }
public function getVideoPreviewImageLink(): ?string public function getVideoPreviewImageLink(): ?string
{ {
$file_system = new Filesystem(); $file_system = new Filesystem();
...@@ -231,4 +237,31 @@ class Capsule ...@@ -231,4 +237,31 @@ class Capsule
return 'https://vumbnail.com/' . $video_id . '.jpg'; 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;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment