php-store-hours
php-store-hours copied to clipboard
group days with identical hours Issue
Hi! Thanks for that class!
there is a issue with '$store_hours->hours_this_week(true)' when using $hours- array like this :
$hours = array( 'mon' => array('11:00-20:30'), 'tue' => array('11:00-13:00', '18:00-20:30'), 'wed' => array('11:00-20:30'), 'thu' => array('11:00-1:30'), // Open late 'fri' => array('11:00-20:30'), 'sat' => array('11:00-1:30'), // Open late 'sun' => array() // Closed all day );
RESULT: (group days with identical hours)
MO, MI, FR 11:00-20:30 Di 11:00-13:00, 18:00-20:30
The result is loosing thu and sat as both are 'open late'
Display full list of open hours is rendering just fine:
ORIGINAL CLASS WITHOUT PERSONAL MODIFICATIONS:
Replace the 'hours_this_week_grouped' with this!
private
function hours_this_week_grouped()
{
$lookup = array_combine(range(1, 7) , $this->config['overview_weekdays']);
$blocks = array();
$hours = array_filter($this->hours,
function ($element)
{
return (count($element) > 0);
});
foreach($hours as $weekday => $hours2)
{
foreach($blocks as & $block)
{
if ($block['hours'] === $hours2)
{
$block['days'][] = $weekday;
continue2;
}
}
unset($block);
$blocks[] = array(
'days' => array(
$weekday
) ,
'hours' => $hours2
);
}
unset($block);
foreach($blocks as $block)
{
$keyparts = array();
$keys = $block['days'];
$buffer = array();
$lastIndex = null;
$minGroupSize = 3;
$keyparts = array();
foreach($keys as $index)
{
if ($lastIndex !== null && $index - 1 !== $lastIndex)
{
if (count($buffer) >= $minGroupSize)
{
$keyparts[] = $lookup[$buffer[0]] . '-' . $lookup[$buffer[count($buffer) - 1]];
}
else
{
foreach($buffer as $b)
{
$keyparts[] = $lookup[$b];
}
}
$buffer = array();
}
$buffer[] = $index;
$lastIndex = $index;
}
if (count($buffer) >= $minGroupSize)
{
$keyparts[] = $lookup[$buffer[0]] . '-' . $lookup[$buffer[count($buffer) - 1]];
}
else
{
foreach($buffer as $b)
{
$keyparts[] = $lookup[$b];
}
}
// Combine
$ret[implode(', ', $keyparts) ] = $this->format_hours($block['hours']);
}
return $ret;
}
hi brandbums!
Fatal error: Call to undefined method StoreHours::format_hours() in /Applications/MAMP/htdocs/php-store-hours-master_orig/StoreHours.class.php on line 373
Hey tillzzz,
Copy and paste the most updated version of this file. Then replace the function. Your getting an error on another function in that class.
hi guys! this bug still persists, the workaround from @brandbums doesn't seem to work. The grouping of identical open hours is messed up. My array looks like:
array (size=7)
'mon' =>
array (size=2)
0 => string '10:00 - 12:00' (length=13)
1 => string '16:00 - 22:00' (length=13)
'tue' =>
array (size=2)
0 => string '10:00 - 12:00' (length=13)
1 => string '16:00 - 22:00' (length=13)
'wed' =>
array (size=2)
0 => string '10:00 - 12:00' (length=13)
1 => string '16:00 - 22:00' (length=13)
'thu' =>
array (size=2)
0 => string '16:00 - 20:00' (length=13)
1 => string '' (length=0)
'fri' =>
array (size=2)
0 => string '18:00 - 22:00' (length=13)
1 => string '' (length=0)
'sat' =>
array (size=2)
0 => string '18:00 - 22:00' (length=13)
1 => string '' (length=0)
'sun' =>
array (size=2)
0 => string '' (length=0)
I fixed it by changing foreach ($blocks as $block) {
to foreach ($blocks as $blockx) {
Apparently there's a bug in the var reset. Maybe by time I will look into it. Just leaving this code for future me's, looking for a quick fix.
I fixed it by changing : line 265 from foreach ($blocks as $block) { to foreach ($blocks as &$block) { everything works fine now.