collection-crud/src/Controller/CameraController.php

138 lines
4.0 KiB
PHP
Raw Normal View History

2018-07-18 11:35:27 -04:00
<?php declare(strict_types=1);
2017-11-30 15:06:13 -05:00
namespace App\Controller;
2017-11-30 15:06:13 -05:00
use App\Entity\Camera;
use App\Form\CameraType;
2022-09-30 10:48:14 -04:00
use App\Traits\DeleteFormTrait;
use App\Traits\FormControllerTrait;
2022-09-29 18:35:53 -04:00
use Doctrine\ORM\EntityManagerInterface;
2018-02-14 15:08:03 -05:00
use Doctrine\ORM\ORMInvalidArgumentException;
2022-03-03 10:53:48 -05:00
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
2022-03-03 11:15:12 -05:00
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
2022-03-03 10:53:48 -05:00
use Symfony\Component\Routing\Annotation\Route;
2017-11-30 15:06:13 -05:00
/**
* Camera controller.
*/
#[Route(path: 'camera')]
class CameraController extends AbstractController
2017-11-30 15:06:13 -05:00
{
2022-03-03 10:53:48 -05:00
use FormControllerTrait;
2022-09-30 10:48:14 -04:00
use DeleteFormTrait;
2022-03-03 10:53:48 -05:00
protected const ENTITY = Camera::class;
2022-09-29 18:35:53 -04:00
protected const TEMPLATE_PATH = 'camera/';
protected const ROUTE_PREFIX = 'camera_';
2022-03-03 10:53:48 -05:00
protected const FORM = CameraType::class;
2022-09-29 18:35:53 -04:00
public function __construct(private readonly EntityManagerInterface $entityManager)
2022-03-03 10:53:48 -05:00
{
}
/**
* Lists all camera entities.
*/
#[Route(path: '/', name: 'camera_index', methods: ['GET'])]
public function indexAction(): Response
{
2022-09-29 18:35:53 -04:00
$receivedItems = $this->entityManager->getRepository(self::ENTITY)->findBy([
2022-03-03 11:15:12 -05:00
'received' => TRUE,
2022-03-03 10:53:48 -05:00
], [
'isWorking' => 'ASC',
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
2022-09-29 18:35:53 -04:00
$newItems = $this->entityManager->getRepository(self::ENTITY)->findBy([
2022-03-03 11:15:12 -05:00
'received' => FALSE,
2022-03-03 10:53:48 -05:00
], [
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
$working = array_filter($receivedItems, [$this, 'isWorking']);
$notWorking = array_filter($receivedItems, [$this, 'isNotWorking']);
return $this->render('camera/index.html.twig', [
'not_received' => $newItems,
'not_working' => $notWorking,
'working' => $working,
]);
}
/**
* Creates a new camera entity.
*/
#[Route(path: '/new', name: 'camera_new', methods: ['GET', 'POST'])]
public function newAction(Request $request): RedirectResponse|Response
{
2022-09-30 10:48:14 -04:00
return $this->itemCreate($request,'camera');
2022-03-03 10:53:48 -05:00
}
/**
* Finds and displays a camera entity.
*/
#[Route(path: '/{id}', name: 'camera_show', methods: ['GET'])]
public function showAction(Camera $camera): Response
{
2022-09-30 10:48:14 -04:00
return $this->itemView($camera,'camera');
2022-03-03 10:53:48 -05:00
}
/**
* Displays a form to edit an existing camera entity.
*
* @throws LogicException
*/
#[Route(path: '/{id}/edit', name: 'camera_edit', methods: ['GET', 'POST'])]
public function editAction(Request $request, Camera $camera): RedirectResponse|Response
{
2022-09-30 10:48:14 -04:00
return $this->itemUpdate($request, $camera,'camera');
2022-03-03 10:53:48 -05:00
}
/**
* Deletes a camera entity.
*
* @throws LogicException
*/
2022-09-30 10:48:14 -04:00
#[Route(path: '/{id}', name: 'camera_delete', methods: ['POST','DELETE'])]
2022-03-03 10:53:48 -05:00
public function deleteAction(Request $request, Camera $camera): RedirectResponse
{
2022-09-30 10:48:14 -04:00
return $this->itemDelete($request, $camera);
2022-03-03 10:53:48 -05:00
}
/**
* Moves a camera to the previouslyOwned table
*
* @throws LogicException
* @throws ORMInvalidArgumentException
*/
#[Route(path: '/{id}/deacquire', name: 'camera_deacquire', methods: ['POST'])]
public function deacquireAction(Request $request, Camera $camera): RedirectResponse
{
return $this->itemDeacquire($request, $camera, 'previously-owned-camera_index');
}
/**
* Creates a form to move
*
* @param Camera $camera The camera entity
*/
private function createDeacquireForm(Camera $camera): FormInterface
{
return $this->buildForm($camera, 'camera_deacquire');
}
private function isWorking(Camera $camera): bool
{
return $camera->getIsWorking();
}
private function isNotWorking(Camera $camera): bool
{
return ! $this->isWorking($camera);
}
2017-11-30 15:06:13 -05:00
}