K2, do not show empty categories

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
Ra_Sp
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Thu Oct 02, 2014 9:56 pm

K2, do not show empty categories

Post by Ra_Sp » Thu Oct 02, 2014 10:19 pm

Hello guys!
I have a problem with one of my modules for K2.
There is the code of the module which shows categories.
Actually it is modified k2tools (search box part):

Code: Select all

<?php
/**
 * @version		$Id: search.php 1492 2012-02-22 17:40:09Z [email protected] $
 * @package		K2
 * @author		JoomlaWorks http://www.joomlaworks.net
 * @copyright	Copyright (c) 2006 - 2012 JoomlaWorks Ltd. All rights reserved.
 * @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */

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

/*
Important note:
If you wish to use the live search option, it's important that you maintain the same class names for wrapping elements, e.g. the wrapping div and form.
*/

if($text=='categorieslayout'){

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

	switch ($params->get('categoriesListOrdering')) {

		case 'alpha':
			$orderby = 'name';
			break;

		case 'ralpha':
			$orderby = 'name DESC';
			break;

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

		case 'reversedefault':
			$orderby = 'id DESC';
			break;

		default:
			$orderby = 'id ASC';
			break;
	}
	$catFilter=($categoryFilter)? "id IN(".$categoryFilter.") AND" : '';
	$query = "SELECT * FROM `#__k2_categories` WHERE ".$catFilter." published=1 AND trash=0 ";

	/*if(K2_JVERSION=='16'){
		$query .= " AND access IN(".implode(',', $user->authorisedLevels()).") ";
		if($mainframe->getLanguageFilter()) {
			$languageTag = JFactory::getLanguage()->getTag();
			$query .= " AND language IN (".$db->Quote($languageTag).", ".$db->Quote('*').") ";
		}
		
	}
	else {
		$query .= " AND access <= {$aid}";
	}*/

	$query .= " ORDER BY {$orderby}";

	$db->setQuery($query);
	$rows = $db->loadObjectList();
//var_Dump( $query );
	?>
	<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2SearchBlock<?php if($params->get('moduleclass_sfx')) echo ' '.$params->get('moduleclass_sfx'); if($params->get('liveSearch')) echo ' k2LiveSearchBlock'; ?>">
	<?php
	echo '<table class="k2ModuleBoxCatsLayout"><tbody>';
	foreach($rows as $k=>$row){
		if($k%3==0){
			echo "<tr>";
		}
		echo "<td>";
			$img = ($row->image)? '<img class="catImg" src="'.JURI::root().'media/k2/categories/'.$row->image.'" />' : '';

			echo '<a href="'.urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($row->id.':'.urlencode($row->alias)))).'">'.$img.'<div class="catTitle">'.$row->name.'</div></a>';
		echo "</td>";
		if(($k+1)%3==0) echo "</tr>";
	}
	if(($k+1)%3!=0) echo str_repeat('<td class="noitem"></td>', (3-(($k+1)%3))). "</tr>";
	echo '</tbody></table>';
	echo '</div>';

}else{
?>

<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2SearchBlock<?php if($params->get('moduleclass_sfx')) echo ' '.$params->get('moduleclass_sfx'); if($params->get('liveSearch')) echo ' k2LiveSearchBlock'; ?>">
	<form action="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=search'); ?>" method="get" autocomplete="off" class="k2SearchBlockForm">

		<input type="text" value="<?php echo $text; ?>" name="searchword" maxlength="<?php echo $maxlength; ?>" size="<?php echo $width; ?>" alt="<?php echo $button_text; ?>" class="inputbox" onblur="if(this.value=='') this.value='<?php echo $text; ?>';" onfocus="if(this.value=='<?php echo $text; ?>') this.value='';" />

		<?php if($button): ?>
		<?php if($imagebutton): ?>
		<input type="image" value="<?php echo $button_text; ?>" class="button" onclick="this.form.searchword.focus();" src="<?php echo JURI::base(true); ?>/components/com_k2/images/fugue/search.png" />
		<?php else: ?>
		<input type="submit" value="<?php echo $button_text; ?>" class="button" onclick="this.form.searchword.focus();" />
		<?php endif; ?>
		<?php endif; ?>

		<input type="hidden" name="categories" value="<?php echo $categoryFilter; ?>" />
		<?php if(!$app->getCfg('sef')): ?>
		<input type="hidden" name="option" value="com_k2" />
		<input type="hidden" name="view" value="itemlist" />
		<input type="hidden" name="task" value="search" />
		<?php endif; ?>
		<?php if($params->get('liveSearch')): ?>
		<input type="hidden" name="format" value="html" />
		<input type="hidden" name="t" value="" />
		<input type="hidden" name="tpl" value="search" />
		<?php endif; ?>
	</form>

	<?php if($params->get('liveSearch')): ?>
	<div class="k2LiveSearchResults"></div>
	<?php endif; ?>
</div>
<?php
}
The layout starts from the stroke:

Code: Select all

