31 August 2010

Joomla: Read custom article parameters from a module

In implementing a web site on Joomla, I came across a common issue: I needed to pass additional parameters with an article into a 3rd-party module. In this case, the module was the Joomla AnythingSlider, a very slick and flexible content slider/carousel.

The initial task of creating the custom parameters wasn't so bad; the first thread in this post covers that. You add the parameter to the advanced section of the file administrator/components/com_content/models/article.xml:

<param name="article_type" type="radio" default="default" 
label="Article Type" description="article type">
<option value="default">Default</option>
<option value="picture">Picture (rotator)</option>
<option value="video">Video (rotator)</option>
</param>
However, reading the values in a module is a different story. Here's the syntax to fetch all the parameters from article.xml:
$paramsdefs = JPATH_COMPONENT.DS.'models'.DS.'article.xml';
$myparams = new JParameter( $paramsdata, $paramsdefs );
And here's how a specific custom parameter is read (in this case, it's called article_type):
echo "Article type: ". $myparams->get('article_type');
This is thanks to another reply to the same post on the Joomla forums, but page 2.

UPDATE:
A good quested was raised about one of the variables used in the above code: $paramdata. Where was it defined? Well, it's not in the above code, but in the AnythingSlider Joomla extension, mod_anythingslider; you can download it from here.

The mod_anythingslider has a helper.php file, which defines the class ModSlideShow:
<?php
/**
* AnythingSlider Helper Classes
*
* @package Joomla 1.5.0
* @subpackage Modules
* @Copyright Copyright (c)2010 Dnote Software
* @license GNU/GPL, see http://www.gnu.org/copyleft/gpl.html
*
* mod_anythingslider is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Parts of this software are based on AnythingSlider By Chris Coyier: http://css-tricks.com
**/

defined('_JEXEC') or die('Direct Access to this location is not allowed.');

