2015-02-23 11:44:12 +01:00
|
|
|
<?php
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
namespace level2;
|
2015-02-23 11:44:12 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
use DateTime;
|
2015-02-23 11:44:12 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
class Level2
|
|
|
|
{
|
2017-04-01 12:30:53 +02:00
|
|
|
public static $imageMatch = 'https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif|\.svg)';
|
2016-04-04 21:50:24 +02:00
|
|
|
public static $urlMatch = '\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$\(\)?!:,.]*[A-Z0-9+&@#\/%=~_|$]';
|
2015-02-23 11:44:12 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getStatus($app)
|
|
|
|
{
|
|
|
|
$spaceAPI = Helpers::spaceAPI($app);
|
2015-02-23 11:44:12 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$Level2[ 'open' ] = $spaceAPI[ 'state' ][ 'open' ];
|
|
|
|
$Level2[ 'lastchange' ] = $spaceAPI[ 'state' ][ 'lastchange' ];
|
|
|
|
$Level2[ 'people' ] = $spaceAPI[ 'sensors' ][ 'people_now_present' ][ 0 ][ 'value' ];
|
|
|
|
$Level2[ 'address' ] = $spaceAPI[ 'location' ][ 'address' ];
|
|
|
|
$Level2[ 'phone' ] = $spaceAPI[ 'contact' ][ 'phone' ];
|
|
|
|
$Level2[ 'logo' ] = $spaceAPI[ 'logo' ];
|
2015-02-23 11:44:12 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return $Level2;
|
2015-02-23 11:44:12 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getJSONCalendar($app)
|
|
|
|
{
|
|
|
|
$googleCalendarJson = 'https://www.googleapis.com/calendar/v3/calendars/'
|
|
|
|
.$app[ 'google' ][ 'calendar_id' ].'/events'
|
|
|
|
.'?singleEvents=true'
|
|
|
|
.'&orderBy=startTime'
|
|
|
|
.'&timeMin='.date('Y-m-d').'T'.date('H').'%3A'.date('i').'%3A'.date('s').'%2B01%3A00'
|
|
|
|
.'&fields=description%2Citems(description%2Crecurrence%2Csummary%2Clocation%2Cstart%2Cend)%2Csummary'
|
|
|
|
.'&key='.$app[ 'google' ][ 'api_key' ];
|
|
|
|
|
|
|
|
return Helpers::JSON2Array($googleCalendarJson);
|
2015-02-26 21:58:46 +01:00
|
|
|
}
|
2015-02-23 15:45:47 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getEventDateTime($googleEvent)
|
|
|
|
{
|
|
|
|
if (array_key_exists('dateTime', $googleEvent[ 'start' ])) {
|
|
|
|
$event[ 'start' ] = strtotime($googleEvent[ 'start' ][ 'dateTime' ]);
|
|
|
|
$event[ 'end' ] = strtotime($googleEvent[ 'end' ][ 'dateTime' ]);
|
2015-02-23 15:45:47 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$event[ 'date' ] = date('l, j. M G:i', $event[ 'start' ]);
|
2015-02-23 15:45:47 +01:00
|
|
|
} else {
|
2016-04-04 21:50:24 +02:00
|
|
|
$event[ 'start' ] = strtotime($googleEvent[ 'start' ][ 'date' ]);
|
|
|
|
$event[ 'end' ] = strtotime($googleEvent[ 'end' ][ 'date' ]);
|
2015-02-23 21:18:41 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$event[ 'date' ] = date('l, j. M', $event[ 'start' ]);
|
2015-02-23 15:45:47 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return $event;
|
2015-02-26 21:58:46 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getImages($googleEvent)
|
|
|
|
{
|
|
|
|
preg_match_all('/'.self::$imageMatch.'/i', $googleEvent[ 'description' ], $image, PREG_PATTERN_ORDER);
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
if (sizeof($image[ 0 ]) > 0) {
|
|
|
|
if ($image[ 0 ][ 0 ] != '') {
|
|
|
|
return $image[ 0 ][ 0 ];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-23 15:45:47 +01:00
|
|
|
}
|
2016-04-04 21:50:24 +02:00
|
|
|
}
|
2015-02-23 15:45:47 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getURLs($googleEvent)
|
|
|
|
{
|
|
|
|
preg_match_all('/'.self::$urlMatch.'/i', $googleEvent[ 'description' ], $url, PREG_PATTERN_ORDER);
|
2015-02-23 21:18:41 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
if (sizeof($url[ 0 ]) > 0) {
|
|
|
|
if ($url[ 0 ][ 0 ] != '') {
|
|
|
|
return $url[ 0 ][ 0 ];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 21:58:46 +01:00
|
|
|
}
|
2015-02-23 15:45:47 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function removeImages($googleEvent)
|
|
|
|
{
|
|
|
|
preg_match_all('/'.self::$imageMatch.'/i', $googleEvent[ 'description' ], $image, PREG_PATTERN_ORDER);
|
2015-02-23 21:18:41 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return preg_replace(
|
|
|
|
'/\n'.self::$imageMatch.'/i',
|
2015-02-26 21:58:46 +01:00
|
|
|
'',
|
|
|
|
$googleEvent[ 'description' ]
|
|
|
|
);
|
|
|
|
}
|
2015-02-23 21:18:41 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function removeURLs($googleEvent)
|
|
|
|
{
|
|
|
|
preg_match_all('/'.self::$urlMatch.'/i', $googleEvent[ 'description' ], $url, PREG_PATTERN_ORDER);
|
2015-02-23 21:18:41 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return preg_replace(
|
|
|
|
'/\n'.self::$urlMatch.'/i',
|
2015-02-26 21:58:46 +01:00
|
|
|
'',
|
|
|
|
$googleEvent[ 'description' ]
|
|
|
|
);
|
|
|
|
}
|
2015-02-23 21:18:41 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getEvents($app)
|
|
|
|
{
|
|
|
|
$googleCalendar = Helpers::JSON2Array($app[ 'cache' ][ 'calendar' ][ 'json' ]);
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
foreach ($googleCalendar[ 'items' ] as $googleEvent) {
|
|
|
|
unset($event);
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$event = self::getEventDateTime($googleEvent);
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$event[ 'name' ] = $googleEvent[ 'summary' ];
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
if (array_key_exists('location', $googleEvent)) {
|
|
|
|
$event[ 'location' ] = $googleEvent[ 'location' ];
|
|
|
|
}
|
2015-02-26 23:38:33 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$event[ 'image' ] = false;
|
|
|
|
$event[ 'url' ] = false;
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
if (array_key_exists('description', $googleEvent)) {
|
|
|
|
$event[ 'description' ] = $googleEvent[ 'description' ];
|
2015-02-26 21:58:46 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$event[ 'image' ] = self::getImages($event);
|
|
|
|
$event[ 'description' ] = self::removeImages($event);
|
|
|
|
$event[ 'url' ] = self::getURLs($event);
|
|
|
|
$event[ 'description' ] = self::removeURLs($event);
|
|
|
|
$event[ 'description' ] = nl2br($event[ 'description' ]);
|
|
|
|
}
|
2015-02-23 15:45:47 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$events[] = $event;
|
2015-02-23 15:45:47 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return $events;
|
2015-02-23 15:45:47 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getEventsByMonth($events, $year, $month)
|
|
|
|
{
|
|
|
|
$year = (int) $year;
|
|
|
|
$month = (int) $month;
|
2015-02-27 01:24:08 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$eventsInMonth = array();
|
2015-02-27 01:24:08 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
foreach ($events as $event) {
|
|
|
|
if ((date('Y', $event[ 'start' ]) == $year) && (date('m', $event[ 'start' ]) == $month)) {
|
|
|
|
$eventsInMonth[] = $event;
|
|
|
|
}
|
2015-02-27 01:24:08 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return $eventsInMonth;
|
2015-02-27 01:24:08 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getEventsByDay($events, $year, $month, $day)
|
|
|
|
{
|
|
|
|
$year = (int) $year;
|
|
|
|
$month = (int) $month;
|
|
|
|
$day = (int) $day;
|
2015-04-24 12:04:24 +02:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$eventsInDay = array();
|
2015-04-24 12:04:24 +02:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
foreach ($events as $event) {
|
|
|
|
if ((date('Y', $event[ 'start' ]) == $year) && (date('m', $event[ 'start' ]) == $month) && (date('d', $event[ 'start' ]) == $day)) {
|
|
|
|
$eventsInDay[] = $event;
|
|
|
|
}
|
2015-04-24 12:04:24 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return $eventsInDay;
|
2015-04-24 12:04:24 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getEvent($events, $startUnix)
|
|
|
|
{
|
|
|
|
$startUnix = (int) $startUnix;
|
2015-05-11 23:44:20 +02:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
foreach ($events as $event) {
|
|
|
|
if ($event[ 'start' ] == $startUnix) {
|
|
|
|
return $event;
|
|
|
|
}
|
2015-05-11 23:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
public static function getChartData($app)
|
|
|
|
{
|
|
|
|
$dowMap = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
|
2015-03-01 15:23:38 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$chartDataQuery = 'SELECT ROUND( AVG( people ) )
|
2015-03-01 15:23:38 +01:00
|
|
|
FROM state
|
2015-03-07 15:06:13 +01:00
|
|
|
WHERE datetime >= curdate() - INTERVAL 312 Hour
|
2015-03-01 15:23:38 +01:00
|
|
|
AND WEEKDAY( datetime ) = ?
|
|
|
|
AND HOUR ( datetime ) = ?';
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
for ($dow = 0; $dow < 7; ++$dow) {
|
|
|
|
$chartDataByDay[ 'name' ] = $dowMap[ $dow ];
|
2015-03-01 15:23:38 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
for ($hod = 0; $hod < 24; ++$hod) {
|
|
|
|
$chartDataByDay[ 'data' ][ $hod % 24 ] = $app[ 'db' ]->fetchColumn(
|
2015-03-01 15:23:38 +01:00
|
|
|
$chartDataQuery,
|
|
|
|
array(
|
|
|
|
$dow,
|
2016-04-04 21:50:24 +02:00
|
|
|
$hod,
|
2015-03-01 15:23:38 +01:00
|
|
|
)
|
|
|
|
);
|
2016-04-04 21:50:24 +02:00
|
|
|
}
|
2015-03-01 15:23:38 +01:00
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
$chart[ $dow ] = $chartDataByDay;
|
2015-03-01 15:23:38 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:50:24 +02:00
|
|
|
return $chart;
|
2015-03-01 15:23:38 +01:00
|
|
|
}
|
2016-04-04 21:50:24 +02:00
|
|
|
}
|