2 namespace App\View\Cell;
7 use Google_Service_Calendar;
13 class GoogleCalendarCell extends Cell
17 * List of valid options that can be passed into this
22 protected $_validCellOptions = [];
25 * returns an array of calendar events based on the google client and
26 * the range between the start and end times.
28 * if the access token has died or suffered some unfortunate accident, then
29 * null is returned and authorization must be reattempted.
31 public static function prepareCalendar($client, $startTime = null, $endTime = null)
33 $cal = new Google_Service_Calendar ( $client );
34 Log::debug ( 'created google calendar object' );
36 $calendarId = 'primary';
39 // default is to start with today, but not with a time.
40 $startTime = new DateTime();
41 $startTime->setTime(0, 0);
45 $endTime = new DateTime($startTime->format('c'));
46 $endTime->modify ( "+31 day" ); // month from now (ish).
49 Log::debug ( 'start: ' . $startTime->format ( 'c' ) . ' end: ' . $endTime->format('c'));
52 // 'maxResults' => 100,
53 'orderBy' => 'startTime',
54 'singleEvents' => TRUE,
55 'timeMin' => $startTime->format('c'),
56 'timeMax' => $endTime->format ( 'c' )
60 $results = $cal->events->listEvents ( $calendarId, $optParams );
61 } catch (\Exception $e) {
62 Log::debug('caught an exception from listing events!');
66 Log::debug ( 'after listEvents call.' );
68 // the calEvents array will be an associative array where the keys are the
69 // dates that events start. the value stored for each key is in turn an associative array of
70 // good stuff about the event. the good stuff includes 'info', for the appointment description,
71 // 'start' for the starting date (plus time perhaps), and 'event' for the raw google event
75 if (count ( $results->getItems () ) == 0) {
76 // nothing to add to array.
78 foreach ( $results->getItems () as $event ) {
79 $info = $event->getSummary ();
81 $start = $event->start->dateTime;
82 if (empty ( $start )) {
83 // simple index by date.
84 $index = $event->start->date;
85 $start = 'All Day'; // our time mutates.
87 // make the index from just the date portion.
88 $bits = str_split($start, strpos($start, 'T'));
91 // clean up the time portion for presentation.
92 $date = new DateTime($start);
93 $start = $date->format('H:i:s');
95 //Log::debug('chopped time of start is: ' . $start);
99 if (array_key_exists($index, $calEvents)) {
100 $existing_entry = $calEvents[$index];
103 array_push ( $existing_entry, [
109 $calEvents[$index] = $existing_entry;
117 * Default display method.
119 * needs to be passed a GoogleClient object that has already been configured with a valid
124 public function display($calEvents)
127 $this->set ( 'calEvents', $calEvents );