<?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('galeria','');
?>      

        <div class="obsah">            
<?php
  if ($_GET['kat'] == '') $_GET['kat'] = '0';
  if ($_GET['aid'] == '') $_GET['aid'] = '0';
  $sql = "SELECT DISTINCT kategoria FROM kpf_photos ORDER BY kategoria ASC;";
  $result = mysql_query($sql, $link);
  if ($result) {
    $i = 1;
    while ($row = mysql_fetch_assoc($result)) {
      if (($_GET['kat'] == '0')) $kat = ''; 
      elseif ($i == $_GET['kat']) {
      $pole[$i]=$row['kategoria'];
      $kat = $pole[$i];}
      $i++;
      echo '';      
    }
  }

  		if (isset($_GET['id'])) {
              if (isset($_POST["submit"])){
                  if (isset($_POST["body"]) && ($_POST["body"] != "") OR
                      isset($_POST["text"]) && ($_POST["text"] != "")){
                        $sql = "INSERT INTO kpf_hodnot (hid, aid, id, body, text, cas) VALUES (NULL, '".$_SESSION['aid']."', '".$_GET['id']."', '" . $_POST["body"] . "', '" . nl2br($_POST["text"]) . "', NOW());";
                        $result = mysql_query($sql, $link);	
           		          if ($result) {
           		          
                          $sql2 = "SELECT A.mail, A.rss_body
                                   FROM kpf_author A, kpf_photos P
                                   WHERE P.aid=A.aid AND P.id=".$_GET['id'].";";
                          $result2 = mysql_query($sql2, $link);
                          if ($result2) {
                              $row2 = mysql_fetch_assoc($result2);
                              if ($row2['rss_body'] == '1'){
                              $mail = $row2['mail'];
                              $f_id = $_GET['id'];
                              mail("$mail", "Nove hodnotenie na sedf-klub.sk", 
                                   "Mily priatel fotografie:)\n\nNa klubovej stranke mas nove hodnotenie ku fotke:\nhttp://sedf-klub.sk/photo.php?id=".$f_id."\n\nAk nechces dostavať informacie o novych hodnoteniach klikni na http://sedf-klub.sk/nastav.php.\n\nS pozdravom\nJano Duriga", "from: kpf@sedf-klub.sk");
                              }
                          }
           		          
                          header("Location: photo.php?id=". $_GET['id']."");
                        } else {
                             echo '<p style="color: #FF0000;">Fotografia sa už vo fotogalérii nachádza.</p>';                
                       }     
                      }
              }                                                                                 
        			$sql = "SELECT P.*, A.meno, A.priezvisko, A.aid, sum(H.body) AS body, UNIX_TIMESTAMP(P.pridane) AS 'dat'
                      FROM kpf_photos P, kpf_author A, kpf_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, A.aid, 0 AS body, UNIX_TIMESTAMP(P.pridane) AS 'dat'
                      FROM kpf_photos P, kpf_author A, kpf_hodnot H
                      WHERE P.aid=A.aid AND P.id=".$_GET['id']." AND P.id NOT IN (SELECT HH.id FROM kpf_hodnot HH);"; 
          		$result = mysql_query($sql, $link);
        			if ($result) {
        			   if ($row = mysql_fetch_assoc($result)) {
        			    $id_a = $row['aid'];
                  $sql2 = "SELECT DISTINCT kategoria FROM kpf_photos ORDER BY kategoria ASC;";
                  $result2 = mysql_query($sql2, $link);
                  if ($result2) {
                    $i = 1;
                    while ($row2 = mysql_fetch_assoc($result2)) {
                      if ($row2['kategoria'] == $row['kategoria']){
                      $k = $i;
                      }
                      $i++;
                    }
                  }
                  if ($kat != '')
                  $sql7 = "SELECT P.id
                          FROM kpf_photos P
                          WHERE P.kategoria='".$kat."'
                          ORDER BY pridane DESC
                          LIMIT 1;";
                  elseif ($_GET['aid'] != '0')
                  $sql7 = "SELECT P.id
                          FROM kpf_photos P
                          WHERE P.aid='".$_GET['aid']."'
                          ORDER BY pridane DESC
                          LIMIT 1;";
                  else 
                  $sql7 = "SELECT P.id
                          FROM kpf_photos P
                          ORDER BY pridane DESC
                          LIMIT 1;";
                  $result7 = mysql_query($sql7, $link);
                  if ($result7) {
                    $row7 = mysql_fetch_assoc($result7);
                    if ($row7['id'] == $_GET['id']){
                      $next = '0';
                    } else {
                        $exist = false;
                        $next = $_GET['id'];
                        do {
                          $next++;
                          if ($kat != '')
                          $sql3 = "SELECT P.id
                                   FROM kpf_photos P
                                   WHERE P.kategoria='".$kat."' AND P.id=".$next.";";
                          elseif ($_GET['aid'] != '0')
                          $sql3 = "SELECT P.id
                                   FROM kpf_photos P
                                   WHERE P.aid=".$_GET['aid']." AND P.id=".$next.";"; 
                          else
                          $sql3 = "SELECT P.id
                                   FROM kpf_photos P
                                   WHERE P.id=".$next.";";                                    
                          $result3 = mysql_query($sql3, $link);
                          if ($result3) {
                            $row3 = mysql_fetch_assoc($result3);
                            if ($row3['id'] == $next) $exist = true;
                          }
                        } while ($exist == false);
                    }
                  } else echo 'sibe ti?'.$kat;
                  
                  if ($kat != '')
                  $sql7 = "SELECT P.id
                          FROM kpf_photos P
                          WHERE P.kategoria='".$kat."'
                          ORDER BY pridane ASC
                          LIMIT 1;";
                  elseif ($_GET['aid'] != '0')
                  $sql7 = "SELECT P.id
                          FROM kpf_photos P
                          WHERE P.aid='".$_GET['aid']."'
                          ORDER BY pridane ASC
                          LIMIT 1;";
                  else
                  $sql7 = "SELECT P.id
                          FROM kpf_photos P
                          ORDER BY pridane ASC
                          LIMIT 1;";
                  $result7 = mysql_query($sql7, $link);
                  if ($result7) {
                    $row7 = mysql_fetch_assoc($result7);
                    if ($row7['id'] == $_GET['id']){
                      $prev = '0';
                    } else {
                        $exist = false;
                        $prev = $_GET['id'];
                        do {
                          $prev--;
                          if ($kat != '')
                          $sql3 = "SELECT P.id
                                   FROM kpf_photos P
                                   WHERE P.kategoria='".$kat."' AND P.id=".$prev.";";
                          elseif ($_GET['aid'] != '0')
                          $sql3 = "SELECT P.id
                                   FROM kpf_photos P
                                   WHERE P.aid=".$_GET['aid']." AND P.id=".$prev.";";   
                          else
                          $sql3 = "SELECT P.id
                                   FROM kpf_photos P
                                   WHERE 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."&kat=".$_GET['kat']."&aid=".$_GET['aid']."'><img src='images/la.png' alt='' /></a>";
                  if ($prev != '0') echo "<a href='photo.php?id=".$prev."&kat=".$_GET['kat']."&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'/>";
                  
                  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?kat='.$k.'">' .$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_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_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_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');
      ?>
