<?php
$druh = "komentare";
include "head.php";
include_head($lang['comments_title'], $druh);

if (!isset($_GET['action'])) {$_GET['action'] = "default";};
switch ($_GET['action']) {

case "edit" : 
$id = $_GET['id'];
$druh = $_GET['druh']; 

echo '<h1>'.$lang['comments_edit_comment'].'</h1>';
echo '<br />';

 if (empty($id) or ($druh != "clanky" && $druh != "stranky")) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['comments_er_1'].'</p>';};
    if ($druh != "clanky" && $druh != "stranky") {echo '<p>'.$lang['comments_er_2'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
  switch ($druh) {
    case "clanky" : $query = mysql_query("SELECT * FROM jtcms_komentare_cl WHERE id='".$id."' LIMIT 1"); break;
    case "stranky" : $query = mysql_query("SELECT * FROM jtcms_komentare_st WHERE id='".$id."' LIMIT 1"); break;   
  };
  
  $result = mysql_fetch_array($query);
  $radku = mysql_num_rows($query);
  
  switch ($druh) {
    case "clanky" : $query_prava = mysql_query("SELECT pridal FROM jtcms_clanky WHERE id='".$result['clanek']."' LIMIT 1"); break;
    case "stranky" : $query_prava = mysql_query("SELECT pridal FROM jtcms_stranky WHERE id='".$result['stranka']."' LIMIT 1"); break;   
  };
  
  $result_prava = mysql_fetch_array($query_prava);
  
 if ($radku == 0 or ($prava == 2 && $result_prava['pridal'] != $_SESSION['userid'])) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if ($radku == 0) {echo '<p>'.$lang['comments_er_3'].'</p>';};
    if ($prava == 2 && $result_prava['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['comments_er_12'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
  

if (!empty($_POST)) {
  $jmeno = htmlspecialchars($_POST['jmeno']);
  $mail = htmlspecialchars($_POST['mail']);
  $web = htmlspecialchars($_POST['web']);
  $obsah = htmlspecialchars($_POST['obsah']);
  
  if (!empty($jmeno) && !empty($obsah)) {
    $jmeno = mysql_real_escape_string($jmeno);
    $mail = mysql_real_escape_string($mail);
    $web = mysql_real_escape_string($web);
    $obsah = mysql_real_escape_string($obsah);
    
    switch ($druh) {
      case "clanky" : mysql_query("UPDATE jtcms_komentare_cl SET jmeno='".$jmeno."', mail='".$mail."', web='".$web."', obsah='".$obsah."' WHERE id='".$id."' LIMIT 1"); break;
      case "stranky" : mysql_query("UPDATE jtcms_komentare_st SET jmeno='".$jmeno."', mail='".$mail."', web='".$web."', obsah='".$obsah."' WHERE id='".$id."' LIMIT 1"); break;
    };
    Header("Location: comments.php?work=edited&jmeno=".rawurlencode($jmeno));
    echo '<script type="text/javascript">location.replace("comments.php?work=edited&jmeno='.rawurlencode($jmeno).'")</script>';
    
  } else {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if (empty($jmeno)) {echo '<li>'.$lang['comments_er_4'].'</li>';};
    if (empty($obsah)) {echo '<li>'.$lang['comments_er_5'].'</li>';};
    echo '</div>';  
  };
};  
?>

<form method="post" action="comments.php?action=edit&amp;id=<?php echo $id; ?>&amp;druh=<?php echo $druh; ?>" id="comment_form">
<label for="kom_jmeno"><?php echo $lang['comments_name']; ?></label><input type="text" name="jmeno" id="kom_jmeno" class="input" value="<?php if (empty($_POST['jmeno'])) {echo $result['jmeno'];} else {echo $_POST['jmeno'];}; ?>" /><br />
<label for="kom_mail"><?php echo $lang['comments_mail']; ?></label><input type="text" name="mail" id="kom_mail" class="input" value="<?php if (empty($_POST['mail'])) {echo $result['mail'];} else {echo $_POST['mail'];}; ?>" /><br />
<label for="kom_web"><?php echo $lang['comments_web']; ?></label><input type="text" name="web" id="kom_web" class="input" value="<?php if (empty($_POST['web'])) {echo $result['web'];} else {echo $_POST['web'];}; ?>" /><br />
<label for="kom_obsah"><?php echo $lang['comments_content']; ?></label><br />
<textarea wrap="hard" name="obsah" id="kom_obsah"><?php if (empty($_POST['obsah'])) {echo $result['obsah'];} else {echo $_POST['obsah'];}; ?></textarea>
<br /><br />
<div class="tac">
<input type="submit" name="submit" value="<?php echo $lang['comments_edit_comment']; ?>" class="submit" />
</div>
</form>

<?php
break;

case "search" :

echo '<h1>'.$lang['comments_search_comments'].'</h1>';
echo '<br />';

?>
<form method="post" action="comments.php?action=search" id="comment_form">
<div class="tac">
  <input type="radio" id="kom_druh_cl" name="druh" value="jtcms_komentare_cl"<?php if (!empty($_POST['druh'])) {if ($_POST['druh'] == "jtcms_komentare_cl") {echo ' checked';};} else {echo 'checked';}; ?> /><label for="kom_druh_cl" class="nlabel"><?php echo $lang['comments_type_1']; ?></label>
  &nbsp;&nbsp;&nbsp;
  <input type="radio" id="kom_druh_st" name="druh" value="jtcms_komentare_st"<?php if (!empty($_POST['druh'])) {if ($_POST['druh'] == "jtcms_komentare_st") {echo ' checked';};}; ?> /><label for="kom_druh_st" class="nlabel"><?php echo $lang['comments_type_2']; ?></label>
</div>
<br />
<label for="kom_id"><?php echo $lang['comments_id']; ?></label><input type="text" name="id" id="kom_id" class="input"<?php if (!empty($_POST['id'])) {echo ' value="'.htmlspecialchars($_POST['id']).'"';}; ?> /><br />
<label for="kom_ip"><?php echo $lang['comments_ip']; ?></label><input type="text" name="ip" id="kom_ip" class="input"<?php if (!empty($_POST['ip'])) {echo ' value="'.htmlspecialchars($_POST['ip']).'"';}; ?> /><br />
<label for="kom_jmeno"><?php echo $lang['comments_name']; ?></label><input type="text" name="jmeno" id="kom_jmeno" class="input"<?php if (!empty($_POST['jmeno'])) {echo ' value="'.htmlspecialchars($_POST['jmeno']).'"';}; ?> /><br />
<label for="kom_mail"><?php echo $lang['comments_mail']; ?></label><input type="text" name="mail" id="kom_mail" class="input"<?php if (!empty($_POST['mail'])) {echo ' value="'.htmlspecialchars($_POST['mail']).'"';}; ?> /><br />
<label for="kom_web"><?php echo $lang['comments_web']; ?></label><input type="text" name="web" id="kom_web" class="input"<?php if (!empty($_POST['web'])) {echo ' value="'.htmlspecialchars($_POST['web']).'"';}; ?> /><br />
<br />
<div class="tac">
  <input type="submit" name="submit" value="<?php echo $lang['comments_search_comments']; ?>" class="submit" />
</div>
</form>
<?php
if (!empty($_POST)) {
  $druh = $_POST['druh'];
  $id = htmlspecialchars($_POST['id']);
  $ip = htmlspecialchars($_POST['ip']);
  $jmeno = htmlspecialchars($_POST['jmeno']);
  $mail = htmlspecialchars($_POST['mail']);
  $web = htmlspecialchars($_POST['web']);
  
  if (!empty($druh) && (!empty($id) || !empty($ip) || !empty($jmeno) || !empty($mail) || !empty($web))) {
    
    $hledani = "";
    if (!empty($id)) {$hledani .= "id='".mysql_real_escape_string($id)."' and ";};
    if (!empty($ip)) {$hledani .= "ip='".mysql_real_escape_string($ip)."' and ";};
    if (!empty($jmeno)) {$hledani .= "jmeno='".mysql_real_escape_string($jmeno)."' and ";};
    if (!empty($mail)) {$hledani .= "mail='".mysql_real_escape_string($mail)."' and ";};
    if (!empty($web)) {$hledani .= "web='".mysql_real_escape_string($web)."' and ";};
    
    $hledani = preg_replace("~ and $~", "", $hledani);
   
    $query = mysql_query("SELECT * FROM ".$druh." WHERE ".$hledani." ORDER BY id DESC");
    $radku = mysql_num_rows($query);
    
    if ($radku == 0) {
      echo '<br /><br />';
      echo '<div class="kom_strankovani">';
      echo '<p>'.$lang['comments_er_11'].'</p>';
      echo '</div>';
    } else {
    
    while ($result = mysql_fetch_array($query)) { 
      show_comment("jtcms_komentare_cl", "jtcms_komentare_st");    
    };
    };      
  };
};

break;

case "delete" :
  $id = $_GET['id'];
  $druh = $_GET['druh'];
  
  echo '<h1>'.$lang['comments_delete_comment'].'</h1>';
  echo '<br />';
  
 if (empty($id) || ($druh != "clanky" && $druh != "stranky")) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['comments_er_1'].'</p>';};
    if ($druh != "clanky" && $druh != "stranky") {echo '<p>'.$lang['comments_er_2'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
  switch ($druh) {
    case "clanky" : $druh = "jtcms_komentare_cl"; break;
    case "stranky" : $druh = "jtcms_komentare_st"; break;
  };
  
  $query = mysql_query("SELECT * FROM ".$druh." WHERE id='".$id."' LIMIT 1");
  $radku = mysql_num_rows($query);
  $result = mysql_fetch_array($query);
  $jmeno = $result['jmeno'];
  
    switch ($druh) {
      case "jtcms_komentare_cl" : $opravneni_query = mysql_query("SELECT pridal FROM jtcms_clanky WHERE id='".$result['clanek']."' LIMIT 1"); break;
      case "jtcms_komentare_st" : $opravneni_query = mysql_query("SELECT pridal FROM jtcms_stranky WHERE id='".$result['stranka']."' LIMIT 1"); break;
    };
    
    $opravneni = mysql_fetch_array($opravneni_query);
    $opravneni = $opravneni['pridal']; 
  
  if ($radku == 0 or ($prava == 2 && $opravneni != $_SESSION['userid'])) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if ($radku == 0) {echo '<p>'.$lang['comments_er_3'].'</p>';};
    if ($prava == 2 && $opravneni != $_SESSION['userid']) {echo '<p>'.$lang['comments_er_12'].'</p>';};
    echo '</div>';
  } else {  
    mysql_query("DELETE FROM ".$druh." WHERE id='".$id."' LIMIT 1");
    Header("Location: comments.php?work=deleted&jmeno=".rawurlencode($jmeno));
    echo '<script type="text/javascript">location.replace("comments.php?work=edited&jmeno='.rawurlencode($jmeno).'")</script>';  
  };
  
break;

case "article-page" :

$id = $_GET['id'];
$druh = $_GET['druh'];

 if (empty($id) || ($druh != "clanky" && $druh != "stranky")) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['comments_er_1'].'</p>';};
    if ($druh != "clanky" && $druh != "stranky") {echo '<p>'.$lang['comments_er_2'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
  switch ($druh) {
    case "clanky" : $mysql_from = "jtcms_komentare_cl WHERE clanek='".$id."'"; break;
    case "stranky" : $mysql_from = "jtcms_komentare_st WHERE stranka='".$id."'"; break;   
  };
 
 if ($druh == "clanky") {$query = mysql_query("SELECT nazev FROM jtcms_clanky WHERE id='".$id."'");} else {$query = mysql_query("SELECT nazev FROM jtcms_stranky WHERE id='".$id."'");};
 $radku = mysql_num_rows($query);
 $nazev = mysql_fetch_array($query);

 if ($radku == 0) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['comments_error'].'</div>';
    if ($druh == "clanky") {echo '<p>'.$lang['comments_er_6'].'</p>';};
    if ($druh == "stranky") {echo '<p>'.$lang['comments_er_7'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  }; 
  
 $query = mysql_query("SELECT * FROM ".$mysql_from." ORDER BY datum DESC");
 $radku = mysql_num_rows($query);

    if ($druh == "clanky") {echo '<h1><small>'.$lang['comments_for_article'].'</small> '.$nazev['nazev'].'</h1>';} else {echo '<h1><small>'.$lang['comments_for_page'].'</small> '.$nazev['nazev'].'</h1>';};
    echo '<br />';
     
    if ($radku == 0) {
      echo '<br /><br />';
      echo '<div class="kom_strankovani">';
      echo '<p>'.$lang['comments_er_8'].'</p>';
      echo '</div>';
    } else {
    
    while ($result = mysql_fetch_array($query)) {
      show_comment("clanky", "stranky");    
    };
};    
break;

default : 

echo '<h1>'.$lang['comments_title'].'</h1>';
echo '<br />';

if (!isset($_GET['work'])) {$_GET['work'] = "default";};
switch ($_GET['work']) {
case "edited" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['comments_work_1'].' <span class="blue">'.stripslashes($_GET['jmeno']).'</span> '.$lang['comments_work_2'].'</p>';
echo '</div>';
break;

case "deleted" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['comments_work_1'].' <span class="blue">'.stripslashes($_GET['jmeno']).'</span> '.$lang['comments_work_3'].'</p>';
echo '</div>';
break;
};
?>

<a href="comments.php?action=search" class="pridat_tlacitko fl"><img src="stuff/img/icons/list.png" alt="+" /> <?php echo $lang['comments_search_comments']; ?></a>

<div class="nf"></div>
<br />
<h2 class="tac">
  <?php
  echo $lang['comments_last_comments'].' ';
    if (isset($_GET['komentare'])) { 
      if ($_GET['komentare'] == "clanky" or !isset($_GET['komentare'])) {echo '<a href="?komentare=clanky" class="kom_aktiv_a">'.$lang['comments_type_1'].'</a>';} else {echo '<a href="?komentare=clanky" class="kom_aktiv">'.$lang['comments_type_1'].'</a>';};
      echo "\n";
      if ($_GET['komentare'] == "stranky") {echo '<a href="?komentare=stranky" class="kom_aktiv_a">'.$lang['comments_type_2'].'</a>';} else {echo '<a href="?komentare=stranky" class="kom_aktiv">'.$lang['comments_type_2'].'</a>';};
    } else {
      echo '<a href="?komentare=clanky" class="kom_aktiv_a">'.$lang['comments_type_1'].'</a>';
      echo "\n";
      echo '<a href="?komentare=stranky" class="kom_aktiv">'.$lang['comments_type_2'].'</a>';
    };
    
echo '</h2>';
echo '<br />';


  $limit = mysql_query("SELECT hodnota FROM jtcms_nastaveni WHERE typ='pocet_kom_adm' LIMIT 1");
  $limit = mysql_fetch_array($limit);
  $limit = $limit['hodnota'];

    if (!isset($_GET['komentare'])) {$_GET['komentare'] = "default";};
    switch ($_GET['komentare']) {
    case "clanky" : $pocet = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_komentare_cl"); break;
    case "stranky" : $pocet = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_komentare_st"); break;
    default : $pocet = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_komentare_cl");
    };
    $pocet = mysql_fetch_array($pocet);
    $pocet = $pocet['pocet'];

  if ($pocet > $limit) {   
      $pocet_stran = $pocet/$limit;
  
    if (isset($_GET['strana'])) {
      $strana = $_GET['strana'];
    } else {
      $strana = 1;
    }; 
    
    if (!isset($_GET['komentare'])) {$_GET['komentare'] = "default";};
    switch ($_GET['komentare']) {      
    case "clanky" : $query = mysql_query("SELECT * FROM jtcms_komentare_cl ORDER BY id DESC LIMIT ".$limit." OFFSET ".($limit*($strana-1))); $druh = "clanky"; break;
    case "stranky" : $query = mysql_query("SELECT * FROM jtcms_komentare_st ORDER BY id DESC LIMIT ".$limit." OFFSET ".($limit*($strana-1))); $druh = "stranky"; break;
    default : $query = mysql_query("SELECT * FROM jtcms_komentare_cl ORDER BY id DESC LIMIT ".$limit." OFFSET ".($limit*($strana-1))); $druh = "clanky";
    };
  } else {
  if (!isset($_GET['komentare'])) {$_GET['komentare'] = "default";};
    switch ($_GET['komentare']) {      
    case "clanky" : $query = mysql_query("SELECT * FROM jtcms_komentare_cl ORDER BY id DESC"); $druh = "clanky"; break;
    case "stranky" : $query = mysql_query("SELECT * FROM jtcms_komentare_st ORDER BY id DESC"); $druh = "stranky"; break;
    default : $query = mysql_query("SELECT * FROM jtcms_komentare_cl ORDER BY id DESC"); $druh = "clanky";
    };
  };
  
  $radku = mysql_num_rows($query);
  
  if ($radku == 0) {
    echo '<div class="kom_strankovani">';
    if ($druh == "clanky") {echo $lang['comments_er_9'];};
    if ($druh == "stranky") {echo $lang['comments_er_10'];};
    echo '</div>';
  
  } else {

  echo '<div id="vypis"></div>';
  while ($result = mysql_fetch_array($query)) {
    show_comment("clanky", "stranky");
  };


  if ($pocet > $limit) {
    echo '<br /><br />';
    echo '<div class="kom_strankovani">';
    echo $lang['comments_go_to_page'].' ';
    for($c=1; $c<$pocet_stran+1; $c++) {
      if (empty($_GET['komentare'])) {
        echo '<a href="?strana='.$c.'#vypis"';
        if (($_GET['strana'] == $c) || (!isset($_GET['strana']) && $c == 1)) {echo ' class="aktiv"';};
        echo '>'.$c.'</a>'."\n";
      } else {
        echo '<a href="?komentare='.$_GET['komentare'].'&amp;strana='.$c.'#vypis"';
        if (($_GET['strana'] == $c) || (!isset($_GET['strana']) && $c == 1)) {echo ' class="aktiv"';};
        echo '>'.$c.'</a>'."\n";      
      };
    };
    echo '</div>';
    };
  };
};
?>
<script type="text/javascript">
function smaz_kom(id, jmeno, druh){
jConfirm("<?php echo $lang['comments_really_del_1']; ?> "+id+" <?php echo $lang['comments_really_del_2']; ?> "+jmeno+"?", "<?php echo $lang['comments_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("comments.php?action=delete&id="+id+"&druh="+druh);};
});
};

function ban_user(ip, jmeno){
jConfirm("<?php echo $lang['comments_really_del_3']; ?> "+ip+"<?php echo $lang['comments_really_del_4']; ?> "+jmeno+"?", "<?php echo $lang['comments_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("set_banlist.php?ip="+ip+"&duvod=<?php echo rawurlencode($lang['comments_really_default_reason']); ?>");};
});
};
</script>
<?php
include_end();
?>