Call helper method with user action on Front End list

248 views
Skip to first unread message

Glenn Arkell

unread,
Nov 20, 2013, 1:23:13 AM11/20/13
to joomla-de...@googlegroups.com
I have a simple events listing component and now I want to add a "register" icon to each event item so that when the user clicks on the icon it calls a helper method to insert a record in the db and then refresh the events listing page.

My default.php file is displaying the icon nicely but what is the best way to "action" the helper method, which has 2 parameters passed to it (event_id and user_id) to insert the new record in the db?  I was thinking of an onclick="" scenario by couldn't work out how to call the method - javascript is not my strong point and still learning MVC.

If I'm attacking this all wrong then let me know.  Any and all advice greatfully received.  Cheers.

Adam Rifat

unread,
Nov 20, 2013, 7:25:45 AM11/20/13
to joomla-de...@googlegroups.com
You could try something like a JHtmlHelper class

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_messages
 *
 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * @package     Joomla.Administrator
 * @subpackage  com_messages
 * @since       1.6
 */
class JHtmlTickets
{
    /**
     * @param    int $value    The state value
     * @param    int $i
     */
    public static function state($value, $i, $enabled = true, $checkbox = 'cb')
   
    {
        $states = array(
            1 => array(
                'unpublish',
                'COM_TICKETS_UNPUBLISH',
                'COM_TICKETS_UNPUBLISH',
                'COM_TICKETS_UNPUBLISH',
                true,
                'publish',
                'publish'
            ),
            0 => array(
                'publish',
                'COM_TICKETS_PUBLISH',
                'COM_TICKETS_PUBLISH',
                'COM_TICKETS_PUBLISH',
                true,
                'unpublish',
                'unpublish'
            ),
      2 => array(
          'testing',
          'COM_TICKETS_TESTING',
          'COM_TICKETS_TESTING',
          'COM_TICKETS_TESTING',
          true,
          'pending',
          'pending'
      ),
      3 => array(
          'pending',
          'COM_TICKETS_PENDING',
          'COM_TICKETS_PENDING',
          'COM_TICKETSs_PENDING',
          true,
          'help',
          'help'
      ),
       
       
        );

        return JHtml::_('jgrid.state', $states, $value, $i, 'tickets.', $enabled, true, $checkbox);
    }
}

Then in you template

   <td>
                  <?php echo JHtml::_('tickets.state', $item->state, $i, $canChange, 'cb'); ?>
                </td>

Where the JHtml::_ calls the state method of you helper class and returns the icon dependent on the state of the item. This relies on $item->state but I guess you could use something else. If you want to show the user is registered just pass $item->registered and then modify the states array in the elper class with whichever controller actions you want to trigger depending on whatever you pass in...

Does that make any sense?

Matt Thomas

unread,
Nov 20, 2013, 8:06:35 AM11/20/13
to joomla-de...@googlegroups.com

I know you said that JavaScript wasn't your stong suite, but this sound like a great use of com_ajax. Take a look at http://docs.joomla.org/Using_Joomla_Ajax_Interface and see what you think.

Best,

Matt Thomas
Founder betweenbrain™
Lead Developer Construct Template Development Framework
Phone: 203.632.9322
Twitter: @betweenbrain
Github: https://github.com/betweenbrain

Sent from mobile. Please excuse any typos and brevity.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Glenn Arkell

unread,
Nov 20, 2013, 4:35:11 PM11/20/13
to joomla-de...@googlegroups.com
Thank you both for providing this guidance.  @Adam, yes makes sense, let me work through it to make it work for me.  @Matt, agreed a perfect opportunity to get into com_ajax... but... I will need to do quite a bit of reading up on this and yes I should too because it is something that will benefit me in future evolutions of my simple components.  I have been delaying my delving into com_ajax due to my lack of knowledge in basic javascript and waiting to see some questions/answers on this group come to light and watch the doco with anticipation.  I guess I should just stop procrastinating and get into it.  Cheers.

Glenn
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsub...@googlegroups.com.

Matt Thomas

unread,
Nov 20, 2013, 5:11:27 PM11/20/13
to Joomla! General Development
Hi Glenn,

I understand, it's a bit of a different world. But, a fun one though. If it helps any, I have a demo Ajax module at https://github.com/Joomla-Ajax-Interface/Ajax-Session-Module, with the heart of the JavaScript portion at https://github.com/Joomla-Ajax-Interface/Ajax-Session-Module/blob/master/mod_session.php#L27. It uses jQuery, so you can find many resources to help you online.

Hope that helps!

Best,

Matt Thomas
Founder betweenbrain
Phone: 203.632.9322
Twitter: @betweenbrain



To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Glenn Arkell

unread,
Nov 21, 2013, 12:03:55 AM11/21/13
to joomla-de...@googlegroups.com
Oh alright, I give in...... I'll give it a go. ;-) - so my first question is do I have to have a module/plugin with my component to use this or can I directly use it from my component ....

ie instead of calling with ?option=com_ajax&module=session should I use ?option=com_ajax&component=mycomponent. 