foreach($rows as $k=>$row){
Please help me to add correct assertion for checking is the category empty or not and if it is empty do not show it. Thank you so much for any help!
PS: there is the search.php file
http://www42.zippyshare.com/v/66814139/file.html

Ra_Sp
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Thu Oct 02, 2014 9:56 pm

Re: K2, do not show empty categories

Post by Ra_Sp » Fri Oct 03, 2014 2:59 pm

I fixed the problem. There is the final version of the code. It is template override for k2tools search box. Which shows k2 categories list as a table with images from each category and categories titles. Categories without articles are not displaying.

Code: Select all

<?php
/**
 * @version		$Id: search.php 1492 2012-02-22 17:40:09Z [email protected] $
 * @package		K2
 * @author		JoomlaWorks http://www.joomlaworks.net
 * @copyright	Copyright (c) 2006 - 2012 JoomlaWorks Ltd. All rights reserved.
 * @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */

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

/*
Important note:
If you wish to use the live search option, it's important that you maintain the same class names for wrapping elements, e.g. the wrapping div and form.
*/

if($text=='categorieslayout'){

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

	switch ($params->get('categoriesListOrdering')) {

		case 'alpha':
			$orderby = 'name';
			break;

		case 'ralpha':
			$orderby = 'name DESC';
			break;

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

		case 'reversedefault':
			$orderby = 'id DESC';
			break;

		default:
			$orderby = 'id ASC';
			break;
	}
	$catFilter=($categoryFilter)? "id IN(".$categoryFilter.") AND" : '';
	$query = "SELECT * FROM `#__k2_categories` WHERE ".$catFilter." published=1 AND trash=0 ";

	/*if(K2_JVERSION=='16'){
		$query .= " AND access IN(".implode(',', $user->authorisedLevels()).") ";
		if($mainframe->getLanguageFilter()) {
			$languageTag = JFactory::getLanguage()->getTag();
			$query .= " AND language IN (".$db->Quote($languageTag).", ".$db->Quote('*').") ";
		}
		
	}
	else {
		$query .= " AND access <= {$aid}";
	}*/

	$query .= " ORDER BY {$orderby}";

	$db->setQuery($query);
	$rows = $db->loadObjectList();
//var_Dump( $query );
	?>
	<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2SearchBlock<?php if($params->get('moduleclass_sfx')) echo ' '.$params->get('moduleclass_sfx'); if($params->get('liveSearch')) echo ' k2LiveSearchBlock'; ?>">
	<?php
	echo '<table class="k2ModuleBoxCatsLayout"><tbody>';
	$i = 0;
foreach($rows as $row) {  
    if (modK2ToolsHelper::countCategoryItems($row->id) > 0) :
        if ($i%3 == 0) echo "<tr>";
        echo "<td>";
            $img = ($row->image)? '<img class="catImg" src="'.JURI::root().'media/k2/categories/'.$row->image.'" />' : '';
 
            echo '<a href="'.urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($row->id.':'.urlencode($row->alias)))).'">'.$img.'<div class="catTitle">'.$row->name.'</div></a>';
        echo "</td>";
        if(($i+1)%3==0) echo "</tr>";
        $i++;
    endif;
}
	if(($k+1)%3!=0) echo str_repeat('<td class="noitem"></td>', (3-(($k+1)%3))). "</tr>";
	echo '</tbody></table>';
	echo '</div>';
}else{
?>

<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2SearchBlock<?php if($params->get('moduleclass_sfx')) echo ' '.$params->get('moduleclass_sfx'); if($params->get('liveSearch')) echo ' k2LiveSearchBlock'; ?>">
	<form action="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=search'); ?>" method="get" autocomplete="off" class="k2SearchBlockForm">

		<input type="text" value="<?php echo $text; ?>" name="searchword" maxlength="<?php echo $maxlength; ?>" size="<?php echo $width; ?>" alt="<?php echo $button_text; ?>" class="inputbox" onblur="if(this.value=='') this.value='<?php echo $text; ?>';" onfocus="if(this.value=='<?php echo $text; ?>') this.value='';" />

		<?php if($button): ?>
		<?php if($imagebutton): ?>
		<input type="image" value="<?php echo $button_text; ?>" class="button" onclick="this.form.searchword.focus();" src="<?php echo JURI::base(true); ?>/components/com_k2/images/fugue/search.png" />
		<?php else: ?>
		<input type="submit" value="<?php echo $button_text; ?>" class="button" onclick="this.form.searchword.focus();" />
		<?php endif; ?>
		<?php endif; ?>

		<input type="hidden" name="categories" value="<?php echo $categoryFilter; ?>" />
		<?php if(!$app->getCfg('sef')): ?>
		<input type="hidden" name="option" value="com_k2" />
		<input type="hidden" name="view" value="itemlist" />
		<input type="hidden" name="task" value="search" />
		<?php endif; ?>
		<?php if($params->get('liveSearch')): ?>
		<input type="hidden" name="format" value="html" />
		<input type="hidden" name="t" value="" />
		<input type="hidden" name="tpl" value="search" />
		<?php endif; ?>
	</form>

	<?php if($params->get('liveSearch')): ?>
	<div class="k2LiveSearchResults"></div>
	<?php endif; ?>
</div>
<?php
}
PS: A special thanks:
  • OlegM for helping with hiding empty categories feature;
  • Sherza for helping with k2tools searchbox adaptation.


Locked

Return to “Joomla! 2.5 Coding”