[SOLVED] Request for Content component's "Archive" Function

Your code modifications and patches you want to share with others.
Locked
baysaa
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Mar 03, 2008 2:44 pm

[SOLVED] Request for Content component's "Archive" Function

Post by baysaa » Fri Jan 22, 2010 10:51 am

Hello,

Apologies if this is the wrong place to request hacks.

I am really in need of a hack to the Archive functionality of the default Content component.

What I need it to do is:
1. In the admin area, an ability to choose a specific Section/Content Category when creating a new "Menu Item" to Link to the "Archive view".

2. On the front-end, need to have the model/controller to pull out archived articles depending on the chosen category.

I really need this, or a similar workaround because a website I'm developing has 3 different content articles, all 3 have current/archived states and the archives need to be displayed separately.

Thank you,
B.
Last edited by baysaa on Mon Jan 25, 2010 10:52 am, edited 1 time in total.

baysaa
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Mar 03, 2008 2:44 pm

Re: Request for Content component's "Archive" Function hack

Post by baysaa » Mon Jan 25, 2010 10:07 am

I think I just solved this on my own. Thanks to anyone who looked at this.

B.

baysaa
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Mar 03, 2008 2:44 pm

Re: [SOLVED] Request for Content component's "Archive" Function

Post by baysaa » Mon Jan 25, 2010 2:06 pm

Solution for anyone else who needs this:

Create 3 new files in root/components/com_content/views/archive/tmpl/:
category.php
category.xml
category_items.php

Contents of category.php

Code: Select all

<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<form id="jForm" action="<?php JRoute::_('index.php')?>" method="post">
<?php if ($this->params->get('show_page_title', 1)) : ?>
	<div class="componentheading<?php echo $this->escape($this->params->get('pageclass_sfx')); ?>"><?php echo $this->escape($this->params->get('page_title')); ?></div>
<?php endif; ?>
	<p>
		<?php if ($this->params->get('filter')) : ?>
		<?php echo JText::_('Filter').'&nbsp;'; ?>
		<input type="text" name="filter" value="<?php echo $this->escape($this->filter); ?>" class="inputbox" onchange="document.jForm.submit();" />
		<?php endif; ?>
		<?php echo $this->form->monthField; ?>
		<?php echo $this->form->yearField; ?>
		<?php echo $this->form->limitField; ?>
		<button type="submit" class="button"><?php echo JText::_('Filter'); ?></button>
	</p>
<?php if(count($this->items) > 0){ ?>
<?php echo $this->loadTemplate('items'); ?>
<?php }else{
	echo '<p>Sorry, but there are no archived articles in this category, or no matching articles in your chosen filter/criteria.</p>';
};?>

	<input type="hidden" name="view" value="archive" />
	<input type="hidden" name="option" value="com_content" />
	<input type="hidden" name="viewcache" value="0" />
</form>
Contents of category.xml:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<metadata>
	<layout title="Category Archived Article List Layout">
		<message>
			<![CDATA[ARCHIVED ARTICLE LIST LAYOUT DESC]]>
		</message>
	</layout>
	<state>
		<name>Category Archived Article List Layout</name>
		<description>ARCHIVED ARTICLE LIST LAYOUT DESC</description>
		<url>
			<param name="id" type="category" label="Category" description="Choose a category..." />
		</url>
		<params>
			<param name="orderby" type="list" default="" label="Order by" description="PARAMORDERBY">
				<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>
		</params>
	</state>
</metadata>
Contents of category_items.php:
This is literally a copy of default_items.php in the same folder.

Now open root/components/com_content/models/archive.php and replace the last method called _buildContentWhere() with the following code:

Code: Select all

