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|
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|
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|
If the sort order Most Recent First is used, the articles would be as shown in Table 3.2.
|Article Name||Created Date|
If the new sort order Recently Touched Date is used, the articles would be as shown in Table 3.3.
|Article Name||Touched Date|
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.
Code 1.1 - Original 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.
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.
Figure 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!