summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/page.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/locationinfo/page.inc.php')
-rw-r--r--modules-available/locationinfo/page.inc.php110
1 files changed, 64 insertions, 46 deletions
diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php
index bc918765..df5b3632 100644
--- a/modules-available/locationinfo/page.inc.php
+++ b/modules-available/locationinfo/page.inc.php
@@ -747,83 +747,101 @@ class Page_LocationInfo extends Page
'id' => $id,
'serverlist' => $serverList,
'serverlocationid' => $locConfig['serverlocationid'],
+ 'openingtimes' => $this->compressTimes($openingtimes),
);
- $data['expertMode'] = !$this->isSimpleMode($openingtimes);
- // !! isSimpleMode might have changed $openingtimes, so order is important here...
- $data['schedule_data'] = json_encode($openingtimes);
echo Render::parse('ajax-config-location', $data);
}
+ private function fmtTime($time)
+ {
+ $t = explode(':', $time);
+ return sprintf('%02d:%02d', $t[0], $t[1]);
+ }
+
/**
* Checks if simple mode or expert mode is active.
* Tries to merge/compact the opening times schedule, and
* will actually modify the passed array iff it can be
- * transformed into easy opening times.
+ * transformed into simple opening times.
*
- * @param array $array of the saved openingtimes.
- * @return bool True if simple mode, false if expert mode
+ * @return array new optimized openingtimes
*/
- private function isSimpleMode(&$array)
+ private function compressTimes(&$array)
{
if (empty($array))
- return true;
+ return [];
// Decompose by day
- $new = array();
+ $DAYLIST = array_flip(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']);
+ $new = [];
foreach ($array as $row) {
$s = $this->getTime($row['openingtime']);
$e = $this->getTime($row['closingtime']);
if ($s === false || $e === false || $e <= $s)
continue;
foreach ($row['days'] as $day) {
+ $day = $DAYLIST[$day] ?? -1;
+ if ($day === -1)
+ continue;
$this->addDay($new, $day, $s, $e);
}
}
- // Merge by timespan, but always keep saturday and sunday separate
- $merged = array();
+ // Merge by timespan
+ $merged = [];
foreach ($new as $day => $ranges) {
foreach ($ranges as $range) {
- if ($day === 'Saturday' || $day === 'Sunday') {
- $add = $day;
- } else {
- $add = '';
+ $range = $range[0] . '#' . $range[1];
+ if (!isset($merged[$range])) {
+ $merged[$range] = [];
}
- $key = '#' . $range[0] . '#' . $range[1] . '#' . $add;
- if (!isset($merged[$key])) {
- $merged[$key] = array();
- }
- $merged[$key][$day] = true;
+ $merged[$range][$day] = true;
}
}
- // Check if it passes as simple mode
- if (count($merged) > 3)
- return false;
- foreach ($merged as $days) {
- if (count($days) === 5) {
- $res = array_keys($days);
- $res = array_intersect($res, array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"));
- if (count($res) !== 5)
- return false;
- } elseif (count($days) === 1) {
- if (!isset($days['Saturday']) && !isset($days['Sunday'])) {
- return false;
- }
+ // Finally transform to display struct
+ $new = [];
+ foreach ($merged as $span => $days) {
+ $out = explode('#', $span);
+ $new[] = [
+ 'days' => $this->buildDaysString(array_keys($days)),
+ 'open' => sprintf('%02d:%02d', ($out[0] / 60), ($out[0] % 60)),
+ 'close' => sprintf('%02d:%02d', ($out[1] / 60), ($out[1] % 60)),
+ ];
+ }
+ return $new;
+ }
+
+ /**
+ * @param array $daysArray List of days, "Monday", "Tuesday" etc. Must not contain duplicates.
+ * @return string Human readable representation of list of days
+ */
+ private function buildDaysString(array $daysArray)
+ {
+ /* Dictionary::translate('monday') Dictionary::translate('tuesday') Dictionary::translate('wednesday')
+ * Dictionary::translate('thursday') Dictionary::translate('friday') Dictionary::translate('saturday')
+ * Dictionary::translate('sunday')
+ */
+ $DAYLIST = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
+ $output = [];
+ $first = $last = -1;
+ sort($daysArray);
+ $daysArray[] = -1; // One trailing element to enforce a flush
+ foreach ($daysArray as $day) {
+ if ($first === -1) {
+ $first = $last = $day;
+ } elseif ($last + 1 === $day) {
+ // Chain
+ $last++;
} else {
- return false;
+ $string = Dictionary::translate($DAYLIST[$first], true);
+ if ($first !== $last) {
+ $string .= ($first + 1 === $last ? ",\xe2\x80\x89" : "\xe2\x80\x89-\xe2\x80\x89")
+ . Dictionary::translate($DAYLIST[$last], true);
+ }
+ $output[] = $string;
+ $first = $last = $day;
}
}
- // Valid simple mode, finally transform back to what we know
- $new = array();
- foreach ($merged as $span => $days) {
- preg_match('/^#(\d+)#(\d+)#/', $span, $out);
- $new[] = array(
- 'days' => array_keys($days),
- 'openingtime' => floor($out[1] / 60) . ':' . ($out[1] % 60),
- 'closingtime' => floor($out[2] / 60) . ':' . ($out[2] % 60),
- );
- }
- $array = $new;
- return true;
+ return implode(', ', $output);
}
private function addDay(&$array, $day, $s, $e)