Skip to content

Commit

Permalink
add layout_config hours limitation and some improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Passama committed Dec 23, 2015
1 parent 9d41a3e commit 23767d3
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 21 deletions.
12 changes: 11 additions & 1 deletion Controller/LineTimetableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,14 @@ public function showScheduleAction($externalNetworkId, $externalLineId)

$externalCoverageId = $perimeter->getExternalCoverageId();

$parameters = array();
$parameters['hourOffset'] = intVal($this->container->getParameter('canal_tp_mtt.hour_offset'));

$schedule = $this->get('canal_tp_mtt.calendar_manager')->getCalendarsForLine(
$externalCoverageId,
$externalNetworkId,
$externalLineId
$externalLineId,
$parameters
);

$navitia = $this->get('canal_tp_mtt.navitia');
Expand Down Expand Up @@ -372,6 +376,12 @@ public function loadCalendarAction(Request $request, $externalNetworkId, $blockI
$parameters['stopPoints'] = $selectedStopPoints;
}

$layoutConfig = $block->getLineTimetable()->getLineConfig()->getLayoutConfig();

$parameters['limits']['min'] = $layoutConfig->getCalendarStart();
$parameters['limits']['max'] = $layoutConfig->getCalendarEnd();
$parameters['hourOffset'] = intVal($this->container->getParameter('canal_tp_mtt.hour_offset'));

$schedule = $this->get('canal_tp_mtt.calendar_manager')->getCalendarForBlock(
$perimeter->getExternalCoverageId(),
$block,
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/CanalTPMttExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function load(array $configs, ContainerBuilder $container)
new FileLocator(__DIR__.'/../Resources/config')
);

$container->setParameter('canal_tp_mtt.hour_offset', $config['hour_offset']);

$loader->load('services.yml');
$loader->load('blocks.yml');
$loader->load('permissions.yml');
Expand Down
7 changes: 6 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('canal_tp_meth');
$rootNode = $treeBuilder->root('canal_tp_mtt');

