<?php

// -----------------------------------------------------------------
// tady můžete nastavit práva (1 - 3)
// here you can set the rights (1 - 3)
if ($rights>=1):
// -----------------------------------------------------------------

// tedy můžeš nastavit práva pro manipulaci se sekcemi obrázků
// here you can set the rights for actions with images sections
if ($rights>=2) {

if ($a=="add_section") {

if ($section!="") {

include "transfer_quick.php";

$check=mysql_query("SELECT id FROM kcms_images_sections WHERE quick='".addslashes($quick)."'");
$check=mysql_fetch_array($check);

if ($check["id"]=="") {

$sql=mysql_query("INSERT INTO kcms_images_sections values ('', '".addslashes($quick)."', '".addslashes($section)."', '".addslashes($public)."')", $dbserver);

echo "<div class=\"popup-success\">$lang_alert_success</div>";

} else {

echo "<div class=\"popup-error\">$lang_alert_existing_section</div>";

}

} else {

echo "<div class=\"popup-error\">$lang_alert_empty_name</div>";

}

}

if ($a=="delete_section" AND $id!="") {

$sql=mysql_query("DELETE FROM kcms_images_sections WHERE id='".addslashes($id)."'", $dbserver);

echo "<div class=\"popup-success\">$lang_alert_success</div>";

}

}

if ($a=="delete_image" AND $image_id!="") {

$filename=mysql_query("SELECT file FROM kcms_images WHERE id='".addslashes($image_id)."'");
$filename=mysql_fetch_array($filename);

if ($filename["file"]!="") {

unlink("./gallery/".$filename["file"]);
$thumb=explode(".", $filename["file"]);
unlink("./gallery/thumbs/".$thumb[0].".jpg");

$sql=mysql_query("DELETE FROM kcms_images WHERE id='".addslashes($image_id)."'", $dbserver);

echo "<div class=\"popup-success\">$lang_alert_success</div>";

}

}

if ($a=="edit_image" AND $image_id!="") {

$sql=mysql_query("UPDATE kcms_images SET section='".$_POST["section"]."' WHERE id='".addslashes($image_id)."'", $dbserver);

echo "<div class=\"popup-success\">$lang_alert_success</div>";

}

if ($a=="rename_section" AND $id!="") {

if ($section!="") {

include "transfer_quick.php";

$old_quick=mysql_query("SELECT quick FROM kcms_images_sections WHERE id='".addslashes($id)."'");
$old_quick=mysql_fetch_array($old_quick);

if ($old_quick["quick"]!=$quick) {

$check=mysql_query("SELECT id FROM kcms_images_sections WHERE quick='".addslashes($quick)."'");
$check=mysql_fetch_array($check);

}

if ($check["id"]=="") {

$sql=mysql_query("UPDATE kcms_images_sections SET name='".addslashes($section)."', quick='".addslashes($quick)."', public='".addslashes($public)."' WHERE id='".addslashes($id)."'", $dbserver);

echo "<div class=\"popup-success\">$lang_alert_success</div>";

} else {

echo "<div class=\"popup-error\">$lang_alert_existing_section</div>";

}

} else {

echo "<div class=\"popup-error\">$lang_alert_empty_name</div>";

}

}

if ($a=="add_image") {

if ($file!="" AND $section!="") {

if ($changeres==0 OR $changeres==1 AND $new_width!="") {

$ext=strrchr($_FILES["file"]["name"], ".");
$new_filename=explode(".", $_FILES["file"]["name"]);
$new_filename=$new_filename[0];

if ($ext==".jpg" OR $ext==".JPG") {
$img_data=imagecreatefromjpeg($_FILES["file"]["tmp_name"]);
$ext="jpg";
}

if ($ext==".png" OR $ext==".PNG") {
$img_data=imagecreatefrompng($_FILES["file"]["tmp_name"]);
$ext="png";
}

if ($ext==".gif" OR $ext==".GIF") {
$img_data=imagecreatefromgif($_FILES["file"]["tmp_name"]);
$ext="gif";
}

$check=mysql_query("SELECT id FROM kcms_images WHERE file='$new_filename.$ext'");
$check=mysql_fetch_array($check);

if ($check["id"]=="") {

$width=imagesx($img_data);
$height=imagesy($img_data);
$v1=$new_width/$width;
$new_height=$v1*$height;

if ($changeres==0) {
$create=imagecreatetruecolor($width, $height);
imagecopyresampled($create, $img_data, 0, 0, 0, 0, $width, $height, imagesx($img_data), imagesy($img_data));
} else {
$create=imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($create, $img_data, 0, 0, 0, 0, $new_width, $new_height, imagesx($img_data), imagesy($img_data));
}

if ($ext=="jpg") {
imagejpeg($create, "./gallery/$new_filename.jpg", 90);
}
if ($ext=="png") {
imagepng($create, "./gallery/$new_filename.png", 1);
}
if ($ext=="gif") {
imagegif($create, "./gallery/$new_filename.gif");
}

$create=imagecreatetruecolor(120, 90);
imagecopyresampled($create, $img_data, 0, 0, 0, 0, 120, 90, imagesx($img_data), imagesy($img_data));
imagejpeg($create, "./gallery/thumbs/$new_filename.jpg", 90);

$sql=mysql_query("INSERT INTO kcms_images values ('', '".addslashes($section)."', '$new_filename.$ext', '".time()."')", $dbserver);
imagedestroy($img_data);

echo "<div class=\"popup-success\">$lang_alert_success</div>";

} else {

echo "<div class=\"popup-error\">$lang_alert_existing_file</div>";

}

} else {

echo "<div class=\"popup-error\">$lang_alert_empty_inputs</div>";

}

} else {

echo "<div class=\"popup-error\">$lang_alert_empty_inputs</div>";

}

}

