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

group days with identical hours Issue #22

Open
seppzzz opened this issue Feb 5, 2017 · 6 comments
Open

group days with identical hours Issue #22

seppzzz opened this issue Feb 5, 2017 · 6 comments

Comments

@seppzzz
Copy link

seppzzz commented Feb 5, 2017

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:

bildschirmfoto 2017-02-05 um 09 29 55

ORIGINAL CLASS WITHOUT PERSONAL MODIFICATIONS:

bildschirmfoto 2017-02-05 um 09 43 02

@brandbums
Copy link

brandbums commented Mar 20, 2017

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;
}

@seppzzz
Copy link
Author

seppzzz commented Mar 21, 2017

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

@brandbums
Copy link

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.

@mycaravam
Copy link

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)

@mycaravam
Copy link

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.

@ghost
Copy link

ghost commented May 21, 2018

I fixed it by changing : line 265 from foreach ($blocks as $block) { to foreach ($blocks as &$block) {
everything works fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants