<?php
if (session_id() == "") session_start();

////////////////////////////////////////////////////////

include_once "../inc/dedolib/Config.inc.php";
include_once "../inc/base/functions.inc.php";
include_once "../inc/dedolib/Collection.inc.php";
include_once "../inc/dedolib/Element.inc.php";
include_once "../inc/base/JSON.php";

include "../lang/$lang/especies.php";
include "../lang/$lang/menu.php";


include_once "../admin_especies/config_especies.inc.php";

////////////////////////////////////////////////////////

list($action,$id) = getAction();
$view = '';

$id = getIdFromPermalink($id,$CONFIG['table'],$CONFIG['id'],$CONFIG['members']['permalink'],'permalink');

if(empty($id)) $id = -1;

if(isset($superAction)) $action = $superAction;

$rutaPagina = array();
$rutaPagina[] = "especies";

	// es una llamada desde otra zona para mostrar el bloque de noticias
	// $action = 'bloqueNoticias';

$CONFIG['listing'] = $CONFIG['listing_public'];

$_REQUEST['CIID'] = isset($_REQUEST['CIID']) ? filter_var($_REQUEST['CIID'],FILTER_SANITIZE_STRING): null;
$defs = new CollectionDefinitions($CONFIG);
$ctxt =& $defs->makeContext(true, $_REQUEST['CIID']);

/*//////////////////////////////////////////////////////
						 ACCIONES
//////////////////////////////////////////////////////*/

if ($action == 'list' || $action == '' || $action == 'buscar')
{
	$ctxt->update();
	$view = 'list';
}
elseif($action == 'bloqueEspecies')
{
	$ctxt->update();
	$view = 'bloqueEspecies';
}
elseif($action == 'bloqueUltimos')
{
	$ctxt->update();
	$view = 'bloqueUltimos';
}
elseif($action == 'bloqueVisitadasUsuario')
{
	$ctxt->update();
	$view = 'bloqueVisitadasUsuario';
}
elseif($action == 'bloquePopulares')
{
	$ctxt->update();
	$view = 'bloquePopulares';
}
elseif($action == 'publicCard')
{
	$view = 'publicCard';
}

/*//////////////////////////////////////////////////////
						 VISTAS
//////////////////////////////////////////////////////*/
if ($view == 'list')
{
	$ctxt->setDefaultOrderBy('nombre_real');
	//$ctxt->setDefaultOrderDir('DESC');
	$ctxt->setPerPage(1000);
	if (isset($_REQUEST['frmnombre']) && $_REQUEST['frmnombre']!='') {
		$_SESSION['frmnombre'] = $_REQUEST['frmnombre'];
	}

	if (!isset($_SESSION['frmnombre'])) {
		$_SESSION['frmnombre'] = 'real';
	}

	$getNombre = (isset($_SESSION['frmnombre']) && $_SESSION['frmnombre']=='comun') ? 'nombre_comun' : 'nombre_real';


	$items = new Collection($defs);

	$wheres = array();

	if (isset($_REQUEST['permalink']) && $_REQUEST['permalink']!='') {
		//$_SESSION['frmclasif'] = $_REQUEST['frmclasif'];
		$wheres[] = "(T0.id_especie IN (".join(',', getEspeciesByCategoria($_REQUEST['permalink'])) ."))";
		$mostratodos = true;
	}

	if (isset($_REQUEST['cuadricula']) && $_REQUEST['cuadricula']!='') {
		$wheres[] = "(T0.id_especie IN (".join(',', getEspeciesByCuadricula($_REQUEST['cuadricula'])) ."))";
		$mostratodos = true;
	}

	$wheres[] = "(T0.visible = 1)";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();

	//$columns = $CONFIG['listing'];

	$especies = array();
	foreach($items->getList() as $id=>$item) {

		// echo "<pre>";print_r($item->getDataFoto('id_foto'));echo "</pre>";
		$fotoData = explode('|', $item->get('id_foto'));
		$minH = $CONFIG['members']['id_foto']['miniature']['h'];
		if($fotoData[0] != '0'){

			// $fileRelativePath = WEB_ROOT . (str_replace("../","",$CONFIG['members']['id_foto']['path']));
			$fileRelativePath = $CONFIG['members']['id_foto']['path'];
			$foto = $fileRelativePath . '/' . $fotoData[0] . '_m.' . $fotoData[5];
			$fotoAlt = $item->get('id_foto_altText');
		}else{
			$foto = '';
			$fotoAlt = '';
		}
		$especies[$id]['order'] 		= to_permalink($item->get($getNombre, false));
		$especies[$id]['nombre'] 		= stripslashes($item->get($getNombre, false));
		$especies[$id]['foto'] 			= $foto;
		$especies[$id]['fotoAlt']		= stripslashes($item->get('nombre_real', false));;
		$especies[$id]['permalink'] 	= stripslashes($item->get('permalink', false));
		$especies[$id]['nombre'] 		= stripslashes($item->get($getNombre, false));
		//$especies[$id]['descripcion'] 	= stripslashes($item->get('descripcion', false));
		//$especies[$id]['clasificacion'] 		= stripslashes($item->get('clasificacion', false));
		$especies[$id]['id_especie'] 		= $id;
	}

	sort($especies);

	$sidebar = "../templates/public_sidebar_especies.inc.php";

	$doc_vista = "view_especies.php";
	include "../templates/public_view.inc.php";
}
elseif($view == 'bloqueUltimos')
{
	$ctxt->setDefaultOrderBy('fecha_creacion');
	//$ctxt->setDefaultOrderDir('DESC');

	$ctxt->setPerPage(2);

	$items = new Collection($defs);

	$wheres = array();
	//$wheres[] = "(T0.fecha_caducidad = '0000-00-00' OR T0.fecha_caducidad >= CURDATE())";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();

	include "view_bloque_sidebar.php";
}
elseif($view == 'bloquePopulares')
{
	$ctxt->setDefaultOrderBy('clicks');
	//$ctxt->setDefaultOrderDir('DESC');

	$ctxt->setPerPage(3);

	$items = new Collection($defs);

	$wheres = array();
	//$wheres[] = "(T0.fecha_caducidad = '0000-00-00' OR T0.fecha_caducidad >= CURDATE())";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();

	include "view_bloque_sidebar.php";
}
elseif($view == 'bloqueVisitadasUsuario')
{
	//$ctxt->setDefaultOrderBy('clicks');
	//$ctxt->setDefaultOrderDir('DESC');

	$ctxt->setPerPage(3);

	$items = new Collection($defs);

	$wheres = array();

	$visitadas = array_reverse($_SESSION['visitada']);

	$wheres[] = "(T0.id_especie IN (".join(', ', $visitadas)."))";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();

	include "view_bloque_sidebar.php";
}
elseif ($view == 'publicCard')
{

	//actualizamos el contador de clicks
	$dbClicks = new DBClass();
	$sqlClicks = "UPDATE ".DBPRE."especies SET clicks = clicks + 1 WHERE  id_especie =".$id;
	$dbClicks->query($sqlClicks);

	$rutaPagina[] = "ficha";
	//$isTopfoto = true;

	$item = new Element($defs);
	$item->readFromDB($id);


	$tx['pagTitle'] = ''.stripslashes($item->getValue('nombre_real')).' - '.$datosEmpresa['nombre'];
	$_SESSION['visitada'][$id] = $id;


	//$sidebar = "../templates/public_sidebar_especies.inc.php";

	$doc_vista = "view_especies_card.php";
	include "../templates/public_view.inc.php";
}
////////////////////////////////////////////////////////

?>
