ACC SHELL

Path : /srv/www/vhosts/centrumlb/administrace/libs/
File Upload :
Current File : /srv/www/vhosts/centrumlb/administrace/libs/fotogalerie.class.php

<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2010
 * @name Zpracování kateogorií
 */
class Fotogalerie extends Tools
{

    public $idFotogalerie = array();
    public $idPopisu = array();
    public $nazev = array();
    public $seoNazev = array();
    public $seoUrl = array();
    public $title = array();
    public $keywords = array();
    public $description = array();
    public $stav = array();
    public $anotace = array();
    public $obsah = array();
    public $lang = array();
    public $typ = array();
    public $exturl = array();
    public $datum = array();
    public $currentpage = "";
    public $totalpages = "";
    public $pagesize = "10";
    public $defaultPicture = 'img1.jpg';

    /**
     * Constructor
     */
    function __construct()
    {
	
    }

    public function getNumRows($query)
    {
	//$query = "SELECT id FROM fotogalerie";
	$result = mysql_query($query);

	if ($result)
	{
	    return mysql_num_rows($result);
	} else
	    return false;
    }

    public function getAdminTable($id = 0, $table = 'fotogalerie')
    {
	if (isset($_SESSION['listuj']))
	    $this->pagesize = $_SESSION['listuj'];

	$recordstart = (isset($_GET['recordstart'])) ? (int) $_GET['recordstart'] : 0;
	$query = "SELECT a.id, a.hloubka, b.nazev AS nazev, b.seo_url, a.parent_id, a.lft, a.rgt, b.stav AS stav, b.datum
                        FROM {$table} a
                            LEFT JOIN {$table}_popis b ON a.id = b.main_id
                        WHERE b.lang  = '" . $_SESSION['jazyk'] . "'
                        ORDER BY a.lft ASC";

	$result = mysql_query($query);
	//$this->debug($query);

	$num_rows = 0;
	if ($result)
	{
	    $num_rows = mysql_num_rows($result);
	}

	if ($num_rows > $this->pagesize)
	{

	    $query .= " LIMIT $recordstart, $this->pagesize";
	    $result = mysql_query($query);
	    $this->totalpages = ceil($num_rows / $this->pagesize);
	    $this->currentpage = ($recordstart / $this->pagesize) + 1;
	}

	if ($num_rows == 0)
	{
	    echo "<tr><td colspan='8'>Nejsou založeny žádné fotogalerie</td></tr>";
	} else
	{
	    //Zjisteni minimalni a maximalni hodnoty pro kazdou kategorii
	    $query = "SELECT parent_id, min(lft) lft, max(rgt) rgt FROM {$table} group by parent_id";
	    //$this->debug($query);
	    $test = mysql_query($query);
	    while ($test1 = mysql_fetch_assoc($test))
	    {
		$showMove[$test1['parent_id']]['lft'] = $test1['lft'];
		$showMove[$test1['parent_id']]['rgt'] = $test1['rgt'];
	    }
	    //$this->debug($showMove);
	    $adresa_fotogalerie = mysql_fetch_array(mysql_query("SELECT * FROM kategorie_popis WHERE typ=2 LIMIT 1"));

	    while ($row = mysql_fetch_assoc($result))
	    {

		echo "<tr>";
		echo '<td><input type="checkbox" name="checkbox[]" value="' . $row['id'] . '" id="" ></td>';
		$fotka = $this->getHlavniFotografie($row['id']);

		$foto = FOTO_F_ADMIN_PATH . $fotka[0] . "." . $fotka[1];
		if (!is_file(ROOT . $foto))
		{
		    $foto = FOTO_F_S_PATH . $fotka[0] . "." . $fotka[1];
		}
		//NDebugger::dump(is_file(ROOT . $foto));
		echo '<td>';
		if (!empty($fotka) && is_file(ROOT . $foto))
		{
		    $foto_large = FOTO_F_L_PATH . $fotka[0] . "." . $fotka[1];
		    echo '<a rel="galerie" href="' . $foto_large . '"><img class="vypis_nahled" src="' . $foto . '" alt="" /></a>';
		}
		echo '</td>';

		echo "<td><a href=\"{$table}_edit.php?id={$row['id']}\">" . $row['nazev'] . "</a><br />";

		$query = "SELECT kp.seo_url, kp.lang, j.nazev, j.obrazek, j.kod
                            FROM {$table}_popis kp
                            LEFT JOIN jazyk j ON j.id = kp.lang
                            WHERE kp.main_id = $row[id] AND kp.stav ='1'
                            ORDER BY j.id";
		$langs = dibi::query($query)->fetchAll();
		//$langs = mysql_query($query);
		foreach ($langs as $n => $r)
		{
		    $adresa_galerie = dibi::query('SELECT url FROM kategorie_popis WHERE typ=2 AND lang = %i', $r->lang, ' LIMIT 1')->fetchSingle();
		    if (empty($adresa_galerie))
		    {
			$adresa_galerie = 'neni-zalozeno';
		    }
		    //NDebugger::barDump($adresa_galerie, 'Lang');
		    echo '<span class="url">';
		    if ($r['lang'] == "1")
		    {

			echo 'http://' . SERVER_NAME . '/' . $adresa_galerie . '/' . $r['seo_url'] . '.html';
		    } else
		    {
			echo 'http://' . SERVER_NAME . '/' . $r['kod'] . '/' . $adresa_galerie . '/' . $r['seo_url'] . '.html';
		    }

		    echo "</span><br />";
		}
		echo "</td>";

		/*
		 * Prirazeni fotogalerie jednotlivym kateogriiim
		 */
		echo '<td>';
		$query = "SELECT cp.nazev cp_nazev, kp.nazev kp_nazev, cpr.id cpr_id, cpr.id_fotogalerie, cpr.id_kategorie
                        FROM {$table}_prirazeni cpr
                        LEFT JOIN {$table}_popis cp ON cp.main_id = cpr.id_fotogalerie
                        LEFT JOIN kategorie_popis kp ON kp.main_id = cpr.id_kategorie
                        WHERE cpr.id_fotogalerie = '" . intval($row['id']) . "'
                          ORDER BY cpr.id";
		$result_prirazeni = mysql_query($query);
		while ($prirazeni = mysql_fetch_array($result_prirazeni))
		{
		    echo "<a href=\"kategorie_edit.php?id={$prirazeni['id_kategorie']}\">" . $prirazeni['kp_nazev'] . "</a> ";
		}
		echo '</td>';

		echo '<td>' . $this->getPocetFotografii($row['id']) . '</td>';

		// jazykove mutace
		$flags = Query::getLanguageFlags($row['id'], 'fotogalerie');
		echo "<td>";
		foreach ($flags as $f)
		{
		    echo "<img class=\"vypis_jazyky\" src=\"images/flags/$f->obrazek\" alt=\"$f->nazev\" title=\"$f->nazev\" />";
		}
		echo "</td>";
		// end of jazykove mutace

		$stav = $row['stav'] == '1' ? "Ano" : "Ne";
		echo "<td><a href=\"?id={$row['id']}&amp;stav=$stav\">$stav</a></td>";

		echo "<td>" . datum($row['datum']) . "</td>";

		if ($showMove[$row['parent_id']]['lft'] != $row['lft'])
		{
		    echo "<td><a href=\"?up=" . $row['id'] . "\" title=\"Posun nahoru\"><img src=\"images/iko/up.png\" alt=\"Posun nahoru\" /></a>";
		} else
		{
		    echo "<td>";
		}

		if ($showMove[$row['parent_id']]['rgt'] != $row['rgt'])
		{
		    echo "<a href=\"?down=" . $row['id'] . "\" title=\"Posun dolů\"><img src=\"images/iko/down.png\" alt=\"Posun dolů\" /></a>";
		} else
		{
		    echo "</td>";
		}

		echo "<td><a href=\"{$table}_edit.php?id=" . $row['id'] . "\" title=\"Editovat\"><img src=\"images/iko/note_edit.png\" alt=\"Editovat\" /></a>";
		echo '<a href="http://' . SERVER_NAME . '/' . PROJEKT . 'galerie/' . $adresa_fotogalerie['seo_url'] . "/" . $row['seo_url'] . '.html" target="_blank" title="Prohlédnout"><img src="images/iko/computer.png" alt="Prohlédnout" /></a>';

		echo '<a href="?smazat=' . $row['id'] . '" class="potvrzujiciLink"  title="Smazat"><img src="images/iko/remove.png" alt="Smazat" /></a></td>';

		echo "</tr>";
	    }
	}
    }

