Link to home
Start Free TrialLog in
Avatar of Eternal_Student
Eternal_StudentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Archive question

Hi,

I am using Joomla and K2 to build my site and there is a CATEGORY which has multiple ITEMS which all have a CREATED BY date/year.

The code below is currently outputting this:

June 2011 (4 items)
May 2011 (2 items)
April 2011 (8 items)
... etc

So it is just spitting them out, as links, in monthly order. What I want is more along the lines of:

> 2011
---> June (4 items)
---> May (2 items)
---> April (8 items)
>2010
>2009


So the months are grouped under the year and the years are expand collapse links. I have the javascript to do this I just need to modify the code below so the hooks are there.

This is the code that is currently in place:

 
function getArchive(&$params) {

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

    $jnow = &JFactory::getDate();
    $now = $jnow->toMySQL();
    $nullDate = $db->getNullDate();

    $query = "SELECT DISTINCT MONTH(created) as m, YEAR(created) as y FROM #__k2_items  WHERE published=1 AND ( publish_up = ".$db->Quote($nullDate)." OR publish_up <= ".$db->Quote($now)." ) AND ( publish_down = ".$db->Quote($nullDate)." OR publish_down >= ".$db->Quote($now)." ) AND trash=0 AND access<={$aid} ";

    $catid = $params->get('archiveCategory', 0);
    if ($catid > 0)
      $query .= " AND catid=".(int)$catid;

    $query .= " ORDER BY created DESC";

    $db->setQuery($query, 0, 1000);
    $rows = $db->loadObjectList();
    $months = array(JText::_('JANUARY'), JText::_('FEBRUARY'), JText::_('MARCH'), JText::_('APRIL'), JText::_('MAY'), JText::_('JUNE'), JText::_('JULY'), JText::_('AUGUST'), JText::_('SEPTEMBER'), JText::_('OCTOBER'), JText::_('NOVEMBER'), JText::_('DECEMBER'), );
    if (count($rows)) {

      foreach ($rows as $row) {
        if ($params->get('archiveItemsCounter')) {
          $row->numOfItems = modK2ToolsHelper::countArchiveItems($row->m, $row->y, $catid);
        } else {
          $row->numOfItems = '';
        }
        $row->name = $months[($row->m) - 1];
        $archives[] = $row;
      }

      return $archives;

    }
  }

Open in new window

Avatar of Dmitrii
Dmitrii
Flag of Russian Federation image

This code only returns an array. The output is somewhere in another place. And the formatting should also be done there.
Avatar of Eternal_Student

ASKER

You are completely right ... I posted this quite late assuming that he outputted code was in there but it is actually in a different file and goes like this:

 
<?php 
/**
 * @version		$Id: archive.php 478 2010-06-16 16:11:42Z joomlaworks $
 * @package		K2
 * @author		JoomlaWorks http://www.joomlaworks.gr
 * @copyright	Copyright (c) 2006 - 2010 JoomlaWorks, a business unit of Nuevvo Webware Ltd. All rights reserved.
 * @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

?>
<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2ArchivesBlock <?php echo $params->get('moduleclass_sfx'); ?>">
  <ul>
    <?php foreach ($months as $month): ?>
    <li>
      <?php if ($params->get('archiveCategory', 0) > 0): ?>
      <a href="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=date&month='.$month->m.'&year='.$month->y.'&catid='.$params->get('archiveCategory')); ?>">
        <?php echo $month->name.' '.$month->y; ?>
        <?php if ($params->get('archiveItemsCounter')) echo '('.$month->numOfItems.')'; ?>
      </a>
      <?php else : ?>
      <a href="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=date&month='.$month->m.'&year='.$month->y); ?>">
        <?php echo $month->name.' '.$month->y; ?>
        <?php if ($params->get('archiveItemsCounter')) echo '('.$month->numOfItems.')'; ?>
      </a>
      <?php endif; ?>
    </li>
    <?php endforeach; ?>
  </ul>
</div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dmitrii
Dmitrii
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, that works perfectly, thanks.

I just wondered about a further modification to the code that would also output the items, so something like this:

> 2011
---> June (4 items)
------> Item name/link #1
------> Item name/link #2
------> Item name/link #3
------> Item name/link #4
---> May (2 items)
---> April (8 items)
Actually this file is a template. It gets the data and formats this data for output.
As I can see from the first piece of code there is no information about Items/Links.

You can modify this code:
foreach ($rows as $row) {
        if ($params->get('archiveItemsCounter')) {
          $row->numOfItems = modK2ToolsHelper::countArchiveItems($row->m, $row->y, $catid);
        }

Open in new window

— add the query to database to select items/links for needed month/year/category.
The same database table has the field 'title' but I cannot see a 'link' field.

In previous places in the php I have used

<?php echo $this->item->link; ?>
<?php echo $this->item->title; ?>

to output links and titles.