<?php
$druh = "galerie";
$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>';
$meta_end = '<script type="text/javascript" src="stuff/tools/fancybox/jt_cms_set.js"></script>';
include "head.php";
include_head($lang['galleries_title'], $druh);

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

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

case "add" :

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

if (!empty($_POST)) {
  $nazev = htmlspecialchars($_POST['nazev']);
  
  if (!empty($nazev)) {
  $nazev = mysql_real_escape_string($nazev);
  $datum_now = StrFTime("%Y-%m-%d %H:%M:%S", Time());
  
  mysql_query("INSERT INTO jtcms_galerie (nazev,datum,pridal) VALUES ('".$nazev."','".$datum_now."','".$_SESSION['userid']."')");
  $query_id = mysql_query("SELECT id FROM jtcms_galerie WHERE nazev='".$nazev."' and datum='".$datum_now."' and pridal='".$_SESSION['userid']."' LIMIT 1");
  $result_id = mysql_fetch_array($query_id);
  $result_id = $result_id['id'];
  
    $umask_old = umask(0000);
    mkdir("./data/gal/".$result_id, 0777);
    umask($umask_old);
  
    Header("Location: galleries.php?action=edit&id=".rawurlencode($result_id)."&edit=edit&ins=1");
    echo '<script type="text/javascript">location.replace("galleries.php?action=edit&id='.rawurlencode($result_id).'&edit=edit&ins=1")</script>';
  } else {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['galleries_error'].'</div>';
    echo '<li>'.$lang['galleries_er_1'].'</li>';
    echo '</div>';
  };
};

break;

