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

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

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

case "add" :

  $nazev = htmlspecialchars($_POST['nazev']);
  $datum_pub = $_POST['datum_pub'];
  
  if (!empty($nazev) && !empty($datum_pub) && !preg_match("~;~", $nazev)) {
  
    if ($datum_pub == "hned") {
      $datum = "NOW()";
    } else {
      $datum = "'".$_POST['year']."-".$_POST['month']."-".$_POST['day']." ".$_POST['hour'].":".$_POST['minute'].":00'"; 
    };
    
    $nazev = mysql_real_escape_string($nazev);
    
    mysql_query("INSERT INTO jtcms_stitky (nazev,pridal,datum) VALUES ('".$nazev."','".$_SESSION['userid']."',".$datum.")");
    Header("Location: tags.php?work=added&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("tags.php?work=added&nazev='.rawurlencode($nazev).'")</script>';
  
  } else {
    echo '<h1>'.$lang['tags_add_tag'].'</h1>';
    echo '<br />';
    
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['tags_error'].'</div>';
    if (empty($nazev)) {echo '<li>'.$lang['tags_er_1'].'</li>';};
    if (preg_match("~;~", $nazev)) {echo '<li>'.$lang['tags_er_9'].'</li>';};
    if (empty($datum_pub)) {echo '<li>'.$lang['tags_er_10'].'</li>';};
    echo '</div>';   
  };

break;

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

echo '<h1>'.$lang['tags_edit_tag'].'</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['tags_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['tags_er_5'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['tags_er_6'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['tags_er_7'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
if (!empty($_POST)) {

  $nazev = htmlspecialchars($_POST['nazev']);
  $pridal = htmlspecialchars($_POST['pridal']);
  $datum_pub = $_POST['datum_pub'];
  
  if (!empty($nazev) && !empty($datum_pub) && !preg_match("~;~", $nazev)) {
  
    if ($datum_pub == "jindy") {
      $datum = "'".$_POST['year']."-".$_POST['month']."-".$_POST['day']." ".$_POST['hour'].":".$_POST['minute'].":00'";
      mysql_query("UPDATE jtcms_stitky SET datum=".$datum." WHERE id='".$id."' LIMIT 1");
    };
    
    $nazev = mysql_real_escape_string($nazev);
    $pridal = mysql_real_escape_string($pridal);
    
    mysql_query("UPDATE jtcms_stitky SET nazev='".$nazev."', pridal='".$pridal."' WHERE id='".$id."' LIMIT 1");
    Header("Location: tags.php?work=edited&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("tags.php?work=edited&nazev='.rawurlencode($nazev).'")</script>';
  
  } else {    
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['tags_error'].'</div>';
    if (empty($nazev)) {echo '<li>'.$lang['tags_er_1'].'</li>';};
    if (preg_match("~;~", $nazev)) {echo '<li>'.$lang['tags_er_9'].'</li>';};
    if (empty($pridal)) {echo '<li>'.$lang['tags_er_8'].'</li>';};
    if (empty($datum_pub)) {echo '<li>'.$lang['tags_er_10'].'</li>';};
    echo '</div>';   
  };

};
?>

<form method="post" action="tags.php?action=edit&amp;id=<?php echo $id; ?>" id="stitky_form">
  <label for="tags_nazev"><?php echo $lang['tags_name']; ?>:</label> <input type="text" name="nazev" id="tags_nazev" class="input" value="<?php if (empty($_POST)) {echo htmlspecialchars($result['nazev']);} else {echo htmlspecialchars($_POST['nazev']);}; ?>" /><br />
  <br />

    <div class="nastaveni" style="display: block;">

<div class="nadpis"><img src="stuff/img/icons/date.png" alt="" /> <?php echo $lang['tags_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['tags_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><br />
<span class="popis">DD.MM.YYYY - HH:MM</span>
</div>

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

<?php if ($prava == 1) { ?>
<br />

<div class="nadpis"><img src="stuff/img/icons/user.png" alt="" /> <?php echo $lang['tags_author']; ?>:</div>
<div class="moznosti">
<select name="pridal" size="1" class="pridal input"> 
<?php $query_user = mysql_query("SELECT * FROM jtcms_uzivatele");

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>
<br />
<?php }; ?>

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

<?php
break;

case "delete" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_stitky 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 '<h1>'.$lang['tags_delete_tag'].'</h1><br />';
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['tags_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['tags_er_5'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['tags_er_6'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['tags_er_7'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  }; 
  
    $query2 = mysql_query("SELECT id, stitky FROM jtcms_clanky WHERE stitky LIKE '%;".$id.";%' or stitky LIKE '".$id.";%' or stitky LIKE '%;".$id."' or stitky='".$id."'");
    
    while ($result2 = mysql_fetch_array($query2)) {
      $stitky = $result2['stitky'];
      if (preg_match(";", $stitky)) {  
        $stitky = str_replace(";".$id.";", ";", $stitky);
        $stitky = str_replace(";".$id, "", $stitky);
        $stitky = str_replace($id.";", "", $stitky);
      } else {
        $stitky = "";
      };
      
      mysql_query("UPDATE jtcms_clanky SET stitky='".$stitky."' WHERE id='".$result2['id']."' LIMIT 1");
    };
    
    mysql_query("DELETE FROM jtcms_stitky WHERE id='".$id."' LIMIT 1");    
    Header("Location: tags.php?work=deleted&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("tags.php?work=deleted&nazev='.rawurlencode($nazev).'")</script>'; 

break;

default : ?>

<div class="filtr_but" onclick="$('.stitky_add_form').slideToggle();"><img src="stuff/img/icons/add.png" alt="" /> <?php echo $lang['tags_submit']; ?></div>

<?php
echo '<h1>'.$lang['tags_h1'].'</h1>';
echo '<br />';

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

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

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

$query = mysql_query("SELECT * FROM jtcms_stitky ORDER BY id");
$radku = mysql_num_rows($query);
?>

<form method="post" action="tags.php?action=add" id="stitky_form" class="stitky_add_form">
  <label for="tags_nazev"><?php echo $lang['tags_name']; ?>:</label> <input type="text" name="nazev" id="tags_nazev" class="input" /><br />
  <br />
  
  <div class="nastaveni_nadpis" onclick="$('.nastaveni').slideToggle();"><?php echo $lang['tags_extended_setting']; ?></div>

    <div class="nastaveni">

<div class="nadpis"><img src="stuff/img/icons/date.png" alt="" /> <?php echo $lang['tags_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['tags_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><br />
<span class="popis">DD.MM.YYYY - HH:MM</span>
</div>

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

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

<table class="tabulka_stitky">
<tr class="prvni_tr">
  <td class="td1 first">&nbsp;<?php echo $lang['tags_name']; ?></td>
  <td class="td4"><img src="stuff/img/icons/document.png" alt="<?php echo $lang['tags_number_articles']; ?>" title="<?php echo $lang['tags_number_articles']; ?>" /></td>
  <td class="td5 last"><?php echo $lang['tags_date']; ?></td>
</tr>
<?php
if ($radku == 0) {
  echo '<tr><td colspan="5" style="text-align: center; padding: 5px;">'.$lang['tags_tags_is_empty'].'</td></tr>';
} else {
  while ($result = mysql_fetch_array($query)) {
    $query2 = mysql_query("SELECT COUNT(*) AS pocet FROM jtcms_clanky WHERE stitky LIKE '%;".$result['id'].";%' or stitky LIKE '".$result['id'].";%' or stitky LIKE '%;".$result['id']."' or stitky='".$result['id']."'");
    $result2 = mysql_fetch_array($query2);
    
    echo '<tr>';
    echo '<td>&nbsp;'.$result['nazev'].'</td>';
    echo '<td>'.$result2['pocet'].'</td>';
    echo '<td class="datum">';
      if (($prava == 2 && $result['pridal'] == $_SESSION['userid']) or $prava == 1) {
        echo '<div class="links">';
         echo '<div>';
          echo '<a href="tags.php?action=edit&amp;id='.$result['id'].'" title="'.$lang['tags_edit_tag'].'"><img src="stuff/img/icons/edit.png" alt="'.$lang['tags_edit_tag'].'" /></a>';
          echo '<img src="stuff/img/icons/trash.png" title="'.$lang['tags_delete_tag'].'" class="smaz" alt="'.$lang['tags_delete_tag'].'" onclick="smaz_stit(\''.$result['id'].'\',\''.$result['nazev'].'\',\''.$result2['pocet'].'\')" />';
         echo '</div>';
        echo '</div>';
      };
      echo '<div class="datum" title="'.StrFTime("%H:%M %d.%m.%Y", strtotime($result["datum"])).'">'.StrFTime("%d.%m.%Y", strtotime($result["datum"])).'</div>';
    echo '</td>';
    echo '</tr>';
    echo "\n";
  };

};
?>
</table>
<?php }; ?>
<script type="text/javascript">
function smaz_stit(id, nazev, pocet){
okno=window.confirm("<?php echo $lang['tags_really_del_1']; ?> "+nazev+"? <?php echo $lang['tags_really_del_2']; ?> "+pocet+" <?php echo $lang['tags_really_del_3']; ?>");
if(okno) location.replace("tags.php?action=delete&id="+id);
};

function smaz_stit(id, nazev, pocet){
jConfirm("<?php echo $lang['tags_really_del_1']; ?> "+nazev+"? <?php echo $lang['tags_really_del_2']; ?> "+pocet+" <?php echo $lang['tags_really_del_3']; ?>", "<?php echo $lang['tags_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("tags.php?action=delete&id="+id);};
});
};
</script>
<?php
include_end();
?>