<?php
defined('_JEXEC') or die('Restricted access'); 

if ( $this->params->def( 'show_page_title', 1 ) ) {
	echo '<h2 class="componentheading'.$this->params->get( 'pageclass_sfx' ).'">'. $this->params->get('page_title'). '</h2>';
}

echo '<div id="phoca-dl-category-box">';


if (!empty($this->category[0])) {
	echo '<div class="pd-category">';
	if ($this->tmpl['display_up_icon'] == 1) {
		
		if (isset($this->category[0]->parentid)) {
			if ($this->category[0]->parentid == 0) {
				
				$linkUp = JRoute::_(PhocaDownloadHelperRoute::getCategoriesRoute());
				$linkUpText = JText::_('COM_PHOCADOWNLOAD_CATEGORIES');
			} else if ($this->category[0]->parentid > 0) {
				$linkUp = JRoute::_(PhocaDownloadHelperRoute::getCategoryRoute($this->category[0]->parentid, $this->category[0]->parentalias));
				$linkUpText = $this->category[0]->parenttitle;
			} else {
				$linkUp 	= '#';
				$linkUpText = ''; 
			}
			echo '<div class="pdtop">'
				.'<a title="'.$linkUpText.'" href="'. $linkUp.'" >'
				.JHTML::_('image', 'components/com_phocadownload/assets/images/up.png', JText::_('COM_PHOCADOWNLOAD_UP'))
				.'</a></div>';
		}
	}
} else {
	echo '<div class="pd-category"><div class="pdtop"></div>';
}


