<?php

/*
* @author: Javier Reyes Gomez (jreyes@escire.com)
* @date: 27/01/2006
* @copyright (C) 2006 Javier Reyes Gomez (eScire.com)
* @license http://www.gnu.org/copyleft/lgpl.html GNU/LGPL
*/

//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
	header("location: index.php");
	exit;
}

include_once ("lib/imagegals/imagegallib.php");
require_once ('lib/workspaces/resourceslib.php');
require_once ('lib/workspaces/workspacelib.php');

global $dbTiki;
global $userlib;
$workspacesLib = new WorkspaceLib($dbTiki);

$workspace = $workspacesLib->get_current_workspace();

if (!isset ($workspace)) {
	$smarty->assign('error_msg', tra("Workspace not selected"));
}else{
	$resourcesLib = new WorkspaceResourcesLib($dbTiki);
	$imageGals = $resourcesLib->get_category_objects($workspace["categoryId"], null, "image gallery");
	
	if (isset($imageGals) && $imageGals!="" && count($imageGals)>0){
		$selectedGal = $imageGals[0];
		if (isset($module_params["name"]) && $module_params["name"]!=""){
			foreach ($imageGals as $key => $imageGal) {
				if ($imageGal["name"]==$module_params["name"]){
					$selectedGal = $imageGal;
				}
			}
		}
		$images = array();
		
		//Maximum numer of images per page
		$maxImages = 3;
		if (isset($module_params["maxImages"]) && $module_params["maxImages"]!=""){
			$maxImages = $module_params["maxImages"];
		}
		
		//Page Offset
		$offset = 0;
		if (isset($module_params["offset"])){
			$offset = $module_params["offset"];
		}
		
		//List images
		$ranking = $imagegallib->list_images($offset, $maxImages, 'created_desc', '', $selectedGal["objId"]);
		
		//Configure Offset
		$cant_pages = ceil($ranking["cant"] / $maxImages);
		$smarty->assign_by_ref('cant_pages', $cant_pages);
		$smarty->assign('actual_page', 1 + ($offset / $maxImages));
		
		if ($ranking["cant"] > ($offset + $maxImages)) {
			$smarty->assign('next_offset', $offset + $maxImages);
		} else {
			$smarty->assign('next_offset', -1);
		}
		
		// If offset is > 0 then prev_offset
		if ($offset > 0) {
			$smarty->assign('prev_offset', $offset - $maxImages);
		} else {
			$smarty->assign('prev_offset', -1);
		}
		
		$images = $ranking["data"];
		$smarty->assign_by_ref('selectedGal', $selectedGal);
	}
	
	$smarty->assign_by_ref('modLastImages', $images);
	$smarty->assign('nonums', isset ($module_params["nonums"]) ? $module_params["nonums"] : 'n');
	$smarty->assign_by_ref('imageGals', $imageGals);
}
?>