<?php
if ($_GET['action'] == "add" or $_GET['action'] == "edit") {$tinymce = 1;} else {$tinymce = 2;};        
$druh = "clanky";
$meta = '<link rel="stylesheet" type="text/css" href="stuff/tools/fancybox/jquery.fancybox-1.3.4.css" media="screen" />';
$meta2 = '<script type="text/javascript" src="stuff/tools/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  $("#check_all").click(function () {
    if ($("#check_all").is(":checked")) {
      $(".stitek").attr("checked", true);
    } else {
      $(".stitek").removeAttr("checked");
    };
  });
});
</script>
';
$meta_end = '<script type="text/javascript" src="stuff/tools/fancybox/jt_cms_set.js"></script>';
include "head.php";
include_head($lang['articles_title'], $druh, $tinymce);

$dnesnidatum = StrFTime("%Y-%m-%d %H:%M:%S", Time());

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

case "add" :

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

if (!empty($_POST)) {

    $obrazek = $_FILES['obrazek'];
    $nazev = $_POST['nazev'];
    $kategorie = $_POST['kategorie'];
    $perex = $_POST['perex'];
    $obsah = $_POST['obsah'];
    $datum_pub = $_POST['datum_pub'];
    $status = $_POST['status'];
    $komentare = $_POST['komentare'];
    if (isset($_POST['stitky'])) {$stitky = $_POST['stitky'];};
    
  if (!empty($nazev) && !empty($perex) && !empty($datum_pub) && !empty($status) && !empty($komentare)) {
    
    if ($datum_pub == "hned") {
     $datum = "'".$dnesnidatum."'";
    } else {
     $datum = "'".$_POST['year']."-".$_POST['month']."-".$_POST['day']." ".$_POST['hour'].":".$_POST['minute'].":00'"; 
    };      
    
    if (!empty($stitky)) {$stitky = implode($stitky,";");};
    
    $nazev = mysql_real_escape_string($nazev);
    $kategorie = intval($kategorie);
    $perex = mysql_real_escape_string($perex);
    $obsah = mysql_real_escape_string($obsah);
    $status = intval($status);
    $komentare = intval($komentare);
    $stitky = mysql_real_escape_string($stitky);
    
    mysql_query("INSERT INTO jtcms_clanky (perex,obsah,datum,nazev,pocet,kategorie,pridal,kom_pov,status,stitky) VALUES ('".$perex."','".$obsah."',".$datum.",'".$nazev."','0','".$kategorie."','".$_SESSION['userid']."','".$komentare."','".$status."','".$stitky."')");
    
    if (!empty($obrazek) && preg_match("~image/(.*)~", $obrazek['type']) && is_file($obrazek['tmp_name'])) {
      $id = mysql_query("SELECT id FROM jtcms_clanky WHERE datum=".$datum." and nazev='".$nazev."' LIMIT 1");
      $id = mysql_fetch_array($id);
      $id = $id['id'];
    
      $img_druh = preg_replace("~^image/~", "", $obrazek['type']);
      move_uploaded_file($obrazek['tmp_name'], "./data/art_img/article_".$id.".".$img_druh);
      mysql_query("UPDATE jtcms_clanky SET obrazek='".mysql_real_escape_string("article_".$id.".".$img_druh)."' WHERE datum=".$datum." and nazev='".$nazev."' LIMIT 1");
    };

    Header("Location: articles.php?work=added&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("articles.php?work=added&nazev='.rawurlencode($nazev).'")</script>';
    
  } else {  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['articles_error'].'</div>';
    if (empty($nazev)) {echo '<li>'.$lang['articles_er_1'].'</li>';};
    if (empty($perex)) {echo '<li>'.$lang['articles_er_2'].'</li>';};
    if (empty($datum_pub)) {echo '<li>'.$lang['articles_er_3'].'</li>';};
    if (empty($status)) {echo '<li>'.$lang['articles_er_4'].'</li>';};
    if (empty($komentare)) {echo '<li>'.$lang['articles_er_5'].'</li>';};
    echo '</div>';
  };
  
  };
  
$query = mysql_query("SELECT * FROM jtcms_kategorie ORDER BY menu_por");
$dnesnidatum = StrFTime("%Y-%m-%d %H:%M:%S", Time());
?>

<form method="post" enctype="multipart/form-data" action="articles.php?action=add" id="clanek_form">
<div class="pole">
<label for="cl_nazev"><?php echo $lang['articles_name']; ?>:</label><input type="text" name="nazev" id="cl_nazev" class="input"<?php if (!empty($_POST['nazev'])) {echo ' value="'.htmlspecialchars($_POST['nazev']).'"';}; ?> /><br />
<label for="cl_obrazek" class="img_label"><?php echo $lang['articles_image']; ?>:</label><input type="file" id="cl_obrazek" min="1" max="1" name="obrazek" accept="image/*" class="file_input" /><br />
<label for="cl_kategorie"><?php echo $lang['articles_category']; ?>:</label>
<select name="kategorie" id="cl_kategorie" size="1" class="input"> 
<option value="0" />--- <?php echo $lang['articles_select_category']; ?> ---
<?php
while ($result = mysql_fetch_array($query)) {
echo '<option value="'.$result['id'].'"';
if (!empty($_POST['kategorie']) && $_POST['kategorie'] == $result['id']) {echo ' selected';}; 
echo ' />'.$result['nazev'];
};
?>
</select>
</div>
<br />
<br />
<label for="cl_perex"><?php echo $lang['articles_perex']; ?>:</label><br />
<textarea id="cl_perex" name="perex" wrap="hard" class="<?php if (!$tinymce_p) {echo 'perex';} else {echo 'perex_tiny';}; ?>"><?php if (!empty($_POST['perex'])) {echo htmlspecialchars($_POST['perex']);}; ?></textarea>
<br /><br />
<label for="cl_obsah"><?php echo $lang['articles_content']; ?>:</label><br />
<textarea id="cl_obsah" name="obsah" wrap="hard" class="<?php if (!$tinymce_p) {echo 'obsah';} else {echo 'obsah_tiny';}; ?>"><?php if (!empty($_POST['obsah'])) {echo htmlspecialchars($_POST['obsah']);}; ?></textarea>
<br /><br />


<div class="nastaveni_nadpis" onclick="$('.nastaveni').slideToggle();"><?php echo $lang['articles_extended_setting']; ?></div>
<div class="nastaveni_stitky_nadpis" onclick="$('.nastaveni_stitky').slideToggle();"><img src="stuff/img/icons/label.png" alt="" /> <?php echo $lang['articles_tags']; ?></div>

<div class="nastaveni_stitky">
<?php
  $query2 = mysql_query("SELECT * FROM jtcms_stitky ORDER BY id");
  $radku2 = mysql_num_rows($query2);
  
if ($radku2 == 0) {
  echo '<p>'.$lang['articles_no_tags'].'</p>';
} else {  
  while ($result2 = mysql_fetch_array($query2)) {
    echo '<div class="polozka">';
    echo '<input type="checkbox" id="stitek_'.$result2['id'].'" class="stitek" name="stitky[]" value="'.$result2['id'].'"';
    if (!empty($_POST) && in_array($result2['id'], $_POST['stitky'])) {echo ' checked';};
    echo ' />';
    echo '<label for="stitek_'.$result2['id'].'">'.$result2['nazev'].'</label>';
    echo '</div>';
  };
};
?>
  <div class="checkbox_check_all"><input type="checkbox" id="check_all" title="<?php echo $lang['articles_check_all_checkboxes']; ?>" /></div>
  <div class="nf"></div>
</div>
                                      
<div class="nastaveni">

<div class="nadpis"><img src="stuff/img/icons/date.png" alt="" /> <?php echo $lang['articles_date']; ?>:</div>
<div class="moznosti">
<input type="radio" name="datum_pub" id="datum_pub_hned" value="hned"<?php if (!empty($_POST)) {if ($_POST['datum_pub'] == "hned") {echo ' checked';};} else {echo ' checked';}; ?> /> <label for="datum_pub_hned"><?php echo $lang['articles_publish_immediately']; ?></label><br />
<input type="radio" name="datum_pub" value="jindy"<?php if (!empty($_POST) && $_POST['datum_pub'] == "jindy") {echo ' checked';}; ?> />
<select name="day" size="1"> 
<?php for($i=1;$i<32;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_datepub('day', $i, 'd');
echo ' />'.$i;
}; ?>
</select>
.
<select name="month" size="1"> 
<?php for($i=1;$i<13;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_datepub('month', $i, 'm');
echo ' />'.$i;
}; ?>
</select>
.
<select name="year" size="1"> 
<?php for($i=2012;$i<2021;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_datepub('year', $i, 'Y');
echo ' />'.$i;
}; ?>
</select>
-
<select name="hour" size="1"> 
<?php for($i=1;$i<24;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_datepub('hour', $i, 'H');
echo ' />'.$i;
}; ?>
</select>
:
<select name="minute" size="1"> 
<?php for($i=1;$i<60;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_datepub('minute', $i, 'M');
echo ' />'.$i;
}; ?>
</select>
<span class="popis">DD.MM.YYYY - HH:MM</span>
</div>

<div class="nf"></div>
<br />

<div class="nadpis"><img src="stuff/img/icons/globe.png" alt="" /> <?php echo $lang['articles_article_status']; ?>:</div>
<div class="moznosti">
<input type="radio" name="status" id="status1" value="1"<?php if (!empty($_POST)) {if ($_POST['status'] == 1) {echo ' checked';};} else {echo ' checked';}; ?> /> <label for="status1"><?php echo $lang['articles_status_1']; ?></label><br />
<input type="radio" name="status" id="status2" value="2"<?php if (!empty($_POST) && $_POST['status'] == 2) {echo ' checked';}; ?> /> <label for="status2"><?php echo $lang['articles_status_2']; ?></label><span class="popis"><?php echo $lang['articles_status_2_des']; ?></span><br />
<input type="radio" name="status" id="status3" value="3"<?php if (!empty($_POST) && $_POST['status'] == 3) {echo ' checked';}; ?> /> <label for="status3"><?php echo $lang['articles_status_3']; ?></label><span class="popis"><?php echo $lang['articles_status_3_des']; ?></span><br />
</div>

<div class="nf"></div>
<br />

<div class="nadpis"><img src="stuff/img/icons/comments.png" alt="" /> <?php echo $lang['articles_comments']; ?>:</div>
<div class="moznosti">
<input type="radio" name="komentare" id="komentare1" value="1"<?php if (!empty($_POST)) {if ($_POST['komentare'] == 1) {echo ' checked';};} else {echo ' checked';}; ?> /> <label for="komentare1"><?php echo $lang['articles_comments_1']; ?></label><br />
<input type="radio" name="komentare" id="komentare2" value="2"<?php if (!empty($_POST) && $_POST['komentare'] == 2) {echo ' checked';}; ?> /> <label for="komentare2"><?php echo $lang['articles_comments_2']; ?></label><br />
</div>

<div class="nf"></div>

</div>

<br /><br />
<div class="nf"></div>

<input type="submit" name="submit" value="<?php echo $lang['articles_save_article']; ?>" class="submit" />
</form>


<?php
break;

case "edit" : 
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_clanky WHERE id='".$id."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);

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

 if (empty($id) or $radku == 0 or ($prava == 2 && $result['pridal'] != $_SESSION['userid'])) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['articles_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['articles_er_e_1'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['articles_er_e_2'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['articles_er_e_3'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  

if (!empty($_POST)) {

    $obrazek = $_FILES['obrazek'];
    $nazev = $_POST['nazev'];
    $kategorie = $_POST['kategorie'];
    $perex = $_POST['perex'];
    $obsah = $_POST['obsah'];
    $datum_pub = $_POST['datum_pub'];
    $status = $_POST['status'];
    $komentare = $_POST['komentare'];
    $pocet = $_POST['pocet'];
    $pridal = $_POST['pridal'];
    if (isset($_POST['stitky'])) {$stitky = $_POST['stitky'];};
    
  if (!empty($nazev) && !empty($perex) && !empty($datum_pub) && !empty($status) && !empty($komentare) && $pocet != "" && !empty($pridal) && is_numeric($pocet)) {
    
    if ($datum_pub == "jindy") {
     $datum = "'".$_POST['year']."-".$_POST['month']."-".$_POST['day']." ".$_POST['hour'].":".$_POST['minute'].":00'";
     mysql_query("UPDATE jtcms_clanky SET datum=".$datum." WHERE id='".$id."' LIMIT 1"); 
    };
    
    if (!empty($stitky)) {$stitky = implode($stitky,";");};
    
    $perex = mysql_real_escape_string($perex);
    $obsah = mysql_real_escape_string($obsah);    
    $nazev = mysql_real_escape_string($nazev);
    $pocet = mysql_real_escape_string($pocet);
    $kategorie = mysql_real_escape_string($kategorie);
    $pridal = mysql_real_escape_string($pridal);
    $komentare = mysql_real_escape_string($komentare);
    $status = mysql_real_escape_string($status); 
    $stitky = mysql_real_escape_string($stitky); 
    
    mysql_query("UPDATE jtcms_clanky SET perex='".$perex."', obsah='".$obsah."', nazev='".$nazev."', pocet='".$pocet."', kategorie='".$kategorie."', pridal='".$pridal."', kom_pov='".$komentare."', status='".$status."', stitky='".$stitky."' WHERE id='".$id."' LIMIT 1");
    
    if (!empty($obrazek) && preg_match("~image/(.*)~", $obrazek['type']) && is_file($obrazek['tmp_name'])) {
      unlink("./data/art_img/".$result['obrazek']);   
      $img_druh = preg_replace("~^image/~", "", $obrazek['type']);
      move_uploaded_file($obrazek['tmp_name'], "./data/art_img/article_".$id.".".$img_druh);
      mysql_query("UPDATE jtcms_clanky SET obrazek='".mysql_real_escape_string("article_".$id.".".$img_druh)."' WHERE id='".$id."' LIMIT 1");
    };
    
    Header("Location: articles.php?work=edited&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("articles.php?work=edited&nazev='.rawurlencode($nazev).'")</script>';
    
  } else {  
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['articles_error'].'</div>';
    if (empty($nazev)) {echo '<li>'.$lang['articles_er_1'].'</li>';};
    if (empty($perex)) {echo '<li>'.$lang['articles_er_2'].'</li>';};
    if (empty($datum_pub)) {echo '<li>'.$lang['articles_er_3'].'</li>';};
    if (empty($status)) {echo '<li>'.$lang['articles_er_4'].'</li>';};
    if (empty($komentare)) {echo '<li>'.$lang['articles_er_5'].'</li>';};
    if ($pocet == "") {echo '<li>'.$lang['articles_er_6'].'</li>';};
    if (!is_numeric($pocet)) {echo '<li>'.$lang['articles_er_7'].'</li>';};
    if (empty($pridal)) {echo '<li>'.$lang['articles_er_8'].'</li>';};
    echo '</div>';
  };
  
  };  
  
  
$query_cat = mysql_query("SELECT * FROM jtcms_kategorie ORDER BY menu_por");
$query_user = mysql_query("SELECT id, jmeno FROM jtcms_uzivatele ORDER BY id");
$dnesnidatum = StrFTime("%Y-%m-%d %H:%M:%S", Time());
?>

<form method="post" enctype="multipart/form-data" action="articles.php?action=edit&amp;id=<?php echo $id; ?>" id="clanek_form">
<div class="pole">
<label for="cl_nazev"><?php echo $lang['articles_name']; ?>:</label><input type="text" name="nazev" id="cl_nazev" class="input" value="<?php if (empty($_POST['nazev'])) {echo htmlspecialchars($result['nazev']);} else {echo htmlspecialchars($_POST['nazev']);}; ?>" /><br />
<label for="cl_obrazek" class="img_label"><?php echo $lang['articles_image']; ?>:</label><input type="file" id="cl_obrazek" min="1" max="1" name="obrazek" accept="image/*" class="file_input" />
<?php if(!empty($result['obrazek']) && file_exists("./data/art_img/".$result['obrazek'])) {echo '<span class="obrazek"><a id="single_image" href="./data/art_img/'.$result['obrazek'].'">'.$result['obrazek'].'</a> <img src="stuff/img/icons/delete.png" alt="'.$lang['articles_delete_image'].'" title="'.$lang['articles_delete_image'].'" class="smaz" onclick="smaz_img(\''.$result['id'].'\')" /></span>';}; ?>
<br />
<label for="cl_kategorie"><?php echo $lang['articles_category']; ?>:</label>
<select name="kategorie" id="cl_kategorie" size="1" class="input"> 
<option value="0" />--- <?php echo $lang['articles_select_category']; ?> ---
<?php
while ($result_cat = mysql_fetch_array($query_cat)) {
echo '<option value="'.$result_cat['id'].'"';
if ($result['kategorie'] == $result_cat['id']) {echo ' selected';};
echo ' />'.$result_cat['nazev'];
};
?>
</select>
</div>
<br />
<br />
<label for="cl_perex"><?php echo $lang['articles_perex']; ?>:</label><br />
<textarea id="cl_perex" name="perex" wrap="hard" class="<?php if (!$tinymce_p) {echo 'perex';} else {echo 'perex_tiny';}; ?>"><?php if (empty($_POST['perex'])) {echo htmlspecialchars($result['perex']);} else {echo htmlspecialchars($_POST['perex']);}; ?></textarea>
<br /><br />
<label for="cl_obsah"><?php echo $lang['articles_content']; ?>:</label><br />
<textarea id="cl_obsah" name="obsah" wrap="hard" class="<?php if (!$tinymce_p) {echo 'obsah';} else {echo 'obsah_tiny';}; ?>"><?php if (empty($_POST['obsah'])) {echo htmlspecialchars($result['obsah']);} else {echo htmlspecialchars($_POST['obsah']);}; ?></textarea>
<br /><br />


<div class="nastaveni_nadpis" onclick="$('.nastaveni').slideToggle();"><?php echo $lang['articles_extended_setting']; ?></div>
<div class="nastaveni_stitky_nadpis" onclick="$('.nastaveni_stitky').slideToggle();"><img src="stuff/img/icons/label.png" alt="" /> <?php echo $lang['articles_tags']; ?></div>

<div class="nastaveni_stitky">
<?php
  $query2 = mysql_query("SELECT * FROM jtcms_stitky ORDER BY id");
  $radku2 = mysql_num_rows($query2);
  
if ($radku2 == 0) {
  echo '<p>'.$lang['articles_no_tags'].'</p>';
} else {
  $stitky = explode(";",$result['stitky']);
    
  while ($result2 = mysql_fetch_array($query2)) {
    echo '<div class="polozka">';
    echo '<input type="checkbox" id="stitek_'.$result2['id'].'" class="stitek" name="stitky[]" value="'.$result2['id'].'"';
    if (empty($_POST)) {if (in_array($result2['id'], $stitky)) {echo ' checked';};} else {if (in_array($result['id'], $_POST['stitky'])) {echo ' checked';};};
    echo ' />';
    echo '<label for="stitek_'.$result2['id'].'">'.$result2['nazev'].'</label>';
    echo '</div>';
  };
};
?>

  <div class="checkbox_check_all"><input type="checkbox" id="check_all" title="<?php echo $lang['articles_check_all_checkboxes']; ?>" /></div>
  <div class="nf"></div>
</div>


<div class="nastaveni">

<div class="nadpis"><img src="stuff/img/icons/date.png" alt="" /> <?php echo $lang['articles_date']; ?>:</div>
<div class="moznosti">
<input type="radio" name="datum_pub" id="datum_pub_hned" value="hned"<?php if (!empty($_POST)) {if ($_POST['datum_pub'] == "hned") {echo ' checked';};} else {if ($dnesnidatum >= $result['datum']) {echo ' checked';};}; ?> /> <label for="datum_pub_hned"><?php echo $lang['articles_publish_immediately']; ?></label><br />
<input type="radio" name="datum_pub" value="jindy"<?php if (!empty($_POST)) {if ($_POST['datum_pub'] == "jindy") {echo ' checked';};} else {if (!($dnesnidatum >= $result['datum'])) {echo ' checked';};}; ?> />
<select name="day" size="1"> 
<?php for($i=1;$i<32;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_result_datepub('day', $i, 'd');
echo ' />'.$i;
}; ?>
</select>
.
<select name="month" size="1"> 
<?php for($i=1;$i<13;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_result_datepub('month', $i, 'm');
echo ' />'.$i;
}; ?>
</select>
.
<select name="year" size="1"> 
<?php for($i=2012;$i<2021;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_result_datepub('year', $i, 'Y');
echo ' />'.$i;
}; ?>
</select>
-
<select name="hour" size="1"> 
<?php for($i=1;$i<24;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_result_datepub('hour', $i, 'H');
echo ' />'.$i;
}; ?>
</select>
:
<select name="minute" size="1"> 
<?php for($i=1;$i<60;$i++) {
echo '<option value="'.$i.'"';
articles_categories_over_post_result_datepub('minute', $i, 'M');
echo ' />'.$i;
}; ?>
</select>
<span class="popis">DD.MM.YYYY - HH:MM</span>
</div>

<div class="nf"></div>
<br />

<div class="nadpis"><img src="stuff/img/icons/globe.png" alt="" /> <?php echo $lang['articles_article_status']; ?>:</div>
<div class="moznosti">
<input type="radio" name="status" id="status1" value="1"<?php articles_over_post_result_public('status', '1'); ?> /> <label for="status1"><?php echo $lang['articles_status_1']; ?></label><br />
<input type="radio" name="status" id="status2" value="2"<?php articles_over_post_result_public('status', '2'); ?> /> <label for="status2"><?php echo $lang['articles_status_2']; ?></label><span class="popis"><?php echo $lang['articles_status_2_des']; ?></span><br />
<input type="radio" name="status" id="status3" value="3"<?php articles_over_post_result_public('status', '3'); ?> /> <label for="status3"><?php echo $lang['articles_status_3']; ?></label><span class="popis"><?php echo $lang['articles_status_3_des']; ?></span><br />
</div>

<div class="nf"></div>
<br />

<div class="nadpis"><img src="stuff/img/icons/comments.png" alt="" /> <?php echo $lang['articles_comments']; ?>:</div>
<div class="moznosti">
<input type="radio" name="komentare" id="komentare1" value="1"<?php articles_over_post_result_public('kom_pov', '1', 'komentare'); ?> /> <label for="komentare1"><?php echo $lang['articles_comments_1']; ?></label><br />
<input type="radio" name="komentare" id="komentare2" value="2"<?php articles_over_post_result_public('kom_pov', '2', 'komentare'); ?> /> <label for="komentare2"><?php echo $lang['articles_comments_2']; ?></label><br />
</div>

<div class="nf"></div>
<br />

<div class="nadpis"><img src="stuff/img/icons/calculator.png" alt="" /> <?php echo $lang['articles_number_of_read']; ?>:</div>
<div class="moznosti">
<input type="text" name="pocet" class="pocet input" value="<?php if (empty($_POST['pocet'])) {echo $result['pocet'];} else {echo htmlspecialchars($_POST['pocet']);}; ?>" />
</div>

<div class="nf"></div>
<?php if ($prava == 1) { ?>
<br />

<div class="nadpis"><img src="stuff/img/icons/user.png" alt="" /> <?php echo $lang['articles_author']; ?>:</div>
<div class="moznosti">
<select name="pridal" size="1" class="pridal input"> 
<?php 
while ($result_user = mysql_fetch_array($query_user)) {
echo '<option value="'.$result_user['id'].'"';
if (empty($_POST)) {if ($result['pridal'] == $result_user['id']) {echo ' selected';};} elseif ($_POST['pridal'] == $result_user['id']) {echo ' selected';};
echo ' />'.$result_user['jmeno'];
};
?>
</select>
</div>

<div class="nf"></div>
<?php }; ?>

</div>

<br /><br />
<div class="nf"></div>

<input type="submit" name="submit" value="<?php echo $lang['articles_save_changes']; ?>" class="submit" />
</form>

<?php
break;

case "delete" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_clanky WHERE id='".$id."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
$nazev = $result['nazev'];
  
 if (empty($id) or $radku == 0 or ($prava == 2 && $result['pridal'] != $_SESSION['userid'])) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['articles_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['articles_er_e_1'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['articles_er_e_2'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['articles_er_e_3'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  }; 
  
    if (file_exists("./data/art_img/".$result['obrazek'])) {unlink("./data/art_img/".$result['obrazek']);};
    mysql_query("DELETE FROM jtcms_clanky WHERE id='".$id."' LIMIT 1");
    mysql_query("DELETE FROM jtcms_komentare_cl WHERE clanek='".$id."'");
    Header("Location: articles.php?work=deleted&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("articles.php?work=deleted&nazev='.rawurlencode($nazev).'")</script>';  
break;

case "delete-img" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_clanky WHERE id='".$id."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
$obrazek = $result['obrazek'];
$nazev = $result['nazev'];
  
 if (empty($id) or (!file_exists("./data/art_img/".$obrazek) or empty($obrazek)) or $radku == 0 or ($prava == 2 && $result['pridal'] != $_SESSION['userid'])) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['articles_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['articles_er_e_1'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['articles_er_e_2'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['articles_er_e_3'].'</p>';};
    if (!file_exists("./data/art_img/".$obrazek) or empty($obrazek)) {echo '<p>'.$lang['articles_er_11'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
    unlink("./data/art_img/".$obrazek);
    Header("Location: articles.php?work=deleted-img&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("articles.php?work=deleted-img&nazev='.rawurlencode($nazev).'")</script>';
      
break;

case "read" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_clanky WHERE id='".intval($id)."' LIMIT 1");
$radku = mysql_num_rows($query);
$result = mysql_fetch_array($query);
  
 if (empty($id) or $radku == 0) {
    echo '<br />';
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['articles_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['articles_er_e_1'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['articles_er_e_2'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };

if (!empty($result['obrazek']) && file_exists("./data/art_img/".$result['obrazek'])) {
  echo '<a class="grouped_elements" rel="group1" href="./data/art_img/'.$result['obrazek'].'"><img src="stuff/img/icons/camera.png" alt="'.$result['obrazek'].'" class="fl" /></a>';
}; 

if (($prava == 2 && $result['pridal'] == $_SESSION['userid']) or $prava == 1) {
    echo '<a href="articles.php?action=edit&amp;id='.$result['id'].'" class="filtr_but"><img src="stuff/img/icons/edit.png" alt="" /> '.$lang['articles_edit_article'].'</a>';
};

echo '<h1>'.$result['nazev'].'</h1>';
echo '<br />';

echo '<div class="art_pag_read">';
echo '<div class="art_perex">';
show_content($result['perex']);
echo '</div>';
echo '<br />';
if (!empty($result['obsah'])) {show_content($result['obsah']); echo '<br />';};
echo '</div>';
echo '<br />';

  $nastaveni = mysql_query("SELECT hodnota FROM jtcms_nastaveni WHERE typ='komentare_raz' LIMIT 1");
  $nastaveni = mysql_fetch_array($nastaveni);
     
if ($nastaveni['hodnota'] == 1) {
  $query_kom = mysql_query("SELECT * FROM jtcms_komentare_cl WHERE clanek='".intval($id)."' ORDER BY id");
} else {
  $query_kom = mysql_query("SELECT * FROM jtcms_komentare_cl WHERE clanek='".intval($id)."' ORDER BY id DESC");
};

$radku = mysql_num_rows($query_kom);

if ($radku == 0) {
  echo '<div class="kom_strankovani">'.$lang['articles_no_comments'].'</div>';
} else {
  $druh = "clanky";
  while ($result = mysql_fetch_array($query_kom)) {
    show_comment("clanky", "stranky");
  };
};

break;

default :


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

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

case "edited" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['articles_article'].' <span class="blue">'.stripslashes($_GET['nazev']).'</span> '.$lang['articles_work_2'].'</p>';
echo '</div>';
break;

case "deleted" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['articles_article'].' <span class="blue">'.stripslashes($_GET['nazev']).'</span> '.$lang['articles_work_3'].'</p>';
echo '</div>';
break;

case "deleted-img" :
echo '<div class="oznameni_div">';
echo '<p>'.$lang['articles_work_4'].' <span class="blue">'.stripslashes($_GET['nazev']).'</span> '.$lang['articles_work_5'].'</p>';
echo '</div>';
break;

};

?>

<a href="articles.php?action=add" class="pridat_tlacitko fl"><img src="stuff/img/icons/create.png" alt="+" /> <?php echo $lang['articles_add_article']; ?></a>
<div class="filtr_but" onclick="$('.filtr_div').slideToggle();"><img src="stuff/img/icons/hierarchy.png" alt="" /> <?php echo $lang['articles_filtr']; ?></div>
<?php if ($prava['kategorie'] != 3) { ?><a href="categories.php" class="filtr_but"><img src="stuff/img/icons/open folder.png" alt="" /> <?php echo $lang['articles_categories']; ?></a><?php }; ?>
<?php if ($prava['stitky'] != 3) { ?><a href="tags.php" class="filtr_but"><img src="stuff/img/icons/label.png" alt="" /> <?php echo $lang['articles_tags']; ?></a><?php }; ?>

<form method="post" action="articles.php" class="filtr_div"<?php if (!empty($_POST)) {echo ' style="display: block;"';}; ?>>

  <select name="stitky" size="1" class="input">
  <option value="" />--- <?php echo $lang['articles_fil_all_tags']; ?> ---
  <?php $query = mysql_query("SELECT id, nazev FROM jtcms_stitky ORDER BY id");
  while ($result = mysql_fetch_array($query)) {
      echo '<option value="'.$result['id'].'"';
      if (!empty($_POST['stitky']) && $_POST['stitky'] == $result['id']) {echo ' selected';};
      echo ' />'.$result['nazev']."\n";
  }; ?>
  </select>

  <select name="kategorie" size="1" class="input">
    <option value="" />--- <?php echo $lang['articles_fil_all_categories']; ?> ---
    <option value="0"<?php if (isset($_POST['kategorie']) && $_POST['kategorie'] != "" && $_POST['kategorie'] == '0') {echo ' selected';}; ?> /><?php echo $lang['articles_uncategorized']."\n"; ?>
    <?php $query = mysql_query("SELECT id, nazev FROM jtcms_kategorie ORDER BY menu_por");
    while ($result = mysql_fetch_array($query)) {
      echo '<option value="'.$result['id'].'"';
      if (!empty($_POST['kategorie']) && $_POST['kategorie'] == $result['id']) {echo ' selected';};
      echo ' />'.$result['nazev']."\n";
    }; ?>
  <select>

  <select name="autor" size="1" class="input">
    <option value="" />--- <?php echo $lang['articles_fil_all_authors']; ?> ---
    <?php $query = mysql_query("SELECT id, jmeno FROM jtcms_uzivatele ORDER BY id");
    while ($result = mysql_fetch_array($query)) {
      echo '<option value="'.$result['id'].'"';
      if (!empty($_POST['autor']) && $_POST['autor'] == $result['id']) {echo ' selected';};
      echo ' />'.$result['jmeno']."\n";
    }; ?>
  <select>

  <select name="status" size="1" class="input">
    <option value="" />--- <?php echo $lang['articles_fil_all_status']; ?> ---
    <option value="1"<?php if (!empty($_POST['status']) && $_POST['status'] == 1) {echo ' selected';}; ?> /><?php echo $lang['articles_status_1']."\n"; ?>
    <option value="2"<?php if (!empty($_POST['status']) && $_POST['status'] == 2) {echo ' selected';}; ?> /><?php echo $lang['articles_status_2']."\n"; ?>
    <option value="3"<?php if (!empty($_POST['status']) && $_POST['status'] == 3) {echo ' selected';}; ?> /><?php echo $lang['articles_status_3']."\n"; ?>
  </select>
  
  <select name="kom_pov" size="1" class="input">
    <option value="" />--- <?php echo $lang['articles_fil_all_comments']; ?> ---
    <option value="1"<?php if (!empty($_POST['kom_pov']) && $_POST['kom_pov'] == 1) {echo ' selected';}; ?> /><?php echo $lang['articles_fil_kom_pov_1']."\n"; ?>
    <option value="2"<?php if (!empty($_POST['kom_pov']) && $_POST['kom_pov'] == 2) {echo ' selected';}; ?> /><?php echo $lang['articles_fil_kom_pov_2']."\n"; ?>
  </select>

  <input type="submit" name="submit" value="<?php echo $lang['articles_filtr']; ?>" class="submit" />
</form>

<div class="nf"></div>

<?php
if (!empty($_POST)) {
  $stitky = $_POST['stitky'];
  $kategorie = $_POST['kategorie'];
  $autor = $_POST['autor'];
  $status = $_POST['status'];
  $kom_pov = $_POST['kom_pov'];
  
  $vypocet = $stitky+$kategorie+$autor+$status+$kom_pov;
  
  if ($vypocet != 0 or $kategorie != "") {
    $dotaz = " WHERE ";
    if (!empty($stitky)) {$dotaz .= "((stitky LIKE '%;".$stitky.";%' or stitky LIKE '".$stitky.";%' or stitky LIKE '%;".$stitky."') or stitky='".$stitky."') and ";};
    if ($kategorie != "") {$dotaz .= "kategorie='".$kategorie."' and ";};
    if (!empty($autor)) {$dotaz .= "pridal='".$autor."' and ";};
    if (!empty($status)) {$dotaz .= "status='".$status."' and ";};
    if (!empty($kom_pov)) {$dotaz .= "kom_pov='".$kom_pov."' and ";};
    
    $dotaz = preg_replace("~ and $~", "", $dotaz);

  } else {
    $dotaz = "";
  };
} else {
  $dotaz = "";
};

  $limit = mysql_query("SELECT hodnota FROM jtcms_nastaveni WHERE typ='pocet_clst_adm' LIMIT 1");
  $limit = mysql_fetch_array($limit);
  $limit = $limit['hodnota'];
  
    $pocet = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_clanky".$dotaz);
    $pocet = mysql_fetch_array($pocet);
    $pocet = $pocet['pocet'];

  if ($pocet > $limit && empty($_POST)) {   
      $pocet_stran = $pocet/$limit;
  
    if (isset($_GET['strana'])) {
      $strana = $_GET['strana'];
    } else {
      $strana = 1;
    }; 
           
    $query = mysql_query("SELECT * FROM jtcms_clanky ORDER BY datum DESC LIMIT ".$limit." OFFSET ".($limit*($strana-1)));
  } else {
    $query = mysql_query("SELECT * FROM jtcms_clanky".$dotaz." ORDER BY datum DESC");
  };
  
  $radku = mysql_num_rows($query);

?>
<table class="tabulka_clanky" id="vypis">
<tr class="prvni_tr">
  <td class="td1 first">&nbsp;<?php echo $lang['articles_article']; ?></td>
  <td class="td2"><?php echo $lang['articles_author']; ?></td>
  <td class="td3"><?php echo $lang['articles_category']; ?></td>
  <td class="td4"><img src="stuff/img/icons/label.png" title="<?php echo $lang['articles_number_of_tags']; ?>" alt="<?php echo $lang['articles_tags']; ?>" /></td>
  <td class="td5"><img src="stuff/img/icons/comments.png" title="<?php echo $lang['articles_number_of_comments']; ?>" alt="<?php echo $lang['articles_comments']; ?>" /></td>
  <td class="td6 last"><?php echo $lang['articles_date']; ?></td>
</tr>
<?php
  if (!empty($_POST)) {
    echo '<tr><td colspan="6" class="filtr_zrus_td">';
    echo '<a href="articles.php" class="filtr_zrus">'.$lang['articles_fil_cancel_filter'].'</a>';
    echo $lang['articles_fil_total'].' <strong>'.$radku.'</strong>';
    echo '</td></tr>';
  };
  
if ($radku == 0) {
  echo '<tr><td colspan="6" style="text-align: center; padding: 5px;">';
    if (empty($_POST)) {echo $lang['articles_no_articles'];} else {echo $lang['articles_no_articles_post'];};
  echo '</td></tr>';
  echo '</table>';
} else {
   
  while ($result = mysql_fetch_array($query)) {
    $query2 = mysql_query("SELECT jmeno FROM jtcms_uzivatele WHERE id='".$result['pridal']."' LIMIT 1");
    $result2 = mysql_fetch_array($query2);
    $query3 = mysql_query("SELECT nazev FROM jtcms_kategorie WHERE id='".$result['kategorie']."' LIMIT 1");
    $result3 = mysql_fetch_array($query3);
    $query4 = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_komentare_cl WHERE clanek='".$result['id']."'");
    $result4 = mysql_fetch_array($query4);
    
    if (!empty($result['stitky'])) {
      preg_match_all("/;/", $result['stitky'], $stitky_pocet);
      $stitky_pocet = count($stitky_pocet[0])+1;
    } else {
      $stitky_pocet = 0;
    };


    echo '<tr>';
    echo '<td>&nbsp;';
      if (!empty($result['obrazek']) && file_exists("./data/art_img/".$result['obrazek'])) {
        echo '<a class="grouped_elements" rel="group1" href="./data/art_img/'.$result['obrazek'].'" title="'.$lang['articles_view_image'].'"><img src="stuff/img/icons/camera.png" alt="'.$result['obrazek'].'" class="obrazek_ikona" /></a>';
      };
      echo '<a href="articles.php?action=read&amp;id='.$result['id'].'">'.$result['nazev'].'</a>';
    echo '</td>';
    echo '<td>'.$result2['jmeno'].'</td>';
    echo '<td>';
      if (empty($result['kategorie'])) {
        if ($prava['kategorie'] != 3) {
          echo '<a href="categories.php?action=detail&amp;kategorie=0" class="prazdno" title="'.$lang['articles_view_detail_category'].'">'.$lang['articles_uncategorized'].'</a>';
        } else {
          echo $result3['nazev'];
        };
      } else {
        if ($prava['kategorie'] != 3) {
          echo '<a href="categories.php?action=detail&amp;kategorie='.$result['kategorie'].'" title="'.$lang['articles_view_detail_category'].'">'.$result3['nazev'].'</a>';
        } else {
          echo $result3['nazev'];
        };
      }; 
    echo '</td>';
    echo '<td>';
      if ($stitky_pocet == 0) {
        echo '<span class="prazdno">'.$stitky_pocet.'</span>';
      } else {
        echo $stitky_pocet;
      };
    echo '</td>';
    echo '<td>';
      if ($result4['pocet'] == 0) {
        echo '<span class="prazdno">'.$result4['pocet'].'</span>';
      } else {
        if ($prava['komentare'] != 3) {
          echo '<a href="comments.php?action=article-page&amp;id='.$result['id'].'&amp;druh=clanky" title="'.$lang['articles_view_comments'].'">'.$result4['pocet'].'</a>';
        } else {
          echo $result4['pocet'];
        };
      };
    echo '</td>';
    echo '<td class="datum">';    
    if (($prava == 2 && $result['pridal'] == $_SESSION['userid']) or $prava == 1) {
        echo '<div class="links">';
          echo '<a href="articles.php?action=edit&amp;id='.$result['id'].'" title="'.$lang['articles_edit_article'].'"><img src="stuff/img/icons/edit.png" title="'.$lang['articles_edit_article'].'" alt="'.$lang['articles_edit_article'].'" /></a>';
          echo '<br />';
          echo '<img src="stuff/img/icons/trash.png" class="smaz" onclick="smaz_cl(\''.$result['id'].'\',\''.addslashes($result['nazev']).'\')" title="'.$lang['articles_delete_article'].'" alt="'.$lang['articles_delete_article'].'" />';
        echo '</div>';
    };     
      echo '<div class="datum" title="'.StrFTime("%H:%M %d.%m.%Y", strtotime($result["datum"])).'">'.StrFTime("%d.%m.%Y", strtotime($result["datum"])).'</div>';
      switch ($result['status']) {
        case "1" : echo '<span class="status green">'.$lang['articles_status_1'].'</span>'; break;
        case "2" : echo '<span class="status red">'.$lang['articles_status_2'].'</span>'; break;
        case "3" : echo '<span class="status blue">'.$lang['articles_status_3'].'</span>'; break;
      };
    echo '</td>';
    echo '</tr>';
    echo "\n";
  };
  
  echo '</table>';

    if ($pocet > $limit && empty($_POST)) {
    echo '<br /><br />';
    echo '<div class="kom_strankovani">';
    echo $lang['articles_go_to_page'].': ';
    for($c=1; $c<$pocet_stran+1; $c++) {
        echo '<a href="?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_cl(id, nazev){
jConfirm("<?php echo $lang['articles_really_del_1']; ?> "+nazev+" <?php echo $lang['articles_really_del_2']; ?>", "<?php echo $lang['articles_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("articles.php?action=delete&id="+id);};
});
};

function smaz_img(id){
jConfirm("<?php echo $lang['articles_really_del_3']; ?>", "<?php echo $lang['articles_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("articles.php?action=delete-img&id="+id);};
});
};

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