if (!empty($this->category[0])) {
	
	// USER RIGHT - Access of categories (if file is included in some not accessed category) - - - - -
	// ACCESS is handled in SQL query, ACCESS USER ID is handled here (specific users)
	$rightDisplay	= 0;
	if (!empty($this->category[0])) {
		$rightDisplay = PhocaDownloadHelper::getUserRight('accessuserid', $this->category[0]->cataccessuserid, $this->category[0]->cataccess, $this->tmpl['user']->authorisedLevels(), $this->tmpl['user']->get('id', 0), 0);
	}
	// - - - - - - - - - - - - - - - - - - - - - -
	
	if ($rightDisplay == 1) {
		echo '<h3>'.$this->category[0]->title. '</h3>';

		// Description
		echo '<div class="contentpane'.$this->params->get( 'pageclass_sfx' ).'">';
		if ( (isset($this->category[0]->description) && $this->category[0]->description != '' && $this->category[0]->description != '<p>&#160;</p>')) {
			echo '<div class="contentdescription'.$this->params->get( 'pageclass_sfx' ).'">';
			echo $this->category[0]->description
				.'</div><p>&nbsp;</p>';
		}
		echo '</div>';

		echo '<div class="pd-c-download-box">';

		
	

		// - - - - -
		// CATEGORIES
		// - - - - -
		
		if (!empty($this->subcategories)) {	
			foreach ($this->subcategories as $valueSubCat) {
				
				echo '<div class="pd-subcategory">';
				echo '<a href="'. JRoute::_(PhocaDownloadHelperRoute::getCategoryRoute($valueSubCat->id, $valueSubCat->alias))
					 .'">'. $valueSubCat->title.'</a>';
				echo ' <small>('.$valueSubCat->numdoc.')</small></div>' . "\n";
				$subcategory = 1;
			}
			
			echo '<div class="phoca-dl-hr" style="clear:both">&nbsp;</div>';
		}

		
		// - - - - -
		// FILES
		// - - - - -
		
		if (!empty($this->files)) {	
			foreach ($this->files as $valueDoc) {
				if ($valueDoc->textonly == 1) {
					
					echo '<div class="pd-c-textonly"><div class="description">';
					echo $valueDoc->description;
					echo '</div></div>' . "\n";
				
				} else {

					// IMAGE FILENAME
					$imageFileName = '';
					$imageFileNameThumbnail = '';
					if ($valueDoc->image_filename !='') {
						$thumbnail = false;
						$thumbnail = preg_match("/phocathumbnail/i", $valueDoc->image_filename);
						if ($thumbnail) {
							$imageFileNameThumbnail = '<div style="margin-top:5px;margin-bottom:5px" ><img src="'.$this->cssimagepath.$valueDoc->image_filename.'" alt="" /></div>';
							$imageFileName 			= '';
						} else {
							$imageFileNameThumbnail = '';
							$imageFileName 			= 'style="background: url(\''.$this->cssimagepath.$valueDoc->image_filename.'\') 0 center no-repeat;"';
						}
					}
					
					// FILESIZE
					if ($valueDoc->filename !='') {
						$absFile = str_replace('/', DS, JPath::clean($this->absfilepath . $valueDoc->filename));
						if (JFile::exists($absFile))
						{
							$fileSize = PhocaDownloadHelper::getFileSizeReadable(filesize($absFile));
						} else {
							$fileSize = '';
						}
					}
					
					
					// DETAILS
					$details = '';
					if ($valueDoc->title != '') {
						$details .= '<h4>'.$valueDoc->title.'</h4>';
					}
					if ($valueDoc->filename != '') {
						$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_FILENAME').': '.PhocaDownloadHelper::getTitleFromFilenameWithExt( $valueDoc->filename ).'</div>';
					}
					if ($fileSize != '') {
						$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_FILESIZE').': '.$fileSize.'</div>';
					}
					
					if ($valueDoc->version != '') {
						$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_VERSION').': '.$valueDoc->version.'</div>';
					}
					if ($valueDoc->license != '') {
						if ($valueDoc->license_url != '') {
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_LICENSE').': '.'<a href="'.$valueDoc->license_url.'" target="_blank">'.$valueDoc->license.'</a></div>';
						} else {
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_LICENSE').': '.$valueDoc->license.'</div>';
						}
					}
					if ($valueDoc->author != '') {
						if ($valueDoc->author_url != '') {
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_AUTHOR').': '.'<a href="'.$valueDoc->author_url.'" target="_blank">'.$valueDoc->author.'</a></div>';
						} else {
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_AUTHOR').': '.$valueDoc->author.'</div>';
						}
					}

					if ($valueDoc->author_email != '') {
						//$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_EMAIL').': '. JHTML::_( 'email.cloak', $valueDoc->author_email).'</div>';
						$protectMail = str_replace('@', '['.JText::_('COM_PHOCADOWNLOAD_AT').']', $valueDoc->author_email);
						$protectMail = str_replace('.', '['.JText::_('COM_PHOCADOWNLOAD_DOT').']', $protectMail);
						$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_EMAIL').': '. $protectMail.'</div>';
					}
					
					// FILEDATE
					$fileDate = '';
					if ((int)$this->tmpl['display_date_type'] > 0) {
						if ($valueDoc->filename !='') {
							$fileDate = PhocaDownloadHelper::getFileTime($valueDoc->filename, $this->tmpl['display_date_type']);
						}
					} else {
						$fileDate = JHTML::Date($valueDoc->date, JText::_('DATE_FORMAT_LC3'));
					}
					
					if ($fileDate != '') {
						$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_DATE').': '.$fileDate.'</div>';
					}
					
					// DESCRIPTION
					$classMiddle 				= 'class="pdfile"';
					$descriptionOutputTop 		= '';
					$descriptionOutputBottom 	= '';
					$descriptionOutputOverlib	= '';
					
					if ($valueDoc->description != '' && $valueDoc->description != '<p>&#160;</p>' && $valueDoc->description != '<p>&nbsp;</p>' && $valueDoc->description != '<p></p>' && $valueDoc->description != '<br />') {
						
						if ($this->tmpl['display_description'] == 1 || $this->tmpl['display_description'] == 4) {
							$descriptionOutputTop 		= '<div class="pd-c-desc">'.$valueDoc->description.'</div>';
							$classMiddle 				= 'class="pdfile"';
						}
						if ($this->tmpl['display_description'] == 2 || $this->tmpl['display_description'] == 5) {	
							$descriptionOutputBottom 	= '<div class="pd-c-desc">'.$valueDoc->description.'</div>';
							$classMiddle 				= '';
						}
						
						if ($this->tmpl['display_description'] == 3 || $this->tmpl['display_description'] == 4 ) {	
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_DESCRIPTION').':<br />'.$valueDoc->description.'</div>';
							$classMiddle 				= 'class="pdfile"';
						}
						
						if ( $this->tmpl['display_description'] == 5 ) {	
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_DESCRIPTION').':<br />'.$valueDoc->description.'</div>';
							$classMiddle 				= '';
						}
						
						if ( $this->tmpl['display_description'] == 6 ) {	
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_DESCRIPTION').':<br />'.$valueDoc->description.'</div>';
							$descriptionOutputTop 	= '<div class="pd-c-pdfile">'.$details .'</div>';
							$classMiddle 				= 'class="pdfile"';
						}
						
						if ( $this->tmpl['display_description'] == 7 ) {	
							$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_DESCRIPTION').':<br />'.$valueDoc->description.'</div>';
							$descriptionOutputBottom 	= '<div class="pd-c-pdfile">'.$details .'</div>';
							$classMiddle 				= '';
						}
						
					} else {
						
					}
					
					
					
					if ($this->tmpl['display_downloads'] == 1) {
						$details .= '<div>'.JText::_('COM_PHOCADOWNLOAD_DOWNLOADS').': '.$valueDoc->hits.' x</div>';
					}			
					
					// Title or name
					if ($this->tmpl['filename_or_name'] == 'title') {
						$displayNameHead = '';
						$displayName = $valueDoc->title;
					} else if ($this->tmpl['filename_or_name'] == 'filename'){
						$displayNameHead = '';
						$displayName = PhocaDownloadHelper::getTitleFromFilenameWithExt( $valueDoc->filename );
					} else if ($this->tmpl['filename_or_name'] == 'filenametitle'){
						$displayNameHead = '<div><strong>'.$valueDoc->title.'</strong></div>';
						$displayName = PhocaDownloadHelper::getTitleFromFilenameWithExt( $valueDoc->filename );
					}
					
					// Overlib
					$details		= PhocaDownloadHelper::strTrimAll($details);
					$overlib 	= "onmouseover=\"return overlib('".htmlspecialchars( addslashes('<div style="text-align:left;padding:5px">'.$details.'</div>') )."', CAPTION, '".JText::_('COM_PHOCADOWNLOAD_DETAILS')."', BELOW, RIGHT, CSSCLASS, TEXTFONTCLASS, 'fontPhocaPDClass', FGCLASS, 'fgPhocaPDClass', BGCLASS, 'bgPhocaPDClass', CAPTIONFONTCLASS,'capfontPhocaPDClass', CLOSEFONTCLASS, 'capfontclosePhocaPDClass', STICKY, MOUSEOFF, CLOSETEXT, '".JText::_('COM_PHOCADOWNLOAD_CLOSE')."');\"";
					$overlib .= " onmouseout=\"return nd();\"";
				
					// LINK
					// Confirm license - go to "file" view
					// or Display File View
					
					if ((int)$valueDoc->confirm_license > 0 || $this->tmpl['display_file_view'] == 1) {
							//$linkDownloadB = '<a href="'. JRoute::_('index.php?option=com_phocadownload&view=file&id='.$valueDoc->id.':'.$valueDoc->alias. $this->tmpl['limitstarturl'].'&Itemid='. JRequest::getVar('Itemid', 1, 'get', 'int')).'">';	// we need pagination to go back
						
							$linkDownloadB = '<a href="'. JRoute::_(PhocaDownloadHelperRoute::getFileRoute($valueDoc->id, $valueDoc->catid,$valueDoc->alias, $valueDoc->categoryalias, $valueDoc->sectionid). $this->tmpl['limitstarturl']).'">';	// we need pagination to go back			
							$linkDownloadE ='</a>';
					} else {
						// EXTERNAL LINK
						if ($valueDoc->link_external != '') {
							
							$linkDownloadB = '<a href="'.$valueDoc->link_external.'" target="'.$this->tmpl['download_external_link'].'" >';
							$linkDownloadE ='</a>';
							
						} else {
						
							//$linkDownloadB = '<a href="'. JRoute::_('index.php?option=com_phocadownload&view=category&id='.$this->category[0]->id.':'.$this->category[0]->alias.'&download='.$valueDoc->id.':'.$valueDoc->alias.$this->tmpl['limitstarturl'].'&Itemid='. JRequest::getVar('Itemid', 1, 'get', 'int')).'">';
							$linkDownloadB = '<a href="'. JRoute::_(PhocaDownloadHelperRoute::getFileRoute($valueDoc->id,$this->category[0]->id,$valueDoc->alias, $this->category[0]->alias, $valueDoc->sectionid, 'download').$this->tmpl['limitstarturl']).'">';
							$linkDownloadE ='</a>';
						
						}
					}
				
					// OUTPUT
					echo '<div class="pd-c-download-item"><div class="pd-c-download-item2">';
					
					
					echo $descriptionOutputTop;			
					echo '<div '.$classMiddle.'>'.$displayNameHead. $imageFileNameThumbnail.'<div class="pd-document'.$this->tmpl['file_icon_size'].'" '.$imageFileName.'><div class="pd-float">';
					echo $linkDownloadB;
					echo $displayName;
					echo $linkDownloadE;
					if ($fileSize !='') {
						echo ' <small style="color:#ccc">('.$fileSize.')</small>';
					}
					echo '</div>';
					
					echo PhocaDownloadHelper::displayNewIcon($valueDoc->date, $this->tmpl['displaynew']);
					echo PhocaDownloadHelper::displayHotIcon($valueDoc->hits, $this->tmpl['displayhot']);
					
					//Specific icons
					if (isset($valueDoc->image_filename_spec1) && $valueDoc->image_filename_spec1 != '') {
						$iconPath	= PhocaDownloadHelper::getPathSet('icon');
						$iconPath	= str_replace ( '../', JURI::base(true).'/', $iconPath['orig_rel_ds']);
						echo '<div class="pd-float"><img src="'.$iconPath . $valueDoc->image_filename_spec1.'" alt="" /></div>';
					} 
					if (isset($valueDoc->image_filename_spec2) && $valueDoc->image_filename_spec2 != '') {
						$iconPath	= PhocaDownloadHelper::getPathSet('icon');
						$iconPath	= str_replace ( '../', JURI::base(true).'/', $iconPath['orig_rel_ds']);
						echo '<div class="pd-float"><img src="'.$iconPath . $valueDoc->image_filename_spec2.'" alt="" /></div>';
					} 
					
					echo '</div>' . "\n";
					
					// - - - - - -
					//Buttons
					// - - - - - - 
					$playerOutput 	= '';
					$previewOutput	= '';
					$detailOutput	= '';
					$downloadOutput	= '';
					
					// MEDIA PLAYER
					if ($this->tmpl['display_play'] == 1) {
						if (isset($valueDoc->filename_play) && $valueDoc->filename_play != '') {
							$fileExt 	= PhocaDownloadHelper::getExtension($valueDoc->filename_play);
							$canPlay	= PhocaDownloadHelper::canPlay($valueDoc->filename_play);
							if ($canPlay) {

								// Special height for music only
								$buttonPlOptions = $this->buttonpl->options;
								if ($fileExt == 'mp3') {
									$buttonPlOptions = $this->buttonpl->optionsmp3;
								}
					
								//$playLink = JRoute::_('index.php?option=com_phocadownload&view=play&id='.$valueDoc->id.':'.$valueDoc->alias.$this->tmpl['limitstarturl'].'&tmpl=component&Itemid='. JRequest::getVar('Itemid', 1, 'get', 'int'));
								$playLink = JRoute::_(PhocaDownloadHelperRoute::getFileRoute($valueDoc->id,$this->category[0]->id,$valueDoc->alias, $valueDoc->categoryalias,$valueDoc->sectionid, 'play').$this->tmpl['limitstarturl']);
								$playerOutput .= '<div class="pdplay'.$this->tmpl['button_style'].'"><div>';
								
								if ($this->tmpl['play_popup_window'] == 1) {
									$playerOutput .= '<a  href="'.$playLink.'" onclick="'. $buttonPlOptions.'" >'. JText::_('COM_PHOCADOWNLOAD_PLAY').'</a>';
								} else {	
									$playerOutput .= '<a class="pd-modal-button" href="'.$playLink.'" rel="'. $buttonPlOptions.'" >'. JText::_('COM_PHOCADOWNLOAD_PLAY').'</a>';
								}
								$playerOutput .= '</div></div>';
							}
						}
					}
					
					// PREVIEW
					if ($this->tmpl['display_preview'] == 1) {
						if (isset($valueDoc->filename_preview) && $valueDoc->filename_preview != '') {
							$fileExt = PhocaDownloadHelper::getExtension($valueDoc->filename_preview);
							if ($fileExt == 'pdf' || $fileExt == 'jpeg' || $fileExt == 'jpg' || $fileExt == 'png' || $fileExt == 'gif') {
					
								$filePath	= PhocaDownloadHelper::getPathSet('file');
								$filePath	= str_replace ( '../', JURI::base(true).'/', $filePath['orig_rel_ds']);
								$previewLink = $filePath . $valueDoc->filename_preview;	
								$previewOutput	.= '<div class="pdpreview'.$this->tmpl['button_style'].'"><div>';
								
								if ($this->tmpl['preview_popup_window'] == 1) {
									$previewOutput .= '<a  href="'.$previewLink.'" onclick="'. $this->buttonpr->options.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
								} else {	
									if ($fileExt == 'pdf') {
										// Iframe - modal
										$previewOutput	.= '<a class="pd-modal-button" href="'.$previewLink.'" rel="'. $this->buttonpr->options.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
									} else {
										// Image - modal
										$previewOutput	.= '<a class="pd-modal-button" href="'.$previewLink.'" rel="'. $this->buttonpr->optionsimg.'" >'. JText::_('COM_PHOCADOWNLOAD_PREVIEW').'</a>';
									}
								}
								$previewOutput	.= '</div></div>';
							}
						}
					}
					
					// DETAIL
					if ($this->tmpl['display_detail'] == 1) {
						$detailOutput	.= '<div class="pddetails'.$this->tmpl['button_style'].'"><div>';
						$detailOutput	.= '<a '.$overlib.' href="#">'. JText::_('COM_PHOCADOWNLOAD_DETAILS').'</a>';
						$detailOutput	.= '</div></div>';
					}	
					
					// DOWNLOAD
					$downloadOutput .= '<div class="pddownload'.$this->tmpl['button_style'].'"><div>';
					$downloadOutput .= $linkDownloadB;
					$downloadOutput .= JText::_('COM_PHOCADOWNLOAD_DOWNLOAD');
					$downloadOutput .= $linkDownloadE;
					$downloadOutput .= '</div></div>';
					
					
					echo '<div style="clear:both"></div>';
					// Everytime we need 4 columns but button are not on the same columns - - - - - 
					$buttonColumns 	= 4;
					$buttonOutput	= '';
					
					
					
					if ($downloadOutput != '') {
						$buttonOutput .= '<div '.$classMiddle.'>'.$downloadOutput.'</div>';
						$buttonColumns--;
					}
					if ($detailOutput != '') {
						$buttonOutput .= '<div '.$classMiddle.'>'.$detailOutput.'</div>';
						$buttonColumns--;
					}
					if ($previewOutput != '') {
						$buttonOutput .= '<div '.$classMiddle.'>'.$previewOutput.'</div>';
						$buttonColumns--;
					}
					if ($playerOutput != '') {
						$buttonOutput .= '<div '.$classMiddle.'>'.$playerOutput.'</div>';
						$buttonColumns--;
					}
					
					for($i = 0;$i < $buttonColumns;$i++) {
						$buttonOutput = '<div '.$classMiddle.'> </div>'.$buttonOutput; 
					}
					// - - - - - 
					
					echo $descriptionOutputBottom;
					
					echo '<div class="pd-c-buttons">'.$buttonOutput.'</div>';
					
					$ratingOutput 		= PhocaDownloadRateHelper::renderRateFile($valueDoc->id, $this->tmpl['display_rating_file']);
					echo $ratingOutput;
					
					echo '<div style="clear:left"></div>';
					$mirrorOutput = '';
					$mirrorOutput1 = PhocaDownloadHelper::displayMirrorLinks(1, $valueDoc->mirror1link, $valueDoc->mirror1title, $valueDoc->mirror1target);
					$mirrorOutput2 = PhocaDownloadHelper::displayMirrorLinks(1, $valueDoc->mirror2link, $valueDoc->mirror2title, $valueDoc->mirror2target);
					
					if ($mirrorOutput1 != '') {
						$mirrorOutput = '<div class="pd-mirror">'.$mirrorOutput1.'</div>';
					}
					if ($mirrorOutput2 != '') {
						$mirrorOutput .= '<div class="pd-mirror">'.$mirrorOutput2.'</div>';
					}
					echo $mirrorOutput;
					
					//Report Link
					$reportOutput = '';
					$reportOutput = PhocaDownloadHelper::displayReportLink(1, $valueDoc->title);
					if ($reportOutput != '') {
						$reportOutput = '<div class="pd-report">'.$reportOutput.'</div>';
					}
					echo $reportOutput;
					
					echo '<div style="clear:both"></div>';
					echo '</div>' . "\n";
					
					
					
					
					echo '</div></div>';
				}
			}
		}
		
		echo '</div>';

// Pagination - - - - -
$this->tmpl['action'] = str_replace('&amp;', '&', $this->tmpl['action']);
$this->tmpl['action'] = str_replace('&', '&amp;', $this->tmpl['action']);
echo '<form action="'.$this->tmpl['action'].'" method="post" name="adminForm">'. "\n";

if (count($this->category[0])) {
	echo '<div class="pgcenter"><div class="pagination">';
	
	if ($this->params->get('show_pagination_limit')) {
		
		echo '<div class="pginline">'
			.JText::_('COM_PHOCADOWNLOAD_DISPLAY_NUM') .'&nbsp;'
			.$this->tmpl['pagination']->getLimitBox()
			.'</div>';
	}
	
	if ($this->params->get('show_pagination')) {
	
		echo '<div style="margin:0 10px 0 10px;display:inline;" class="sectiontablefooter'.$this->params->get( 'pageclass_sfx' ).'" id="pg-pagination" >'
			.$this->tmpl['pagination']->getPagesLinks()
			.'</div>'
		
			.'<div style="margin:0 10px 0 10px;display:inline;" class="pagecounter">'
			.$this->tmpl['pagination']->getPagesCounter()
			.'</div>';
	}
	echo '</div></div>'. "\n";

}
//echo '<input type="hidden" name="controller" value="category" />';
echo JHTML::_( 'form.token' );
echo '</form>';
// - - - - - - - - - - - -
		
		
		/* TODO Joomla! returns error message
		if (JComponentHelper::isEnabled('com_jcomments', true) && $this->tmpl['display_category_comments'] == 1) {
			include_once(JPATH_BASE.DS.'components'.DS.'com_jcomments'.DS.'jcomments.php');
			echo JComments::showComments($this->category[0]->id, 'com_phocadownload', JText::_('COM_PHOCADOWNLOAD_CATEGORY') .' '. $this->category[0]->title);
		}*/
	} else {
		echo '<h3>'.JText::_('COM_PHOCADOWNLOAD_CATEGORY'). '</h3>';
		echo '<div class="pd-error">'.JText::_('COM_PHOCADOWNLOAD_NO_RIGHTS_ACCESS_CATEGORY').'</div>';
	}
} else {
	echo '<h3>&nbsp;</h3>';
}
echo '</div></div>' . $this->tmpl['phoca_download'];
?>
