From ac3bf65a757bc8d2125e8af364dc667736bc9c02 Mon Sep 17 00:00:00 2001
From: Moreau Elise <moreau.elise13@gmail.com>
Date: Sat, 10 Oct 2020 11:15:46 +0200
Subject: [PATCH] add class and form to modify group's information when you're
 admin

---
 .../Actions/Group/ModifyGroupAction.php       | 65 +++++++++++++++++++
 .../Actions/Group/ViewModifyGroupForm.php     | 35 ++++++++++
 2 files changed, 100 insertions(+)
 create mode 100644 src/Application/Actions/Group/ModifyGroupAction.php
 create mode 100644 src/Application/Actions/Group/ViewModifyGroupForm.php

diff --git a/src/Application/Actions/Group/ModifyGroupAction.php b/src/Application/Actions/Group/ModifyGroupAction.php
new file mode 100644
index 0000000..fd1bee3
--- /dev/null
+++ b/src/Application/Actions/Group/ModifyGroupAction.php
@@ -0,0 +1,65 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Application\Actions\Group;
+
+use Psr\Http\Message\ResponseInterface as Response;
+use Psr\Http\Message\ServerRequestInterface as Request;
+use App\Domain\Group\Group ;
+use DateTime;
+
+class ModifyGroupAction extends GroupAction
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function action(): Response
+    {
+
+        $parsedRequestBody = (array)$this->request->getParsedBody();
+
+        $name =  $this->checkvalue($parsedRequestBody['name']);
+        $description = $this->checkvalue($parsedRequestBody['description']);
+        $private = $this->checkvalue($parsedRequestBody['private']);
+        $group_id = $this->checkvalue($parsedRequestBody['id']);
+
+        if (!$this->groupRepository->findOneBy(array('name' => $name))) {
+            return $this->twig->render(
+                $this->response, "/group/modify_group.twig",
+                array(
+                    "name" => $name,
+                    "description" => $description,
+                    "private" => $private,
+                    "name_formstatus" => true,
+                    "formstatus" => "error"
+                )
+            );
+        }
+        $group_db = $this->groupRepository->find($group_id);
+
+        $group_db->setGroupname($name);
+        $group_db->setDescription($description);
+        $group_db->setPrivate($private);
+
+        $this->em->persist($group_db);
+        $this->em->flush();
+
+        $this->logger->info("Your account has been changed.");
+
+        return $this->response
+            ->withHeader('Location', '/account')
+            ->withStatus(302);
+    }
+
+    /**
+     * Remove html chars
+     * @return string
+     */
+    protected function checkValue($value): string
+    {
+        $value = strip_tags($value);
+        $value = htmlspecialchars_decode($value);
+        return $value;
+    }
+
+}
diff --git a/src/Application/Actions/Group/ViewModifyGroupForm.php b/src/Application/Actions/Group/ViewModifyGroupForm.php
new file mode 100644
index 0000000..8a7e00e
--- /dev/null
+++ b/src/Application/Actions/Group/ViewModifyGroupForm.php
@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Application\Actions\Group;
+
+use Psr\Http\Message\ResponseInterface as Response;
+use App\Domain\Group\GroupNotFoundException;
+
+class ViewModifyGroupForm extends GroupAction
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function action(): Response
+    {
+        $parsedRequestBody = (array)$this->request->getParsedBody();
+
+        $groupId = (int) $parsedRequestBody['groupId'];;
+        $group = $this->groupRepository->find($groupId);
+        
+        if ($group->checkAdmin($_SESSION['userId'])){
+            return $this->response
+            ->withHeader('Location', '/account')
+            ->withStatus(302);
+        }
+        
+        $group = $this->groupRepository->find($groupId);
+
+        if (!isset($group)) {
+            throw new GroupNotFoundException();
+        }
+
+        return $this->twig->render($this->response, "/group/modify_group.twig", ["group" => $group, "session" => $_SESSION]);
+    }
+}
-- 
GitLab