    public function getFotogalerieOption($table = 'fotogalerie', $id = '0')
    {

	$query = "SELECT a.id, b.nazev AS nazev, a.hloubka
                    FROM {$table} a
                    LEFT JOIN {$table}_popis b ON a.id = b.main_id WHERE b.lang  = '" . $_SESSION['jazyk'] . "'";

	if ($table == 'kategorie')
	{
	    $query .= " AND hloubka > 0";
	}
	$query .= " ORDER BY a.lft ASC";

	//$this->debug($query);
	printf("<option value=\"default\">---</option>\n");

	if (self::getNumRows($query) != '0')
	{
	    $result = mysql_query($query);

	    while ($row = mysql_fetch_assoc($result))
	    {
		$selected = '';
		if ($row['id'] == $id)
		    $selected = 'selected';
		$nazev = str_repeat('&nbsp;', $row['hloubka'] * 3) . $row['nazev'];
		printf("<option name=\"%d\" id=\"%d\" value=\"%d\" $selected>%s</option>\n", $row['id'], $row['id'], $row['id'], $nazev);
	    }
	    if (isset($result))
	    {
		mysql_free_result($result);
	    }
	}
    }

    public function getTreeTable($id = 0, $kategorie = 'fotogalerie')
    {
	$query = "SELECT k.id, k.hloubka, kp.nazev AS nazev, k.parent_id, k.lft, k.rgt, kt.nazev AS typ, kp.stav AS stav
                    FROM {$kategorie} k
                    	LEFT JOIN {$kategorie}_popis kp ON k.id = kp.main_id
                    	LEFT JOIN {$kategorie}_typ kt ON kp.typ = kt.id
                    WHERE kp.lang  = '" . $_SESSION['jazyk'] . "'
                    ORDER BY k.lft ASC";

	//$this->debug($query);

	$result = mysql_query($query);

	if (mysql_num_rows($result) == 0)
	{
	    echo "<tr>";
	    echo "<td>Nejsou založeny žádné fotogalerie.</td>";
	    echo "</tr>";
	} else
	{
	    //Zjisteni minimalni a maximalni hodnoty pro kazdou kategorii
	    $query = "SELECT parent_id, min(lft) lft, max(rgt) rgt FROM {$kategorie} group by parent_id";
	    //$this->debug($query);
	    $test = mysql_query($query);
	    while ($test1 = mysql_fetch_assoc($test))
	    {
		$showMove[$test1['parent_id']]['lft'] = $test1['lft'];
		$showMove[$test1['parent_id']]['rgt'] = $test1['rgt'];
	    }
	    //$this->debug($showMove);
	    while ($row = mysql_fetch_assoc($result))
	    {

		echo "<tr>";
		echo "<td><input type=\"checkbox\" name=\"checkbox[]\" value=\"" . $row['id'] . "\" id=\"\" ></td>";
		echo "<td>" . str_repeat("&nbsp;", $row['hloubka'] * 3) . "<a href=\"{$kategorie}_edit.php?id={$row['id']}\">" . $row['nazev'] . "</a></td>";

		echo "<td>" . $row['typ'] . "</td>";
		echo "<td><a href=\"#\">";
		echo $row['stav'] == '1' ? "ANO" : "NE";
		echo "</a></td>";
		echo "<td>29.3.2010</td>";

		if ($showMove[$row['parent_id']]['lft'] != $row['lft'])
		{
		    echo "<td>[<a href=\"?up=" . $row['id'] . "\">nahoru</a>]";
		    //echo "<td>[<a class=\"zvysit\" href=\"?up=" . $row['id'] . "\"></a>]</td>";
		} else
		{
		    echo "<td>";
		}

		if ($showMove[$row['parent_id']]['rgt'] != $row['rgt'])
		{
		    echo "[<a href=\"?down=" . $row['id'] . "\">dolu</a>]</td>";
		    //echo "<td><a class=\"snizit\" href=\"?down=" . $row['id'] . "\"></a></td>";
		} else
		{
		    echo "</td>";
		}
		echo "<td>[<a href=\"?smazat=" . $row['id'] . "\" onclick=\"return window.confirm('Opravdu vymazat?')\">smazat</a>]";
		echo "[<a href=\"{$kategorie}_edit.php?id=" . $row['id'] . "\">editovat</a>]</td>";


		echo "</tr>";
	    }
	}
    }

