Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature edit line timetable #90

Open
wants to merge 6 commits into
base: line-timetable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

abstract class AbstractController extends Controller
{
Expand All @@ -14,6 +16,17 @@ protected function isGranted($businessId, $object = null)
}
}

/**
* Checking the request is POST ajax
*
* @param Request $request
*/
protected function isPostAjax(Request $request)
{
if (!($request->isXmlHttpRequest() && $request->isMethod('POST')))
throw new AccessDeniedException();
}

protected function addFlashIfSeasonLocked($season)
{
$isLocked = (!empty($season) && $season->isLocked());
Expand Down Expand Up @@ -50,4 +63,19 @@ protected function addFlashMessage($type, $message, $parameters = array())
)
);
}

/**
* Preparing a JsonResponse
*
* @param array $data
* @param integer $statusCode
*/
protected function prepareJsonResponse($data = array(), $statusCode = JsonResponse::HTTP_STATUS_OK)
{
$response = new JsonResponse();
$response->setData($data);
$response->setStatusCode($statusCode);

return $response;
}
}
236 changes: 188 additions & 48 deletions Controller/BlockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace CanalTP\MttBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use CanalTP\MttBundle\Entity\Block;
use CanalTP\MttBundle\Entity\BlockRepository;
use CanalTP\MttBundle\Entity\Timetable;
use CanalTP\MttBundle\Entity\LineTimetable;
use CanalTP\MttBundle\Entity\StopTimetable;
use CanalTP\MttBundle\Form\Type\Block\BlockType;

class BlockController extends AbstractController
{
Expand All @@ -12,40 +17,129 @@ class BlockController extends AbstractController
*
* @param string $externalNetworkId
* @param integer $timetableId
* @param string $blockType
* @param string $domId
* @param string $type
* @param integer $rank
*/
public function addAction(Request $request, $externalNetworkId, $timetableId, $type, $rank)
{
$this->isGranted(
array(
'BUSINESS_MANAGE_LINE_TIMETABLE',
'BUSINESS_MANAGE_STOP_TIMETABLE'
)
);

$blockManager = $this->get('canal_tp_mtt.block_manager');
$timetableManager = $this->get(Timetable::$managers[$type]);
$timetable = $timetableManager->find($timetableId);

$data = array(
'rank' => $rank,
'type' => $type,
'domId' => null
);

$block = $blockManager->findOrCreate(-1, $timetable, $data);

$form = $this->createForm(
new BlockType(),
$block,
array(
'action' => $this->generateUrl(
'canal_tp_mtt_block_add',
array(
'externalNetworkId' => $externalNetworkId,
'timetableId' => $timetableId,
'type' => $type,
'rank' => $rank
)
),
'em' => $this->get('doctrine.orm.entity_manager')
)
);

$form->handleRequest($request);
if ($form->isValid()) {
$blockManager = $this->get('canal_tp_mtt.block_manager');
$block = $form->getData();
$blockManager->save($block, $form['number']->getData());

if ($timetable instanceof StopTimetable) {
return $this->redirect(
$this->generateUrl(
'canal_tp_mtt_stop_timetable_edit',
array(
'externalNetworkId' => $externalNetworkId,
'seasonId' => $timetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $timetable->getLineConfig()->getExternalLineId(),
'externalRouteId' => $timetable->getExternalRouteId()
)
)
);
} elseif ($timetable instanceof LineTimetable) {
return $this->redirect(
$this->generateUrl(
'canal_tp_mtt_line_timetable_render',
array(
'externalNetworkId' => $externalNetworkId,
'seasonId' => $timetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $timetable->getLineConfig()->getExternalLineId(),
'mode' => 'edit'
)
)
);
}
}

return $this->render(
'CanalTPMttBundle:Block:form.html.twig',
array(
'form' => $form->createView(),
)
);
}

/**
* returns form for a given block type
* or save content of the block using Form factory
*/
public function editAction(
Request $request,
$externalNetworkId,
$timetableId,
$type,
$blockId,
$blockType,
$domId
)
{
$domId,
$rank
) {
$this->isGranted(
array(
'BUSINESS_MANAGE_LINE_TIMETABLE',
'BUSINESS_MANAGE_STOP_TIMETABLE'
)
);

$timetableManager = $this->get(Timetable::$managers[$type]);
$timetable = $timetableManager->find($timetableId);

$blockTypeFactory = $this->get('canal_tp_mtt.form.factory.block');
$stopTimetableManager = $this->get('canal_tp_mtt.stop_timetable_manager');
$blockManager = $this->get('canal_tp_mtt.block_manager');

$perimeterManager = $this->get('nmm.perimeter_manager');

$perimeter = $perimeterManager->findOneByExternalNetworkId(
$this->getUser()->getCustomer(),
$externalNetworkId
);
$stopTimetable = $stopTimetableManager->getStopTimetableById(
$timetableId,
$perimeter->getExternalCoverageId()
);

$data = array(
'dom_id' => $domId,
'type_id' => $blockType
'type' => $blockType,
'rank' => $rank,
'domId' => $domId
);

$block = $stopTimetable->getBlockByDomId($domId);

if (empty($block)) {
$block = new Block();
$block->setStopTimetable($stopTimetable);
}
$block = $blockManager->findOrCreate($blockId, $timetable, $data);

$blockTypeFactory->init(
$blockType,
Expand All @@ -57,42 +151,83 @@ public function editAction(
$form = $blockTypeFactory->buildForm()
->setAction($this->getRequest()->getRequestUri())
->setMethod('POST')->getForm();
$form->handleRequest($this->getRequest());

$form->handleRequest($request);

if ($form->isValid()) {
$blockTypeFactory->buildHandler()->process($form->getData(), $stopTimetable);
$blockTypeFactory->buildHandler()->process($form->getData(), $timetable);

return $this->redirect(
$this->generateUrl(
if ($timetable instanceof StopTimetable) {
return $this->redirectToRoute(
'canal_tp_mtt_stop_timetable_edit',
array(
'externalNetworkId' => $externalNetworkId,
'seasonId' => $stopTimetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $stopTimetable->getLineConfig()->getExternalLineId(),
'externalRouteId' => $stopTimetable->getExternalRouteId()
'externalNetworkId' => $externalNetworkId,
'seasonId' => $timetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $timetable->getLineConfig()->getExternalLineId(),
'externalRouteId' => $timetable->getExternalRouteId()
)
)
);
);
} elseif ($timetable instanceof LineTimetable) {
return $this->redirectToRoute(
'canal_tp_mtt_line_timetable_render',
array(
'externalNetworkId' => $externalNetworkId,
'seasonId' => $timetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $timetable->getLineConfig()->getExternalLineId(),
'mode' => 'edit'
)
);
}
}

return $this->render(
'CanalTPMttBundle:Block:get_form.html.twig',
array(
'form' => $form->createView(),
'form' => $form->createView(),
)
);
}

public function deleteAction($timetableId, $blockId, $externalNetworkId)
{
/**
* Deleting a block from a Timetable
*
* @param string $externalNetworkId
* @param integer $timetableId
* @param string $type
* @param integer $blockId
*/
public function deleteAction(
$externalNetworkId,
$timetableId,
$type,
$blockId
) {
$this->isGranted(
array(
'BUSINESS_MANAGE_LINE_TIMETABLE',
'BUSINESS_MANAGE_STOP_TIMETABLE'
)
);

$perimeterManager = $this->get('nmm.perimeter_manager');
$perimeter = $perimeterManager->findOneByExternalNetworkId(
$this->getUser()->getCustomer(),
$externalNetworkId
);
$stopTimetableManager = $this->get('canal_tp_mtt.stop_timetable_manager');
$repo = $this->getDoctrine()->getRepository('CanalTPMttBundle:Block');

$block = $repo->find($blockId);
$timetableManager = $this->get(Timetable::$managers[$type]);
$timetable = $timetableManager->find($timetableId);

if ($timetable->getLineConfig()->getSeason()->getPerimeter() !== $perimeter) {
throw new Exception('This timetable is not accessible via the network: ' . $externalNetworkId);
}

$block = $timetable->getBlockById($blockId);

if (empty($block)) {
throw new Exception('The block ' . $blockId . ' is not linked to Timetable ' . $timetableId);
}

if (!$block) {
throw $this->createNotFoundException(
$this->get('translator')->trans(
Expand All @@ -101,24 +236,29 @@ public function deleteAction($timetableId, $blockId, $externalNetworkId)
)
);
} else {
$this->getDoctrine()->getEntityManager()->remove($block);
$this->getDoctrine()->getEntityManager()->flush();
$this->get('canal_tp_mtt.block_manager')->delete($block);
}
$stopTimetable = $stopTimetableManager->getStopTimetableById(
$timetableId,
$perimeter->getExternalCoverageId()
);

return $this->redirect(
$this->generateUrl(
if ($timetable instanceof StopTimetable) {
return $this->redirectToRoute(
'canal_tp_mtt_stop_timetable_edit',
array(
'externalNetworkId' => $externalNetworkId,
'seasonId' => $stopTimetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $stopTimetable->getLineConfig()->getExternalLineId(),
'externalRouteId' => $stopTimetable->getExternalRouteId()
'externalNetworkId' => $externalNetworkId,
'seasonId' => $timetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $timetable->getLineConfig()->getExternalLineId(),
'externalRouteId' => $timetable->getExternalRouteId()
)
)
);
);
} elseif ($timetable instanceof LineTimetable) {
return $this->redirectToRoute(
'canal_tp_mtt_line_timetable_render',
array(
'externalNetworkId' => $externalNetworkId,
'seasonId' => $timetable->getLineConfig()->getSeason()->getId(),
'externalLineId' => $timetable->getLineConfig()->getExternalLineId(),
'mode' => 'edit'
)
);
}
}
}
Loading