?>

<h2><?php echo($lang_images); ?></h2>

<?php if ($f2==""): ?>

<form action="./admin.php?f=images&amp;a=add_image" method="post" enctype="multipart/form-data">

<div><strong><?php echo($lang_form_upload_image); ?>:</strong><?php if ($a=="add_image" AND $_FILES["file"]["name"]=="") { echo " <span class=\"input-error\">&nbsp;!&nbsp;</span>"; } ?>
<br />
<input type="file" name="file" />
<br />
<strong><?php echo($lang_form_section); ?>:</strong><?php if ($a=="add_image" AND $_POST["section"]=="") { echo " <span class=\"input-error\">&nbsp;!&nbsp;</span>"; } ?>
<br />
<select name="section">
<option value="">...</option>
<?php

$print=mysql_query("SELECT id, name FROM kcms_images_sections ORDER by name DESC");
while (list($id, $name) = mysql_fetch_row($print)) {
echo "<option value=\"$id\"";
if ($_POST["section"]==$id) { echo " selected=\"selected\""; }
echo ">$name</option>\n";
}

?>
</select>
<br />
<strong><?php echo($lang_form_resolution); ?>:</strong>
<br />
<input type="radio" name="changeres" value="0"<?php if ($a=="" OR $a=="add_image" AND $_POST["changeres"]==0) { echo " checked=\"checked\""; } ?> /> <?php echo($lang_form_default); ?>
<br />
<input type="radio" name="changeres" id="changeres" value="1"<?php if ($a=="add_image" AND $_POST["changeres"]==1) { echo " checked=\"checked\""; } ?> /> <?php echo($lang_form_change_width); ?> <input type="text" name="new_width" style="width: 30px;" onclick="getElementById('changeres').checked='checked'" value="<?php echo($_POST["new_width"]); ?>"<?php if ($a=="add_image" AND $_POST["new_width"]=="" AND $changeres==1) { echo " class=\"input-error\""; } ?> /> px
<br />
<input type="submit" name="submit" value="<?php echo($lang_button_save); ?>" /></div>

</form>

<?php

// tedy můžeš nastavit práva pro manipulaci se sekcemi obrázků (přidávací formulář)
// here you can set the rights for actions with images sections (addform)
if ($rights>=2):

?>

<form action="./admin.php?f=images&amp;a=add_section" method="post">

<div><strong><?php echo($lang_form_add_section); ?>:</strong><br /><input type="text" name="section" style="width: 150px;" /> <input type="checkbox" name="public" value="1" /> <?php echo($lang_form_public_gallery); ?> <input type="submit" name="submit" value="<?php echo($lang_button_save); ?>" /></div>

</form>

<?php endif; ?>

<table>
<tr>
<th style="width: 45%;"><?php echo($lang_form_section_name); ?></th>
<th style="width: 20%;"><?php echo($lang_form_public_gallery); ?></th>
<th style="width: 35%;"><?php echo($lang_form_options); ?></th>
</tr>
<?php

$print=mysql_query("SELECT id, name, public FROM kcms_images_sections ORDER by id DESC");
while (list($id, $name, $public) = mysql_fetch_row($print)) {

$count=mysql_query("SELECT COUNT(*) AS id FROM kcms_images WHERE section='".addslashes($id)."'");
$count=mysql_fetch_array($count);

echo "<tr>
<td>$name (".$count["id"].")</td>
<td style=\"text-align: center;\">";
if ($public==1) { echo "$lang_yes"; } else { echo "$lang_no"; }
echo "</td>
<td style=\"text-align: center;\"><input type=\"button\" value=\"$lang_button_browse\" onclick=\"parent.location='./admin.php?f=images&amp;f2=browse_section&amp;id=$id';\" />";

// tedy můžeš nastavit práva pro manipulaci se sekcemi obrázků (tlačítka)
// here you can set the rights for actions with images sections (buttons)
if ($rights>=2) {
echo " <input type=\"button\" value=\"$lang_button_edit\" onclick=\"parent.location='./admin.php?f=images&amp;f2=edit_section&amp;id=$id';\" /> <input type=\"button\" value=\"$lang_button_delete\" onclick=\"if (!confirm('$lang_alert_sure')) return false; parent.location='./admin.php?f=images&amp;a=delete_section&amp;id=$id';\" />";
}

echo "</td>
</tr>\n";

}