Or is this potentially something that could be included in future releases perhaps?
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-general+unsubscribe@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.

Glenn Arkell

unread,
Nov 21, 2013, 1:50:54 AM11/21/13
to joomla-de...@googlegroups.com
Hi Matt,

I've assumed that I needed a module so working through your session example I've created my (first) module called mod_gaeventrego which has the update of the database method in the helper called "public static function registerAttendeeAjax()".  It has all installed successfully so fingers crossed.

Coming back to my component now in the default.php view tmpl, how do I "action" (probably not the right word) this?

I did have ... <a class="btn btn-micro hasTooltip" title="" onclick="return listItemTask(cb'.$i.',attendees.unpublish)" href="javascript:void(0);" "="" data-original-title="Cancel Attendance?"><i class="icon-attending"></i></a>  obviously trying to copy the backend admin type publish/unpublish action.

So in essence I'm still at a loss of how to trigger this process.  thanks for pushing me into this anyway, I need to do it.  Cheers.

Glenn

Matt Thomas

unread,
Nov 21, 2013, 9:51:25 AM11/21/13
to Joomla! General Development
Hi Glenn,

My sincerest apologies. When I first read your note, I overlooked that fact that you are the developer of the component in reference. For some reason I had assumed you were developing a module to work with another component. I am sorry for that confusion.

All is not lost, and much is to be gained from this. Essentially, the change is that you do not need to use com_ajax, or even develop a module for Ajax functionality, unless you really want to. I updated the docs at http://docs.joomla.org/Using_Joomla_Ajax_Interface to try to clarify this, but com_ajax is generally used when a module or plugin is interacting with a component that someone else developed. Com_ajax is essentially an entry point for HTTP requests for modules and plugins, but you don't need that as your component can be that entry point.

In your case, your component's view could make the Ajax requests directly to your component. For example, your Javascript can call 

?option=com_mycomponent&event_id=foo&user_id=bar

 In your controller, you'd check for these parameters and act of them accordingly.

But, I digress from your question.

Essentially, the way I'd handle it is by using jQuery's .on() function (http://api.jquery.com/on/). If you just want to trigger a method, you could do something as simple as:


$(document).on('click','.icon-attending', function (){ $.ajax({
url: "option=com_mycomponent&action=cancel&event_id=123&user_id=456",
cache: false, success: function(){ alert('Cancelled!'); },
})});


This is just an untested example, but hopefully something to help you get started. The example at https://github.com/Joomla-Ajax-Interface/Ajax-Session-Module/blob/master/mod_session.php#L29 is a bit more involved, but could be something to play with to develop the idea further.

Hope that helps!

To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Glenn Arkell

unread,
Nov 21, 2013, 9:12:34 PM11/21/13
to joomla-de...@googlegroups.com
Hi Matt,

This is brilliant, thanks for your patience.  I think I was having a brain fade when I first started working on this idea and totally forgot about setting my task for the controller and checking these parameters.  Doh.  Anyway I think my brain is still in fade mode with collecting the passed parameters?

My view now has the below for each event record listed.
<a class="btn btn-micro hasTooltip" title="" href="index.php?option=com_mycomponent&task=events.eventrego&eventaction=1&event_id=234&attendee=567'" data-original-title="Attend Event?"><i class="icon-notattending"></i></a>

But I'm not getting any data passed into the controller.  The only way I got anything was with a very unsavory $_GET().

    public function eventrego()
    {
        // Initialise variables.
        $app    = JFactory::getApplication();
        $model    = $this->getModel('events');

        // Get the data from the form POST
        //$data = JRequest::getVar('jform', array(), 'post', 'array');  // nothing received here
        //$data = $app->input->get('jform', array(), 'array');  // nothing received here
       
        $data['event_id'] = $_GET['event_id'];
        $data['attendee'] = $_GET['attendee'];
        $data['eventaction'] = $_GET['eventaction'];


            // Now perform the appropriate action
            if ($data['eventaction'] && $data['event_id']) {
                    $updevent    = $model->registerAttendee($data['event_id'], $data['attendee']);
            }
            if (!$data['eventaction'] && $data['event_id']) {
                    $updevent    = $model->cancelAttendee($data['event_id'], $data['attendee']);
            }
       
            $this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=events', false));

        return true;

Matt Thomas

unread,
Nov 22, 2013, 10:50:36 PM11/22/13
to Joomla! General Development
Hi Glenn,

Apologies for the delay. When using JInput, the first argument is the URL parameter. So, in your case, you could change:

$data = $app->input->get('jform', array(), 'array');

to

$data['event_id'] = $app->input->get('event_id');

I think that will address the issue with not getting any data passed into the controller.


To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.

Glenn Arkell

unread,
Nov 23, 2013, 12:10:18 AM11/23/13
to joomla-de...@googlegroups.com
Hi Matt,

No need to apologise, specially from someone who "gives" so much to this group.  This is the first time I've actually used a link rather than a full form to send data, and this was the missing link I needed. Thanks for this.  Cheers.

Glenn
Reply all
Reply to author
Forward
0 new messages