From f7847b42e8485787b9b946f05ec0a03c6ee5dff7 Mon Sep 17 00:00:00 2001 From: Baptiste-Moench Date: Fri, 26 Feb 2021 09:10:42 +0100 Subject: [PATCH] =?UTF-8?q?#35:=20Ajout=20des=20champ=20date=20de=20cr?= =?UTF-8?q?=C3=A9ation=20de=20compte=20et=20derni=C3=A8re=20activit=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/AdminController.php | 85 +++++++++++------------- src/Controller/SecurityController.php | 14 +++- src/Controller/UserController.php | 28 ++++---- src/Entity/Customer.php | 46 +++++++++++++ src/Migrations/Version20210226073340.php | 37 +++++++++++ templates/admin/list.html.twig | 49 ++++++++------ 6 files changed, 177 insertions(+), 82 deletions(-) create mode 100644 src/Migrations/Version20210226073340.php diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index 6300b8f..03917a1 100755 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -2,36 +2,25 @@ namespace App\Controller; -use App\Entity\Customer; use App\Entity\HalfDayAdjustment; use App\Form\HalfDayAdjustmentType; use App\Repository\HalfDayAdjustmentRepository; -use App\Form\CustomerSettingsAccountType; -use App\Form\CustomerSettingsPasswordType; -use App\Form\CustomerSettingsProfileType; use App\Repository\CheckInRepository; use App\Repository\OptionsRepository; use App\Repository\SubscriptionRepository; -use App\Service\Services; -use Doctrine\Common\Persistence\ObjectManager; +use DateTime; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; -use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; -use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; -use Symfony\Component\Validator\Constraints\DateTime; use App\Form\HalfDayType; use App\Form\MonthType; use App\Form\PlaceType; use App\Form\PromoType; -use App\Form\CustomerType; use App\Form\CustomerSettingStatusType; use App\Form\TextHomeType; use App\Repository\CustomerRepository; use App\Repository\PromoRepository; -use phpDocumentor\Reflection\Types\Null_; class AdminController extends AbstractController { @@ -42,7 +31,7 @@ class AdminController extends AbstractController private $optionsRepository; private $promoRepository; private $halfDayAdjustmentRepository; - private $om; + private $manager; public function __construct( CheckInRepository $checkInRepository, @@ -51,7 +40,7 @@ public function __construct( OptionsRepository $optionsRepository, PromoRepository $promoRepository, HalfDayAdjustmentRepository $halfDayAdjustmentRepository, - ObjectManager $om + EntityManagerInterface $manager ) { $this->checkInRepository=$checkInRepository; $this->customerRepository=$customerRepository; @@ -59,7 +48,7 @@ public function __construct( $this->optionsRepository=$optionsRepository; $this->promoRepository=$promoRepository; $this->halfDayAdjustmentRepository=$halfDayAdjustmentRepository; - $this->om=$om; + $this->manager=$manager; } /** @@ -67,6 +56,10 @@ public function __construct( */ public function adminHome() { + ($this->getUser())->setLastActivityAt(new DateTime()); + $this->manager->persist($this->getUser()); + $this->manager->flush(); + return $this->render('admin/home.html.twig'); } @@ -133,9 +126,9 @@ public function activate($id) } else { $customer->setActive(0); } - $this->om->persist($customer); - $this->om->persist($promo); - $this->om->flush(); + $this->manager->persist($customer); + $this->manager->persist($promo); + $this->manager->flush(); return $this->redirect($_SERVER['HTTP_REFERER']); } @@ -169,16 +162,16 @@ public function profile($id, Request $request) $counter->handleRequest($request); if ($counter->isSubmitted() && $counter->isValid()) { - $this->om->persist($promo); - $this->om->flush(); + $this->manager->persist($promo); + $this->manager->flush(); }; $status = $this->createForm(CustomerSettingStatusType::class, $customer); $status->handleRequest($request); if ($status->isSubmitted() && $status->isValid()) { - $this->om->persist($customer); - $this->om->flush(); + $this->manager->persist($customer); + $this->manager->flush(); } return $this->render( @@ -221,8 +214,8 @@ public function boom() ] ); $raz = $subscriptions->setActive(0); - $this->om->persist($raz); - $this->om->flush(); + $this->manager->persist($raz); + $this->manager->flush(); } return $this->render('admin/raz.html.twig'); @@ -241,8 +234,8 @@ public function switch($id) } else { $switch = $customer->setRole('ROLE_USER'); } - $this->om->persist($switch); - $this->om->flush(); + $this->manager->persist($switch); + $this->manager->flush(); return $this->redirect($_SERVER['HTTP_REFERER']); } @@ -279,8 +272,8 @@ public function text(Request $request) $formtext->handleRequest($request); if ($formtext->isSubmitted() && $formtext->isValid()) { - $this->om->persist($text); - $this->om->flush(); + $this->manager->persist($text); + $this->manager->flush(); $this->addFlash( 'option', @@ -292,8 +285,8 @@ public function text(Request $request) $formplace->handleRequest($request); if ($formplace->isSubmitted() && $formplace->isValid()) { - $this->om->persist($place); - $this->om->flush(); + $this->manager->persist($place); + $this->manager->flush(); $this->addFlash( 'option', @@ -305,8 +298,8 @@ public function text(Request $request) $formhalfday->handleRequest($request); if ($formhalfday->isSubmitted() && $formhalfday->isValid()) { - $this->om->persist($halfday); - $this->om->flush(); + $this->manager->persist($halfday); + $this->manager->flush(); $this->addFlash( 'option', @@ -318,8 +311,8 @@ public function text(Request $request) $formmonth->handleRequest($request); if ($formmonth->isSubmitted() && $formmonth->isValid()) { - $this->om->persist($month); - $this->om->flush(); + $this->manager->persist($month); + $this->manager->flush(); $this->addFlash( 'option', @@ -356,8 +349,8 @@ public function textActive() } else { $option->setActive(1); } - $this->om->persist($option); - $this->om->flush(); + $this->manager->persist($option); + $this->manager->flush(); return $this->redirect($_SERVER['HTTP_REFERER']); } @@ -417,10 +410,10 @@ public function price(OptionsRepository $optionsRepository, Request $request) } } elseif ($checkin->getHalfDay() == 0) { if (!isset($days[$customer_id])) { - $days[$customer_id] = 0; + $days[$customer_id] = 0; } - }; - + } + $days[$customer_id] -= $checkin->getFree(); if (isset($free[$customer_id])) { $free[$customer_id] += $checkin->getFree(); @@ -468,7 +461,7 @@ public function price(OptionsRepository $optionsRepository, Request $request) 'label' => 'Month' ])->getContent(); - // For the detailed display of the customer's checkins in the facturation table + // For the detailed display of the customer's checkins in the facturation table $all_checkins = []; foreach ($customers as $key => $customer) { $user_checkins = $this->checkInRepository->findBy( @@ -498,7 +491,7 @@ public function price(OptionsRepository $optionsRepository, Request $request) $Mois_arrivee_num = $Arrivee->format('m'); $Annee_arrivee_num = $Arrivee->format('Y'); $Heure_arrivee = $Arrivee->format('H:i:s'); - + $Depart = $checkin->getLeaving(); if($Depart != null) { @@ -519,7 +512,7 @@ public function price(OptionsRepository $optionsRepository, Request $request) $Demijournees = $checkin->getHalfDay(); $Demijournees_offertes = $checkin->getFree(); - + $Difference_depart_arrivee = $checkin->getDiff(); $tab[$line][] = [ 'jour_arrivee_str' => $Jour_arrivee_str, @@ -564,8 +557,8 @@ public function price(OptionsRepository $optionsRepository, Request $request) $formHalfDayAdjustment->handleRequest($request); if ($formHalfDayAdjustment->isSubmitted() && $formHalfDayAdjustment->isValid()) { - $this->om->persist($ajustement); - $this->om->flush(); + $this->manager->persist($ajustement); + $this->manager->flush(); } $all_adjustments = []; @@ -576,7 +569,7 @@ public function price(OptionsRepository $optionsRepository, Request $request) ['id' => 'DESC'] ); }; - + return $this->render( 'admin/facturation.html.twig', [ diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 950f398..abe459e 100755 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -12,7 +12,8 @@ use App\Repository\CustomerRepository; use App\Repository\OptionsRepository; use App\Service\Services; -use Doctrine\Common\Persistence\ObjectManager; +use DateTime; +use Doctrine\ORM\EntityManagerInterface; use function Sodium\add; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -29,6 +30,7 @@ class SecurityController extends AbstractController private $utils; private $adminMail = "coworking-flex@alsacedigitale.org"; private $options; + private $manager; /** * SecurityController constructor. @@ -37,10 +39,12 @@ class SecurityController extends AbstractController */ public function __construct( AuthenticationUtils $utils, - OptionsRepository $options + OptionsRepository $options, + EntityManagerInterface $manager ) { $this->utils = $utils; $this->options = $options; + $this->manager = $manager; } /** @@ -110,6 +114,10 @@ public function index( ); if ($this->getUser()) { + ($this->getUser())->setLastActivityAt(new DateTime()); + $this->manager->persist($this->getUser()); + $this->manager->flush(); + if ($this->getUser()->getRole() == 'ROLE_USER') { return $this->redirectToRoute('user_home'); } elseif ($this->getUser()->getRole() == 'ROLE_ADMIN') { @@ -212,9 +220,11 @@ public function create( $hash = $encoder->encodePassword($customer, $customer->getPassword()); if ($customerRepository->findAll()) { $customer->setPassword($hash) + ->setCreatedAt(new DateTime()) ->setRole('ROLE_USER'); } else { $customer->setPassword($hash) + ->setCreatedAt(new DateTime()) ->setRole('ROLE_ADMIN'); } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 177ba79..0a27411 100755 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -2,7 +2,6 @@ namespace App\Controller; -use App\Entity\Customer; use App\Form\CustomerSettingsAccountType; use App\Form\CustomerSettingsPasswordType; use App\Form\CustomerSettingsProfileType; @@ -11,7 +10,7 @@ use App\Repository\SubscriptionRepository; use App\Repository\HalfDayAdjustmentRepository; use App\Service\Services; -use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -19,7 +18,6 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; -use Symfony\Component\Validator\Constraints\DateTime; /** * Class UserController @@ -31,15 +29,19 @@ class UserController extends AbstractController { private $subscriptionRepository; private $checkInRepository; + private $manager; + private $halfDayAdjustmentRepository; public function __construct( SubscriptionRepository $subscriptionRepository, CheckInRepository $checkInRepository, - HalfDayAdjustmentRepository $halfDayAdjustmentRepository + HalfDayAdjustmentRepository $halfDayAdjustmentRepository, + EntityManagerInterface $manager ) { $this->subscriptionRepository = $subscriptionRepository; $this->checkInRepository = $checkInRepository; $this->halfDayAdjustmentRepository = $halfDayAdjustmentRepository; + $this->manager = $manager; } /** @@ -48,6 +50,10 @@ public function __construct( */ public function home(Services $services) { + ($this->getUser())->setLastActivityAt(new \DateTime()); + $this->manager->persist($this->getUser()); + $this->manager->flush(); + $subscription = $this->subscriptionRepository->findOneBy(['customer' => $this->getUser()]); $checkin = $this->checkInRepository->findOneBy(['customer' => $this->getUser(), 'leaving' => null]); @@ -89,14 +95,12 @@ public function account() /** * @Route("/account/settings", name="user_account_settings", methods="GET|POST") * @param Request $request - * @param ObjectManager $manager * @param AuthenticationUtils $utils * @param UserPasswordEncoderInterface $encoder * @return Response */ public function accountSettings( Request $request, - ObjectManager $manager, AuthenticationUtils $utils, UserPasswordEncoderInterface $encoder ):Response { @@ -104,7 +108,7 @@ public function accountSettings( $formProfile->handleRequest($request); if ($formProfile->isSubmitted() && $formProfile->isValid()) { - $manager->flush(); + $this->manager->flush(); $this->addFlash( 'CustomerSettingsValid', 'Modifications enregistrées avec succès.' @@ -125,7 +129,7 @@ public function accountSettings( if ($encoder->isPasswordValid($this->getUser(), $formPassword->get('old_password')->getData())) { $this->getUser()->setPassword($encoder->encodePassword($this->getUser(), $formPassword ->get('password')->getData())); - $manager->flush(); + $this->manager->flush(); $this->addFlash( 'CustomerSettingsValid', 'Modifications enregistrées avec succès.' @@ -144,7 +148,7 @@ public function accountSettings( $formAccount->handleRequest($request); if ($formAccount->isSubmitted() && $formAccount->isValid()) { - $manager->flush(); + $this->manager->flush(); $this->addFlash( 'CustomerSettingsValid', 'Modifications enregistrées avec succès.' @@ -238,7 +242,7 @@ public function listing(OptionsRepository $optionsRepository) $Mois_arrivee_num = $Arrivee->format('m'); $Annee_arrivee_num = $Arrivee->format('Y'); $Heure_arrivee = $Arrivee->format('H:i:s'); - + $Depart = $checkin->getLeaving(); if($Depart != null) { @@ -259,7 +263,7 @@ public function listing(OptionsRepository $optionsRepository) $Demijournees = $checkin->getHalfDay(); $Demijournees_offertes = $checkin->getFree(); - + $Difference_depart_arrivee = $checkin->getDiff(); $all_checkins[$line][] = [ 'jour_arrivee_str' => $Jour_arrivee_str, @@ -291,7 +295,7 @@ public function listing(OptionsRepository $optionsRepository) } else { - $all_days[$key] = $day['demi_journees'] - $day['demi_journees_free']; + $all_days[$key] = $day['demi_journees'] - $day['demi_journees_free']; } } } diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 79c5be4..f3db4f6 100755 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -124,6 +124,16 @@ class Customer implements UserInterface */ private $status; + /** + * @ORM\Column(type="datetime", nullable=true) + */ + private $createdAt; + + /** + * @ORM\Column(type="datetime", nullable=true) + */ + private $lastActivityAt; + public function __construct() { $this->subscriptions = new ArrayCollection(); @@ -396,4 +406,40 @@ public function setStatus(string $status): self return $this; } + + /** + * @return mixed + */ + public function getLastActivityAt() + { + return $this->lastActivityAt; + } + + /** + * @param mixed $lastActivityAt + * @return Customer + */ + public function setLastActivityAt($lastActivityAt): self + { + $this->lastActivityAt = $lastActivityAt; + return $this; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param mixed $createdAt + * @return Customer + */ + public function setCreatedAt($createdAt): self + { + $this->createdAt = $createdAt; + return $this; + } } diff --git a/src/Migrations/Version20210226073340.php b/src/Migrations/Version20210226073340.php new file mode 100644 index 0000000..f240999 --- /dev/null +++ b/src/Migrations/Version20210226073340.php @@ -0,0 +1,37 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE half_day_adjustment (id INT AUTO_INCREMENT NOT NULL, customer_id INT NOT NULL, counteradd INT DEFAULT NULL, counterremove INT DEFAULT NULL, arrival_month VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); + $this->addSql('ALTER TABLE customer ADD status VARCHAR(255) DEFAULT NULL, ADD created_at DATETIME DEFAULT NULL, ADD last_activity_at DATETIME DEFAULT NULL'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('DROP TABLE half_day_adjustment'); + $this->addSql('ALTER TABLE customer DROP status, DROP created_at, DROP last_activity_at'); + } +} diff --git a/templates/admin/list.html.twig b/templates/admin/list.html.twig index 9a826ba..91c4617 100644 --- a/templates/admin/list.html.twig +++ b/templates/admin/list.html.twig @@ -22,6 +22,8 @@ Statut Société Role + Date de création + Dernière activité Profil @@ -29,31 +31,34 @@ {% for customer in customers %} - - - {{ customer.lastname }} - {{ customer.firstname }} - {{ customer.username }} - {{ customer.mail }} - {% if customer.status is null or customer.status is empty %} - {% else %} {{ customer.status }} {% endif %} - {{ customer.society }} - {% if customer.role == 'ROLE_USER' %} - Utilisateur - {% else %} - Administrateur - {% endif %} - - - Voir le profil - - - + + + {{ customer.lastname }} + {{ customer.firstname }} + {{ customer.username }} + {{ customer.mail }} + {% if customer.status is null or customer.status is empty %} - {% else %} {{ customer.status }} {% endif %} + {{ customer.society }} + {% if customer.role == 'ROLE_USER' %} + Utilisateur + {% else %} + Administrateur + {% endif %} + + + {% if customer.createdAt is null %}-{% else %}{{ customer.createdAt | date('d-m-Y') }}{% endif %} + + + {% if customer.lastActivityAt is null %}-{% else %}{{ customer.lastActivityAt | date('d-m-Y') }}{% endif %} + + + Voir le profil + + + {% endfor %} - - -