Skip to content

Commit

Permalink
add line-timetable block edition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Passama committed Dec 9, 2015
1 parent 771298e commit 97c0046
Show file tree
Hide file tree
Showing 44 changed files with 1,622 additions and 177 deletions.
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(
'domId' => $domId,
'type' => $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

0 comments on commit 97c0046

Please sign in to comment.