isGranted('ROLE_SUPER_ADMIN')) { $organizations = $this->entityManager->getRepository(Organizations::class)->findBy(['isActive' => true]); } else { $user = $this->getUser(); if (!$user) { return $this->redirectToRoute('app_login'); } $userIdentifier = $user->getUserIdentifier(); $organizations = $this->entityManager->getRepository(UsersOrganizations::class)->findOrganizationsByUserEmailAndRoleName($userIdentifier, 'ADMIN'); if (!$organizations) { // if user is not admin in any organization, throw access denied throw $this->createNotFoundException(self::ACCESS_DENIED); } } return $this->render('organization/index.html.twig', [ 'organizations' => $organizations, ]); } #[Route('/new', name: 'new', methods: ['GET', 'POST'])] public function new(Request $request): Response { if (!$this->isGranted('ROLE_SUPER_ADMIN')) { throw $this->createNotFoundException(self::ACCESS_DENIED); } $form = $this->createForm(OrganizationForm::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $organization = $form->getData(); // dd($form); $logoFile = $form->get('logoUrl')->getData(); if ($logoFile) { $currentDate = (new \DateTime())->format('Y-m-d'); $organizationName = preg_replace('/[^a-zA-Z0-9]/', '_', $organization->getName()); $extension = $logoFile->guessExtension(); $newFilename = $currentDate . '_' . $organizationName . $extension; // Move the file to the directory where logos are stored $logoFile->move( $this->getParameter('logos_directory'), $newFilename ); // Update the 'logoUrl' property to store the file name $organization->setLogoUrl($newFilename); } $this->entityManager->persist($organization); $this->entityManager->flush(); $this->addFlash('success', 'Organization created successfully'); return $this->redirectToRoute('organization_index'); } return $this->render('organization/new.html.twig', [ 'form' => $form->createView(), ]); } #[Route('/{id}', name: 'show', requirements: ['id' => '\d+'], methods: ['GET'])] public function show(int $id, ActionService $actionService): Response { if ($this->isGranted('ROLE_ADMIN')) { $user = $this->getUser(); if (!$user) { return $this->redirectToRoute('app_login'); } //Don't care about the null pointer because if no UO found, it won't pass the previous check $organization = $this->entityManager->getRepository(Organizations::class)->find($id); $newUsers = $this->entityManager->getRepository(UsersOrganizations::class)->getLastNewActiveUsersByOrganization($organization); $adminUsers = $this->entityManager->getRepository(UsersOrganizations::class)->getAdminUsersByOrganization($organization); // reusing the method to avoid code duplication even though it returns an array of UsersOrganizations $org = $this->usersOrganizationService->findActiveUsersByOrganizations([$organization]); // get all applications $applications = $this->organizationsService->getApplicationsWithAccessStatus($organization); $actions = $organization->getActions()->toArray(); usort($actions, static function ($a, $b) { return $b->getDate() <=> $a->getDate(); }); //get the last 10 activities $actions = array_slice($actions, 0, 10); $activities = array_map(static function ($activity) use ($actionService) { return [ 'date' => $activity->getDate(), // or however you access the date 'actionType' => $activity->getActionType(), 'users' => $activity->getUsers(), 'color' => $actionService->getActivityColor($activity->getDate()) ]; }, $actions); } else { throw $this->createNotFoundException(self::ACCESS_DENIED); } return $this->render('organization/show.html.twig', [ 'organization' => $organization, 'adminUsers' => $adminUsers, 'newUsers' => $newUsers, 'org' => !empty($org) ? $org[0] : null, 'applications' => $applications, 'activities' => $activities ]); } #[Route('/edit/{id}', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'POST'])] public function edit(Request $request): Response { $id = $request->attributes->get('id'); if (!$this->isGranted('ROLE_SUPER_ADMIN')) { throw $this->createNotFoundException(self::ACCESS_DENIED); } $organization = $this->entityManager->getRepository(Organizations::class)->find($id); if (!$organization) { throw $this->createNotFoundException(self::NOT_FOUND); } $form = $this->createForm(OrganizationForm::class, $organization); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $logoFile = $form->get('logoUrl')->getData(); if ($logoFile) { $currentDate = (new \DateTime())->format('Y-m-d'); $organizationName = preg_replace('/[^a-zA-Z0-9]/', '_', $organization->getName()); $extension = $logoFile->guessExtension(); $newFilename = $currentDate . '_' . $organizationName . '.' . $extension; // Move the file to the directory where logos are stored $logoFile->move( $this->getParameter('logos_directory'), $newFilename ); // Update the 'logoUrl' property to store the file name $organization->setLogoUrl($newFilename); } $this->entityManager->persist($organization); $this->entityManager->flush(); $this->addFlash('success', 'Organization updated successfully'); return $this->redirectToRoute('organization_index'); } return $this->render('organization/edit.html.twig', [ 'form' => $form->createView(), 'organization' => $organization, ]); } }