<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
// <<-- VERKNUEPFTE GALERIEN -->>
function fetch_gallery($param)
{
    global $db,$THEME, $lang, $area;
    $gals = explode(";", $param);
    $tmpl = new Koobi("templates/");
    $i = 0;
    while (list($key, $val) = each($gals)) {
        $sql = $GLOBALS['db']->Query("SELECT title,id FROM " . PREFIX . "_galerie WHERE id = '$val' AND active='1'");
        $row = $sql->fetchrow();
        $gallink = "index.php?area=$area&amp;p=gallery&amp;action=showimages&amp;galid=" . $row->id . "";
        $galstring[$i]['link'] = $gallink;
        $galstring[$i]['galid'] = $row->id;
        $galstring[$i]['galname'] = $row->title;

        $sql = $GLOBALS['db']->Query("SELECT id,path FROM " . PREFIX . "_galerie_bilder WHERE  gal_id='" . $row->id . "' order by rand() limit 1");
        $row_g = $sql->fetchrow();

        if (is_file("uploads/galerie/" . $row_g->path)) {
            $galstring[$i]['img'] = "uploads/galerie/" . $row_g->path ;
        } 
        $galstring[$i]['id'] = $row_g->id;
        $i++;
    } 
    $GLOBALS['tmpl']->assign('area', $area);
    $GLOBALS['tmpl']->assign('lang', $lang);
    $GLOBALS['tmpl']->assign('theme', $THEME);
    $GLOBALS['tmpl']->assign('galstring', $galstring);
    $linkedg = $tmpl->fetch("$THEME/linked/galleries.tpl");
    return $linkedg;
}


// ==============================================
// <<-- NEUE GALERIEN AUSWAHL-FELD -->>
// ==============================================
function newgals($param)
{
    global $db,$THEME, $lang, $area, $global;
    $tmpl = new Koobi("templates/");
    $sql = $GLOBALS['db']->Query("SELECT  descr,  ctime, id, title, area FROM " . PREFIX . "_galerie WHERE area ='$area' AND active = '1' order by ctime desc limit $param[show]");
    $items = array();
    while ($row = $sql->fetchrow()) {
        $sql_c = $GLOBALS['db']->Query("SELECT gal_id FROM " . PREFIX . "_galerie_bilder WHERE gal_id='$row->id'");
        $sql_r = $GLOBALS['db']->Query("SELECT   path,id FROM " . PREFIX . "_galerie_bilder WHERE gal_id='$row->id' order by rand()");
        $row_r = $sql_r->fetchrow();

        if (is_file("uploads/galerie_icons/" . $row->icon)) {
            $row->gicon = "uploads/galerie_icons/" . $row->icon;
        } else {
            if (is_file("uploads/galerie/" . $row_r->path)) {
                $row->gicon = "system/thumb.php?img_id=$row_r->id&amp;smode=1";
            } else {
                $row->gicon = "system/thumb.php?img_id=9876543&amp;smode=1";
            } 
        } 

        $gallink = "index.php?area=$area&amp;p=gallery&amp;action=showimages&amp;galid=" . $row->id . "";

        $row->desc = $row->descr;
        $row->glink = $gallink;
        $row->pics = $sql_c->numrows();
        $row->ctime = $row->ctime;
        array_push($items, $row);
    } 
    $GLOBALS['tmpl']->assign('row', $row);
    $GLOBALS['tmpl']->assign('lang', $lang);
    $GLOBALS['tmpl']->assign('theme', $THEME);
    $GLOBALS['tmpl']->assign('area', $area);
    $GLOBALS['tmpl']->assign('items', $items);

    if ($items) echo $tmpl->fetch("$THEME/gallery/newgals.tpl");
}


