CB Helloworld Plugin (part 2)

As promised (here), this article adds more functionality to the initial CB Helloworld plugin (described here). Please read the initial article first and if you have installed the CB Helloworld plugin provided in this first article please unistall it before proceeding any further. You can access the new helloworld plugin package here.

In the first plugin we displayed a simple message provided to the code by the Tab parameter hwTabMessage. In this extended plugin we will get a little more personal. We will display this simple message followed by some information about the viewer and the profile being viewed.

Information about the logged in user is contained in the global Joomla variable $my. This variable points to a mosUser object that contains a lot of information regarding the logged in user. In order to access it, we must declare is with a global $my; statement (see line 38 of the helloworld.php file of the new CB Helloworld plugin also shown bellow) in our getDisplayTab function. The first item in the $my mosUser object is the id item. This item is reference by the code $my->id and it basically contains the Joomla userid of the logged in user. If this value is 0, then there is no user logged in (this means that a public guest is viewing the site). Another item in this global $my array is the name item referenced by the code $my->name. This code basically returns the name of the logged in Joomla user. Of course if no user is logged in this code will return null. Line 56 of the helloworld.php file contains the logic to display Viewer is Guest or Viewer is {name} (where {name} is substituted by $my->name) depending on a logged in viewer (thus $my->id !=0) or a guest viewer ($my->id ==0). There are many other useful items in the global $my array. You can easily look at these items by changing line 39 to print_r($my); (this will dump the entire array to the screen when you view a profile). You should be able to use this simple print_r statement to figure out more information provided in the mosUser object regarding the current logged in user. Hopefully, you will be able to see how to figure out the group the logged-in user belongs to, the username, the email, etc. Take this as a homework assignment  .

Now lets see how can we access additional information about the actual profile being viewed. This is actually provided for us by the CB plugin API. The $user object which is referenced in the getDisplayTab function contains all the information about the viewed profile user (actually all of the fields in the comprofiler table for this profile user are contained in this $user object). Using the print_r($user); in line 39 we can see all the goodies in the object referenced by the $user variable. You will see all the extra fields defined by CB - either via the CB field Management backend or any installed plugins. Lines 58 and 60 use the $user variable to display the profile owners name and the profile owners email. 

The relevant code referenced above follows: 

37 function getDisplayTab($tab,$user,$ui) {
38  global $my;
39  
40  $return = null;
41  $params = $this->params; // get parameters (plugin and related tab)
42  
43  $is_helloworld_plug_enabled = $params->get('hwPlugEnabled', "1");
44  $helloworld_tab_message = $params->get('hwTabMessage', "Hello Joomlapolitans!");
45  
46  if ($is_helloworld_plug_enabled != "0") {
47   if($tab->description != null) {
48    $return .= "\t\t<div class=\"tab_Description\">"
49     . $tab->description // html content is allowed in descriptions
50     . "</div>\n";
51   }
52   $return .= "\t\t<div>\n"
53    . "<p>"
54    . htmlspecialchars($helloworld_tab_message) // make all other output html-safe
55    . "</p><p>"
56    . "Viewer is <strong>" . ($my->id !=0 ? $my->name : "Guest") . "</strong>"
57    . "</p><p>"
58    . "Profile Owner is <strong>" . $user->name . "</strong>"
59    . "</p><p>"
60    . "Profile Owner's email is <strong>" . $user->email . "</strong>"
61    . "</p>"
62    . "</div>\n";
63  }
64  return $return;
65 } // end or getDisplayTab function

and if a logged-in user named Nick A. (me) is viewing a testers profile (where the testers firstname, middlename and lastname are all given values tester2) the actual (part 2) hello world tab (on my test site) renders:

I would like to thank Beat for reviewing this article and code (and for the million other things he has been doing for us) and to add the following lines of code between lines 61 and 62 above:

if ( $my->id == $user->id ) {
    $return .= "<p>You are looking at yourself !</p>"; }

The above lines demonstrate how to differentiate between the two objects but as Beat says "your profile should look as much the same to you as to others". So consider the above lines as not the best practice.

Once again welcome to the wonderfull world of CB Plugin development! Please rate and discuss this article - depending on the feedback I might be persuaded to continue the series.

{mos_sb_discuss:7}

Facebook Twitter LinkedIn