Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to import json array into jQuery calendar


Reply

3 Posts   7047 Views

Avatar
joelg

12 July 2009 at 11:04pm Community Member, 129 Posts

Hi

I'm trying to feed a jQuery calendar with data from Silverstripe. I've installed the perfectly well-working Calendar Event module, but all I want now is to pull out some Calendar Events and return them as a json array. Here is the code for initiating the calendar:

$(document).ready(function() {
   
      $('#calendar').fullCalendar({
         draggable: true,
         events: "json_events.php",
         eventDrop: function(event, delta) {
            alert(event.title + ' was moved ' + delta + ' days\n' +
               '(should probably update your database)');
         },
         loading: function(bool) {
            if (bool) $('#loading').show();
            else $('#loading').hide();
         }
      });
      
   });

And here is the json_events.php:

<?php

   $year = date('Y');
   $month = date('m');

   echo json_encode(array(
   
      array(
         'id' => 1,
         'title' => "Event1",
         'start' => "$year-$month-10",
         'url' => "http://yahoo.com/"
      ),
      
      array(
         'id' => 2,
         'title' => "Event2",
         'start' => "$year-$month-20",
         'end' => "$year-$month-22",
         'url' => "http://yahoo.com/"
      )
   
   ));

?>

The question is just, how or what should I do to get this working with silverstripe. Should I use the restfull server API? And if yes, how would I do that since I can't find any documentation on how to use the restfull server with json export.

Many thanks

Joel

Avatar
banal

13 July 2009 at 1:41am Community Member, 901 Posts

Hm. Just write a method in your Controller, that returns your Data as json?
Something like:

// put this in your Page_Controller or <whatever>_Controller
public function json(){
   // gather event data to an array
   return json_encode($arrayData);
}

Then you can call the URL of that page and append /json to it.
eg. http://localhost/silverstripe/page/json (will call the json method of the page-Controller)

Avatar
joelg

13 July 2009 at 9:35pm Community Member, 129 Posts

But of course, yes, thanks, works like a charm.