datetime offset + syntax

master
Thierry Degeling 2016-04-04 21:50:24 +02:00
parent 24c415fbaa
commit 2915d8a778
1 changed files with 132 additions and 181 deletions

View File

@ -2,17 +2,15 @@
namespace level2; namespace level2;
use Silex\Application; use DateTime;
use \DateTime;
use \DateTimeZone;
class Level2 { class Level2
{
static public $imageMatch = 'https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif)'; public static $imageMatch = 'https?:\/\/[^ ]+?(?:\.jpg|\.png|\.gif)';
static public $urlMatch = '\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$\(\)?!:,.]*[A-Z0-9+&@#\/%=~_|$]'; public static $urlMatch = '\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$\(\)?!:,.]*[A-Z0-9+&@#\/%=~_|$]';
static public function getStatus ( $app ) {
public static function getStatus($app)
{
$spaceAPI = Helpers::spaceAPI($app); $spaceAPI = Helpers::spaceAPI($app);
$Level2[ 'open' ] = $spaceAPI[ 'state' ][ 'open' ]; $Level2[ 'open' ] = $spaceAPI[ 'state' ][ 'open' ];
@ -23,11 +21,10 @@
$Level2[ 'logo' ] = $spaceAPI[ 'logo' ]; $Level2[ 'logo' ] = $spaceAPI[ 'logo' ];
return $Level2; return $Level2;
} }
static public function getJSONCalendar ( $app ) { public static function getJSONCalendar($app)
{
$googleCalendarJson = 'https://www.googleapis.com/calendar/v3/calendars/' $googleCalendarJson = 'https://www.googleapis.com/calendar/v3/calendars/'
.$app[ 'google' ][ 'calendar_id' ].'/events' .$app[ 'google' ][ 'calendar_id' ].'/events'
.'?singleEvents=true' .'?singleEvents=true'
@ -37,71 +34,53 @@
.'&key='.$app[ 'google' ][ 'api_key' ]; .'&key='.$app[ 'google' ][ 'api_key' ];
return Helpers::JSON2Array($googleCalendarJson); return Helpers::JSON2Array($googleCalendarJson);
} }
static public function getEventDateTime ( $googleEvent ) { public static function getEventDateTime($googleEvent)
{
if (array_key_exists('dateTime', $googleEvent[ 'start' ])) { if (array_key_exists('dateTime', $googleEvent[ 'start' ])) {
$event[ 'start' ] = strtotime($googleEvent[ 'start' ][ 'dateTime' ]); $event[ 'start' ] = strtotime($googleEvent[ 'start' ][ 'dateTime' ]);
$event[ 'end' ] = strtotime($googleEvent[ 'end' ][ 'dateTime' ]); $event[ 'end' ] = strtotime($googleEvent[ 'end' ][ 'dateTime' ]);
$event[ 'date' ] = date('l, j. M G:i', $event[ 'start' ]); $event[ 'date' ] = date('l, j. M G:i', $event[ 'start' ]);
} else { } else {
$event[ 'start' ] = strtotime($googleEvent[ 'start' ][ 'date' ]); $event[ 'start' ] = strtotime($googleEvent[ 'start' ][ 'date' ]);
$event[ 'end' ] = strtotime($googleEvent[ 'end' ][ 'date' ]); $event[ 'end' ] = strtotime($googleEvent[ 'end' ][ 'date' ]);
$event[ 'date' ] = date('l, j. M', $event[ 'start' ]); $event[ 'date' ] = date('l, j. M', $event[ 'start' ]);
} }
return $event; return $event;
} }
static public function getImages( $googleEvent ) { public static function getImages($googleEvent)
{
preg_match_all('/'.self::$imageMatch.'/i', $googleEvent[ 'description' ], $image, PREG_PATTERN_ORDER); preg_match_all('/'.self::$imageMatch.'/i', $googleEvent[ 'description' ], $image, PREG_PATTERN_ORDER);
if (sizeof($image[ 0 ]) > 0) { if (sizeof($image[ 0 ]) > 0) {
if ($image[ 0 ][ 0 ] != '') { if ($image[ 0 ][ 0 ] != '') {
return $image[ 0 ][ 0 ]; return $image[ 0 ][ 0 ];
} else { } else {
return false; return false;
}
}
} }
} public static function getURLs($googleEvent)
{
}
static public function getURLs( $googleEvent ) {
preg_match_all('/'.self::$urlMatch.'/i', $googleEvent[ 'description' ], $url, PREG_PATTERN_ORDER); preg_match_all('/'.self::$urlMatch.'/i', $googleEvent[ 'description' ], $url, PREG_PATTERN_ORDER);
if (sizeof($url[ 0 ]) > 0) { if (sizeof($url[ 0 ]) > 0) {
if ($url[ 0 ][ 0 ] != '') { if ($url[ 0 ][ 0 ] != '') {
return $url[ 0 ][ 0 ]; return $url[ 0 ][ 0 ];
} else { } else {
return false; return false;
}
}
} }
} public static function removeImages($googleEvent)
{
}
static public function removeImages( $googleEvent ) {
preg_match_all('/'.self::$imageMatch.'/i', $googleEvent[ 'description' ], $image, PREG_PATTERN_ORDER); preg_match_all('/'.self::$imageMatch.'/i', $googleEvent[ 'description' ], $image, PREG_PATTERN_ORDER);
return preg_replace( return preg_replace(
@ -109,11 +88,10 @@
'', '',
$googleEvent[ 'description' ] $googleEvent[ 'description' ]
); );
} }
static public function removeURLs( $googleEvent ) { public static function removeURLs($googleEvent)
{
preg_match_all('/'.self::$urlMatch.'/i', $googleEvent[ 'description' ], $url, PREG_PATTERN_ORDER); preg_match_all('/'.self::$urlMatch.'/i', $googleEvent[ 'description' ], $url, PREG_PATTERN_ORDER);
return preg_replace( return preg_replace(
@ -121,15 +99,13 @@
'', '',
$googleEvent[ 'description' ] $googleEvent[ 'description' ]
); );
} }
static public function getEvents ( $app ) { public static function getEvents($app)
{
$googleCalendar = Helpers::JSON2Array($app[ 'cache' ][ 'calendar' ][ 'json' ]); $googleCalendar = Helpers::JSON2Array($app[ 'cache' ][ 'calendar' ][ 'json' ]);
foreach ($googleCalendar[ 'items' ] as $googleEvent) { foreach ($googleCalendar[ 'items' ] as $googleEvent) {
unset($event); unset($event);
$event = self::getEventDateTime($googleEvent); $event = self::getEventDateTime($googleEvent);
@ -144,7 +120,6 @@
$event[ 'url' ] = false; $event[ 'url' ] = false;
if (array_key_exists('description', $googleEvent)) { if (array_key_exists('description', $googleEvent)) {
$event[ 'description' ] = $googleEvent[ 'description' ]; $event[ 'description' ] = $googleEvent[ 'description' ];
$event[ 'image' ] = self::getImages($event); $event[ 'image' ] = self::getImages($event);
@ -152,40 +127,32 @@
$event[ 'url' ] = self::getURLs($event); $event[ 'url' ] = self::getURLs($event);
$event[ 'description' ] = self::removeURLs($event); $event[ 'description' ] = self::removeURLs($event);
$event[ 'description' ] = nl2br($event[ 'description' ]); $event[ 'description' ] = nl2br($event[ 'description' ]);
} }
$events[] = $event; $events[] = $event;
} }
return $events; return $events;
} }
static public function getEventsByMonth( $events, $year, $month ) { public static function getEventsByMonth($events, $year, $month)
{
$year = (int) $year; $year = (int) $year;
$month = (int) $month; $month = (int) $month;
$eventsInMonth = array(); $eventsInMonth = array();
foreach ($events as $event) { foreach ($events as $event) {
if ((date('Y', $event[ 'start' ]) == $year) && (date('m', $event[ 'start' ]) == $month)) { if ((date('Y', $event[ 'start' ]) == $year) && (date('m', $event[ 'start' ]) == $month)) {
$eventsInMonth[] = $event; $eventsInMonth[] = $event;
} }
} }
return $eventsInMonth; return $eventsInMonth;
} }
static public function getEventsByDay( $events, $year, $month, $day ) { public static function getEventsByDay($events, $year, $month, $day)
{
$year = (int) $year; $year = (int) $year;
$month = (int) $month; $month = (int) $month;
$day = (int) $day; $day = (int) $day;
@ -193,39 +160,28 @@
$eventsInDay = array(); $eventsInDay = array();
foreach ($events as $event) { foreach ($events as $event) {
if ((date('Y', $event[ 'start' ]) == $year) && (date('m', $event[ 'start' ]) == $month) && (date('d', $event[ 'start' ]) == $day)) { if ((date('Y', $event[ 'start' ]) == $year) && (date('m', $event[ 'start' ]) == $month) && (date('d', $event[ 'start' ]) == $day)) {
$eventsInDay[] = $event; $eventsInDay[] = $event;
} }
} }
return $eventsInDay; return $eventsInDay;
} }
static public function getEvent( $events, $startUnix ) { public static function getEvent($events, $startUnix)
{
$startUnix = (int) $startUnix; $startUnix = (int) $startUnix;
foreach ($events as $event) { foreach ($events as $event) {
if ($event[ 'start' ] == $startUnix) { if ($event[ 'start' ] == $startUnix) {
return $event; return $event;
}
}
} }
} public static function getChartData($app)
{
}
static public function getChartData( $app ) {
$dowMap = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); $dowMap = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$localtime_offset = date('Z') / 3600;
$chartDataQuery = 'SELECT ROUND( AVG( people ) ) $chartDataQuery = 'SELECT ROUND( AVG( people ) )
FROM state FROM state
@ -233,27 +189,22 @@
AND WEEKDAY( datetime ) = ? AND WEEKDAY( datetime ) = ?
AND HOUR ( datetime ) = ?'; AND HOUR ( datetime ) = ?';
for( $dow = 0; $dow < 7; $dow++ ) { for ($dow = 0; $dow < 7; ++$dow) {
$chartDataByDay[ 'name' ] = $dowMap[ $dow ]; $chartDataByDay[ 'name' ] = $dowMap[ $dow ];
for( $hod = 0; $hod < 24; $hod++ ) { for ($hod = 0; $hod < 24; ++$hod) {
$chartDataByDay[ 'data' ][ ($hod + $localtime_offset) % 24 ] = $app[ 'db' ]->fetchColumn( $chartDataByDay[ 'data' ][ $hod % 24 ] = $app[ 'db' ]->fetchColumn(
$chartDataQuery, $chartDataQuery,
array( array(
$dow, $dow,
$hod $hod,
) )
); );
} }
$chart[ $dow ] = $chartDataByDay; $chart[ $dow ] = $chartDataByDay;
} }
return $chart; return $chart;
} }
} }