Adding an action to return a JSON feed

A place for users and developers of the Xataface to discuss and receive support.

Adding an action to return a JSON feed

Postby gthorne » Tue Aug 07, 2012 11:31 am

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.
gthorne
 
Posts: 32
Joined: Tue Jul 17, 2012 2:47 pm

Re: Adding an action to return a JSON feed

Postby shannah » Tue Aug 07, 2012 12:04 pm

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Adding an action to return a JSON feed

Postby gthorne » Tue Aug 07, 2012 9:15 pm

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?
gthorne
 
Posts: 32
Joined: Tue Jul 17, 2012 2:47 pm

Re: Adding an action to return a JSON feed

Postby shannah » Tue Aug 07, 2012 9:28 pm

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Adding an action to return a JSON feed

Postby shannah » Tue Aug 07, 2012 9:29 pm

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Adding an action to return a JSON feed

Postby gthorne » Wed Aug 08, 2012 9:32 am

Thanks, Steve. Authentication will be handed by the web server (we have an LDAP server here and use mod_ldap).
gthorne
 
Posts: 32
Joined: Tue Jul 17, 2012 2:47 pm

Re: Adding an action to return a JSON feed

Postby shannah » Wed Aug 08, 2012 9:53 am

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
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Adding an action to return a JSON feed

Postby gthorne » Wed Aug 08, 2012 1:14 pm

Done, and added the JSON header as well. I'll admit to being a little sloppy when trying to push something out the door.
gthorne
 
Posts: 32
Joined: Tue Jul 17, 2012 2:47 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 10 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved