6 minutes reading time (1287 words)

Sorting Articles by Recently Touched Date

Joomla 1.5 has several sort orders defined to arrange the articles in a specific sequence for displaying in the blog and list layouts. The sort orders are primarily based on the created date, title, hits, and pre-defined order of the articles. There may be situations where one would like to see a combination of articles in the order that has been recently modified and/or created like the option View New Posts in the Joomla forums. Currently, there is no sort order available to handle this situation. This paper discusses about how to implement this new sort order, Recently Touched Date.

The sort orders that are currently defined in Joomla! are Oldest First, Most Recent First, Title - Alphabetical, Title - Reverse Alphabetical, Author - Alphabetical, Author - Reverse Alphabetical, Most Hits, Least Hits, and Order. The description for these sort orders are presented in Table-1.

Sort Order Description of How Articles are Displayed
Oldest First Articles are displayed starting with the oldest and ending with the most recent
Most Recent First Articles are displayed starting with the most recent and ending with the oldest
Title – Alphabetical Articles are displayed by Title in alphabetical order (A to Z)
Title - Reverse Alphabetical Articles are displayed by Title in reverse alphabetical order (Z to A)
Author – Alphabetical Articles are displayed by Author in alphabetical order (A to Z)
Author - Reverse Alphabetical Articles are displayed by Author in reverse alphabetical order (Z to A)
Most Hits Articles are displayed by the number of hits, starting with the one with the most hits and ending with the one with the least hits
Least Hits Articles are displayed by the number of hits, starting with the one with the least hits and ending with the one with the most hits
Order Articles are ordered according to the Order column entered in the Article Manager
Table 1 - Current Sort Order in Joomla! 1.5

 

Consider the example of Articles shown in Table 2. For simplicity, only the date is shown in the place of date-time.

Article Name Created Date Modified Date Touched Date
Alpha 2011-09-01 2011-09-05 2011-09-05
Beta 2011-09-02 2011-09-02
Gamma 2011-09-03 2011-09-10 2011-09-10
Delta 2011-09-04 2011-09-04
Epsilon 2011-09-05 2011-09-07 2011-09-07
Zeta 2011-09-06 2011-09-06
Table-2 Sample Data

 

Based on the data, if the sort order Oldest First is used, the articles would be as shown in Table 3.1.

Article Name Created Date
Alpha 2011-09-01
Beta 2011-09-02
Gamma 2011-09-03
Delta 2011-09-04
Epsilon 2011-09-05
Zeta 2011-09-06
Table 3.1 - Result of the sort order Oldest First

 

If the sort order Most Recent First is used, the articles would be as shown in Table 3.2.

Article Name Created Date
Zeta 2011-09-06
Epsilon 2011-09-05
Delta 2011-09-04
Gamma 2011-09-03
Beta 2011-09-02
Alpha 2011-09-01
Table 3.2 - Result of the sort order Most Recent First

 

If the new sort order Recently Touched Date is used, the articles would be as shown in Table 3.3.

Article Name Touched Date
Gamma 2011-09-10
Epsilon 2011-09-07
Zeta 2011-09-06
Alpha 2011-09-05
Delta 2011-09-04
Beta 2011-09-02
Table 3.3 - Result of the sort order Recently Touched First

 

In the new sort order Recently Touched Date discussed, a potential challenge lies in handling the last modified date-time. For the articles that were created but not modified, the modified date-time is filled as '0000-00-00 00:00:00'. This is not a true representation of date-time. For the articles that were modified, the modified date-time will reflect the true date-time of change (e.g., '2011-10-15 11:20:35'). So, how could the articles could be shown in the Recently Touched Date order if they have to be sorted in the descending order of the touched date-time?

For the articles that were modified, we take the modified date-time. For the articles that were created but not modified, we take the created date-time.

Implementation of this algorithm requires changes to 2 core Joomla files; one to show the new sort order and another to introduce the sort order logic in the SQL (Structured Query Language) code. For demonstration purpose, the new sort order would be introduced in the Category Blog Layout.

First, a new option value (tdate) to reflect the new sort order in the drop-down box of the parameter Primary Order that exists in the tab/slide Parameters (Advance) of the Category Blog Layout’s Menu Item: [ Edit ] screen has to be added. The file to modify for this is blog.xml (original code shown in Code 1.1) located in the directory \components\com_content\views\category\tmpl. The parameter that has the label Primary Order has to be changed for this as shown in Code 1.2.



<param name="orderby_sec" type="list" default="" label="Primary Order" description="Order that the items will be displayed in.">
<option value="">Default</option>
<option value="date">Oldest first</option>
<option value="rdate">Most recent first</option>
<option value="alpha">Title Alphabetical</option>
<option value="ralpha">Title Reverse-Alphabetical</option>
<option value="author">Author Alphabetical</option>
<option value="rauthor">Author Reverse-Alphabetical</option>
<option value="hits">Most Hits</option>
<option value="rhits">Least Hits</option>
<option value="order">Ordering</option>
</param>

Code 1.1 - Original code in the file blog.xml

 



<param name="orderby_sec" type="list" default="" label="Primary Order" description="Order that the items will be displayed in.">
<option value="">Default</option>
<option value="date">Oldest first</option>
<option value="rdate">Most recent first</option>
<option value="alpha">Title Alphabetical</option>
<option value="ralpha">Title Reverse-Alphabetical</option>
<option value="author">Author Alphabetical</option>
<option value="rauthor">Author Reverse-Alphabetical</option>
<option value="hits">Most Hits</option>
<option value="rhits">Least Hits</option>
<option value="tdate">Recently Touched first</option>
<option value="order">Ordering</option>
</param>

Code 1.2 - Modified code in the file blog.xml



Second, a new SQL code segment to reflect the new sort order Recently Touched Date has to be introduced in the file query.php (original code shown in Code 2.1) located in the directory \components\com_content\helpers.  In this file the function orderbySecondary sets the sort order.  The SQL code segment for the value tdate would be added as shown in Code 2.2.



function orderbySecondary($orderby)
{
switch ($orderby)
{
case 'date' :
$orderby = 'a.created';
break;

case 'rdate' :
$orderby = 'a.created DESC';
break;

case 'alpha' :
$orderby = 'a.title';
break;

case 'ralpha' :
$orderby = 'a.title DESC';
break;

case 'hits' :
$orderby = 'a.hits DESC';
break;

case 'rhits' :
$orderby = 'a.hits';
break;

case 'order' :
$orderby = 'a.ordering';
break;

case 'author' :
$orderby = 'a.created_by_alias, u.name';
break;

case 'rauthor' :
$orderby = 'a.created_by_alias DESC, u.name DESC';
break;

case 'front' :
$orderby = 'f.ordering';
break;

default :
$orderby = 'a.ordering';
break;
}

return $orderby;
}

Code 2.1 - Original code in the file query.php

 



function orderbySecondary($orderby)
{
switch ($orderby)
{
case 'date' :
$orderby = 'a.created';
break;

case 'rdate' :
$orderby = 'a.created DESC';
break;

case 'alpha' :
$orderby = 'a.title';
break;

case 'ralpha' :
$orderby = 'a.title DESC';
break;

case 'hits' :
$orderby = 'a.hits DESC';
break;

case 'rhits' :
$orderby = 'a.hits';
break;

case 'order' :
$orderby = 'a.ordering';
break;

case 'author' :
$orderby = 'a.created_by_alias, u.name';
break;

case 'rauthor' :
$orderby = 'a.created_by_alias DESC, u.name DESC';
break;

case 'front' :
$orderby = 'f.ordering';
break;

case 'tdate' :
$orderby = "CASE a.modified WHEN '0000-00-00 00:00:00' THEN a.created ELSE a.modified END DESC";
break;

default :
$orderby = 'a.ordering';
break;
}

return $orderby;
}

Code 2.2 - Modified code in the file query.php

 

Now, click on the drop-down arrow of the Primary Order parameter in the corresponding Category Blog Layout’s Menu Item: [ Edit ] screen. Woila! You see the additional sort option Recently Touched first as sown in Figure 1.

Primary_Order_-_ModifiedFigure 1 - Modified Drop-down List

Similar changes can be implemented in the Category List Layout, Section Blog Layout, Section List Layout, and Front-end Blog Layout just by changing the corresponding XML files (e.g., blog.xml, default.xml) and by introducing the new sort order Recently Touched Date.

As the files edited are core Joomla files, you cannot adopt the override technique. So, make sure to have a copy of them before modifying them. In addition, take a copy of the modified files also as these files may be overwritten by future releases of Joomla!

0
Joomla 1.6/1.7 Redirect Component Tutorial
Responsive Design
 

Comments

Already Registered? Login Here
No comments made yet. Be the first to submit a comment

By accepting you will be accessing a service provided by a third-party external to https://magazine.joomla.org/