Joomla: Write and call a helper function in a component

Gisto picture Gisto · Mar 17, 2012 · Viewed 8k times · Source

Fledgling Joomla / PHP developer, hitting a wall understanding how to do this. Everything I found searching has been for older versions of Joomla or other frameworks and so it's all confusing the first time around.

I want to have a helper function that I can call from anywhere in my component. Basically it takes a userID input and returns their full name, let's say hair color and height. Here's the function:

function get_profile_info($userID) {

        $db =& JFactory::getDBO();          
        $query = $db->getQuery(true);

        $query->SELECT('u.id as UserID
                    , u.name AS Name
                    , up.profile_value AS Hair
                    , up2.profile_value AS Height
                    ');
        $query->FROM (' #__users AS u');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 1');
        $query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 2');
        $query->WHERE(' u.id = '.$userID.'');
        $query->GROUPBY (' u.id
                    , u.name
                    , up.profile_value
                    ');


       $db->setQuery($query);   
       return $query;                   
}

I'd put this in a file called "lookups.php" within the "helpers" folder of my component...but here's where I'm not sure what to do next. Top of lookups.php has the obligatory:

<?php defined ( '_JEXEC' ) or die;

So two questions: 1) Do I put everything in a class or keep it as a series of functions (since there will be others)?

2) How do I pass the userID and get back the values of Name, Hair, and Height in the view (view.html.php / default.php)?

Thank you!

==================================

Edit: Based on @Shaz 's response below here's where I'm at (again, just starting off and trying to wrap my head around some of this):

lookups.php

abstract class LookupHelper {

        var $Name;
        var $Hair;
        var $Height;

    public function get_profile_info($userID) {
                ... 
                (same as above until the next line)

                $db->setQuery($query);
                $result=$db->loadRow();
                $Name = $result[1];
                $Hair = $result[2];
                $Height = $result[3];
        $getprofileinfo = array(Name=>$Name, Hair=>$Hair, Height=>$Height);
                $return $getprofileinfo;
           }
    }

Then in my default.php (will probably move to view.html.php):

    $getprofileinfo = Jview::loadHelper('lookups'); //got an error without this
    $getprofileinfo = LookupHelper::get_profile_info($userID);

    $Name = $getprofileinfo[Name];
    $Hair = $getprofileinfo[Hair];
    $Height = $getprofileinfo[Height];

So...it's working - but it seems like there's a much easier way of doing this (specifically manually creating an array then recalling by position). Thoughts?

Thank you!!

Answer

Shaz picture Shaz · Mar 17, 2012
  1. Create the helper class and include there all your functions:

    abstract class HelloWorldHelper { /* functions */ }

  2. Call the function and store the result:

    $var = HelloWorldHelper::get_profile_info($thatId);