case "edit" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_galerie WHERE id='".$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['galleries_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['galleries_er_2'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['galleries_er_3'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();                                                                
  };
  
if (($prava == 1) or ($prava == 2 && $result['pridal'] == $_SESSION['userid'])) {
  echo '<div class="filtr_but" onclick="$(\'#galerie_form\').slideToggle();"><img src="stuff/img/icons/edit.png" alt="" /> '.$lang['galleries_edit_gallery'].'</div>';
  echo '<div class="filtr_but" onclick="$(\'.checkbox_obal\').slideToggle(); $(\'.galerie_vypis .submit\').slideToggle();"><img src="stuff/img/icons/trash.png" alt="" /> '.$lang['galleries_delete_photos'].'</div>';
};

echo '<h1 class="tal">'.$lang['galleries_gallery'].': '.$result['nazev'].'</h1>';
echo '<br />';

if (isset($_GET['error']) && $_GET['error'] == 1) {
  echo '<div class="oznameni_div">';
  echo '<p>'.$lang['galleries_er_4'].'</p>';
  echo '</div>';
};

if (isset($_GET['ins']) && $_GET['ins'] == 1) {
  echo '<div class="oznameni_div">';
  echo '<p>'.$lang['galleries_ins_1'].'</p>';
  echo '</div>';
};
  
if (!empty($_POST)) {

  $nazev = htmlspecialchars($_POST['nazev']);
  $pole = $_FILES['soubor'];
  
  if (!empty($nazev) && ($prava == 1 or ($prava == 2 && $result['pridal'] == $_SESSION['userid']))) {
  
    $nazev = mysql_real_escape_string($nazev);
    mysql_query("UPDATE jtcms_galerie SET nazev='".$nazev."' WHERE id='".$id."' LIMIT 1");
    
    if (!empty($pole['tmp_name'][0])) {
    echo '<div class="gallery_upload_div">';
      $c = 0;
      $query2 = mysql_query("SELECT * FROM jtcms_nastaveni WHERE typ='galerie_max_upload' LIMIT 1");
      $result2 = mysql_fetch_array($query2);
  
      for ($i = 0; $i < count($pole['tmp_name']); $i++) {      
        if ($pole['size'][$i] < ($result2['hodnota']+1) && preg_match("~image/(.*)~", $pole['type'][$i]) && is_file($pole['tmp_name'][$i]) && !file_exists("./data/gal/".$id."/".$pole['name'][$i])) {
          move_uploaded_file($pole['tmp_name'][$i], "data/gal/".$id."/".$pole['name'][$i]);
          if (file_exists("./data/gal/".$id."/".$pole['name'][$i])) {
            echo '<li><img src="stuff/img/icons/tick.png" alt="" /> '.$pole['name'][$i].'</li>';
            $c++;
          };
        } else {
          echo '<li title="';
            if ($pole['size'][$i] > $result2['hodnota']) {echo $lang['galleries_er_5'].' '.$result2['hodnota'].'. ';};
            if (!preg_match("~image/(.*)~", $pole['type'][$i])) {echo $lang['galleries_er_6'].' ';};
            if (!is_file($pole['tmp_name'][$i])) {echo $lang['galleries_er_7'].' ';};
            if (file_exists("./data/gal/".$id."/".$pole['name'][$i])) {echo $lang['galleries_er_8'];};
          echo '">';
          echo '<img src="stuff/img/icons/delete.png" alt="" /> '.$pole['name'][$i].'</li>';
        };
      };
    echo '<div class="nf"></div>';
    echo '</div><br />';
    }; 
    
  } else {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['galleries_error'].'</div>';
    if (empty($nazev)) {echo '<li>'.$lang['galleries_er_1'].'</li>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<li>'.$lang['galleries_er_9'].'</li>';};
    echo '</div>';
  };
};
?>
  
  <form method="post" enctype="multipart/form-data" action="galleries.php?action=edit&amp;id=<?php echo $id; ?>" id="galerie_form"<?php if (empty($_POST) && (!isset($_GET['edit']) or $_GET['edit'] != "edit")) {echo ' style="display: none;"';}; ?>>
    <label for="gal_nazev"><?php echo $lang['galleries_name']; ?>:</label> <input type="text" name="nazev" id="gal_nazev" class="input" value="<?php if (empty($_POST['nazev'])) {echo $result['nazev'];} else {echo $_POST['nazev'];}; ?>" /><br />
    <label for="gal_soubory"><?php echo $lang['galleries_select_file']; ?>:</label> <input type="file" min="1" id="gal_soubory" name="soubor[]" max="999" accept="image/*" class="file_input" multiple /><br />
    <br />
    <input type="submit" name="submit" value="<?php echo $lang['galleries_add_photos_edit']; ?>" class="submit" />
  </form>
  
  <?php 
  $query2 = mysql_query("SELECT * FROM jtcms_nastaveni WHERE typ='galerie_nahled' LIMIT 1");
  $result2 = mysql_fetch_array($query2);
  $sirka = $result2['hodnota'];
  $vyska = $result2['hodnota2']; 
?>  

  <form method="post" action="galleries.php?action=delete-img&amp;id=<?php echo $id; ?>" class="galerie_vypis">
  <input type="submit" name="submit" value="<?php echo $lang['galleries_delete_photos']; ?>" class="submit" /><br />
<?php
  $adresar = opendir("./data/gal/".$id);
    $num = 0;
    while (readdir($adresar)){
      $num++;
    };
    closedir("./data/gal/".$id);
    $num = $num-2;
    
  if ($num == 0) {
    echo $lang['galleries_gallery_is_empty'].'<br /><br />';
  } else {
    $adresar = opendir("./data/gal/".$id);
    while ($soubor = readdir($adresar)){
      if ($soubor != "." && $soubor != ".." && is_file("./data/gal/".$id."/".$soubor)) {
        echo '<span class="img_obal"><div class="checkbox_obal"';
        if (isset($_GET['edit']) && $_GET['edit'] == "delete") {echo ' style="display: block;"';};
        echo '><input type="checkbox" name="obrazek[]" value="'.$soubor.'" /></div><a class="grouped_elements" rel="group1" href="./data/gal/'.$id.'/'.$soubor.'"><img src="./data/gal/'.$id.'/'.$soubor.'" alt="'.$soubor.'" style="max-width: '.$sirka.'px; max-height: '.$vyska.'px;" /></a></span>'."\n";
      };
    };
  };
?>
  </form>
  
<?php
break;

case "delete-all" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_galerie 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['galleries_ac_delete_all'].'</h1>';
    echo '<br />';
    
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['galleries_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['galleries_er_2'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['galleries_er_3'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['galleries_er_9'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
    $adresar = opendir("data/gal/".$id."/");
    while ($soubor = readdir($adresar)){
        if (!is_dir($soubor)) {
          unlink("./data/gal/".$id."/".$soubor);
        };
    };
    
    rmdir("./data/gal/".$id."/");
  
    mysql_query("DELETE FROM jtcms_galerie WHERE id='".$id."' LIMIT 1");
    Header("Location: galleries.php?work=deleted&nazev=".rawurlencode($nazev));
    echo '<script type="text/javascript">location.replace("galleries.php?work=deleted&nazev='.rawurlencode($nazev).'")</script>';  
    
break;
  
case "delete-img" :
$id = $_GET['id']; 
$query = mysql_query("SELECT * FROM jtcms_galerie 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['galleries_ac_delete_img'].'</h1>';
    echo '<br />';
    
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['galleries_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['galleries_er_2'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['galleries_er_3'].'</p>';};
    if ($prava == 2 && $result['pridal'] != $_SESSION['userid']) {echo '<p>'.$lang['galleries_er_9'].'</p>';};
    echo '</div>';
    
    include_end();
    exit();
  };
  
if (!empty($_POST)) {
  $obrazek = $_POST['obrazek'];
  
  foreach ($obrazek as $val) {
    unlink("./data/gal/".$id."/".$val);
  }; 
  
    Header("Location: galleries.php?action=edit&id=".$id);
    echo '<script type="text/javascript">location.replace("galleries.php?action=edit&id='.$id.'")</script>';
} else {
    Header("Location: galleries.php?action=edit&id=".$id."&error=1");
    echo '<script type="text/javascript">location.replace("galleries.php?action=edit&id='.$id.'&error=1")</script>';
};
  
break;

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

 if (empty($id) or $radku == 0) {
    echo '<div class="oznameni_div">';
    echo '<div class="chyba">'.$lang['galleries_error'].'</div>';
    if (empty($id)) {echo '<p>'.$lang['galleries_er_2'].'</p>';};
    if ($radku == 0) {echo '<p>'.$lang['galleries_er_3'].'</p>';};
    echo '</div>';
    
    include_end();                
    exit();               
  };                              
?>

<h1><?php echo $lang['galleries_view_code_1']; ?></h1> 
<br /> 
<div class="kod_vlozeni" id="kod">
<?php echo $lang['galleries_view_code_2']; ?> <strong><?php echo $result['nazev']; ?></strong><br /><br />
<?php echo $lang['galleries_view_code_3']; ?><br /><br />
<textarea wrap="hard">
[jtcms_gallery=<?php echo $id; ?>]
</textarea>
</div>

<?php
break;

default :
?>                                                                                                                                       

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

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

  $query = mysql_query("SELECT * FROM jtcms_galerie ORDER BY datum DESC");
  $radku = mysql_num_rows($query);
?>

  <form method="post" action="galleries.php?action=add" id="galerie_add_form">
    <label for="gal_nazev"><?php echo $lang['galleries_name']; ?>:</label> <input type="text" name="nazev" id="gal_nazev" class="input" /><br />
    <br />
    <input type="submit" name="submit" value="<?php echo $lang['galleries_create_gallery']; ?>" class="submit" />
  </form>
  
<table class="tabulka_galerie">
<tr class="prvni_tr">
  <td class="td1 first">&nbsp;<?php echo $lang['galleries_id']; ?></td>
  <td class="td2"><?php echo $lang['galleries_name']; ?></td>
  <td class="td3"><?php echo $lang['galleries_author']; ?></td>
  <td class="td4"><img src="stuff/img/icons/camera.png" alt="<?php echo $lang['galleries_number_of_photos']; ?>" title="<?php echo $lang['galleries_number_of_photos']; ?>" /></td>
  <td class="td5 last"><?php echo $lang['galleries_date']; ?></td>  
</tr>
<?php
if ($radku == 0) {
  echo '<tr><td colspan="5" style="text-align: center; padding: 5px;">'.$lang['galleries_no_galleries'].'</td></tr>';
} 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);
  
    $adresar = opendir("./data/gal/".$result['id']);
    $num = 0;
    while ($soubor = readdir($adresar)){
      $num++;
    };
    $num = $num-2;
      
    echo '<td>&nbsp;'.$result['id'].'</td>';
    echo '<td><a href="galleries.php?action=edit&amp;id='.$result['id'].'">'.$result['nazev'].'</a></td>';
    echo '<td>'.$result2['jmeno'].'</td>';
    echo '<td>'.$num.'</td>';
    echo '<td class="datum">';
      echo '<div class="links">';
        echo '<div>';
          echo '<a href="galleries.php?action=code&amp;id='.$result['id'].'" title="'.$lang['galleries_view_code'].'"><img src="stuff/img/icons/wizard.png" alt="'.$lang['galleries_view_code'].'" /></a>';
          if (($prava == 2 && $result['pridal'] == $_SESSION['userid']) or $prava == 1) {
            echo '<a href="galleries.php?action=edit&amp;id='.$result['id'].'&amp;edit=edit" title="'.$lang['galleries_edit_gallery'].'"><img src="stuff/img/icons/edit.png" alt="'.$lang['galleries_edit_gallery'].'" /></a>';
            echo '<img src="stuff/img/icons/trash.png" title="'.$lang['galleries_delete_gallery'].'" class="smaz" alt="'.$lang['galleries_delete_gallery'].'" onclick="smaz_gal_all(\''.$result['id'].'\',\''.$result['nazev'].'\',\''.$num.'\')" />';
          };
         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>  
<script type="text/javascript">
function smaz_gal_all(id, nazev, pocet){
jConfirm("<?php echo $lang['galleries_really_del_1']; ?> "+nazev+" <?php echo $lang['galleries_really_del_2']; ?> "+pocet+"<?php echo $lang['galleries_really_del_3']; ?>", "<?php echo $lang['galleries_really_del_headline']; ?>", function(r) {
    if (r == true) {location.replace("galleries.php?action=delete-all&id="+id);};
});
};
</script>
<?php
};

include_end();
?>