<?php
/**
* FileName: mod_remositoryCat.php
* Date: April 2009
* License: GNU General Public License
* Script Version #: 3.50
* ReMOSitory Version #: 3.50 or above
* Author: Martin Brampton - martin@remository.com (http://remository.com)
* Copyright: Martin Brampton 2006-9
**/

class mod_remositoryCat extends mod_remositoryBase {

	public function showFileList ($module, &$content, $area, $params) {
		$interface = remositoryInterface::getInstance();
		$database = $interface->getDB();

		/*********************Configuration*********************/
		// Set to '1' to Show the Description, set to 0 to not show it
		$showsmall = $this->remos_get_module_parm($params,'showsmall',0);
		// Max number of entries to show
		$max = $this->remos_get_module_parm($params,'max',5 );
		// Max number of description characters
		$maxchars = $this->remos_get_module_parm($params,'maxchars',100);
		// Size for icons
		$iconsize = $this->remos_get_module_parm($params,'iconsize',16);
		// Select Sorting methode : sort by id, name or files
		$sortcat = $this->remos_get_module_parm($params,'sortcat','filecount');
		// Select Sorting methode : ascending or descending
		$sortorder = $this->remos_get_module_parm($params, 'sortorder','DESC');
		// The container within which folders are to be displayed
		$container = $this->remos_get_module_parm($params, 'container', 0);

		$max = max($max,1);
		$maxchars = max($maxchars,20);
		if ($sortcat != 'filecount' AND $sortcat != 'id' AND $sortcat != 'name') $sortcat = 'filecount';
		if ($sortorder != 'ASC' AND $sortorder != 'DESC') $sortorder = 'DESC';
		/*******************************************************/

		$repository = remositoryRepository::getInstance();
		$tabclass_arr=explode(",",$repository->tabclass);

		// Find out $Itemid and make base URI
		$base_url = 'index.php?option=com_remository&Itemid='.$this->remos_getItemID('com_remository').'&func=select&id=';

		$sql = "SELECT id, name, description, filecount, icon from #__downloads_containers WHERE parentid=$container";
		$user = $interface->getUser();
		if (!$repository->See_Files_no_download AND !$user->isAdmin()) {
			$authoriser = aliroAuthoriser::getInstance();
			$refuseSQL = $authoriser->getRefusedListSQL ('aUser', $user->id, 'remosFolder', 'download,edit', 'id');
			if ($refuseSQL) $sql .= ' AND '.$refuseSQL;
		}
		$sql .= " ORDER BY $sortcat $sortorder LIMIT $max";
		$database->setQuery( $sql );
		$categories = $database->loadObjectList();
		if (!$categories) $categories = array();

		// $this->remos_module_CSS ();
		$content = <<<START_CATS
		
		<table cellspacing="2" cellpadding="1" border="0" width="100%">

START_CATS;

		$tabcnt = 0;
		foreach ($categories as $category) {
			if ($showsmall) {
				$sdesc = '';
				if ($category->description<>'') {
					$slen = 0;
					$stripped = strip_tags($category->description);
					$slen = strlen($stripped);
					if ($slen>=$maxchars) $sdesc="<br/>".substr($stripped,0,$maxchars).'...';
					else $sdesc="<br/>".$stripped;
				} 
				elseif ($category->name<>'') {
					$slen = 0;
					$slen = strlen($category->name);
					if ($slen>=$maxchars) $sdesc="<br/>".substr($category->name,0,$maxchars).'...';
					else $sdesc="<br/>".$category->name;
				}
			}
			else $sdesc = '';
			$curicon = remositoryRepository::RemositoryImageURL('folder_icons/'.($category->icon ? $category->icon : 'generic.png'), $iconsize, $iconsize);
			$filesnum = $category->filecount ? $category->filecount : 0;
			$component_url = $interface->sefRelToAbs($base_url.$category->id);
			$content .= <<<ADD_CAT
			
			<tr class="{$tabclass_arr[$tabcnt]}"><td width="80%"><a href="$component_url">$curicon $category->name</a>$sdesc</td><td width="15%" class="number">($filesnum)</td></tr>
			
ADD_CAT;

			$tabcnt = 1 - $tabcnt;
		}
		$content .= "\n</table>\n";	}

}