Skip to content

Commit

Permalink
Merge pull request #1508 from wielebenwir/bugfix/issue-1505
Browse files Browse the repository at this point in the history
fix #1505
  • Loading branch information
hansmorb committed Jun 22, 2024
2 parents a700c7d + 98e1679 commit c60046f
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 62 deletions.
15 changes: 15 additions & 0 deletions src/Model/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ protected function mapRestrictions( array &$slots ) {
}
}

public function getStartTimestamp(): int {
$dt = new DateTime( $this->getDate() );
$dt->modify( 'midnight' );

return $dt->getTimestamp();
}

public function getEndTimestamp(): int {
$dt = new DateTime( $this->getDate() );
$dt->modify( '23:59:59' );

return $dt->getTimestamp();
}


/**
* Remove empty and merge connected slots.
*
Expand Down
29 changes: 29 additions & 0 deletions src/Repository/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,35 @@ private static function filterTimeframesForCurrentUser( $posts ): array {
} );
}

/**
* Will filter out all timeframes that are not in the given timerange.
*
* @param \CommonsBooking\Model\Timeframe[] $timeframes
* @param int $startTimestamp
* @param int $endTimestamp
*
* @return \CommonsBooking\Model\Timeframe[]
* @throws Exception
*/
public static function filterTimeframesForTimerange( array $timeframes, int $startTimestamp, int $endTimestamp ): array {
return array_filter( $timeframes, function ( $timeframe ) use ( $startTimestamp, $endTimestamp ) {
//filter out anything in the future
if ( $timeframe->getStartDate() > $endTimestamp ) {
return false;
}
//always include infinite timeframes
if ( ! $timeframe->getEndDate() ) {
return true;
}
//filter out anything in the past
if ( $timeframe->getEndDate() < $startTimestamp ) {
return false;
}

return true;
} );
}

/**
* Instantiate models for posts.
* Why? In some cases we need more than WP_Post methods and for this case we have Models, that enrich WP_Post
Expand Down
55 changes: 39 additions & 16 deletions src/View/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,22 +406,45 @@ public static function getCalendarDataArray( $item, $location, string $startDate
*
* @return \CommonsBooking\Model\Timeframe|null
*/
private static function getClosestBookableTimeFrameForToday( $bookableTimeframes ): ?\CommonsBooking\Model\Timeframe {
// Sort timeframes by startdate
usort(
$bookableTimeframes,
function ( \CommonsBooking\Model\Timeframe $item1, \CommonsBooking\Model\Timeframe $item2 ) {
$item1StartDateDistance = abs( time() - $item1->getStartDate() );
$item1EndDateDistance = abs( time() - $item1->getEndDate() );
$item1SmallestDistance = min( $item1StartDateDistance, $item1EndDateDistance );

$item2StartDateDistance = abs( time() - $item2->getStartDate() );
$item2EndDateDistance = abs( time() - $item2->getEndDate() );
$item2SmallestDistance = min( $item2StartDateDistance, $item2EndDateDistance );

return $item2SmallestDistance <=> $item1SmallestDistance;
}
);
public static function getClosestBookableTimeFrameForToday( $bookableTimeframes ): ?\CommonsBooking\Model\Timeframe {
$today = new Day( date( 'Y-m-d' ) );
$todayTimeframes = \CommonsBooking\Repository\Timeframe::filterTimeframesForTimerange( $bookableTimeframes, $today->getStartTimestamp(), $today->getEndTimestamp() );
$todayTimeframes = array_filter( $todayTimeframes, function ( $timeframe ) use ( $today ) { //also consider repetition
return $today->isInTimeframe( $timeframe );
} );
switch ( count( $todayTimeframes ) ) {
case 1:
$bookableTimeframes = $todayTimeframes;
break;
case 0:
usort( $bookableTimeframes, function ( $a, $b ) {
$aStartDate = $a->getStartDate();
$bStartDate = $b->getStartDate();

if ( $aStartDate == $bStartDate ) {
$aStartTimeDT = $a->getStartTimeDateTime();
$bStartTimeDT = $b->getStartTimeDateTime();

return $bStartTimeDT <=> $aStartTimeDT;
}

return $bStartDate <=> $aStartDate;
} );
break;
default: //More than one timeframe for current day
// consider starttime and endtime
$now = new DateTime();
/** @var \CommonsBooking\Model\Timeframe $todayTimeframes */
$bookableTimeframes = array_filter( $todayTimeframes, function ( $timeframe ) use ( $now ) {
$startTime = $timeframe->getStartTime();
$startTimeDT = new DateTime( $startTime );
$endTime = $timeframe->getEndTime();
$endTimeDT = new DateTime( $endTime );

return $startTimeDT <= $now && $now <= $endTimeDT;
} );
break;
}

return array_pop( $bookableTimeframes );
}
Expand Down
13 changes: 13 additions & 0 deletions tests/php/Model/DayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,17 @@ public function testGetRestrictions() {
$this->assertTrue(count($this->instance->getRestrictions()) == 1);
}


public function testGetStartTimestamp() {
$start = strtotime( self::CURRENT_DATE . ' midnight' );
$this->assertEquals( $start, $this->instance->getStartTimestamp() );
}

public function testGetEndTimestamp() {
$end = strtotime( self::CURRENT_DATE . ' 23:59:59' );
$this->assertEquals( $end, $this->instance->getEndTimestamp() );
}



}
16 changes: 16 additions & 0 deletions tests/php/Repository/TimeframeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ public function testGetHoliday() {
);
}

public function testFilterTimeframesForTimerange() {
$allTimeframeModels = array_map( function ( $timeframeId ) {
return new \CommonsBooking\Model\Timeframe( $timeframeId );
}, $this->allTimeframes );
//should return everything in the repetition
$filteredTimeframes = Timeframe::filterTimeframesForTimerange(
$allTimeframeModels,
$this->repetition_start,
$this->repetition_end
);
$this->assertEqualsCanonicalizing( $this->allTimeframes, array_map( function ( $timeframe ) {
return $timeframe->ID;
}, $filteredTimeframes ) );

}

protected function setUp(): void {
parent::setUp();
$this->repetition_start = strtotime( self::CURRENT_DATE );
Expand Down
Loading

0 comments on commit c60046f

Please sign in to comment.