A little bit of everything…
RSS icon Home icon
  • Retrieving custom data into EventList

    Posted on February 17th, 2009 Aleix No comments

    This is the second part of the post Adding custom fields to EventList, where I explained how custom data fields can be added to EventList Joomla Component by Schlu. In this post I will cover how to take full advantage from those custom fields by making EventList RC1.0 for Joomla 1.5 retrieve the information submitted through them.

    As I mentioned in the first part, EventList does not allow (yet) a user-friendly system for adding custom fields, therefore there’s no user-friendly system either to retrieve custom data into EventList. Some modifications to the core files of the component are required for this purpose. The good news is, that it is not as complicate as it might seem at first.


    Custom fields in layout

    Custom fields in layout

    The example

    Let’s at first remember the example from the first part: we have a drop down menu (or text field) which allows users to input the number of guests they would like to bring along to the specific event. This information is submitted together with the user registration and it is stored in the same database table where the user registration.

    Of course this data might be useless if we cannot manipulate it, but merely dump it into a database table. An alternative is to query the database and retrieve this information from an external script, which is relatively easy. But what if we want to use this data in EventList? We need that EventList retrieves this data from the database.

    InĀ  our particular case, we want to show the number of guests in the list of registered users: the number of guests each user has registered will appear next to the user name.


    The steps

    This is what we need to do:

    1. Modify the function which retrieves the data
    2. Modify the page layout (frontend)


    Step one: modifying the function which retrieves the data

    To do so we first need to find this function. The file where the function is found is different depending on which page we want the retrieved information to be displayed, and the code for retrieving the data for different pages might differ! In our case we want to retrieve the data and show it in the details.php page. To do so, first open the file containing the set of functions for details.php:

    components/com_eventlist/models/details.php

    This page should already sound to you, from the first post. Here there’s the function we modified to store our custom data to the database, remember? The difference is that we now want to modify the code of a different function.

    Find the function getRegisters. You will see a number of MySQL query lines and all we need to do is add few characters to the first of them. Find the line of code below, within the function. This is how it looks like before making the changes:

    $query = 'SELECT '.$name.' AS name, r.uid'
    

    Look carefully the code below to see how it should look like after you make the changes:

    $query = 'SELECT '.$name.' AS name, r.uid, r.invitats'
    

    So, that was it. What we did is to add r.invitats to the set of columns from which to retrieve data. For example, if your column is named guests, you would have added r.guests instead. As two tables are joined, the r. indicates we’re fetching the data from the jos_eventlist_register table, and the name coming after can be of any column which exists in this table.

    Custom added column

    jos_eventlist_register with a custom added column highlighted in red

    This code retrieves the data from the database. But we want to display it too.


    Step two: modifying the page layout (frontend)

    So what comes next? Time to modify our template and specify where exactly the data will be shown once retrieved. Remember we want to show our results where the list of registered users to an event is outputted. We shall therefore open the template file of the desired portion of the component:

    components/com_eventlist/views/details/tmpl/default_attendees.php

    In the code, find a reference to echo $register->name; or similar. This is where the name of the registered user is displayed. Notice that the line is in a loop, because what the code does is to get all the registered users to the event from the database and loop the results. Next to it, and with the format you wish, add $register->invitats (assuming that you queried the database in step one for r.invitats.

    So the following line:

    echo $register->name . ' + ' . $register->name . ' guest(s).';
    

    Would output results similar to the following (samples):

    John + 1 guest(s).

    Karla + 3 guest(s).

    Adam + 0 guest(s).

    Etc.

    As you can see it can be very practical to be able to handle custom data and display it together with the default data. It isn’t so complicate, in the end, when you know how to do it…


    Conclusion

    EventList is a great component for Joomla. Despite the fact that it might seem to be very limited to some, those of us with a bit of programming knowledge can turn it into a very flexible tool. In the beginning, I didn’t dare to make any modifications to the code (looked too confusing at first); but in the end I realised that it is not too complicated, after all. One must first spend some time reading through the code to understand how it works. Once this is successfully achieved, the rest is all about patience, lots of testing and creativity!

     

    Related posts:


    Leave a reply