<?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/actividades.php";
include "../lang/$lang/menu.php";


include_once "../admin_actividades/config_actividades.inc.php";

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

$isTopfoto = true;

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;
if(isset($superFecha)) $fecha = strtotime($superFecha[2]."-".$superFecha[0]."-".$superFecha[1]); // YYYY-MM-DD

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


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

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

$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 == 'listadoFechas' || $action == 'listadoPasadas' || $action == 'soloTalleres' || $action == 'soloSalidas' || $action == '' || $action == 'buscar')
{
	$ctxt->update();
	$view = 'list';
}
elseif($action == 'listadoPasadas')
{
	$ctxt->update();
	$view = 'listadoPasadas';
}
elseif($action == 'bloqueSalidas')
{
	$ctxt->update();
	$view = 'bloqueSalidas';
}
elseif($action == 'bloqueCharlas')
{
	$ctxt->update();
	$view = 'bloqueCharlas';
}
elseif($action == 'bloqueSalidasActuales')
{
	$ctxt->update();
	$view = 'bloqueSalidasActuales';
}
elseif($action == 'bloqueTalleres')
{
	$ctxt->update();
	$view = 'bloqueTalleres';
}
elseif($action == 'publicCardActuales')
{
	$view = 'publicCardActuales';
}
elseif($action == 'publicCard')
{
	$view = 'publicCard';
}

/*//////////////////////////////////////////////////////
						 VISTAS
//////////////////////////////////////////////////////*/

if ($view == 'list')
{
	$ctxt->setDefaultOrderBy('fecha');
	$ctxt->setDefaultOrderDir('ASC');
	$ctxt->setPerPage(100000);
	
	$items = new Collection($defs);

	$wheres = array();
	$wheres[] = "(T0.visible=1)";

	if ($action=='listadoPasadas') {
		//$titularEventos = __tx('talleres-salidas');
		$wheres[] = "(T0.fecha < NOW())";
		$subTitularEventos = __tx('Terminados');
		$ctxt->setDefaultOrderDir('DESC');
	} elseif ($action=='listadoFechas' && isset($_GET['fecha'])) {
		//$titularEventos = __tx('talleres-salidas');
		$date=explode('/',$_GET['fecha']);
		$fechaSelect = strtotime((int)$date[2]."-".(int)$date[0]."-".(int)$date[1]);
		$wheres[] = "(T0.fecha = '".date('Y-m-d',$fechaSelect)."')";
		$subTitularEventos = $_GET['date'];
	}else {
		//$titularEventos = __tx('talleres-salidas');
		$wheres[] = "(T0.fecha > DATE_SUB( NOW(), INTERVAL 7 DAY ))"; //listamos los eventos de hace una semana en adelante
		$subTitularEventos = __tx('programados');
	}
	
	//$wheres[] = "(T0.tipo=".$_GET['tipo'].")";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();
	
	$columns = $CONFIG['listing'];

	$actividades = array();
	foreach($items->getList() as $id=>$item) {
		$fotoData = explode('|', $item->get('id_foto'));
		//printr($fotoData);
		if($fotoData[0] != '0'){
			$fileRelativePath = WEB_ROOT . (str_replace("../","",$CONFIG['members']['id_foto']['path']));
			//$fileRelativePath = $CONFIG['members']['id_foto']['path'];
			$foto = $fileRelativePath . '/' . $fotoData[6] . '.' . $fotoData[5];
			$fotoAlt = $item->get('id_foto_altText');
		}else{
			$foto = '';
			$fotoAlt = '';
		}
		
		$actividades[$id]['foto'] 		= $foto;
		$actividades[$id]['foto_alt'] 		= $fotoAlt;
		$actividades[$id]['titulo'] 		= stripslashes($item->get('titulo', false));
		$actividades[$id]['permalink'] 	= $item->get('permalink', false);
		$actividades[$id]['fecha'] 		= ($lang == 'en' || $lang == 'eu') ? $item->get('fecha', false):$item->get('fecha', true);
		$actividades[$id]['descripcion'] 	= stripslashes($item->get('descripcion', false));
		$actividades[$id]['nota'] 	= stripslashes($item->get('nota', false));
		$actividades[$id]['tipo'] 	= $item->get('tipo', true);
		$actividades[$id]['idioma'] 	= ($item->get('idioma', false)<3) ? $item->get('idioma', true) : '';
		$actividades[$id]['lugar'] 	= stripslashes($item->get('lugar', false));
		$actividades[$id]['precio'] 	= ($item->get('gratis', false)==1) ? __tx('gratuito') : $item->get('precio', false) ;
		$actividades[$id]['dia_teoria'] 	= stripslashes($item->get('dia_teoria', false));
		$actividades[$id]['dia_practicas'] 	= stripslashes($item->get('dia_practicas', false));
		$actividades[$id]['inscripciones'] 	= stripslashes($item->get('inscripciones', false));
		
	}
	//printr($actividades);
	$doc_vista = "view_actividades.php";
	include "../templates/public_view.inc.php";
}
elseif($view == 'bloqueSalidas')
{
	$ctxt->setDefaultOrderBy('fecha');
	$ctxt->setDefaultOrderDir('ASC');
	
	$ctxt->setPerPage(100);
	
	$items = new Collection($defs);

	$wheres = array();
	$wheres[] = "(T0.fecha > DATE_SUB( NOW(), INTERVAL 7 DAY ))"; //listamos los eventos de hace una semana en adelante.
	$wheres[] = "(T0.tipo = 2)"; //listamos sólo las salidas.
	$wheres[] = "(T0.visible=1)";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();
	
	include "view_bloque_actividades.php";
}
elseif($view == 'bloqueTalleres')
{
	$ctxt->setDefaultOrderBy('fecha');
	$ctxt->setDefaultOrderDir('ASC');
	
	$ctxt->setPerPage(100);
	
	$items = new Collection($defs);

	$wheres = array();
	$wheres[] = "(T0.fecha > DATE_SUB( NOW(), INTERVAL 7 DAY ))"; //listamos los eventos de hace una semana en adelante.
	$wheres[] = "(T0.tipo = 1)"; //listamos sólo las salidas.
	$wheres[] = "(T0.visible=1)";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();
	
	include "view_bloque_actividades.php";
}
elseif($view == 'bloqueCharlas')
{
	$ctxt->setDefaultOrderBy('fecha');
	$ctxt->setDefaultOrderDir('ASC');
	
	$ctxt->setPerPage(100);
	
	$items = new Collection($defs);

	$wheres = array();
	$wheres[] = "(T0.fecha > DATE_SUB( NOW(), INTERVAL 7 DAY ))"; //listamos los eventos de hace una semana en adelante.
	$wheres[] = "(T0.tipo = 3)"; //listamos sólo las charlas.
	$wheres[] = "(T0.visible=1)";
	$where = count($wheres) ? join(" AND ", $wheres) : '';

	if(count($wheres)) $items->addConstraint($where);
	$items->addContextConstraints();
	$items->readFromDBShort();
	
	include "view_bloque_actividades.php";
}
elseif ($view == 'publicCard') 
{
	$item = new Element($defs);
	$item->readFromDB($id);
	
	
	$tx['pagTitle'] = ''.stripslashes($item->getValue('titulo')).' - '.$datosEmpresa['nombre'];
	
	$doc_vista = "view_actividades_card.php";
	include "../templates/public_view.inc.php";
}
////////////////////////////////////////////////////////

?>