<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   atarda | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //  
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
if(!checkactive('gallery')) include(dirname(__FILE__)."/lib/notactive.php" );
$sname = simpletitle($lang['gallery']);
$ok = 1;
$num = 1;
if (isset($_REQUEST['pp']) && $_REQUEST['pp']!="") { $limit = $_REQUEST['pp']; } else { $limit = 10; }


if (!permission("showgalerie")) {
	$EOUT = stderror("nopermission", $lang['nopermission_t'], str_replace("__URL__", "index.php", $lang['redirect']), "index.php", 1);
	$error = 1;
	$NOOUT = 1;
	$ok = 0;
	
	} else {
	
	
	
	// ===================================================
	// <<-- Aktionen, wenn Zugriff -->>
	// <<-- Galerieübersicht -->>
	// ===================================================
	if( ($_REQUEST['action']=="showimages") || !($_REQUEST['action']) ){
//		$gallery = array();
//		getGallery(0, "", $gallery, $area, 1, $orderby, $ascdesc, 1);
		$gallery = getGallery2(0, $area, 1, $orderby, $ascdesc, 1);

		$tmpl->assign("navig", GALNAVI);
		$tmpl->assign("gallery", $gallery);
	}
	
	
	
	// ===================================================	
	// <<-- Galeriebilder anzeigen -->>
	// ===================================================	
	if($_REQUEST['action']=="showimages"){
		$sql = new dbc("SELECT picspp,picspr FROM " . PREFIX . "_settings");
		$row = $sql->fetchrow();
		
		$limit = $row->picspp;
		$preflimit = $limit;
		if($_POST['pp']!="") $limit = $_POST['pp'];
		
		switch($_REQUEST['ascdesc']){
			case "asc": $ascdesc = "asc"; break;
			case "desc": $ascdesc = "desc"; break;
			default : $ascdesc = "desc"; break;
		}
		
		$sql = new dbc("SELECT * FROM ".PREFIX."_gallery WHERE id=$_REQUEST[galid] ");
		$row_gal = $sql->fetchrow();
		$gp = $row_gal->product;
		
		if($row_gal->cancomment=='1'){
			$tmpl->assign('comments',1);
		}
		
		if($row_gal->product!=""){
			$sql = new dbc("SELECT title FROM ".PREFIX."_products WHERE id='$row_gal->product' ");
			$row_p = $sql->fetchrow();
			$tmpl->assign('productname', kstripslashes($row_p->title));
		}
		
		
		
		
		
		$sql = new dbc("SELECT id FROM ".PREFIX."_gallery_items WHERE gal_id=$_REQUEST[galid] ");
		$gallery = array();
		
		$num = $sql->numrows();
		$numall = $num;
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$sql = new dbc("SELECT descr, name, id, path, hits FROM ".PREFIX."_gallery_items WHERE gal_id=$_REQUEST[galid] order by id $ascdesc limit $a,$limit");
		$num = $sql->numrows();
		$maxi = $row->picspr;
		
		
		$i = 1;
		$ti = 1;
		while($row=$sql->fetchrow()){
			$tr = "";
			$sqlc = new dbc("SELECT id FROM ".PREFIX."_gallerycomments WHERE imgid='$row->id'");
			$row->comments = $sqlc->numrows();
			$row->image = "<img border=\"0\" vspace=\"0\" src=\"system/thumb.php?img_id=".$row->id."\" />";
			$row->img_id = $row->id;
			if( ($i == $maxi) && ($num != $ti) ) $tr = 1; 
			if($i > $maxi)  $i = 1;
			$row->imagecount = $i;
			$row->tr = $tr;
			
			$i++;
			$ti ++;
			array_push($gallery,$row);
		}
		
		
		// ===============================================================
		// <<-- 
		//      VERKNUEPFTE NEWS & ARTIKEL
		//      SOLLTEN PRODUKTE VERWENDET WERDEN, WERDEN NEWS DES GLEICHEN
		//      PRODUKTS AUSGELESEN, WENN NICHT, WIRD DAS SCHLAGWORT ALS 
		//      VERKNUEPFUNG GEWÄHLT. 
		// -->>
		// ===============================================================
		if(($row_gal->product!="") && ($row_gal->product!=0) ){
			$dbfirst = "product='$row_gal->product'";
		} else {
			if($row_gal->matchword!="") { 
				$dbfirst = "matchword like '%$row_gal->matchword%'"; 
			} else {
				$dbfirst = "matchword = 'unknown'";
			}
		}
		
		// ===============================================================
		// <<-- NEWS -->>
		// ===============================================================
		$q = "SELECT title,newsid,time_start FROM ".PREFIX."_news 
		WHERE
		($dbfirst) 
		&& 
		(((time_end>=".time().") || (time_end='1') || (time_end='0')) && (((ispublic='1')) && (time_start<=".time().")))
		ORDER BY time_start DESC";
		
		if( $row_gal->product!="" || $row_gal->matchword!="" ) { 
			$sql = new dbc($q); 
			$i = 0;
			while($row_linkednews = $sql->fetchrow()){
				$linked_news[$i]['title'] = $row_linkednews->title;
				$linked_news[$i]['newsid'] = $row_linkednews->newsid;
				$linked_news[$i]['time'] = mygmt($row_linkednews->time_start);
				$i++;
				$assign_linkednews = 1;
			}
			$tmpl->assign('linkednews', $linked_news);
		}
		
		// ===============================================================
		// <<-- ARTIKEL -->>
		// ===============================================================
		if( $row_gal->product!="" || $row_gal->matchword!="" ) { 
			$sql2 = new dbc("SELECT id, title, time_start, type FROM ".PREFIX."_articles   
			WHERE 
			($dbfirst)
			&&
			(((time_end>=".time().") || (time_end='1') || (time_end='0')) && (((active='1')) && (time_start<=".time().")))
			ORDER BY time_start DESC");
			$i = 0;
			while($row_linkedarticles = $sql2->fetchrow()){
				$linked_articles[$i]['title'] = $row_linkedarticles->title;
				$linked_articles[$i]['id'] = $row_linkedarticles->id;
				$linked_articles[$i]['time'] = mygmt($row_linkedarticles->time_start);
				$linked_articles[$i]['type'] = getArticleType($row_linkedarticles->type);
				$linked_articles[$i]['type_num'] = $row_linkedarticles->type;
				$i++;
				$assign_linkedarticles = 1;
			}
			$tmpl->assign('linkedarticles', $linked_articles);
		}
		// ===============================================================
		// <<-- ARTIKEL -->>
		// ===============================================================
		$sql2 = new dbc("SELECT id, title, time_start, type FROM ".PREFIX."_articles   
		WHERE 
		($dbfirst)
		&&
		(((time_end>=".time().") || (time_end='1') || (time_end='0')) && (((active='1')) && (time_start<=".time().")))
		ORDER BY time_start DESC");
		$i = 0;
		include(BASEDIR . '/inc/functions_top.php');
		while($row_linkedarticles = $sql2->fetchrow()){
			$linked_articles[$i]['title'] = $row_linkedarticles->title;
			$linked_articles[$i]['id'] = $row_linkedarticles->id;
			$linked_articles[$i]['time'] = mygmt($row_linkedarticles->time_start);
			$linked_articles[$i]['type'] = getArticleType($row_linkedarticles->type);
			$linked_articles[$i]['type_num'] = $row_linkedarticles->type;
			$i++;
		}
		$tmpl->assign('linkedarticles', $linked_articles);
		
		#$row_gal->descr = autowords($row_gal->descr);
		
		// HIGHLIGHT 4.2.0
		$row_gal->descr = high($row_gal->descr);
		
		
		$subgalleries = getGallery2($_REQUEST['galid'], $area, 1, $orderby, $ascdesc, 1);
		$tmpl->assign('subgal', $subgalleries);
		$tmpl->assign('galid', $_REQUEST['galid']);
		$tmpl->assign('numall', $numall);
		$tmpl->assign('ctime', mygmt($row_gal->ctime));
		$tmpl->assign('preflimit', $preflimit);
		$tmpl->assign('area', $area);
		$tmpl->assign('author', fetchusername($row_gal->uid));
		$tmpl->assign('uid', $row_gal->uid);
		$tmpl->assign('row_gal', $row_gal);
		$tmpl->assign('lang', $langadmin);
		$tmpl->assign('gallery', $gallery);
		$tmpl->assign('ascdesc', $ascdesc);
		$tmpl->assign('pp', $_POST['pp']);
		$tmpl->assign('navigation', getNavigationPath($_REQUEST['galid'], 'gallery', 'gallery&amp;action=showimages', 'galid', 'id', 'title'));
		
		$headernav = getNavigationPath($_REQUEST['galid'], 'gallery', 'gallery&amp;action=showimages', 'galid', 'id', 'title');
		
		
		if ($numall > $limit){
			$tmpl->assign('navi', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?nav=1&amp;p=gallery&amp;action=showimages&amp;galid=".$_REQUEST['galid']."&amp;area=".$area."&amp;pp=".$limit."&amp;ascdesc=".$ascdesc."&amp;page={s}\">{t}</a> "));
		}
		
		$tmpl->assign('gallery', $gallery);
		$sname = simpletitle($lang['gallery'] . $lang['f_navi_separator'] . getNavigationptitle($_REQUEST['galid'], 'gallery', '', 'id', 'id', 'title'));
		
	}
	
	
	// ===================================================	
	// <<-- Zuweisen und ausgeben -->>
	// ===================================================	
	switch( $_REQUEST['action'] ){
		case '' : 
		$title = $lang['gallery'];
		$hnav = $title ;
		break;
		case 'overview' : $title = $lang['gallery']; break;
		case 'showimages' : 
		$title = $lang['galleryshowimages'];
		$hnav = "<a class=\"boxnavi\" href=\"index.php?p=gallery&amp;area=$area\">". $lang['gallery'] . "</a>". SEPARATOR . $headernav ;
		break;
		
	}
	
	
	$tmpl->assign('area', $area);
	$tmpl->assign('lang', $lang);
	$content = parsetrue('container/'.container('gallery'), $hnav, $tmpl->fetch('gallery/gallery.tpl'));
	if(($assign_linkednews==1) && (checkactive(news)) ) { $content .= parsetrue("container/".container('gallery'), $lang['newslinked'], $tmpl->fetch('gallery/linkednews.tpl')); };
	if(($assign_linkedarticles==1) && (checkactive(articles)) )  { $content .= parsetrue("container/".container('gallery'), $lang['articleslinked'], $tmpl->fetch('gallery/linkedarticles.tpl')); };
	$tmpl->assign('content', $content);
	
}

?>