Page 1 of 1

Adding an action to return a JSON feed

PostPosted: Tue Aug 07, 2012 11:31 am
by gthorne
I'm trying to use this calender widget on my dashboard page. In order to do so, I need to get a JSON feed from another table. It has a pretty specific requirement for the feed, so I think it's best to roll my own JSON action. I've searched through the forums and wiki, and am a little confused on how I can do this. Can I just define a new action in {site_dir}/actions/get_json.php then call it on the uri line like this?
Code: Select all
-table=cal_data&-action=get_json


I'm sure this is easy, I just can't wrap my mind around it yet.

Re: Adding an action to return a JSON feed

PostPosted: Tue Aug 07, 2012 12:04 pm
by shannah
That's pretty much right. Check out this section from the Getting Started tutorial for an example:
http://xataface.com/documentation/tutor ... ce_actions

-Steve

Re: Adding an action to return a JSON feed

PostPosted: Tue Aug 07, 2012 9:15 pm
by gthorne
I ended up doing this:

Code: Select all
<?php
class actions_get_json {
    function handle(&$params){
        $dates = df_get_records_array('cal_data', array());

        // Commented out the following and used json_encode instead
        //df_display(array('my_dates'=>$dates), 'events_json.html');

        $date_array = array ();

        foreach ($dates as $evt)
        {
                $a = array(
                        "id" => $evt->val('id'),
                        "title" => $evt->val('short_desc'),
                        "start" => $evt->strval('event_start_time'),
                        "end" => $evt->strval('event_end_time'),
                );

                array_push ($date_array, $a);
        }


        echo json_encode ($date_array);
    }
}


Am I breaking anything by doing the echo from the action, instead of going through the smarty template?

Also, how do I pass/receive custom parameters to the action?

Re: Adding an action to return a JSON feed

PostPosted: Tue Aug 07, 2012 9:28 pm
by shannah
You can get parameters the same way you do in any PHP script (e.g. $_GET or $_POST). In addition you could use the Dataface_Application::getQuery() method which returns a massaged version of the query parameters (it guarantees at least -table and -action will be set).

One thing to note. You should return a request header to let clients know that the data type is JSON. Some browsers may choke if you don't.

You may want to refer to the export_json action that is defined in the xataface actions/export_json.php file for pointers.

-Steve

Re: Adding an action to return a JSON feed

PostPosted: Tue Aug 07, 2012 9:29 pm
by shannah
One other thing to notice is that you don't have any permissions or security set up on your action so it will be available to the world. If this is public data, fine. If not, you should implement some security.

-Steve

Re: Adding an action to return a JSON feed

PostPosted: Wed Aug 08, 2012 9:32 am
by gthorne
Thanks, Steve. Authentication will be handed by the web server (we have an LDAP server here and use mod_ldap).

Re: Adding an action to return a JSON feed

PostPosted: Wed Aug 08, 2012 9:53 am
by shannah
All the same, I suggest the following change:
Code: Select all
foreach ($dates as $evt)
        {
                $a = array(
                        "id" => $evt->val('id'),
                        "title" => $evt->val('short_desc'),
                        "start" => $evt->strval('event_start_time'),
                        "end" => $evt->strval('event_end_time'),
                );

                array_push ($date_array, $a);
        }

to
Code: Select all
foreach ($dates as $evt)
        {
        if ( !$evt->checkPermission('view') ) continue;
                $a = array(
                        "id" => $evt->val('id'),
                        "title" => $evt->val('short_desc'),
                        "start" => $evt->strval('event_start_time'),
                        "end" => $evt->strval('event_end_time'),
                );

                array_push ($date_array, $a);
        }


Note the line:
Code: Select all
if ( !$evt->checkPermission('view') ) continue;


This will just guarantee that users who don't have the 'view' permission for an event won't see it. If you aren't using Xataface permissions, then users are granted all permissions and this will still work. If you start to use Xataface permissions later, this action will respect those permissions.

-Steve

Re: Adding an action to return a JSON feed

PostPosted: Wed Aug 08, 2012 1:14 pm
by gthorne
Done, and added the JSON header as well. I'll admit to being a little sloppy when trying to push something out the door.