function _buildContentWhere()
	{
		global $mainframe;

		// Initialize some variables
		$user	=& JFactory::getUser();
		$db		=& JFactory::getDBO();
		$aid	= (int) $user->get('aid', 0);
		$category	= JRequest::getVar( 'id', 0, '', 'int' );

		// First thing we need to do is build the access section of the clause
		$where = ' WHERE a.access <= '.$aid;
		$where .= ' AND s.access <= '.$aid;
		$where .= ' AND cc.access <= '.$aid;
		$where .= ' AND s.published = 1';
		$where .= ' AND cc.published = 1';

		$where .= ' AND a.state = \'-1\'';
		$year	= JRequest::getInt( 'year' );
		if ($year) {
			$where .= ' AND YEAR( a.created ) = \''.$year.'\'';
		}
		$month	= JRequest::getInt( 'month' );
		if ($month) {
			$where .= ' AND MONTH( a.created ) = \''.$month.'\'';
		}
		if ($category != 0){
			$where .= ' AND a.catid = '.$category;
		}

		/*
		 * If we have a filter... lets tack the AND clause
		 * for the filter onto the WHERE clause of the archive query.
		 */
		$filter = JRequest::getString('filter', '', 'post');
		if ($filter) {
			// clean filter variable
			$filter = JString::strtolower($filter);
			$filter	= $db->Quote( '%'.$db->getEscaped( $filter, true ).'%', false );

			// Get the page/component configuration
			$params = &$mainframe->getParams();
			switch ($params->get('filter_type', 'title'))
			{
				case 'title' :
				     default :
					$where .= ' AND LOWER( a.title ) LIKE '.$filter;
					break;

				case 'author' :
					$where .= ' AND ( ( LOWER( u.name ) LIKE '.$filter.' ) OR ( LOWER( a.created_by_alias ) LIKE '.$filter.' ) )';
					break;

				case 'hits' :
					$where .= ' AND a.hits LIKE '.$filter;
					break;
			}
		}
		return $where;
	}
Now you will see a new view called "Category Archived Articles List Layout" under Articles->Archive when creating a new menu item. Once you choose this view you can select a category.

Regards.
B.

atgdesign
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Oct 29, 2007 6:33 pm

Re: [SOLVED] Request for Content component's "Archive" Function

Post by atgdesign » Mon Feb 22, 2010 9:30 pm

Love the idea, got this message when making and trying a new menu item


Parse error: syntax error, unexpected $end, expecting T_FUNCTION in /components/com_content/models/archive.php on line 215

Joomla 1.5.14, thought on what that might be?

Thanks

baysaa
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Mar 03, 2008 2:44 pm

Re: [SOLVED] Request for Content component's "Archive" Function

Post by baysaa » Tue Feb 23, 2010 9:15 am

This error is usually a result of a missing curly bracket } which means u might have deleted the class closing bracket by mistake when pasting the function in. Please check to make sure that there's 1 curly closing bracket } AFTER the last closing bracket of the function you pasted on models/archive.php

Here's the full models/archive.php :

Code: Select all

<?php
/**
 * @version		$Id: archive.php 11681 2009-03-08 20:52:50Z willebil $
 * @package		Joomla
 * @subpackage	Content
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Content Component Archive Model
 *
 * @package 	Joomla
 * @subpackage	Content
 * @since		1.5
 */
class ContentModelArchive extends JModel
{
	/**
	 * Article list array
	 *
	 * @var array
	 */
	var $_data = array();

	/**
	 * Article total
	 *
	 * @var integer
	 */
	var $_total = array();

	/**
	 * Method to get the archived article list
	 *
	 * @access public
	 * @return array
	 */
	function getData()
	{
		global $mainframe;
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			// Get the page/component configuration
			$params = &$mainframe->getParams();

			// Get the pagination request variables
			$limit		= JRequest::getVar('limit', $params->get('display_num', 20), '', 'int');
			$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');

			$query = $this->_buildQuery();

			$this->_data = $this->_getList($query, $limitstart, $limit);
		}