function getGallery($id, $prefix, &$gallery, &$area, $active, $orderby, $ascdesc, $sorting = "0")
{
    global $db,$TFORMAT, $THEME, $lang, $_COOKIE;

    switch ($_REQUEST['orderby']) {
        case "date" : $orderby = "ctime";
            break;
        case "author" : $orderby = "uid";
            break;
        case "title" : $orderby = "title";
            break;
        default : $orderby = "posi";
            break;
    } 

    switch ($_REQUEST['ascdesc']) {
        case "asc": $ascdesc = "ASC";
            break;
        case "desc": $ascdesc = "DESC";
            break;
        default : $ascdesc = "DESC";
            break;
    } 
    // auswahl nach alpha
    if ($sorting == 1) {
        switch ($_REQUEST['charakter']) {
            case "1" :
                for($i = 1;$i < 10;$i++) {
                    $loop .= " OR title  LIKE '" . $i . "%'";
                } 
                $c_sort = " AND(title like '0%' $loop)";
                break;

            case "" :
            case "all" :
                $c_sort = "";
                break;

            default :
                $c_sort = " AND(title LIKE '" . $_REQUEST['charakter'] . "%')";
                break;
        } 

        $limit = limit();
        if ($active == 1) $zusatz = " AND active=1";

        $query = $GLOBALS['db']->Query("
			SELECT id
			FROM " . PREFIX . "_galerie WHERE parent_id = $id AND area = $area $c_sort 
			");

        $num = $query->numrows();
        $seiten = ceil($num / $limit);
        $a = prepage() * $limit - $limit;

        $query = $GLOBALS['db']->Query("
			SELECT *
			FROM " . PREFIX . "_galerie WHERE parent_id = $id AND area = $area $c_sort $zusatz 
			ORDER BY $orderby $ascdesc limit $a,$limit
			");

        if ($num > $limit) 
		{
            $GLOBALS['GalNavi'] = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?do=sort&amp;p=gallery&amp;area=$_REQUEST[area]&amp;charakter=$_REQUEST[charakter]&amp;orderby=$_REQUEST[orderby]&amp;ascdesc=$_REQUEST[ascdesc]&amp;page={s}\">{t}</a> ");
        } else {
            $GLOBALS['GalNavi'] = '';
        } 
        // wenn admin
    } else {
        $query = $GLOBALS['db']->Query("
			SELECT *
			FROM " . PREFIX . "_galerie WHERE parent_id = $id AND area = $area  $zusatz 
			ORDER BY $orderby $ascdesc");
    } 
    if (!$query->numrows()) return;
    $posi = "";

    while ($item = $query->fetchrow())
	{
		$sql = $GLOBALS['db']->Query("SELECT id,path FROM " . PREFIX . "_galerie_bilder WHERE  gal_id='" . $item->id . "' limit 1");
        $row_g = $sql->fetchrow();

        if (is_file("uploads/galerie/" . $row_g->path))
		{
            $item->galthumb = $row_g->id;
        } else {
            $item->galthumb = 987654321;
        } 

        $item->ulink = "index.php?area=" . $area . "&amp;p=user&amp;id=" . $item->uid . "";
        $item->glink = "index.php?area=$area&amp;p=gallery&amp;action=showimages&amp;galid=" . $item->id . "";
        $item->visible_title = $prefix . " " . $item->title;
        $item->created = mygmt($item->ctime);
        $item->user = fetchusername($item->uid);
        $item->toggleimage = $toggleimage_gal;
        $item->toggleimage_td = $toggleimage_td;
        $item->dispgal = $disp_gal;

        $sql = $GLOBALS['db']->Query("SELECT distinct id FROM " . PREFIX . "_galerie_bilder WHERE gal_id=$item->id ");
        $item->pics = $sql->numrows();

        array_push($gallery, $item);
        getGallery($item->id, $prefix . "- - ", $gallery, $area, $active, $orderby, $ascdesc, $sorting);
    } 

    return;
}


function galdropdown($kat_id, $i, $url)
{
    global $db,$parent_id, $area;

    $sql = $GLOBALS['db']->Query("SELECT *,Name_{$LC} AS title FROM " . PREFIX . "_galerie WHERE parent_id='$kat_id' AND area='$area' order by title asc");
    while ($row = $sql->fetcharray()) {
        $gdrop .= '<option  value="' . $row['id'] . '" ';
        if ($row['active'] == 1) {
            $gdrop .= 'class="active" ' ;
        } else {
            $gdrop .= 'class="notactive" ' ;
        } 

        if ($row['parent_id'] == 0) {
            $gdrop .= ' style="font-weight:bold;" ';
        } ;
        if ($row['id'] != $_REQUEST['id'] && $row['parent_id'] != $_REQUEST['id']) {
            if ($_GET['galid'] == $row['id']) {
                $gdrop .= "selected";
            } ;
            $gdrop .= '>';
        } 
        for($r = 1;$r < $i;$r++) {
            $gdrop .= '--';
        } 
        unset($all);
        $sqls = $GLOBALS['db']->Query("SELECT DISTINCT id FROM " . PREFIX . "_galerie_bilder WHERE gal_id='" . $row["id"] . "'");
        $all = $sqls->numrows();

        $gdrop .= ' ' . stripslashes($row["title"]) . ' [' . $all . ']' . '</option>';
        $gdrop .= galdropdown($row["id"], $i + 1, $url);
    } 
    return $gdrop;
}


function getGallery2($id, $area, $active, $orderby, $ascdesc, $sorting = "0")
{
    $LC = $_SESSION['Langcode'];

	$_REQUEST['orderby'] = (isset($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'ctime';
	$_REQUEST['ascdesc'] = (isset($_REQUEST['ascdesc'])) ? $_REQUEST['ascdesc'] : 'DESC';
	$_REQUEST['charakter'] = (isset($_REQUEST['charakter'])) ? $_REQUEST['charakter'] : 'all';
	
    switch ($_REQUEST['orderby']) {
        case "date" : $orderby = "ctime";
            break;
        case "author" : $orderby = "uid";
            break;
        case "title" : $orderby = "Name_{$LC}";
            break;
        default : $orderby = "ctime";
            break;
    } 

    switch ($_REQUEST['ascdesc']) {
        case "asc": $ascdesc = "ASC";
            break;
        case "desc": $ascdesc = "DESC";
            break;
        default : $ascdesc = "DESC";
            break;
    } 
    // auswahl nach alpha
    if ($sorting == 1) {
        switch ($_REQUEST['charakter']) {
            case "1" :
                for($i = 1;$i < 10;$i++) {
                    $loop .= " OR Name_{$LC} LIKE '" . $i . "%'";
                } 
                $c_sort = " AND(Name_{$LC} LIKE '0%' $loop)";
                break;

            case "" :
            case "all" :
                $c_sort = "";
                break;

            default :
                $c_sort = " AND(Name_{$LC} LIKE '" . $_REQUEST['charakter'] . "%')";
                break;
        } 

        $limit = limit();
        if ($active == 1) $zusatz = " AND active=1";

        $query = $GLOBALS['db']->Query("
				SELECT
					id
				FROM
					" . PREFIX . "_galerie
				WHERE
					parent_id = $id AND
					area = $area $c_sort 
			");

        $num = $query->numrows();
        $seiten = ceil($num / $limit);
        $a = prepage() * $limit - $limit;

        $gsql = "SELECT
					*,
					Name_{$LC} AS title 
				FROM
					" . PREFIX . "_galerie
				WHERE
					parent_id = $id AND
					area = $area
				$c_sort $zusatz 
				ORDER BY
					$orderby $ascdesc
				LIMIT $a, $limit";
        $query = $GLOBALS['db']->Query($gsql);

        if ($num > $limit) {
            $GLOBALS['GalNavi'] = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?do=sort&amp;p=gallery&amp;area=$_REQUEST[area]&amp;charakter=$_REQUEST[charakter]&amp;orderby=$_REQUEST[orderby]&amp;ascdesc=$_REQUEST[ascdesc]&amp;page={s}\">{t}</a> ");
        } else {
           $GLOBALS['GalNavi'] = '';
        } 
        // wenn admin
    } else {
        $query = $GLOBALS['db']->Query("
				SELECT
					*
				FROM
					" . PREFIX . "_galerie
				WHERE
					parent_id = $id AND
					area = $area  $zusatz 
				ORDER BY $orderby $ascdesc");
    } 

    if (!$query->numrows()) return;
    $posi = "";
    $gallery = array();

    while ($item = $query->fetchrow()) {
       
        $sql = $GLOBALS['db']->Query("SELECT id, path FROM " . PREFIX . "_galerie_bilder WHERE  gal_id='" . $item->id . "' limit 1");
        $row_g = $sql->fetchrow();

		if(file_exists('cache/small_' . $row_g->path))
		{
			$item->small_thumb = '<img src="cache/small_' . $row_g->path . '" border="0" alt="" />';
		} else {
			$item->small_thumb = '<img src="system/thumb.php?img_id=' . $row_g->id . '&amp;smode=1" border="0" alt="" />';
		}
		
        if (is_file("uploads/galerie/" . $row_g->path)) {
            $item->galthumb = $row_g->id;
        } else {
            $item->galthumb = 987654321;
        } 

        $item->ulink = "index.php?area=" . $area . "&amp;p=user&amp;id=" . $item->uid . "";
        $item->glink = "index.php?area=$area&amp;p=gallery&amp;action=showimages&amp;galid=" . $item->id . "";
        $item->visible_title = @$prefix . " " . $item->title;
        $item->created = stime($item->ctime);
        $item->user = fetchusername($item->uid);
       	$sql = $GLOBALS['db']->Query("SELECT distinct id FROM " . PREFIX . "_galerie_bilder WHERE gal_id = $item->id ");
        $item->pics = $sql->numrows(); 
        // untergalerien ===========================================
        $s_query = "
				SELECT
					g.id,
					g.Name_{$LC} AS title
				FROM
					" . PREFIX . "_galerie AS g
				WHERE
					g.parent_id = " . $item->id;

        $result = $GLOBALS['db']->Query($s_query);

        $subgalleries = array();

        while ($subgallery = $result->fetchrow()) {
            $p_query = "SELECT distinct id FROM " . PREFIX . "_galerie_bilder WHERE gal_id = " . $subgallery->id;
            $p_result = $GLOBALS['db']->Query($p_query);
            $subgallery->pics = $p_result->numrows();
            $subgalleries[] = $subgallery;
        } 

        $item->subgalleries = $subgalleries; 
        // =========================================================
        array_push($gallery, $item);
    } 
    return $gallery;
} 

?>