<?php
class Edit_content {

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

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

  //---[CZ]: úprava článku
  public function article() {
  
    if ($_POST["article_title"]!="" AND $_POST["article_link"]!="" AND $_POST["section"]!="" AND $_POST["article_type"]!="") {
    
    $check_access=$this->db->query("SELECT author FROM kcms_articles WHERE id=".intval($_GET["id"]));
    $check_access=$this->db->fetch_array($check_access);
    
    if ($check_access["author"]==$_SESSION["user_id"] OR $this->login->check_access("confirm_articles")==1) {
    
    $old_quick=$this->db->query("SELECT quick FROM kcms_articles WHERE id=".intval($_GET["id"]));
    $old_quick=$this->db->fetch_array($old_quick);
    
    if ($old_quick["quick"]!=$_POST["article_link"]) {

    $check_duplication=$this->db->query("SELECT id FROM kcms_articles WHERE quick='".$this->db->escape($_POST["article_link"])."'");
    $check_duplication=$this->db->fetch_array($check_duplication);
    
    } else { $check_duplication["id"]=""; }

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

    if ($_POST["pereximg"]=="/gallery/" OR $_POST["pereximg"]=="") { $pereximg=""; } else { $pereximg=$_POST["pereximg"]; }

    $sql=$this->db->query("UPDATE kcms_articles SET
    quick='".$this->db->escape($_POST["article_link"])."',
    title='".$this->db->escape(htmlspecialchars($_POST["article_title"], ENT_NOQUOTES))."',
    type=".intval($_POST["article_type"]).",
    hp=".intval($_POST["hp"]).",
    complete=".intval($_POST["complete"]).",
    confirmed=".intval($_POST["confirmed"]).",
    discussion=".intval($_POST["discussion"]).",
    show_article_info=".intval($_POST["show_article_info"]).",
    top=".intval($_POST["top"]).",
    section=".intval($_POST["section"]).",
    perex='".$this->db->escape($_POST["perex"])."',
    text='".$this->db->escape($_POST["text"])."',
    keywords='".$this->db->escape($_POST["keywords"])."',
    edited=".time().",
    pereximg='".$this->db->escape($pereximg)."'
    WHERE id=".intval($_GET["id"]));
    
    if ($_POST["cancel_publication"]==1 AND $this->login->check_access("publish_articles")==1) {
    
    $sql=$this->db->query("UPDATE kcms_articles SET published=0 WHERE id=".intval($_GET["id"]));
    
    } else if ($this->login->check_access("publish_articles")==1) {
    
    $sql=$this->db->query("UPDATE kcms_articles SET published=".mktime($_POST["hours"], $_POST["minutes"], 0, $_POST["month"], $_POST["day"], $_POST["year"]).", publisher=".$_SESSION["user_id"]." WHERE id=".intval($_GET["id"]));
    
    }

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

    } else {

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

    }
    
    } else {
    
    $this->status->print_error(LANG_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_EDITATION);
    
    }
    
    } else {
    
    $this->status->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);
    
    }
    
  }
  
  public function get_article_data($column) {
  
    $sql=$this->db->query("SELECT $column FROM kcms_articles WHERE id=".intval($_GET["id"]));
    $sql=$this->db->fetch_array($sql);
    
    return $this->db->unescape($sql["$column"]);
    
  }
  
  public function psb() {
  
    if ($this->get_article_data("published")==0) { $date=getdate(time()); } else { $date=getdate($this->get_article_data("published")); }
  
    echo "<select name=\"day\">";
    
    for ($i=1; $i<=31; $i++) {
    echo "<option value=\"$i\"";
    if ($date["mday"]==$i) { echo " selected=\"selected\""; }
    echo ">$i</option>\n";
    }

    echo "</select> . <select name=\"month\">";
    
    for ($i=1; $i<=12; $i++) {
    echo "<option value=\"$i\"";
    if ($date["mon"]==$i) { echo " selected=\"selected\""; }
    echo ">$i</option>\n";
    }

    echo "</select> . <select name=\"year\">";

    for ($i=2000; $i<=2020; $i++) {
    echo "<option value=\"$i\"";
    if ($date["year"]==$i) { echo " selected=\"selected\""; }
    echo ">$i</option>\n";
    }
    
    if ($date["hours"]>=0 AND $date["hours"]<=9) { $date["hours"]="0".$date["hours"]; }
    if ($date["minutes"]>=0 AND $date["minutes"]<=9) { $date["minutes"]="0".$date["minutes"]; }

    echo "</select>&nbsp;&nbsp;&nbsp;<input type=\"text\" name=\"hours\" value=\"".$date["hours"]."\" style=\"width: 20px;\" /> : <input type=\"text\" name=\"minutes\" value=\"".$date["minutes"]."\" style=\"width: 20px;\" />";
    
  }

}
?>