		return $this->_data;
	}

	/**
	 * Method to get the total number of content items for the frontpage
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	// JModel override to add alternating value for $odd
	function &_getList( $query, $limitstart=0, $limit=0 )
	{
		$result =& parent::_getList($query, $limitstart, $limit);

		$odd = 1;
		foreach ($result as $k => $row) {
			$result[$k]->odd = $odd;
			$odd = 1 - $odd;
		}

		return $result;
	}

	function _buildQuery()
	{
		global $mainframe;
		// Get the page/component configuration
		$params = &$mainframe->getParams();

		// If voting is turned on, get voting data as well for the content items
		$voting	= ContentHelperQuery::buildVotingQuery($params);

		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();

		$query = 'SELECT a.id, a.title, a.alias, a.title_alias, a.introtext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by,'.
			' a.checked_out, a.checked_out_time, a.publish_up, a.publish_down, a.attribs, a.hits, a.images, a.urls, a.ordering, a.metakey, a.metadesc, a.access, cc.title AS category, s.title AS section,' .
			' 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,'.
			' CHAR_LENGTH( a.`fulltext` ) AS readmore, u.name AS author, u.usertype, g.name AS groups'.$voting['select'] .
			' FROM #__content AS a' .
			' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
			' LEFT JOIN #__sections AS s ON s.id = a.sectionid' .
			' LEFT JOIN #__users AS u ON u.id = a.created_by' .
			' LEFT JOIN #__groups AS g ON a.access = g.id'.
			$voting['join'].
			$where.
			$orderby;

		return $query;
	}

	function _buildContentOrderBy()
	{
		$filter_order		= JRequest::getCmd('filter_order');
		$filter_order_Dir	= JRequest::getWord('filter_order_Dir');

		$orderby = ' ORDER BY ';
		if ($filter_order && $filter_order_Dir) {
			$orderby .= $filter_order.' '.$filter_order_Dir.', ';
		}

		// Get the page/component configuration
		$params = $this->getState('parameters.menu');
		if (!is_object($params)) {
			$params = &JComponentHelper::getParams('com_content');
		}

		// Special ordering for archive articles
		$orderby_sec	= $params->def('orderby', 'rdate');
		$primary		= ContentHelperQuery::orderbySecondary($orderby_sec);
		$orderby		.= $primary;

		return $orderby;
	}

	function _buildContentWhere()
	{
		global $mainframe;

		// Initialize some variables
		$user	=& JFactory::getUser();
		$db		=& JFactory::getDBO();
		$aid	= (int) $user->get('aid', 0);
		$category	= JRequest::getVar( 'id', 0, '', 'int' );

		// First thing we need to do is build the access section of the clause
		$where = ' WHERE a.access <= '.$aid;
		$where .= ' AND s.access <= '.$aid;
		$where .= ' AND cc.access <= '.$aid;
		$where .= ' AND s.published = 1';
		$where .= ' AND cc.published = 1';

		$where .= ' AND a.state = \'-1\'';
		$year	= JRequest::getInt( 'year' );
		if ($year) {
			$where .= ' AND YEAR( a.created ) = \''.$year.'\'';
		}
		$month	= JRequest::getInt( 'month' );
		if ($month) {
			$where .= ' AND MONTH( a.created ) = \''.$month.'\'';
		}
		if ($category != 0){
			$where .= ' AND a.catid = '.$category;
		}

		/*
		 * If we have a filter... lets tack the AND clause
		 * for the filter onto the WHERE clause of the archive query.
		 */
		$filter = JRequest::getString('filter', '', 'post');
		if ($filter) {
			// clean filter variable
			$filter = JString::strtolower($filter);
			$filter	= $db->Quote( '%'.$db->getEscaped( $filter, true ).'%', false );

			// Get the page/component configuration
			$params = &$mainframe->getParams();
			switch ($params->get('filter_type', 'title'))
			{
				case 'title' :
				     default :
					$where .= ' AND LOWER( a.title ) LIKE '.$filter;
					break;

				case 'author' :
					$where .= ' AND ( ( LOWER( u.name ) LIKE '.$filter.' ) OR ( LOWER( a.created_by_alias ) LIKE '.$filter.' ) )';
					break;

				case 'hits' :
					$where .= ' AND a.hits LIKE '.$filter;
					break;
			}
		}
		return $where;
	}
}

atgdesign
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Oct 29, 2007 6:33 pm

Re: [SOLVED] Request for Content component's "Archive" Function

Post by atgdesign » Tue Feb 23, 2010 1:01 pm

Cool, I'll give it a try

atgdesign
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Oct 29, 2007 6:33 pm

Re: [SOLVED] Request for Content component's "Archive" Function

Post by atgdesign » Tue Feb 23, 2010 3:26 pm

Works, Thanks. This should really be a part of the core.

Wonderful, just what was holding up a whole project.

sonfire11
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sun Jun 27, 2010 3:40 pm

Re: [SOLVED] Request for Content component's "Archive" Funct

Post by sonfire11 » Wed Jun 30, 2010 9:24 am

atgdesign wrote:Works, Thanks. This should really be a part of the core.
Wonderful, just what was holding up a whole project.
I hear you there. At this point, the entire website is being held up because of not being able to view archives by section (That and when J! 1.6 finally comes out). Either way, is there a fix like this for sections as a whole? I.E. I have events, news, blogs and so on. Can we do this for just the sections as well?


Locked

Return to “Core Hacks and Patches”