Home API Docs Linkr Putting it all together Using the LinkrObject Class

Using the LinkrObject Class

Attention: open in a new window.PDFPrintEmail
The LinkrObject class was meant for extensions that are made up of categories and items. Using it is simple, and can diminish the size of your script dramatically. Let's use the Agora External link (a forum component) extension as an example:
var LinkrAgora = LinkrObject.getInstance();

The getInstance method takes 4 arguments:

Object options example:
var LinkrAgora_options =
{
  title : 'Agora',
  objName : 'LinkrAgora'
};

Search settings example:
var LinkrAgora_search =
{
  defaultType : 'category',
  multiple : true,
  options : {},
  results : {}
}; 
 
The default search type was defined as category. That means that anytime the search type is not specified, category will be assumed. Next, we add the SQL queries. The way Agora works is, users post there comments in topics which are assigned to a forum. Each forum can either be part of another forum (in which case it is refered to as a subforum) or part of a category. See w3schools External link for SQL tutorials.
// SQL queries.
// For every "type" there's an object.
LinkrAgora_search.sql = {
  category : {},
  forum : {},
  topic : {}
};
 
// SQL query for retrieving all categories
LinkrAgora_search.sql.category.all =
  'SELECT id, cat_name AS name '+
  'FROM #__agora_categories '+
  'WHERE enable = 1 '+
  'ORDER BY disp_position ASC, cat_name ASC';
 
// SQL for searching categories
LinkrCCB_search.sql.category.query =
  'SELECT id, cat_name AS name '+
  'FROM #__agora_categories '+
  'WHERE enable = 1 AND cat_name LIKE [query] '+
  'ORDER BY disp_position ASC, cat_name ASC';
 
// SQL query for forums and subforums.
// This one here is tricky, since forums
// and subforms should be treated slightly
// differently, and that's why we should
// opt for a function instead of an object
// to handle them.
LinkrAgora_search.sql.forum = function(id, query, opts)
{
  var sql =
  ' SELECT id, forum_name AS name '+
  ' FROM #__agora_forums '+
  ' WHERE enable = 1 ';
 
  // Add search query
  if (q)
  {
    sql +=
    ' AND ( LOWER(forum_name) LIKE '+ q +
    ' OR LOWER(forum_desc) LIKE '+ q +
    ' OR LOWER(forum_mdesc) LIKE '+ q +
    ' OR LOWER(forum_key) LIKE '+ q +' ) ';
  }
 
  // Parent id
  if ($type(id) == 'number')
  {
    // The column name will determine
    // if the parent is a forum or a category
    var col = $pick(opts.column, 'cat_id');
    sql += 'AND '+ col +' = '+ id;
 
    if (cn == 'cat_id')
      sql += ' AND parent_forum_id = 0';
  }
 
  return sql +' ORDER BY disp_position, forum_name';
};
 
// SQL query for forum topics
LinkrCCB_search.sql.topic.all =
  'SELECT id, subject AS name '+
  'FROM #__agora_topics '+
  'ORDER BY posted ASC';
 
// SQL for searching topics
LinkrCCB_search.sql.topic.query =
  'SELECT t.id, t.subject AS name '+
  'FROM #__agora_topics AS t '+
  'LEFT JOIN #__agora_posts AS p ON p.topic_id = t.id '+
  'WHERE ( LOWER(t.poster) LIKE [query] '+
  'OR LOWER(t.subject) LIKE [query] '+
  'OR LOWER(t.descrip_t) LIKE [query] '+
  'OR LOWER(p.message) LIKE [query] ) '+
  'GROUP BY t.id '
  'ORDER BY t.last_post DESC, t.posted DESC';
 
// SQL for topics within a forum
LinkrCCB_search.sql.topic.id =
  'SELECT id, subject AS name '+
  'FROM #__agora_topics '+
  'WHERE forum_id = [id]'+
  'ORDER BY posted';
 

Layout methods example:
var LinkrAgora_layout =
{
  // Category layout
  category : function(dbCategory)
  {
    // First call:
    // Retrieve category from database
    if ($type(dbCategory) == 'number')
    {
      // SQL query
      var q = '... insert query here ...';
 
      // Query database and use this
      // method as callback
      var callback = this.layout.category.bind(this);
      return this.tmpl.dbObject(q, callback);
    }
 
    // Second call: dbCategory is an object
    // (not a number, like in the first call).
    // Check for errors
    var e = this.tmpl.isError(dbCategory);
    if (e !== false) return this.display(e);
 
    // Fade in layout
    this.layout._genericLayoutDelayed({
      // ...
      // ... layout options
      // ...
    });
  },
 
  // Forum layout
  forum : function(dbForum)
  {
    // ... dislpay forum layout
  },
 
  // Topic layout
  topic : function(dbTopic)
  {
    // ...
  }
};
You can find documentation on the use of bind (on functions) here External link.

Template methods example:
var LinkrAgora_tmpl =
{
  url : function(type, id)
  {
    // Base for all URLs
    var url = 'index.php?option=com_agora';
 
    // View
    switch (type)
    {
      // Category
      case 'category':
        url += '&id='+ id;
        break;
 
      // Forum, Topic
      case 'forum':
      case 'topic':
        url += '&task='+ type +'&id='+ id;
        break;
    }
 
    // Return URL
    return url;
  }
};
You might not need helper methods at all. As you can see, we have a url method here to build the URL for any category, forum, or topic.

Thus, the final statement for getting an object instance will look like:
var LinkrAgora = LinkrOject.getInstance(
 
        // 1st argument: object options
        LinkrAgora_options,
 
        // 2nd: search settings
        LinkrAgora_search,
 
        // 3rd: layout methods
        LinkrAgora_layout,
 
        // 4th: helper methods
        LinkrAgora_tmpl
);

Get the fully working plugin from JoomlaCode External link.
Linkr 2.3.7 (20 May 2010) is finally out. This release tackles many bugs and will make the overall usage a little smoother.