Skip to content

Instantly share code, notes, and snippets.

@lipeRomani
Created July 12, 2017 14:40
Show Gist options
  • Save lipeRomani/508ff67b46f76070c4778862074fa34b to your computer and use it in GitHub Desktop.
Save lipeRomani/508ff67b46f76070c4778862074fa34b to your computer and use it in GitHub Desktop.
<?php
namespace Wideti\AdminBundle\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Wideti\AdminBundle\Form\GroupType;
use Wideti\DomainBundle\Document\Group\Group;
use Wideti\DomainBundle\Helpers\Controller\AdminControllerHelper;
use Wideti\DomainBundle\Service\Configuration\ConfigurationServiceAware;
use Wideti\DomainBundle\Service\Group\GroupServiceAware;
use Wideti\WebFrameworkBundle\Aware\FlashMessageAware;
use Wideti\WebFrameworkBundle\Aware\TwigAware;
class GroupController
{
use GroupServiceAware;
use TwigAware;
use ConfigurationServiceAware;
use FlashMessageAware;
/**
* @var AdminControllerHelper
*/
private $controllerHelper;
public function __construct(AdminControllerHelper $controllerHelper)
{
$this->controllerHelper = $controllerHelper;
}
public function createAction(Request $request)
{
$group = new Group();
$defaultConfigurations = $this->groupService->getAllDefaultConfigurations();
$form = $this->controllerHelper->createForm(new GroupType(), [
'entity' => $group,
'configurations' => $defaultConfigurations
]);
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$groupToSave = $this->groupService->prepareGroupToSave($data);
$this->groupService->create($groupToSave);
return $this->controllerHelper->redirect($this->controllerHelper->generateUrl('group_list'));
}
return $this->render(
'@Admin/Group/new.html.twig',
[
'form' => $form->createView(),
'defaultConfigurations' => $defaultConfigurations
]
);
}
public function editAction(Group $group, Request $request)
{
$form = $this->controllerHelper->createForm(new GroupType(), $group, [
'data' => [
'entity' => $group,
'configurations' => $group->getConfigurations()
]
]);
$form->handleRequest($request);
if ($form->isValid()) {
$before = $this->groupService->checkModuleIsActive('blockPerTimeOrAccessValidity');
$data = $form->getData();
$groupToUpdate = $this->groupService->prepareGroupToSave($data);
$this->groupService->create($groupToUpdate);
$after = $this->groupService->checkModuleIsActive('blockPerTimeOrAccessValidity');
if ($before || $after) {
$this->configurationService->deleteExpiration();
}
return $this->controllerHelper->redirect($this->controllerHelper->generateUrl('group_list'));
}
$defaultConfigurations = $this->groupService->getAllDefaultConfigurations();
return $this->render(
'@Admin/Group/edit.html.twig',
[
'form' => $form->createView(),
'defaultConfigurations' => $defaultConfigurations,
'group' => $group
]
);
}
public function listAction(Request $request)
{
$formFilter = $this->controllerHelper->createForm(
'wspot_group_form_filter',
null,
[
'action' => $this->controllerHelper->generateUrl('group_list')
]
);
$formFilter->handleRequest($request);
$groups = $this->groupService->getAllGroups($formFilter->getData());
return $this->render(
'@Admin/Group/index.html.twig',
[
"groups" => $groups,
'form' => $formFilter->createView(),
'btnCancel' => false
]
);
}
public function deleteAction(Group $group)
{
$client = $this->getLoggedClient();
$before = $this->groupService->checkModuleIsActive('blockPerTimeOrAccessValidity');
$result = $this->groupService->remove($group, $client);
$after = $this->groupService->checkModuleIsActive('blockPerTimeOrAccessValidity');
if ($before || $after) {
$this->configurationService->deleteExpiration();
}
if ($result) {
$message = [
'status' => "success",
'message'=> 'Grupo removido com sucesso, estamos movendo os visitantes para o Grupo Visitantes'
];
return new JsonResponse($message);
}
$message = [
"status" => 'error',
'message' => 'Não é possível excluir o grupo selecionado.'
];
return new JsonResponse($message);
}
public function showAction(Group $group, Request $request)
{
$groupId = $request->get('id');
$formFilter = $this->controllerHelper->createForm(
'wspot_guest_group_find',
null,
[
'action' => $this->controllerHelper->generateUrl('group_show', ['id' => $groupId])
]
);
$formFilter->handleRequest($request);
$page = $request->get('page');
if ($formFilter->isValid()) {
$valueSearch = $formFilter->getData()['value'];
$guests = $this->groupService->getGuestsByGroupPaginated($group, $page, 20, $valueSearch);
} else {
$guests = $this->groupService->getGuestsByGroupPaginated($group, $page);
}
$allGroups = $this->groupService->getAllGroups();
return $this->render(
'@Admin/Group/show.html.twig',
[
'group' => $group,
'guests' => $guests,
'form' => $formFilter->createView(),
'allGroups' => $allGroups
]
);
}
public function transferGuestAction(Request $request)
{
$groupShowId = $request->get('group-show-id');
$groupShortcode = $request->get('group');
$guestsIds = $request->get('guest-id');
$this->groupService->sendGuestTo($groupShortcode, $guestsIds);
$this->setFlashMessage('notice', 'Visitantes transferidos com sucesso.');
return $this->controllerHelper->redirect(
$this->controllerHelper->generateUrl('group_show', ['id' => $groupShowId])
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment