0
votes

I'm using CodeIgniter to build an application with a Fullcalendar and I'm trying to integrate Google calendar.

I had trouble using the JS and preferred to use PHP, so I have a controller called Calendar.php to call the Google Calendar API, get the primary calendar and list the events.

Edit: This is what I'm getting now:

enter image description here

The object looks like this

GET http://localhost/admin/calendar/gcal...?start=2016-09-25&end=2016-11-06&_=1476324367596

200 OK 2.68s

Response

[{"title":"lobna elatreby @nrc","type":"gcal","calendar_id":356,"notes":"notes","start":"2010-03-23T14 :30:00-04:00","end":"2010-03-23T15:30:00-04:00"},{"title":"Biology Test 1","type":"gcal","calendar_id" :356,"notes":"notes","start":"2013-02-08T18:30:00-05:00","end":"2013-02-08T19:30:00-05:00"}]

I used json_encode(['events'=>$eventsArr]) and I got the same error

Deprecation warning: moment().zone is deprecated, use moment().utcOffset instead. Arguments: 2010-03-23T14:30:00-04:00

deprecate/fullcalendar/moment.min.js:309:98...............

TypeError: t.start is undefined

function C(t){null==t.allDay&&(t.allDay=!(t.start.hasTime()||t.end&&t.end.hasTime...

I tried download & updated Moment.js, I tried changing the date format ... Google Calender API "The time, as a combined date-time value (formatted according to RFC3339). A time zone offset is required unless a time zone is explicitly specified in timeZone."

1
Just copy the request response and verify if the string is a valid JSON using (JSON formatter){jsonformatter.curiousconcept.com/] (for example). If the json isn't valid, the events won't be rendered. To reply to this message be sure to add @milz on the comment.Luís Cruz
@milz you are absolutely right, I'm trying to fix the date format but codeigniter date helper will not format it to RFC-4627. It will format to ISO-8601, HTTP Cookies, Atom and a few RFC formats that I used the jsonformatter with and apparently don't work. Is there a way to convert the date using standard php? thank younivanmorgan
Please copy the json string and post it on your question instead of the image. I cannot test it without the full json string. Thanks.Luís Cruz
I am still having problems with the date ... I updated my post and edited and added the object. @milznivanmorgan
So it is the object not the timezone. I got it to render after using your code for the object and using strtotime to change the date format .. thanks. you were a lot of help. I appreciate you taking the time to make the fiddle and I looked at different ones on your profile. will follow you @milznivanmorgan

1 Answers

0
votes

What you are returning from the server is not a valid JSON and the issue doesn't have to do with the date. Just take a look at the screenshot you've posted, that has something like:

{"events": {"description":"lobna elatreby @nrc", "start": "2010-03-23T14:30:00-04:00", "end": "2010-03-23T15:30:00-04:00"}}{"events":...

You're setting the multiple events and this is because of the code on the gcalendar_events function. You need to update the code to (take a look at the end of the method:

function gcalendar_events() {
    $client = $this->getClient();

    $client->addScope(Google_Service_Calendar::CALENDAR);
    // $client->setRedirectUri(site_url('calendar/gcalendar'));
    //$client->setAccessType('offline'); need calendar events to appear even if not logged in to google
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
        $access_token = $_SESSION['access_token'];

        $service = new Google_Service_Calendar($client);
        $id = 'primary';   
        $calendar = new Google_Service_Calendar_Calendar();
        $calendar = $service->calendars->get('primary');

        $event = new Google_Service_Calendar_Event();

        $events = $service->events->listEvents($id);

        $eventsArr = [];

        foreach ($events->getItems() as $event) {          
            // Append a new evet to the $eventsArr
            $eventsArr[] = array()
                'description' => $event->getSummary(),               
                'start'=> $event->getStart()->dateTime,                   
                'end' =>  $event->getEnd()->dateTime,
            ); 
        }  

        // Return a single `events` with all the `$eventsArr`
        echo json_encode(['events' => $eventsArr]);
    }
}

This should return something like:

{"events": [{"description":"lobna elatreby @nrc", "start": "2010-03-23T14:30:00-04:00", "end": "2010-03-23T15:30:00-04:00"}, {"description": "...", "start": ...}]