<?php

include('../includes/hlava.php');
title('Klub priateľov fotografie', '../');

if ($link) {
  if ($db_selected) {
        		
include('../includes/vedla.php');
odkaz('../');
include('../includes/menu.php');
menu('tema', '../');    
?>
            <ul class="menu pmenu">
              <li><a href="index.php" title="zadanie fototémy">Úvod</a></li>
              <li><a href="galeria.php" class="aktual" title="galeria fototémy">Galéria</a></li>
              <li><a href="archiv.php" title="staré fototémy">Archív</a></li>
          
<?php
if (!(isset($_SESSION['login']) && ($_SESSION['login'] != ""))) :
// pouzivatel sa musi prihlasit, treba zobrazit prihlasovaci formular
?>
<?
else :
// pouzivatel je prihlaseny
    ?>
              <li class="medzera">&nbsp;</li>
              <li><a href="vloz.php" title="vloženie fotky do fototémy">Vložiť fotografiu</a></li>
    
<?php
endif; // kontrola SESSION
        	
?>                    
          
            </ul>
        <div class="obsah">    		
<?php
  		if (isset($_GET['id'])) {        		
              if (isset($_POST["submit"])){
                  if (isset($_POST["body"]) && ($_POST["body"] != "") OR
                      isset($_POST["text"]) && ($_POST["text"] != "")){
                        $sql = "INSERT INTO kpf_tema_hodnot (hid, aid, id, body, text, cas) VALUES (NULL, '".$_SESSION['aid']."', '".$_GET['id']."', '" . $_POST["body"] . "', '" . $_POST["text"] . "', NOW());";
                        $result = mysql_query($sql, $link);
           		          if ($result) {
                          header("Location: photo.php?id=". $_GET['id']."");
                        } else {
                             echo '<p style="color: #FF0000;">Fotografia sa už vo fotogalérii nachádza.</p>';                
                       }     
                      }
              }
              
              if ($_GET['aid'] == '') $_GET['aid'] = '0';
              if ($_GET['tem'] == '') $_GET['tem'] = '0'; 
              
        			$sql = "SELECT P.*, A.meno, A.priezvisko, sum(H.body) AS body, UNIX_TIMESTAMP(P.pridane) AS 'dat'
                      FROM kpf_tema_photos P, kpf_author A, kpf_tema_hodnot H
                      WHERE H.id=P.id AND P.aid=A.aid AND P.id=".$_GET['id']." 
                      GROUP BY A.priezvisko
                      UNION
                      SELECT P.*, A.meno, A.priezvisko, 0 AS body, UNIX_TIMESTAMP(P.pridane) AS 'dat'
                      FROM kpf_tema_photos P, kpf_author A, kpf_tema_hodnot H
                      WHERE P.aid=A.aid AND P.id=".$_GET['id']." AND P.id NOT IN (SELECT HH.id FROM kpf_tema_hodnot HH);"; 
          		$result = mysql_query($sql, $link);
        			if ($result) {
        			   if ($row = mysql_fetch_assoc($result)) {
        			     $subor = $row['foto'];
        			     
              $sql2 = "SELECT P.id, P.tema
                       FROM kpf_tema_photos P
                       WHERE P.tema='".$_GET['tem']."'
                       ORDER BY pridane DESC
                       LIMIT 1;";
              $result2 = mysql_query($sql2, $link);
              if ($result2) {
                $row2 = mysql_fetch_assoc($result2);
                if ($row2['id'] == $_GET['id'])
                  $next = '0';
                else {
                  $exist = false;
                  $next = $_GET['id'];
                  do {
                    $next++;
                    $sql3 = "SELECT P.id
                            FROM kpf_tema_photos P
                            WHERE P.tema=".$_GET['tem']." AND P.id=".$next.";";
                    $result3 = mysql_query($sql3, $link);
        			      if ($result3) {
                      $row3 = mysql_fetch_assoc($result3);
                      if ($row3['id'] == $next) $exist = true;  
                    }
                  } while ($exist == false);
                  
                }
              }   			     
              $sql2 = "SELECT P.id, P.tema
                       FROM kpf_tema_photos P
                       WHERE P.tema='".$_GET['tem']."'
                       ORDER BY pridane ASC
                       LIMIT 1;";
              $result2 = mysql_query($sql2, $link);
              if ($result2) {
                $row2 = mysql_fetch_assoc($result2);
                if ($row2['id'] == $_GET['id'])
                  $prev = '0';
                else {
                  $exist = false;
                  $prev = $_GET['id'];
                  do {
                    $prev--;
                    $sql3 = "SELECT P.id
                            FROM kpf_tema_photos P
                            WHERE P.tema=".$_GET['tem']." AND P.id=".$prev.";";
                    $result3 = mysql_query($sql3, $link);
        			      if ($result3) {
                      $row3 = mysql_fetch_assoc($result3);
                      if ($row3['id'] == $prev) $exist = true;  
                    }
                  } while ($exist == false);
                  
                }
              }
        			     
        			     
                  echo "<p class='sipky'>";
                  if ($next == '0') echo "<a href='photo.php?id=".$next."' class='skri'><img src='../images/la.png' alt='' /></a>";
                  else echo "<a href='photo.php?id=".$next."&tem=".$_GET['tem']."&aid=".$_GET['aid']."'><img src='../images/la.png' alt='' /></a>";
                  if ($prev != '0') echo "<a href='photo.php?id=".$prev."&tem=".$_GET['tem']."&aid=".$_GET['aid']."'><img src='../images/ra.png' alt='' /></a></p>
                  ";
                  else echo '</p>';        			     
        			     
        			     echo "<img src='photos/".$row['foto']."' alt='".$row['nazov']."' title='".$row['nazov']."' class='fotka' />";
?>
      
                  <?php echo '<h3>' .$row['nazov'].'</h3>';
                        echo '<p class="infofotka">';
                        echo '<em>autor:</em> <a href="../author.php?id='.$row['aid'].'" class="fmeno">' .$row['meno'].' '.$row['priezvisko'].'</a><br />';
                        echo '<em>kategória:</em> <a href="galeria.php">' .$row['kategoria'].'</a><br /><br />';
                        echo '<em>pridaná:</em> '.date("d.m.Y, H:i:s", $row['dat']).'<br />';
                        echo '<em>body:</em> ' .$row['body'].'</p>';?>     
                  <p class="infotech"><em>technické informácie:</em><br />
                  <?php echo $row['tech'];?> <br /></p>
                  <p class="infotech">
                  <em>poznámky:</em><br />
                  <?php echo $row['pozn'];?></p>    
          <hr />
          <p class="clearf"></p>
            
<?php
  if ($_SESSION['aid'] != '') {
    $label = false;

?>

    <form class="login" action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post" enctype="multipart/form-data">
      <p>

<?php
  $sql = "SELECT P.aid
          FROM kpf_tema_photos P
          WHERE P.id=".$_GET['id'].";";
  $result = mysql_query($sql, $link);
  if ($result) {
    $row = mysql_fetch_assoc($result);
    $id_autor = $row['aid'];
  } 

  $sql = "SELECT H.body
          FROM kpf_tema_hodnot H
          WHERE H.id=".$_GET['id']." AND H.aid=".$_SESSION['aid']."
          ORDER BY body DESC;";
  $result = mysql_query($sql, $link);
  if ($result) {
    $row = mysql_fetch_assoc($result);
    if(($row['body'] == '0') OR ($row['body'] == '')) $label = true; 
  }

if (($_SESSION['aid'] != $id_autor) AND $label) {
?>

        <label>1 <input name="body" value="1" type="radio"/></label>
        <label>2 <input name="body" value="2" type="radio"/></label>
        <label>3 <input name="body" value="3" type="radio"/></label>
        <label>4 <input name="body" value="4" type="radio"/></label>
        <label>5 <input name="body" value="5" type="radio"/></label>
        <textarea name="text" cols="45" rows="4"></textarea>

<?php
} else 
        echo'<textarea class="texta" name="text" cols="45" rows="4"></textarea>';
?>

        <input class="ok" name="submit" type="submit" value="posli"/>
      </p>
    </form> 
            
<?php
      $sql = "SELECT H.*, A.meno, A.priezvisko, A.afotka, UNIX_TIMESTAMP(H.cas) AS 'dat'
              FROM kpf_tema_hodnot H, kpf_author A
              WHERE H.id=".$_GET['id']." AND A.aid=H.aid
              ORDER BY H.cas DESC;";
		$result = mysql_query($sql, $link);
        		  if ($result) {
        		  $pocet = mysql_num_rows($result);
        		  if ($pocet > 0) {   
                   		     
                while ($row = mysql_fetch_assoc($result)) {
                  echo '<p class="hodnot"><a href="../author.php?id='.$row['aid'].'">';
                  echo "<img src='../photos/authors/".$row['afotka']."' alt='".$row['meno'].' '.$row['priezvisko']."' class='ico'/></a>";
                  if ($row['body']!='0') {
                  echo '<b class="b'.$row['body'].'">'.$row['body'].'</b>'; }
                  else {                  
                  echo '';}
                  echo '<a href="../author.php?id='.$row['aid'].'">'.$row['meno'].' '.$row['priezvisko'].'</a>';
                  echo ' <span class="date">'.date("d.m.Y H:i:s", $row['dat']).'</span><br />';
                  echo '<span class="text">'.$row['text'].'</span>';
                  echo '</p>
                  ';
                 }
               } else {
              }
        			mysql_free_result($result);
        		} else {
        			echo '<p style="color: #FF0000;">Chyba pri získavaní údajov z databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';
        		}
        		}
           }
        	} else {
        	  echo '<p style="color: #FF0000;">Fotografia sa nenachádza vo fotogalérii.</p>';
        	}
        		} // end vyber db
        		
        		
        		
        		mysql_close($link);
        	} // end spojenie s db
      } else {
        //neboli zadane vsetky potrebne udaje
        echo "<p style='color: #FF0000;'>Nebolo odoslané ID fotografie, ktorú treba zobraziť.</p>";
      }
include('../includes/pata.php');
      ?>
