adding example apps, fixing powerup issues
[feisty_meow.git] / production / example_apps / shared_calendar / src / View / Cell / GoogleCalendarCell.php
1 <?php
2 namespace App\View\Cell;
3
4 use Cake\View\Cell;
5 use Cake\Log\Log;
6 use DateTime;
7 use Google_Service_Calendar;
8
9
10 /**
11  * GoogleCalendar cell
12  */
13 class GoogleCalendarCell extends Cell
14 {
15
16     /**
17      * List of valid options that can be passed into this
18      * cell's constructor.
19      *
20      * @var array
21      */
22     protected $_validCellOptions = [];
23
24     /*
25      * returns an array of calendar events based on the google client and
26      * the range between the start and end times.
27      * 
28      * if the access token has died or suffered some unfortunate accident, then
29      * null is returned and authorization must be reattempted.
30      */
31     public static function prepareCalendar($client, $startTime = null, $endTime = null)
32     {           
33         $cal = new Google_Service_Calendar ( $client );
34         Log::debug ( 'created google calendar object' );
35         
36         $calendarId = 'primary';
37         
38         if (! $startTime) {
39                 // default is to start with today, but not with a time.
40                 $startTime = new DateTime();
41                 $startTime->setTime(0, 0);
42         }
43         
44         if (! $endTime ) {
45                 $endTime = new DateTime($startTime->format('c'));
46                 $endTime->modify ( "+31 day" ); // month from now (ish).
47         }
48         
49         Log::debug ( 'start: ' . $startTime->format ( 'c' )  . ' end: ' . $endTime->format('c'));
50                 
51         $optParams = array (
52                         // 'maxResults' => 100,
53                         'orderBy' => 'startTime',
54                         'singleEvents' => TRUE,
55                         'timeMin' => $startTime->format('c'),
56                         'timeMax' => $endTime->format ( 'c' )
57         );
58         
59         try {
60                 $results = $cal->events->listEvents ( $calendarId, $optParams );
61         } catch (\Exception $e) {
62                 Log::debug('caught an exception from listing events!');
63                 return null;
64         }
65         
66         Log::debug ( 'after listEvents call.' );
67         
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
72         // contents.
73         $calEvents = [ ];
74         
75         if (count ( $results->getItems () ) == 0) {
76                 // nothing to add to array.
77         } else {
78                 foreach ( $results->getItems () as $event ) {
79                         $info = $event->getSummary ();
80                         
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.
86                         } else {
87                                 // make the index from just the date portion.
88                                 $bits = str_split($start, strpos($start, 'T'));
89                                 $index = $bits[0];
90                                 
91                                 // clean up the time portion for presentation.
92                                 $date = new DateTime($start);
93                                 $start = $date->format('H:i:s');
94                                 
95                                 //Log::debug('chopped time of start is: ' . $start);
96                         }
97                         
98                         $existing_entry = [];
99                         if (array_key_exists($index, $calEvents)) {
100                                 $existing_entry = $calEvents[$index];
101                         }
102                         
103                         array_push ( $existing_entry, [
104                                         'info' => $info,
105                                         'start' => $start,
106                                         'event' => $event,
107                         ] );
108                         
109                         $calEvents[$index] = $existing_entry;
110                 }
111         }
112         
113         return $calEvents;
114     }
115     
116     /**
117      * Default display method.
118      *
119      * needs to be passed a GoogleClient object that has already been configured with a valid
120      * access token.
121      *
122      * @return void
123      */
124     public function display($calEvents)
125     {
126         
127         $this->set ( 'calEvents', $calEvents );
128     }
129 }