class ModSlideShow
{
/**
* Returns a list of articles
*/
function getSlideshow(&$params, &$access)
{
global $mainframe;

$db =& JFactory::getDBO();
$user =& JFactory::getUser();
$aid = $user->get('aid', 0);

$catid = (int) $params->get('catid', 0);
$items = (int) $params->get('items', 0);

$contentConfig = &JComponentHelper::getParams( 'com_content' );
$noauth = !$contentConfig->get('show_noauth');
$date =& JFactory::getDate();
$now = $date->toMySQL();

$nullDate = $db->getNullDate();

// Ordering
switch ($params->get( 'ordering' ))
{
case 'o_asc':
$ordering = 'a.ordering';
break;
case 'o_dsc':
$ordering = 'a.ordering DESC';
break;
case 'm_dsc':
$ordering = 'a.modified DESC, a.created DESC';
break;
case 'c_dsc':
default:
$ordering = 'a.created DESC';
break;
}

// query to determine article count
$query = 'SELECT a.*,' .
' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'.
' FROM #__content AS a' .
' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
' WHERE a.state = 1 ' .
($noauth ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' ) ' .
' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )' .
' AND cc.id = '. (int) $catid .
' AND cc.section = s.id' .
' AND cc.published = 1' .
' AND s.published = 1' .
' ORDER BY '.$ordering ;
$db->setQuery($query, 0, $items);
$rows = $db->loadObjectList();

return $rows;
}

}
?>
Then in the mod_anythingslider.php, we define a few more variables:
$params->set('intro_only', 1);
$params->set('hide_author', 1);
$params->set('hide_createdate', 0);
$params->set('hide_modifydate', 1);

// Disable edit ability icon
$access = new stdClass();
$access->canEdit = 0;
$access->canEditOwn = 0;
$access->canPublish = 0;
// Other code here...
$slideShow = ModSlideShow::getSlideshow($params, $access);
Now we bring it all together in the default.php, which generates the HTML for the slider:
/* 
Variables used throughout the main foreach loop.
Don't define these in the loop b/c they'll get
created for each article in the carousel.
*/
$paramsdefs = JPATH_COMPONENT.DS.'models'.DS.'article.xml';
$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
$dispatcher =& JDispatcher::getInstance();

/*
Main foreach loop of the carousel component. Runs
once for each article in the slideshow.
*/
foreach ($slideShow as $item){
echo "<li>";
$aparams =& $item->parameters;
$params->merge($aparams);

$item->text = $item->introtext;
$results = $dispatcher->trigger('onPrepareContent', array (& $item, & $params, $limitstart));

$paramsdata = $item->attribs;
$myparams = new JParameter( $paramsdata, $paramsdefs );
$article_type = $myparams->get('article_type');
// More processing inside the foreach...
}
Hope this helps those confused by the $paramdata variable.

Add Finger-Swipe Support to Webpages

http://padilicious.com/code/touchevents/

27 August 2010

Multiple $(document).ready()

A page can contain multiple jQuery calls to the $(document).ready() function. This comes in handy if you're generating code at run-time or mixing static JavaScript with code generated via PHP or other server-side language. In addition, you can abbreviate the call to:
$(function() {
// do something on document ready
});

How to find element based on a string

Say you're looking for a string in the HTML and wish to get the parent element(s) that contains it. This Stackoverflow.com thread covers the technique, using a wildcard ("*") for all page elements. You can narrow it down by using a specific element type ("div" for example) instead of the wildcard.

40 New JavaScript Tutorials with Helping Techniques

http://www.tutoriallounge.com/2010/08/40-new-javascript-tutorials-with-helping-techniques/

jQuery loading remote/external javascript files using getScript()

http://colourgray.wordpress.com/2008/09/22/jquery-loading-external-javascript-files-using-getscript/

20 Methods for Upping Your Current Web Design Skills

http://www.onextrapixel.com/2010/08/24/20-methods-for-upping-your-current-web-design-skills/

Slideshowify: Ken Burns Slideshow Effect as a jQuery Plugin

http://www.subchild.com/2010/06/10/ken-burns-effect-as-a-jquery-plugin-slideshowify/

Extensible Autocomplete

http://blog.jqueryui.com/2010/08/extensible-autocomplete/

Sortable, Resizable, Editable HTML Table With TableKit

http://www.greepit.com/2010/08/sortable-resizable-editable-html-table-with-tablekit/

26 August 2010

Add Quick Launch bar to Windows 7

The Quick Launch feature can be recreated in Windows 7, though why it's hidden is puzzling. This article provides step-by-step instructions on how to do this and even position it on the left, next to the start button.

22 August 2010

Learning RDFa

A great resource for learning RDFa.

20 August 2010

W3C cheatsheet

A nice cheatsheet from the good people at W3C on developing for mobile, accessibility, i18n, and typography.

19 August 2010

Quick Tip: An Introduction to Sammy.js

This tutorial introduces the Sammy.js, a useful jQuery library for writing RESTful evented JavaScript applications.

18 August 2010

SpriteMe: Automated CSS sprite creation

CSS sprites (sometimes referred to as "sliding doors") provide a nifty way to minimize the number of images site visitors have to download. However, creating them can be tricky. The powerful SpriteMe tool comes to the rescue.

Just save this link as a bookmark then go to a page and launch the bookmark. Easy, right?

17 August 2010

Top Free Script Fonts On The Web

http://webexpedition18.com/articles/top-free-script-fonts-on-the-web/

25 Fantastic jQuery Techniques and Effects For Creating Awesome Websites

http://artatm.com/2010/08/25-fantastic-jquery-techniques-and-effects-for-creating-awesome-websites/

Disappearing background image in the Web Developer plug-in for Firefox

One of the most powerful ways to view and edit the CSS of web sites on Firefox is the Web Developer extension. However, I recently noticed that when I selected Edit CSS from the extension's toolbar, the background images on the page disappeared.

Some research revealed a good thread on this issue. The problem had to do with the "../" notation in the stylesheet; changing these to be relative to the site root fixed the problem. For example, this:
background: transparent 
url(../img/bg.png) no-repeat 0 0;
becomes this:
background: transparent 
url(/cms_prd/global/img/bg.png) no-repeat 0 0;
This is assuming that the background image is here:
http://localhost/cms_prd/global/img/bg.png

30 Inspiring and Well Designed Website Footers

http://designtutorials4u.com/30-inspiring-and-well-designed-website-footers/

ASP.NET health monitoring

http://msdn.microsoft.com/en-us/library/bb398933.aspx

Adobe & Typekit team up on Web font delivery

http://blogs.adobe.com/jnack/2010/08/adobe-typekit-team-up-on-web-font-delivery.html

03 August 2010

How to install Joomla on Windows

For development purposes, I needed Joomla installed on my Windows XP system. However, Joomla has some infrastructure requirements often not found on Windows boxes. Here are some steps on how to successfully install Joomla on your PC:
  1. Download the XAMPP for Windows package. I downloaded the EXE and installed it at the root C: in the XAMPP folder.
  2. If you also have IIS running on your system, after the installation, change the default port used by Apache.
  3. The XAMPP installation will automatically ask you if you want to launch its control panel. If it's not already running, go to Start > XAMPP for Windows > XAMPP Control Panel.
  4. Click to start the Apache and MySql services (if they're not already running). When they successfully start, you'll see a green indicator saying "Running" next to each service. [Note: If the services take too long to start, it's more than likely because port 80 is being used by IIS; follow Step 2 above to change the Apache port to something else and try to start the services again.]

  5. Download the latest version of Joomla.
  6. Create a folder named joomla under C:\xampp\htdocs\
  7. Unzip the contents of the Joomla ZIP file into this new folder.
  8. Open your browser and navigate to http://localhost/ or, if you changed the Apache port, to http://localhost:xx/, where xx is the port number you assigned to Apache in Step 2.
  9. Click the phpMyAdmin link and go to "Privileges" on the MySQL database.
  10. Choose "Add a new User" and give it a name and password you can remember.
  11. Under "Global privileges" for this user, click "Check All" to grant all rights to this user.
  12. Click Go to create the user.
  13. Go to the Databases tab and create a database for Joomla (could be called joomla).
  14. Open your browser and navigate to http://localhost/joomla/ or, if you changed the Apache port, to http://localhost:xx/joomla/, where xx is the port number you assigned to Apache.
  15. Follow the wizard until you get to the database screen. Here, enter the information for the database and user you just created. For the host, use localhost. Important: Do not enter the port number after the host name, such as localhost:xx because this will not work and it won't generate an error; enter only the word localhost into the Host Name field.
  16. Upon successful completion, Joomla asks you to delete the installation folder. Go ahead and do that.
  17. To see the Joomla home page, reload http://localhost/joomla/ or, if you changed the Apache port, http://localhost:xx/joomla/, where xx is the port number you assigned to Apache.



Additional resources on installing Joomla with XAMPP on Windows and other platforms can be found on the Joomla docs site.

Scr.im: Email Address Shortener

http://scr.im/