$rootNode
->children()
Expand All @@ -31,6 +31,11 @@ public function getConfigTreeBuilder()
->scalarNode('server')->defaultValue(
'http://localhost'
)->end()
->end()
->end()
->scalarNode('hour_offset')
->info('Hour used for day switching in navitia route_schedules')
->defaultValue(4)
->end()
->end()
;
Expand Down
2 changes: 2 additions & 0 deletions Resources/public/css/line.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@
/* line-timetable style */
.line-timetable-calendar {
width:100%;
font-family:ralewayregular;
}
.line-timetable-calendar tr {
height:14px;
line-height:14px;
vertical-align:middle;
font-size:10px;
}
Expand Down
2 changes: 2 additions & 0 deletions Resources/public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ li.active .add-stop-point {
}
.line-timetable-calendar {
width: 100%;
font-family:ralewayregular;
}
.line-timetable-calendar tr {
height:14px;
line-height:14px;
vertical-align:middle;
font-size:10px;
}
Expand Down
6 changes: 3 additions & 3 deletions Resources/public/js/layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['jquery'], function($) {
define(['jquery', 'sf_routes'], function($) {

var layout = {};
var global_params = {};
Expand Down Expand Up @@ -34,14 +34,14 @@ define(['jquery'], function($) {
var _bind_blocks_listeners = function()
{
var $blocks = _get_blocks();
$blocks.each(function(){
$blocks.each(function() {
var $block = $(this);
// bind click listener if there is no menu inside
if ($block.find('*[role=menu]').length === 0) {
$block.click(_get_remote_modal);
} else {
$block.find('*[role=menu]').siblings('button.btn').dropdown();
$block.click(function(event){
$block.click(function(event) {
if ($block.find('*[role=menu]').parents('.btn-group').hasClass('open') === false) {
$block.find('*[role=menu]').siblings('button.btn').click();
return false;
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Layouts/default-line-layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{{ macros.img(lblock.domId, lblock, class, lblock.rank, mode) }}
{% elseif lblock.type == 'calendar' %}
{% set calendar = calendars|findCalendar(lblock.content, lblock.externalRouteId) %}
{{ macros.line_calendar(lblock.domId, lblock, class, lblock.rank, mode, calendar) }}
{{ macros.line_calendar(lblock.domId, lblock, class, lblock.rank, mode, calendar, 26) }}
{% endif %}
{% if mode == 'edit' and loop.last %}
{{ macros.add_block(loop.index+1) }}
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Layouts/macros.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
data-type="{{ block.type }}"
data-rank="{{ rank|default(0) }}"
data-icon="calendar"
data-columns="24"
data-columns="{{ columnsLimit }}"
{% endif %}
>
{% if mode == 'edit' and block and block.rank > 0 %}
Expand Down
152 changes: 139 additions & 13 deletions Services/CalendarManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,19 @@ public function isIncluded($calendarId, Season $season)
public function getCalendarForBlock(
$externalCoverageId,
Block $block,
$parameters = array()
$parameters
) {
$calendar = $this->navitia->getCalendar($externalCoverageId, $block->getContent())->calendars[0];

if (empty($parameters['hourOffset'])) {
throw new \Exception('The hour offset have to be set');
}

$parameters['startProductionDate'] = $this->navitia->getStartProductionDate(
$externalCoverageId,
$parameters['hourOffset']
);

return $this->getRouteSchedules(
$externalCoverageId,
$block->getExternalRouteId(),
Expand All @@ -460,11 +469,21 @@ public function getCalendarForBlock(
public function getCalendarsForLine(
$externalCoverageId,
$externalNetworkId,
$externalLineId
$externalLineId,
$parameters
) {
$routes = $this->navitia->getLineRoutes($externalCoverageId, $externalNetworkId, $externalLineId);
$calendars = $this->navitia->getLineCalendars($externalCoverageId, $externalNetworkId, $externalLineId);

if (empty($parameters['hourOffset'])) {
throw new \Exception('The hour offset have to be set');
}

$parameters['startProductionDate'] = $this->navitia->getStartProductionDate(
$externalCoverageId,
$parameters['hourOffset']
);

$schedule = array();
foreach ($routes as $route) {
$externalRouteId = $route->id;
Expand All @@ -474,7 +493,8 @@ public function getCalendarsForLine(
$schedule[$externalRouteId]['calendars'][$calendar->id] = $this->getRouteSchedules(
$externalCoverageId,
$externalRouteId,
$calendar
$calendar,
$parameters
);
}
}
Expand All @@ -493,14 +513,14 @@ private function getRouteSchedules(
$externalCoverageId,
$externalRouteId,
&$calendar,
$parameters = array()
$parameters
) {
try {
$routeSchedules = $this->navitia->getRouteSchedulesByRouteAndCalendar(
$externalCoverageId,
$externalRouteId,
$calendar->id,
\DateTime::createFromFormat('Ymd', $calendar->validity_pattern->beginning_date)
$parameters['startProductionDate']
);
} catch(\Exception $e) {
return $this->createEmptyLineCalendar($calendar);
Expand All @@ -516,7 +536,25 @@ private function getRouteSchedules(
throw new \Exception('No stop points found for the route : '.$externalRouteId);
}

$this->buildFullCalendar($routeSchedules);
$this->buildFullCalendar($routeSchedules, $parameters['hourOffset']);

$stopTimesToDelete = array();

if (!empty($parameters['limits'])) {
$this->processTimeLimits(
$parameters['limits'],
$parameters['hourOffset'],
current($routeSchedules->route_schedules['metadata'])['firstHour']
);
$this->cutCalendar($routeSchedules->route_schedules, $parameters['limits'], $stopTimesToDelete);
}

if (count($stopTimesToDelete) > 0) {
$this->deleteStopTimes(
$routeSchedules->route_schedules,
$stopTimesToDelete
);
}

unset($routeSchedules->headers, $routeSchedules->exceptions);

Expand Down Expand Up @@ -569,9 +607,10 @@ function($stop) use ($stopPoint) {
}

/**
* Build full calendar.
* Building full calendar.
*
* @param mixed &$schedule
* @param integer $hourOffset
*
* Building a full one-line calendar array.
* The calendar's structure in JSON is :
Expand All @@ -598,7 +637,7 @@ function($stop) use ($stopPoint) {
* It's type is just "trip" at the end of the function but it
* can be replaced later by something else (frequency for example).
*/
private function buildFullCalendar(&$schedule)
private function buildFullCalendar(&$schedule, $hourOffset)
{
$calendar = array(
'columns' => count($schedule->route_schedules[0]->date_times),
Expand All @@ -608,18 +647,22 @@ private function buildFullCalendar(&$schedule)

foreach ($schedule->route_schedules as $lineNumber => $stop) {
$stopTimes = array();
$previousDatetime = null;
$previousHour = null;
$dayOffset = false;
foreach ($stop->date_times as $columnNumber => $detail) {
$hour = intVal(substr($detail->date_time, 0, 2));

if (empty($detail->date_time)) {
if (!isset($calendar['metadata'][$columnNumber])) {
$calendar['metadata'][$columnNumber] = null;
}
$stopTimes[] = null;
} else {
// Detecting day change by hours comparison
if ($previousDatetime && !$dayOffset && $previousDatetime > intVal($detail->date_time)) {
$dayOffset = true;
// Detecting day / day+1 limit looking at hours and hourOffset
if ($previousHour && !$dayOffset && $previousHour > $hour) {
if ($previousHour > $hourOffset && $hour < $hourOffset) {
$dayOffset = true;
}
}

$date = strtotime($detail->date_time);
Expand Down Expand Up @@ -654,7 +697,7 @@ function ($link) {
$calendar['metadata'][$columnNumber]['arrivalStop'] = $stop->stop_point->name;
}

$previousDatetime = intVal($detail->date_time);
$previousHour = $hour;
}
}

Expand All @@ -667,4 +710,87 @@ function ($link) {

$schedule->route_schedules = $calendar;
}

/**
* Processing time limits using a reference date and the first
* hour of the first trip in calendar
*
* @param array &$limits
* @param integer $hourOffset
* @param time $firstTrip
*/
private function processTimeLimits(&$limits, $hourOffset, $firstTrip)
{
if (empty($limits['min']) || intVal($limits['min']) < 0) {
throw new \Exception('The low limit is invalid');
}

if (empty($limits['max']) || intVal($limits['max']) < 0) {
throw new \Exception('The high limit is invalid');
}

$referenceDate = new \Datetime(date('Y-m-d\TH:i:s', $firstTrip));

// The date is D-1 if the first hour is < hourOffset
if (intval($referenceDate->format('H')) < $hourOffset) {
$referenceDate->sub(new \DateInterval('P1D'));
}

$referenceDate = $referenceDate->format('Y-m-d');

foreach ($limits as $idx => $hour)
{
$hour = (int)$hour;
if ($hour > 24) {
$limits[$idx] = strtotime(
"+1 day",
strtotime($referenceDate." ".date('His', mktime($hour - 24, 0, 0)))
);
} else {
$limits[$idx] = strtotime($referenceDate." ".date('His', mktime($hour, 0, 0)));
}
}
}

/**
* Cutting calendar hours using limitations
*
* @param mixed &$schedule
* @param array $limits
* @param array &$stopTimesToDelete
*/
private function cutCalendar(&$schedule, $limits, &$stopTimesToDelete)
{
foreach ($schedule['metadata'] as $columnNumber => $metadata)
{
if ($metadata['firstHour'] < $limits['min']
|| $metadata['firstHour'] > $limits['max']
) {
unset($schedule['metadata'][$columnNumber]);
$stopTimesToDelete[] = $columnNumber;
$schedule['columns'] -= 1;
}
}
}

/**
* Deleting stopTimes from stops in a calendar array.
* stopTimesToDelete contains the deleted stopTimes position.
*
* @param array $stops
* @param array $stopTimesToDelete
*/
private function deleteStopTimes(&$schedule, $stopTimesToDelete)
{
$columnsToDelete = 0;
foreach ($schedule['stops'] as $key => $stop) {
foreach (array_keys($stop['stopTimes']) as $columnNumber) {
if (in_array($columnNumber, $stopTimesToDelete)) {
unset($stop['stopTimes'][$columnNumber]);
$columnsToDelete++;
}
}
$schedule['stops'][$key]['stopTimes'] = $stop['stopTimes'];
}
}
}
Loading

0 comments on commit 23767d3

Please sign in to comment.