?>
</table>

<?php

endif; if ($f2=="edit_section"):

$section=mysql_query("SELECT name, public FROM kcms_images_sections WHERE id='".addslashes($id)."'");
$section=mysql_fetch_array($section);

?>

<form action="./admin.php?f=images&amp;a=rename_section&amp;id=<?php echo($id); ?>" method="post">

<div><strong><?php echo($lang_form_rename_section." ".$section["name"]); ?>:</strong><br /><input type="text" name="section" style="width: 150px;" value="<?php echo($section["name"]); ?>" /> <input type="checkbox" name="public" value="1"<?php if ($section["public"]==1) { echo " checked=\"checked\""; } ?> /> <?php echo($lang_form_public_gallery); ?> <input type="submit" name="submit" value="<?php echo($lang_button_rename); ?>" /></div>

</form>

<?php

endif; if ($f2=="edit_image"):

$image=mysql_query("SELECT file FROM kcms_images WHERE id='".addslashes($image_id)."'");
$image=mysql_fetch_array($image);

$filename=explode(".", $image["file"]);

?>

<form action="./admin.php?f=images&amp;a=edit_image&amp;image_id=<?php echo($image_id); ?>" method="post">

<div><strong><?php echo($lang_form_thumbnail); ?>:</strong><br /><?php echo "<a href=\"./gallery/".$image["file"]."\"><img src=\"./gallery/thumbs/".$filename[0].".jpg\" alt=\"$file\" /></a>"; ?></div>

<div><strong><?php echo($lang_form_section); ?>:</strong><br /><select name="section">
<?php

$print=mysql_query("SELECT id, name FROM kcms_images_sections ORDER by name DESC");
while (list($s_id, $name) = mysql_fetch_row($print)) {
echo "<option value=\"$s_id\"";
if ($id==$s_id) { echo " selected=\"selected\""; }
echo ">$name</option>\n";
}

?>
</select> <input type="submit" name="submit" value="<?php echo($lang_button_save); ?>" /></div>

</form>

<?php

endif; if ($f2=="browse_section"):

$section=mysql_query("SELECT name FROM kcms_images_sections WHERE id='".addslashes($id)."'");
$section=mysql_fetch_array($section);

?>

<table>
<tr>
<th colspan="6" class="table-title"><?php echo($section["name"]); ?></th>
</tr>
<tr>
<th><?php echo($lang_form_thumbnail); ?></th>
<th><?php echo($lang_form_informations); ?></th>
<th style="width: 12%;"><?php echo($lang_form_options); ?></th>
<th><?php echo($lang_form_thumbnail); ?></th>
<th><?php echo($lang_form_informations); ?></th>
<th style="width: 12%;"><?php echo($lang_form_options); ?></th>
</tr>
<?php

$i=0;
$i2=0;

$count=mysql_query("SELECT COUNT(*) AS id FROM kcms_images WHERE section='".addslashes($id)."'");
$count=mysql_fetch_array($count);

$print=mysql_query("SELECT id, file, added FROM kcms_images WHERE section='".addslashes($id)."' ORDER by added DESC");
while (list($id, $file, $added) = mysql_fetch_row($print)) {

if ($i==0) { echo "<tr>\n"; }
$i++;
$i2++;

$filename=explode(".", $file);
if ($filename[1]=="jpg") {
$img_data=imagecreatefromjpeg("./gallery/$file");
}
if ($filename[1]=="png") {
$img_data=imagecreatefrompng("./gallery/$file");
}
if ($filename[1]=="gif") {
$img_data=imagecreatefromgif("./gallery/$file");
}

echo "<td><a href=\"./gallery/$file\"><img src=\"./gallery/thumbs/".$filename[0].".jpg\" alt=\"$file\" /></a></td>
<td>$file<span class=\"filedesc\">".date("d.m.Y, H:i", $added)."<br />".imagesx($img_data)."×".imagesy($img_data)." px<br />".round(filesize("./gallery/$file")/1024)." kB</span></td>
<td style=\"text-align: center;\"><input type=\"button\" value=\"$lang_button_edit\" onclick=\"parent.location='./admin.php?f=images&amp;f2=edit_image&amp;id=".$_GET["id"]."&amp;image_id=$id';\" /><input type=\"button\" value=\"$lang_button_delete\" onclick=\"if (!confirm('$lang_alert_sure')) return false; parent.location='./admin.php?f=images&amp;f2=browse_section&amp;id=".$_GET["id"]."&amp;a=delete_image&amp;image_id=$id';\" /></td>";

if ($i==2) { $i=0; echo "\n</tr>\n"; }
if ($i==1 AND $i2==$count["id"]) { echo "\n<td>&nbsp;</td>\n<td>&nbsp;</td>\n<td>&nbsp;</td>\n</tr>\n"; }

}

?>
</table>

<?php endif; else: $access_denied=1; endif; ?>