Skip to content
Snippets Groups Projects
Commit 6327b72d authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Debut de la gestion des types de sessions

parent f6dd37c2
No related branches found
No related tags found
No related merge requests found
Showing
with 533 additions and 0 deletions
<?php
namespace Formation;
use Formation\Controller\SessionTypeController;
use Formation\Controller\SessionTypeControllerFactory;
use Formation\Form\SessionType\SessionTypeForm;
use Formation\Form\SessionType\SessionTypeFormFactory;
use Formation\Form\SessionType\SessionTypeHydrator;
use Formation\Form\SessionType\SessionTypeHydratorFactory;
use Formation\Provider\Privilege\SessiontypePrivileges;
use Formation\Service\SessionType\SessionTypeService;
use Formation\Service\SessionType\SessionTypeServiceFactory;
use Laminas\Router\Http\Literal;
use UnicaenPrivilege\Guard\PrivilegeController;
return [
'bjyauthorize' => [
'guards' => [
PrivilegeController::class => [
[
'controller' => SessionTypeController::class,
'action' => [
'index',
],
'privileges' => [
SessiontypePrivileges::SESSIONTYPE_INDEX
],
],
],
],
],
// 'navigation' => [
// 'default' => [
// 'home' => [
// 'pages' => [
// 'gestion' => [
// 'pages' => [
// 'session' => [
// 'label' => 'Sessions',
// 'route' => 'session',
// 'resource' => PrivilegeController::getResourceId(SessionController::class, 'index'),
// 'order' => 1100,
// 'icon' => 'fas fa-angle-right',
// ],
// ],
// ],
// ],
// ],
// ],
// ],
'router' => [
'routes' => [
'session' => [
'type' => Literal::class,
'options' => [
'route' => '/session',
],
'may_terminate' => true,
'child_routes' => [
/** crud ******************************************************************/
'type' => [
'type' => Literal::class,
'options' => [
'route' => '/type',
'defaults' => [
/** @see SessionTypeController::indexAction() */
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [],
],
],
],
],
],
'service_manager' => [
'factories' => [
SessionTypeService::class => SessionTypeServiceFactory::class,
],
],
'controllers' => [
'factories' => [
SessionTypeController::class => SessionTypeControllerFactory::class,
],
],
'form_elements' => [
'factories' => [
SessionTypeForm::class => SessionTypeFormFactory::class,
],
],
'hydrators' => [
'factories' => [
SessionTypeHydrator::class => SessionTypeHydratorFactory::class,
],
],
'view_helpers' => [
'invokables' => [
],
],
];
\ No newline at end of file
<?php
namespace Formation\Controller;
use Formation\Form\SessionType\SessionTypeFormAwareTrait;
use Formation\Service\SessionType\SessionTypeServiceAwareTrait;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
class SessionTypeController extends AbstractActionController
{
use SessionTypeServiceAwareTrait;
use SessionTypeFormAwareTrait;
public function indexAction(): ViewModel
{
$sessionsTypes = $this->getSessionTypeService()->getSessionsTypes(true);
return new ViewModel([
'title' => "Types de session",
'sessionsTypes' => $sessionsTypes,
]);
}
}
\ No newline at end of file
<?php
namespace Formation\Controller;
use Formation\Form\SessionType\SessionTypeForm;
use Formation\Service\SessionType\SessionTypeService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class SessionTypeControllerFactory
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): SessionTypeController
{
/**
* @var SessionTypeService $sessionTypeService
* @var SessionTypeForm $sessionTypeForm
*/
$sessionTypeService = $container->get(SessionTypeService::class);
$sessionTypeForm = $container->get('FormElementManager')->get(SessionTypeForm::class);
$controller = new SessionTypeController();
$controller->setSessionTypeForm($sessionTypeForm);
$controller->setSessionTypeService($sessionTypeService);
return $controller;
}
}
\ No newline at end of file
<?php
namespace Formation\Entity\Db;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use UnicaenUtilisateur\Entity\Db\HistoriqueAwareInterface;
use UnicaenUtilisateur\Entity\Db\HistoriqueAwareTrait;
class SessionType implements HistoriqueAwareInterface {
use HistoriqueAwareTrait;
private ?int $id = null;
private ?string $libelle = null;
private ?string $description = null;
private Collection $sessions;
public function __construct() {
$this->sessions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(?string $libelle): void
{
$this->libelle = $libelle;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): void
{
$this->description = $description;
}
/** @return Session[] */
public function getSessions(): array
{
return $this->sessions->toArray();
}
}
\ No newline at end of file
<?php
namespace Formation\Form\SessionType;
use Laminas\Form\Element\Button;
use Laminas\Form\Element\Text;
use Laminas\Form\Element\Textarea;
use Laminas\Form\Form;
use Laminas\InputFilter\Factory;
class SessionTypeForm extends Form {
public function init(): void
{
//libelle
$this->add([
'type' => Text::class,
'name' => 'etiquette',
'options' => [
'label' => "Etiquette <span title='Champ obligatoire'></span>:",
'label_options' => ['disable_html_escape' => true,],
],
'attributes' => [
'id' => 'etiquette',
],
]);
//description
$this->add([
'name' => 'description',
'type' => Textarea::class,
'options' => [
'label' => "Description :",
'label_options' => [ 'disable_html_escape' => true,],
],
'attributes' => [
'id' => 'description',
'class' => 'tinymce',
],
]);
//button
$this->add([
'type' => Button::class,
'name' => 'creer',
'options' => [
'label' => '<i class="fas fa-save"></i> Enregistrer',
'label_options' => [
'disable_html_escape' => true,
],
],
'attributes' => [
'type' => 'submit',
'class' => 'btn btn-success',
],
]);
//input filter
$this->setInputFilter((new Factory())->createInputFilter([
'libelle' => ['required' => true,],
'description' => ['required' => false,],
]));
}
}
\ No newline at end of file
<?php
namespace Formation\Form\SessionType;
trait SessionTypeFormAwareTrait
{
protected SessionTypeForm $sessionTypeForm;
public function getSessionTypeForm(): SessionTypeForm
{
return $this->sessionTypeForm;
}
public function setSessionTypeForm(SessionTypeForm $sessionTypeForm): void
{
$this->sessionTypeForm = $sessionTypeForm;
}
}
\ No newline at end of file
<?php
namespace Formation\Form\SessionType;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class SessionTypeFormFactory
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): SessionTypeForm
{
/**
* @var SessionTypeHydrator $hydrator
*/
$hydrator = $container->get('HydratorManager')->get(SessionTypeHydrator::class);
$form = new SessionTypeForm();
$form->setHydrator($hydrator);
return $form;
}
}
\ No newline at end of file
<?php
namespace Formation\Form\SessionType;
use Formation\Entity\Db\SessionType;
use Laminas\Hydrator\HydratorInterface;
class SessionTypeHydrator implements HydratorInterface
{
public function extract(object $object): array
{
/** @var SessionType $object */
$data = [
'libelle' => $object->getLibelle(),
'description' => $object->getDescription(),
];
return $data;
}
public function hydrate(array $data, object $object): object
{
$libelle = (isset($data['libelle']) AND trim($data['libelle']) !== '')?trim($data['libelle']):null;
$description = (isset($data['description']) AND trim($data['description']) !== '')?$data['description']:null;
/** @var SessionType $object */
$object->setLibelle($libelle);
$object->setDescription($description);
return $object;
}
}
\ No newline at end of file
<?php
namespace Formation\Form\SessionType;
use Psr\Container\ContainerInterface;
class SessionTypeHydratorFactory
{
public function __invoke(ContainerInterface $container): SessionTypeHydrator
{
$hydrator = new SessionTypeHydrator();
return $hydrator;
}
}
\ No newline at end of file
<?php
namespace Formation\Provider\Privilege;
use UnicaenPrivilege\Provider\Privilege\Privileges;
class SessiontypePrivileges extends Privileges
{
const SESSIONTYPE_INDEX = 'sessiontype-sessiontype_index';
const SESSIONTYPE_AFFICHER = 'sessiontype-sessiontype_afficher';
const SESSIONTYPE_AJOUTER = 'sessiontype-sessiontype_ajouter';
const SESSIONTYPE_MODIFIER = 'sessiontype-sessiontype_modifier';
const SESSIONTYPE_HISTORISER = 'sessiontype-sessiontype_historiser';
const SESSIONTYPE_SUPPRIMER = 'sessiontype-sessiontype_supprimer';
}
<?php
namespace Formation\Service\SessionType;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use DoctrineModule\Persistence\ProvidesObjectManager;
use Formation\Entity\Db\SessionType;
use Laminas\Mvc\Controller\AbstractActionController;
use RuntimeException;
class SessionTypeService
{
use ProvidesObjectManager;
/** GESTION DES ENTITÉS *******************************************************************************************/
public function create(SessionType $sessionType): SessionType
{
$this->getObjectManager()->persist($sessionType);
$this->getObjectManager()->flush();
return $sessionType;
}
public function update(SessionType $sessionType): SessionType
{
$this->getObjectManager()->flush();
return $sessionType;
}
public function historise(SessionType $sessionType): SessionType
{
$sessionType->historiser();
$this->getObjectManager()->flush();
return $sessionType;
}
public function restore(SessionType $sessionType): SessionType
{
$sessionType->dehistoriser();
$this->getObjectManager()->flush();
return $sessionType;
}
public function delete(SessionType $sessionType): SessionType
{
$this->getObjectManager()->remove($sessionType);
$this->getObjectManager()->flush();
return $sessionType;
}
/** REQUETAGE *****************************************************************************************************/
public function createQueryBuilder(): QueryBuilder
{
$qb = $this->getObjectManager()->getRepository(SessionType::class)->createQueryBuilder('sessiontype')
->leftJoin('sessiontype.session', 'session')
;
return $qb;
}
/** @return SessionType[] */
public function getSessionsTypes(bool $withHisto = false): array
{
$qb = $this->createQueryBuilder()
->orderBy('sessiontype.libelle', 'ASC')
;
if (!$withHisto) $qb = $qb->andWhere('sessiontype.histoDestruction IS NULL');
$result = $qb->getQuery()->getResult();
return $result;
}
/** @return string[] */
public function getSessionsTypesAsOptions(bool $withHisto = false): array
{
$sessionsTypes = $this->getSessionsTypes($withHisto);
$options = [];
foreach ($sessionsTypes as $sessionType) {
$options[$sessionType->getId()] = $sessionType->getLibelle();
}
return $options;
}
public function getSessionType(?int $id): ?SessionType
{
$qb = $this->getObjectManager()->createQueryBuilder()
->andWhere('sessiontype.id = :id')->setParameter('id', $id);
try {
$result = $qb->getQuery()->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
throw new RuntimeException("Plusieurs [".SessionType::class."] partagent le même id [".$id."]",0,$e);
}
return $result;
}
public function getRequestedSessionType(AbstractActionController $controller, string $params = 'session-type'): ?SessionType
{
$id = $controller->params()->fromRoute($params);
$result = $this->getSessionType($id);
return $result;
}
/** FACADE ********************************************************************************************************/
}
\ No newline at end of file
<?php
namespace Formation\Service\SessionType;
trait SessionTypeServiceAwareTrait
{
private SessionTypeService $sessionTypeService;
public function getSessionTypeService(): SessionTypeService
{
return $this->sessionTypeService;
}
public function setSessionTypeService(SessionTypeService $sessionTypeService): void
{
$this->sessionTypeService = $sessionTypeService;
}
}
\ No newline at end of file
<?php
namespace Formation\Service\SessionType;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class SessionTypeServiceFactory
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): SessionTypeService
{
/** @var EntityManager $entityManager */
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$service = new SessionTypeService();
$service->setObjectManager($entityManager);
return $service;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment