<?php
class Images {

  private $db;
  private $urls;
  private $status;

  public function __construct() {
    $this->db = new Db_layer();
    $this->urls = new Urls();
    $this->status = new Status_messages();
  }

  //---[CZ]: nahrání obrázku
  public function upload() {
  
    if ($_FILES["file"]["name"]!="" AND $_POST["gallery"]!="") {

    $ok=0;

    $check=$this->db->query("SELECT COUNT(*) AS c FROM kcms_images WHERE file='".$this->db->escape($_FILES["file"]["name"])."'");
    $check=$this->db->fetch_array($check);

    if ($check["c"]==0) {

    $explode=explode(".", $_FILES["file"]["name"]);

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

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

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

    if ($_POST["changeres"]==0) {

    move_uploaded_file($_FILES["file"]["tmp_name"], "./gallery/".$_FILES["file"]["name"]);

    $ok=1;

    } else if ($_POST["changeres"]==1 AND $_POST["new_width"]!="") {

    $width=imagesx($img_data);
    $height=imagesy($img_data);
    $i=$_POST["new_width"]/$width;
    $new_height=$i*$height;

    $create=imagecreatetruecolor($_POST["new_width"], $new_height);
    imagecopyresampled($create, $img_data, 0, 0, 0, 0, $_POST["new_width"], $new_height, imagesx($img_data), imagesy($img_data));

    if ($explode[1]=="jpg" OR $explode[1]=="JPG") {
    imagejpeg($create, "./gallery/".$_FILES["file"]["name"], 100);
    }
    if ($explode[1]=="png" OR $explode[1]=="PNG") {
    imagepng($create, "./gallery/".$_FILES["file"]["name"], 0);
    }
    if ($explode[1]=="gif" OR $explode[1]=="GIF") {
    imagegif($create, "./gallery/".$_FILES["file"]["name"]);
    }

    $ok=1;

    } else {

    $this->status->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);

    }

    if ($ok==1) {

    if (imagesx($img_data)>imagesy($img_data)) {
    $i=120/imagesx($img_data);
    $new_height=$i*imagesy($img_data);
    $width=120;
    } else {
    $i=90/imagesx($img_data);
    $new_height=$i*imagesy($img_data);
    $width=90;
    }

    $create=imagecreatetruecolor($width, $new_height);
    imagecopyresampled($create, $img_data, 0, 0, 0, 0, $width, $new_height, imagesx($img_data), imagesy($img_data));
    imagejpeg($create, "./gallery/thumbs/".$explode[0].".jpg", 90);

    $sql=$this->db->query("INSERT INTO kcms_images (section, file, added, title, uploader) values (".intval($_POST["gallery"]).", '".$this->db->escape($_FILES["file"]["name"])."', ".time().", '".$this->db->escape($_POST["title"])."', ".$_SESSION["user_id"].")");

    imagedestroy($img_data);

    }

    $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);

    } else {

    $this->status->print_error(LANG_THIS_FILE_ALREADY_EXISTS);

    }

    } else {

    $this->status->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);

    }
    
  }
  
  //---[CZ]: smazání galerie
  public function delete_gallery() {
  
    $sql=$this->db->query("SELECT file FROM kcms_images WHERE section=".intval($_GET["id"]));
    while(list($file)=$this->db->fetch_row($sql)) {

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

    $sql=$this->db->query("DELETE FROM kcms_images WHERE section=".intval($_GET["id"]));
  
    $sql=$this->db->query("DELETE FROM kcms_images_sections WHERE id=".intval($_GET["id"]));
    
    $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
  }
  
  //---[CZ]: smazání obrázku
  public function delete_image($file) {

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

    $sql=$this->db->query("DELETE FROM kcms_images WHERE file='".$this->db->escape($file)."'");
    
    $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
  }
  
  //---[CZ]: editace galerie
  public function edit_gallery() {
  
    if ($_POST["gallery"]!="") {
    
    //---[CZ]: definování proměnných
    if (!isset($_POST["public"])) { $_POST["public"]=0; }
    
    $quick=$this->urls->transfer_to_seo($_POST["gallery"]);

    $old_quick=$this->db->query("SELECT quick FROM kcms_images_sections WHERE id=".intval($_GET["id"]));
    $old_quick=$this->db->fetch_array($old_quick);

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

    $check=$this->db->query("SELECT id FROM kcms_images_sections WHERE quick='".$this->db->escape($quick)."'");
    $check=$this->db->fetch_array($check);

    } else { $check["id"]=""; }
    
    if ($check["id"]=="") {
  
    $sql=$this->db->query("UPDATE kcms_images_sections SET quick='".$this->db->escape($quick)."', name='".$this->db->escape($_POST["gallery"])."', public=".intval($_POST["public"])." WHERE id=".intval($_GET["id"]));
    
    $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
    } else {
    
    $this->status->print_error(LANG_THIS_GALLERY_ALREADY_EXISTS);
    
    }
    
    } else {
    
    $this->status->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);
    
    }
    
  }
  
  //---[CZ]: editace obrázku
  public function edit_image() {
  
    $sql=$this->db->query("UPDATE kcms_images SET title='".$this->db->escape($_POST["title"])."', section=".intval($_POST["gallery"]).", uploader=".intval($_POST["uploader"])." WHERE id=".intval($_GET["id"]));
    
    $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
  }
  
  //---[CZ]: načteme galerie pro selectbox
  public function load_galleries($current) {
  
    $sql=$this->db->query("SELECT id, name FROM kcms_images_sections ORDER by name");
    while(list($id, $name)=$this->db->fetch_row($sql)) {
    echo "<option value=\"$id\"";
    if ($current==$id) { echo " selected=\"selected\""; }
    echo ">".$this->db->unescape($name)."</option>\n";
    }
    
  }
  
  //---[CZ]: přidání galerie
  public function add_gallery() {
  
    if ($_POST["gallery"]!="") {

    $quick=$this->urls->transfer_to_seo($_POST["gallery"]);

    $check=$this->db->query("SELECT id FROM kcms_images_sections WHERE quick='$quick'");
    $check=$this->db->fetch_array($check);

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

    $sql=$this->db->query("INSERT INTO kcms_images_sections (quick, name) values ('$quick', '".$this->db->escape($_POST["gallery"])."')");

    $this->status->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);

    } else {

    $this->status->print_error(LANG_THIS_GALLERY_ALREADY_EXISTS);

    }

    } else {

    $this->status->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);

    }
    
  }
  
  //---[CZ]: vypsání galerií
  public function print_galleries() {
  
    $print=$this->db->query("SELECT id, name, public FROM kcms_images_sections ORDER by name");
    while(list($id, $name, $public)=$this->db->fetch_row($print)) {

    $count=$this->db->query("SELECT COUNT(*) AS id FROM kcms_images WHERE section=$id");
    $count=$this->db->fetch_array($count);

    echo "<tr>
    <td>".$this->db->unescape($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\" class=\"yellowbutton\" value=\"".LANG_BROWSE."\" onclick=\"parent.location='./admin.php?function=images&amp;part=browse_gallery&amp;id=$id';\" /> <input type=\"button\" class=\"yellowbutton\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?function=images&amp;part=edit_gallery&amp;id=$id';\" /> <input type=\"button\" value=\"".LANG_DELETE."\" class=\"redbutton\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?function=images&amp;action=delete_gallery&amp;id=$id';\" /></td>
    </tr>\n";

    }
  
  }
  
  //---[CZ]: vypsání miniatur obrázků
  public function print_images() {
  
    $i=0;
    
    if ($_GET["function"]=="") { $location="index"; $on_one_row=3; } else { $location="admin"; $on_one_row=4; }
    
    if ($_GET["function"]=="my_content") {
    $where="uploader=".$_SESSION["user_id"];
    } else {
    if ($location=="admin") {
    $where="section=".intval($_GET["id"]);
    } else {
    $sql=$this->db->query("SELECT id FROM kcms_images_sections WHERE quick='".$this->db->escape($_GET["gallery"])."'");
    $sql=$this->db->fetch_array($sql);
    $where="section=".$sql["id"];
    }
    }

    $count=$this->db->query("SELECT COUNT(*) AS id FROM kcms_images WHERE $where");
    $count=$this->db->fetch_array($count);

    $print=$this->db->query("SELECT id, file, added, title, section, uploader FROM kcms_images WHERE $where ORDER by added DESC");
    while(list($id, $file, $added, $title, $section, $uploader)=$this->db->fetch_row($print)) {
    
    $user=$this->db->query("SELECT login FROM ".TABLE_USERS." WHERE id=$uploader");
    $user=$this->db->fetch_array($user);
    $i++;

    $filename=explode(".", $file);
    if ($filename[1]=="jpg" OR $filename[1]=="JPG") {
    $img_data=imagecreatefromjpeg("./gallery/$file");
    }
    if ($filename[1]=="png" OR $filename[1]=="PNG") {
    $img_data=imagecreatefrompng("./gallery/$file");
    }
    if ($filename[1]=="gif" OR $filename[1]=="GIF") {
    $img_data=imagecreatefromgif("./gallery/$file");
    }
    
    $gallery=$this->db->query("SELECT name FROM kcms_images_sections WHERE id=$section");
    $gallery=$this->db->fetch_array($gallery);

    echo "<div class=\"gallery-cell\""; if ($location=="index" AND $i==1) { echo " style=\"margin-left: 0px;\""; } echo ">
    <span class=\"bubble\">";
    
    if ($title!="" AND $location=="admin") { echo "<strong>".LANG_TITLE.":</strong> $title<br />"; }
    if ($location=="index") { echo "<strong>".LANG_FILE.":</strong> $file<br />"; }
    
    echo "<strong>".LANG_UPLOADER.":</strong> ".$user["login"]." (".date("j.n.Y H:i", $added).")<br /><strong>".LANG_SIZE.":</strong> ".imagesx($img_data)."&times;".imagesy($img_data)." px, ".round(filesize("./gallery/$file")/1024)." kB</span>";
    
    if ($location=="admin") { echo "<input type=\"button\" class=\"yellowbutton\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?function=images&amp;part=edit_image&amp;id=$id';\" /> <input type=\"button\" class=\"redbutton\" value=\"".LANG_DELETE."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?function=images&amp;part=browse_gallery&amp;id=$section&amp;action=delete_image&amp;file=".rawurlencode($file)."';\" /><br /><span class=\"desc\">"; }
    
    echo "<a href=\"".$this->urls->root()."gallery/$file\"><img src=\"".$this->urls->root()."gallery/thumbs/".$filename[0].".jpg\" alt=\"$file\" /></a>";
    
    if ($location=="admin") { echo "<br /><strong>$file</strong></span>"; } elseif ($title!="") { echo "<br /><span class=\"small-text\">$title</span>"; }
    
    echo "</div>\n\n";

    if ($i==$on_one_row) { $i=0; echo "<div class=\"float-ending\"><!-- --></div>\n\n"; }

    }
    
    if ($i!=$on_one_row) { echo "<div class=\"float-ending\"><!-- --></div>\n\n"; }
    
    if ($this->db->num_rows($print)==0) { echo "<p style=\"text-align: center;\"><em>".LANG_NO_IMAGES_IN_THIS_GALLERY."</em></p>"; }
  
  }
  
  //---[CZ]: získání dat pro editaci
  public function read($column, $table) {
  
    $sql=$this->db->query("SELECT $column FROM kcms_$table WHERE id=".intval($_GET["id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
    
  }
  
  //---[CZ]: načteme uživatele
  public function load_users($current) {
  
    $sql=$this->db->query("SELECT id, login FROM ".TABLE_USERS." WHERE rights>0 ORDER by login");
    while(list($id, $login)=$this->db->fetch_row($sql)) {
    echo "<option value=\"$id\"";
    if ($current==$id) { echo " selected=\"selected\""; }
    echo ">".$this->db->unescape($login)."</option>\n";
    }
    
  }

}
?>