Passed
Push — master ( 216452...419cb0 )
by Anthony
02:46
created

AccountsController::editUserAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Routing\Annotation\Route;
9
10
class AccountsController extends Controller
11
{
12
	//-------------------------------------------- DISPLAY VIEWS ---------------------------------------------------------------//
13
	/**
14
	 * @Route("/accounts/", name="ribsadmin_accounts")
15
	 * @return Response function that return a list of all users that are archived or not and different of current accout
16
	 */
17
	public function AccountsListAction(): Response
18
	{
19
		$em = $this->getDoctrine()->getManager();
20
		$current_account = $this->getUser()->getUser();
21
		
22
		$users = $em->getRepository("RibsAdminBundle:Account")->findAllUserArchived($current_account);
23
		$users_archived = $em->getRepository("RibsAdminBundle:Account")->findAllUserArchived($current_account, true);
24
		
25
		return $this->render('@RibsAdmin/accounts/list-all-accounts.html.twig', [
26
			"users" => $users,
27
			"users_archived" => $users_archived
28
		]);
29
	}
30
	
31
	/**
32
	 * @Route("/accounts/create/", name="ribsadmin_accounts_create")
33
	 * @Route("/accounts/edit", name="ribsadmin_accounts_edit")
34
	 * @return Response
35
	 */
36
	public function editUserAction(): Response {
37
		return $this->render("@RibsAdmin/accounts/create.html.twig");
38
	}
39
	//-------------------------------------------- END DISPLAY VIEWS -----------------------------------------------------------//
40
	
41
	/**
42
	 * @Route("/account/archive/{guid}/{activate}", name="ribsadmin_account_archive")
43
	 * @param string $guid
44
	 * @return RedirectResponse function that will disable or activate a user
45
	 */
46
	public function archiveAccount(string $guid, bool $activate = false): RedirectResponse
47
	{
48
		$em = $this->getDoctrine()->getManager();
49
		
50
		$user = $em->getRepository("RibsAdminBundle:User")->findOneBy(["guid" => $guid]);
51
		
52
		if ($user) {
53
			if ($activate === true) {
54
				$user->setArchived(false);
55
				$word = "activated";
56
			} else {
57
				$user->setArchived(true);
58
				$word = "disabled";
59
			}
60
			
61
			$em->persist($user);
62
			$em->flush();
63
			
64
			$this->addFlash("success-flash", "The user " . $user->getFirstname() . " " . $user->getLastname() .
65
				" was " . $word . " sucessfuly");
66
		}
67
		
68
		return $this->redirectToRoute("ribsadmin_accounts");
69
	}
70
}