    /*
     * @params int id - id fotogalerie
     * @params int idkategorie - id kategorie do ktere priradim galerii
     */

    public function priraditFotogalerii($id, $idkategorie, $lang)
    {

	$id = intval($id);
	$idkategorie = intval($idkategorie);
	$lang = intval($lang);

	if ($idkategorie != "")
	{
	    $pocet = dibi::query('
		SELECT id_fotogalerie
		FROM fotogalerie_prirazeni
		WHERE id_fotogalerie = %i', $id, '
		    AND id_kategorie = %i', $idkategorie, ' LIMIT 1');
	    if (count($pocet) > '0')
	    {
		Tools::redirect('Fotogalerie už je této kategorii přiřazena.', 'INFO');
	    } else
	    {
		//$query = "INSERT INTO clanky_prirazeni (id_clanku, id_kategorie, jazyk) VALUES($idclanku, $idkategorie, $lang)";
		$query = "INSERT INTO fotogalerie_prirazeni (id_fotogalerie, id_kategorie) VALUES($id, $idkategorie)";
		NDebugger::barDump($query, 'Prirazeni fotogalerie');
		//exit;
		$insert = dibi::query($query);

		if (!$insert)
		{
		    Tools::redirect('Fotogalerii se nepodařilo přiřadit.', 'ERR');
		}
	    }
	}
    }

    public function getPrirazeni($id = 0, $table = 'fotogalerie')
    {
	if (isset($_SESSION['listuj']))
	    $this->pagesize = $_SESSION['listuj'];

	$this->pagesize = 10;
	$recordstart = (isset($_GET['recordstart'])) ? (int) $_GET['recordstart'] : 0;

	$query = "SELECT cp.id, cp.seo_url, cp.seo_nazev, cp.anotace, cp.nazev galerie_nazev,
	    kp.nazev kategorie_nazev, cpr.id cpr_id, cpr.id_fotogalerie, cpr.id_kategorie
                        FROM fotogalerie_prirazeni cpr
                        LEFT JOIN fotogalerie_popis cp ON cp.main_id = cpr.id_fotogalerie
                        LEFT JOIN kategorie_popis kp ON kp.main_id = cpr.id_kategorie
                        WHERE cpr.id_fotogalerie = '" . intval($id) . "'
                          ORDER BY cpr.id";
//Multilang -> AND cpr.jazyk = cp.lang
	//dibi::test($query);

	$result = dibi::query($query)->fetchAll();

	$num_rows = count($result);

	if ($num_rows > $this->pagesize)
	{

	    $query .= " LIMIT $recordstart, $this->pagesize";
	    $result = dibi::query($query)->fetchAll();
	}

	//NDebugger::barDump($result, 'Prirazeni');
	//echo "<br />celkem zaznamu: " . $num_rows . "<br />\n";

	$this->totalpages = ceil($num_rows / $this->pagesize);
	//echo "celkem stranek: " . $totalpages . "<br />\n";

	$this->currentpage = ($recordstart / $this->pagesize) + 1;

	return $result;
    }

    public function getPocetFotografii($id = '0')
    {
	$query = "SELECT id FROM fotografie";

	if ($id != '0')
	{
	    $query .= " WHERE id_fotogalerie = '" . intval($id) . "'";
	}
	$result = mysql_query($query);
	if ($result)
	{
	    return mysql_num_rows($result);
	} else
	    return false;
    }

    public function getHlavniFotografie($id = '0')
    {
	$query = "SELECT nazev, pripona FROM fotografie WHERE id_fotogalerie = '" . intval($id) . "' AND hlavni='1'";
	$result = mysql_query($query);
	//$this->debug($query);
	if ($result)
	{
	    $result = mysql_fetch_row($result);
	    return $result;
	}
	return false;
    }

    /**
     * @todo refaktor it / nemuze presmerovavat z tridy
     */
    public function setHlavniFotografie($id = '0', $id_fotogalerie = '0')
    {
	$ID_FOTKY = intval($id);
	$ID_GALERIE = intval($id_fotogalerie);

	if (!$ID_GALERIE)
	{
	    Tools::message('SYS ERR - Není zadáno id galerie.', 'ERR');
	    return false;
	}
	//NDebugger::dump($ID_FOTKY);NDebugger::dump($ID_GALERIE);exit;
	if (!$ID_FOTKY && $ID_GALERIE != '0')
	{
	    dibi::query('UPDATE fotografie SET hlavni = 0 WHERE id_fotogalerie = %i', $ID_GALERIE);
	    $rand_id = dibi::query('SELECT id FROM fotografie WHERE id_fotogalerie = %i', $ID_GALERIE, ' ORDER BY rand() LIMIT 1')->fetchSingle();
	    dibi::query('UPDATE fotografie SET hlavni = 1 WHERE id_fotogalerie = %i', $ID_GALERIE, ' AND id = %i', $rand_id);
	    Tools::message('Smazali jste hlavní fotografii. Jako hlavní tedy byla náhodně vybrána nová fotografie.', 'INFO');
	} elseif ($ID_FOTKY != '0' && $ID_GALERIE != '0')
	{
	    mysql_query("START TRANSACTION");
	    dibi::query('UPDATE fotografie SET hlavni = 0 WHERE id_fotogalerie = %i', $ID_GALERIE);
	    dibi::query('UPDATE fotografie SET hlavni = 1 WHERE id_fotogalerie = %i', $ID_GALERIE, ' AND id = %i', $ID_FOTKY);
	    mysql_query("COMMIT");
	}

	return true;
    }

    public function getFotografie($id = '0', $admin = true)
    {
	if ($admin)
	{
	    $query = "SELECT id, nazev, nazev_fotografie, pripona, hlavni, stav, alt, title FROM fotografie WHERE id_fotogalerie = '" . intval($id) . "'";
	} else
	{
	    $query = "SELECT a.id, a.nazev, nazev_fotografie, a.pripona, a.hlavni, a.stav, a.title, a.alt
                        FROM fotografie a
                        LEFT JOIN fotogalerie_popis b
                            ON a.id_fotogalerie = b.main_id
                        WHERE b.seo_url = '" . $id . "' AND a.stav=1";
	}

	return dibi::query($query)->fetchAll();
    }

    public function getFotografieInfo($id = '0', $lang = false)
    {

	if ($lang)
	{
	    $query = "
		SELECT a.id, a.id_fotogalerie, a.nazev, a.nazev_fotografie, a.pripona, a.hlavni, a.stav, b.alt, b.title
		FROM fotografie a
		LEFT JOIN fotografie_popis b ON a.id = b.id_fotografie
		WHERE a.id = '" . intval($id) . "' AND b.lang='" . $lang . "'";
	} else
	{
	    $query = "SELECT id, id_fotogalerie, nazev, nazev_fotografie, pripona, hlavni, stav, alt, title FROM fotografie WHERE id = '" . intval($id) . "'";
	}

	//dibi:query()->fetchAll();
	$result = mysql_query($query);

	if (!empty($result) && mysql_num_rows($result) != '0')
	{
	    while ($row = mysql_fetch_array($result))
	    {
		$fotografie['id'][] = $row['id'];
		$fotografie['id_fotogalerie'][] = $row['id_fotogalerie'];
		$fotografie['nazev'][] = $row['nazev'];
		$fotografie['nazev_fotografie'][] = $row['nazev_fotografie'];
		$fotografie['pripona'][] = $row['pripona'];
		$fotografie['hlavni'][] = $row['hlavni'];
		$fotografie['stav'][] = $row['stav'];
		$fotografie['alt'][] = $row['alt'];
		$fotografie['title'][] = $row['title'];
	    }

	    return $fotografie;
	} else
	    return false;
    }

    public function getLastFotografie($lang = 1, $count = '1', $table = false)
    {

	$query = "SELECT a.id, a.nazev, nazev_fotografie, a.pripona, a.hlavni, a.stav, a.title, a.alt, b.seo_url, c.kod
                        FROM fotografie a
                        LEFT JOIN fotogalerie_popis b
                            ON a.id_fotogalerie = b.main_id
                        LEFT JOIN jazyk c
                            ON c.id = b.lang
                        WHERE a.stav=1
                            AND b.stav = 1
                            AND b.lang  = '" . $lang . "'
                        ORDER BY a.datum DESC
                        LIMIT $count";

	$result = dibi::query($query);
	if ($result)
	{
	    return $result->fetchAll();
	} else
	    return false;
    }

    public function getRandomFotografie($lang = 1, $count = 1)
    {

	$query = "SELECT a.id, a.nazev, nazev_fotografie, a.pripona, a.hlavni, a.stav, a.title, a.alt, b.seo_url, c.kod
                        FROM fotografie a
                        LEFT JOIN fotogalerie_popis b
                            ON a.id_fotogalerie = b.main_id
                        LEFT JOIN jazyk c
                            ON c.id = b.lang
                        WHERE a.stav=1
                            AND b.stav = 1
                            AND b.lang  = '" . $lang . "'
                        ORDER BY rand()
                        LIMIT $count";

	$result = dibi::query($query);
	if ($result)
	{
	    return $result->fetchAll();
	}
	return false;
    }

    function rotujFotku($id)
    {
	$query = "SELECT id, id_fotogalerie, nazev, nazev_fotografie, pripona, hlavni, stav, alt, title FROM fotografie WHERE id = '" . intval($id) . "' LIMIT 1";

	$result = mysql_query($query);
	$this->debug("Dotaz na fotku: " . $query, true);
	$fotka = mysql_fetch_array($result);

	$size_of_picture = array('large', 'medium', 'small');

	foreach ($size_of_picture as $size)
	{
	    $image = new SimpleImage();

	    $target_path = $_SERVER['DOCUMENT_ROOT'] . PROJEKT . "/fotky/$size/" . $fotka['nazev'] . "." . $fotka['pripona'];

	    $image->load($target_path);
	    $image->rotate();
	    $image->save($target_path);
	}
    }

    public function zakazatCelouKategorii($id = '-1', $kategorie = 'fotogalerie')
    {
	$query = "UPDATE {$kategorie}_popis SET stav = '0' WHERE id_{$kategorie} = '" . intval($id) . "'";
	//$this->debug($query);
	$result = mysql_query($query);
    }

    public function zakazatVsechnyFotografie($id = '-1', $kategorie = 'fotografie')
    {
	$query = "UPDATE {$kategorie} SET stav = '0' WHERE id = '" . intval($id) . "'";
	$result = mysql_query($query);
	//$this->debug($query);
    }

    public function povolitCelouKategorii($id = '-1', $kategorie = 'fotogalerie')
    {
	$query = "UPDATE {$kategorie}_popis SET stav = '1' WHERE id_{$kategorie} = '" . intval($id) . "'";
	$result = mysql_query($query);
	//$this->debug($query);
    }

    public function povolitVsechnyFotografie($id = '-1', $kategorie = 'fotografie')
    {
	$query = "UPDATE {$kategorie} SET stav = '1' WHERE id = '" . intval($id) . "'";
	//$this->debug($query);
	$result = mysql_query($query);
    }

    public function zrusitPrirazeni($id, $table = 'fotogalerie')
    {
	$query = "DELETE FROM {$table}_prirazeni WHERE id = '" . intval($id) . "'";
	//$this->debug($query);
	$result = mysql_query($query);

	if ($result)
	{
	    $_SESSION['message_ok'][] = "Zrušení přiřazení proběhlo v pořádku.";
	    Tools::redirect($_SERVER['HTTP_REFERER']);
	} else
	{
	    $_SESSION['message_ko'][] = "Zrušení přiřazení se nepovedlo.";
	    Tools::redirect($_SERVER['HTTP_REFERER']);
	}
    }

    public function smazatFotku($id, $idgalerie)
    {
	$file = dibi::query('SELECT * FROM fotografie WHERE id=%i', $id)->fetch();
	//ndebugger::dump($file);exit;
	if (dibi::delete('fotografie')->where('id=%i', $file->id)->execute())
	{
	    dibi::delete('fotografie_popis')->where('id_fotografie=%i', $file->id)->execute();
	    unlink(ROOT . FOTO_F_ADMIN_PATH . "$file->id.jpg");
	    unlink(ROOT . FOTO_F_S_PATH . "$file->id.jpg");
	    unlink(ROOT . FOTO_F_M_PATH . "$file->id.jpg");
	    unlink(ROOT . FOTO_F_L_PATH . "$file->id.jpg");
	    if ($file->hlavni == 1)
	    {
		$this->setHlavniFotografie(false, $file->id_fotogalerie);
	    }
	    Tools::message('Vymazání fotografie proběhlo v pořádku.', 'OK');
	} else
	{
	    Tools::message('Vymazání fotografie se nepovedlo.', 'ERR');
	}
    }

    public function smazatVsechnyFotografie($id, $idgalerie = "")
    {
	if (dibi::delete('fotografie')->where('id=%i', $id)->execute())
	{
	    dibi::delete('fotografie_popis')->where('id_fotografie=%i', $id)->execute();
	    unlink(ROOT . FOTO_F_ADMIN_PATH . "$id.jpg");
	    unlink(ROOT . FOTO_F_S_PATH . "$id.jpg");
	    unlink(ROOT . FOTO_F_M_PATH . "$id.jpg");
	    unlink(ROOT . FOTO_F_L_PATH . "$id.jpg");
	} else
	{
	    Tools::message('Vymazání fotografie se nepovedlo.', 'ERR');
	}
	//Tools::redirect($_SERVER['HTTP_REFERER']);
    }

    public function getDetail($id = '', $lang = false, $table = 'fotogalerie')
    {
	if (!$lang)
	    $lang = $_SESSION['jazyk'];
	$query = "
            SELECT a.id AS idFotogalerie, b.id AS idPopisu, b.id AS id_popisu, a.hloubka, b.nazev AS nazev, b.seo_nazev, b.seo_url,
                        b.title AS title, c.nazev AS jazyk, c.obrazek, c.kod, b.fblike, b.komentare,
                        b.keywords, b.description, b.stav, b.anotace, b.obsah, b.typ, b.exturl, b.datum
            FROM {$table} a
            LEFT JOIN {$table}_popis b ON a.id = b.main_id
            LEFT JOIN jazyk c ON c.id = b.lang
            WHERE b.seo_url = '" . $id . "'
                AND b.lang  = '" . $lang . "'
		AND c.status  = '1'
            LIMIT 1;
            ";
	return dibi::query($query)->fetch();
    }

    public function getDetailAdmin($id = '', $lang = false)
    {
	if (!$lang)
	    $lang = $_SESSION['jazyk'];
	$query = "
            SELECT a.id AS idFotogalerie, b.id AS idPopisu, a.hloubka, b.nazev AS nazev, b.seo_nazev, b.seo_url,
                        b.title AS title, c.nazev AS jazyk, c.obrazek, c.kod,b.komentare, b.fblike,
                        b.keywords, b.description, b.stav, b.anotace, b.obsah, b.typ, b.exturl, b.datum
            FROM fotogalerie a
            LEFT JOIN fotogalerie_popis b ON a.id = b.main_id
            LEFT JOIN jazyk c ON c.id = b.lang
            WHERE b.main_id = '" . $id . "'
                AND b.lang  = '" . $lang . "'
		AND c.status  = '1'
            LIMIT 1;
            ";
	return dibi::query($query)->fetch();
    }

    public function getInfo($id = '', $lang = -1, $table = 'fotogalerie', $admin = '')
    {
	//$id = intval($id);
	if ($lang == '-1')
	    $lang = $_SESSION['jazyk'];
	if ($id == 'index')
	{
	    $find = "a.id='1'";
	}
	if ($id == '')
	{
	    $find = "b.stav = '1'";
	} elseif ($admin == 'admin')
	{
	    $find = "a.id = '$id'";
	} else
	{
	    $find = "b.seo_url = '$id'";
	}
	$query = "SELECT a.id AS idFotogalerie, b.id AS idPopisu, a.hloubka, b.nazev AS nazev, b.seo_nazev, b.seo_url,
                        b.title AS title, c.nazev AS jazyk, c.obrazek, c.kod,
                        b.keywords, b.description, b.stav, b.anotace, b.obsah, b.typ, b.exturl, b.datum
                    FROM {$table} a
                    LEFT JOIN {$table}_popis b ON a.id = b.main_id
                    LEFT JOIN jazyk c ON c.id = b.lang
                    WHERE $find
                        AND b.lang  = '" . $lang . "'
			AND c.status  = '1'
                        ";
	//echo 'Fotogalerie sql -> ' . $query;
	//$this->debug('Fotogalerie GetInfo -> ' . $query, true);

	$result = mysql_query($query);
	if (mysql_num_rows($result) != 0)
	{
	    while ($row = mysql_fetch_assoc($result))
	    {
		$this->idFotogalerie = $row['idFotogalerie'];
		$this->idPopisu = $row['idPopisu'];
		$this->nazev = $row['nazev'];
		$this->seoNazev = $row['seo_nazev'];
		$this->seoUrl = $row['seo_url'];
		$this->title = $row['title'];
		$this->anotace = $row['anotace'];
		$this->obsah = $row['obsah'];
		$this->stav = $row['stav'];
		$this->exturl = $row['exturl'];
		$this->datum = $row['datum'];
		$this->typ = $row['typ'];
	    }
	} else
	{
	    $this->idFotogalerie = '';
	    $this->idPopisu = '';
	    $this->nazev = '';
	    $this->seoNazev = '';
	    $this->seoUrl = '';
	    $this->title = '';
	    $this->keywords = '';
	    $this->description = '';
	    $this->anotace = '';
	    $this->obsah = '';
	    $this->stav = '';
	    $this->typ = '';
	    $this->extrul = '';
	    $this->datum = '';
	}
    }

    public function getInfoAdmin($id = '', $lang = -1, $table = 'fotogalerie', $admin = '')
    {
	//$id = intval($id);
	if ($lang == '-1')
	    $lang = $_SESSION['jazyk'];
	if ($id == 'index')
	{
	    $find = "a.id='3'";
	}
	if ($id == '')
	{
	    $find = "b.stav = '1'";
	} elseif ($admin == 'admin')
	{
	    $find = "a.id = '$id'";
	} else
	{
	    $find = "b.seo_url = '$id'";
	}
	$query = "SELECT a.id AS idFotogalerie, b.id AS idPopisu, a.hloubka, b.nazev AS nazev, b.seo_nazev, b.seo_url,
                        b.title AS title, c.nazev AS jazyk, c.obrazek, c.kod,
                        b.keywords, b.description, b.stav, b.anotace, b.obsah, b.typ, b.exturl, b.datum
                    FROM {$table} a
                    LEFT JOIN {$table}_popis b ON a.id = b.main_id
                    LEFT JOIN jazyk c ON c.id = b.lang
                    WHERE $find
                        AND b.lang  = '" . $lang . "'
			AND c.status  = '1'
                        ";

	$this->debug($query, true);

	$result = mysql_query($query);
	if (mysql_num_rows($result) != 0)
	{
	    while ($row = mysql_fetch_assoc($result))
	    {
		array_push($this->idFotogalerie, $row['idFotogalerie']);
		array_push($this->idPopisu, $row['idPopisu']);
		array_push($this->nazev, $row['nazev']);
		array_push($this->seoNazev, $row['seo_nazev']);
		array_push($this->seoUrl, $row['seo_url']);
		array_push($this->title, $row['title']);
		array_push($this->anotace, html_entity_decode($row['anotace']));
		array_push($this->obsah, html_entity_decode($row['obsah']));
		array_push($this->stav, $row['stav']);
		array_push($this->exturl, $row['exturl']);
		array_push($this->datum, $row['datum']);
		array_push($this->typ, $row['typ']);
	    }
	} else
	{
	    $this->idFotogalerie = '';
	    $this->idPopisu = '';
	    $this->nazev = '';
	    $this->seoNazev = '';
	    $this->seoUrl = '';
	    $this->title = '';
	    $this->keywords = '';
	    $this->description = '';
	    $this->anotace = '';
	    $this->obsah = '';
	    $this->stav = '';
	    $this->typ = '';
	    $this->extrul = '';
	    $this->datum = '';
	}
    }

    public function fetchAll($arr)
    {
	if (isset($arr['pagesize']))
	{
	    $this->pagesize = $arr['pagesize'];
	}
	$lang = isset($arr['lang']) ? $arr['lang'] : '1';

	$recordstart = (isset($_GET['recordstart'])) ? (int) $_GET['recordstart'] : 0;

	$query = "SELECT a.id, a.hloubka, b.nazev AS nazev, a.parent_id, a.lft, a.rgt,
		b.stav AS stav, b.lang, b.datum, b.seo_nazev, b.seo_url, b.anotace, b.obsah, j.kod
                FROM fotogalerie a
                LEFT JOIN fotogalerie_popis b ON a.id = b.main_id
                LEFT JOIN jazyk j ON b.lang = j.id
                WHERE b.lang  = '" . $arr['lang'] . "'
                AND b.stav = '1'
                ORDER BY a.lft ASC";

	$rows = dibi::query($query);

	$pocetZaznamu = count($rows);

	if ($pocetZaznamu > $this->pagesize)
	{

	    $this->totalpages = ceil($pocetZaznamu / $this->pagesize);

	    $this->currentpage = ($recordstart / $this->pagesize) + 1;

	    return $rows->fetchAll($recordstart, $this->pagesize);
	} else
	{
	    return $rows->fetchAll();
	}
    }

    public function fetchPrazeneGalerie($arr)
    {
	if (isset($arr['pagesize']))
	{
	    $this->pagesize = $arr['pagesize'];
	}
	$lang = isset($arr['lang']) ? $arr['lang'] : '1';

	$recordstart = (isset($_GET['recordstart'])) ? (int) $_GET['recordstart'] : 0;

	$query = "SELECT a.id, a.hloubka, b.nazev AS nazev, a.parent_id, a.lft, a.rgt, b.stav AS stav, b.datum, b.seo_nazev, b.seo_url, b.anotace, b.obsah, j.kod
                FROM fotogalerie a
                LEFT JOIN fotogalerie_popis b ON a.id = b.main_id
                LEFT JOIN fotogalerie_prirazeni c ON c.id_fotogalerie = b.main_id
                LEFT JOIN jazyk j ON b.lang = j.id
                WHERE b.lang  = '" . $lang . "'
                AND b.stav = '1'
                ORDER BY a.lft ASC";

	$rows = dibi::query($query);

	$pocetZaznamu = count($rows);
	//NDebugger::barDump($pocetZaznamu, 'PocetGalerii');
	//NDebugger::barDump($this->pagesize, 'Pagesize');
	//NDebugger::barDump($pocetZaznamu, 'Pocetzaznamu');
	if ($pocetZaznamu > $this->pagesize)
	{

	    $this->totalpages = ceil($pocetZaznamu / $this->pagesize);

	    $this->currentpage = ($recordstart / $this->pagesize) + 1;

	    return $rows->fetchAll($recordstart, $this->pagesize);
	} else
	{
	    return $rows->fetchAll();
	}
    }

}

$Fotogalerie = new Fotogalerie();

ACC SHELL 2018