<?php
class Articles extends Admin {

  public function __construct() { parent::__construct(); }

  //---[CZ]: výpis
  public function output() {
  
    echo "<table>
    <tr>
    <th style=\"width: 40%;\">".LANG_ARTICLE_TITLE."</th>
    <th style=\"width: 17%;\" class=\"small-text\">".LANG_AUTHOR."<br />".LANG_SECTION."</th>
    <th style=\"width: 20%;\" class=\"small-text\">".LANG_CREATION_DATE."<br />".LANG_PUBLICATION_DATE."</th>
    <th style=\"width: 23%;\">".LANG_OPTIONS."</th>
    </tr>";
  
    if ($_GET["function"]=="archive") {
    if (!isset($_GET["month"])) { $_GET["month"]=date("m", time()); }
    if (!isset($_GET["year"])) { $_GET["year"]=date("Y", time()); }
    $where="published>".mktime(0, 0, 0, $_GET["month"], 1, $_GET["year"])." AND published<".mktime(23, 59, 59, $_GET["month"], 31, $_GET["year"]);
    }
    if ($_GET["function"]=="my_content") { $where="author=".intval($_SESSION["user_id"]); }
    if ($_GET["function"]=="unpublished_articles") { $where="published=0"; }
    if ($_GET["function"]=="unverified_articles") { $where="confirmed=0"; }
    
    $sql=$this->db->query("SELECT id, quick, title, section, added, published, author FROM kcms_articles WHERE $where ORDER by added DESC");
    while($data=$this->db->fetch_array($sql)) {
    
      echo "<tr>
    <td><a href=\"".$this->urls->article($data["quick"])."\" target=\"_blank\">".$this->db->unescape($data["title"])."</a></td>
    <td style=\"text-align: center;\" class=\"small-text\">".parent::author_link($data["author"])."<br />".parent::article_section($data["section"])."</td>
    <td style=\"text-align: center;\" class=\"small-text\">".date("d.m.Y, H:i", $data["added"])."<br />";
    if ($data["published"]!=0) { echo (date("d.m.Y, H:i", $data["published"])); } else { echo LANG_NOT_PUBLISHED_YET; }
    echo "</td>
    <td style=\"text-align: center;\">";
    
    if ($_GET["function"]=="archive") { echo "<input type=\"button\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?function=edit_content&amp;tab=article&amp;id=".$data["id"]."';\" /><input type=\"button\" class=\"redbutton\" value=\"".LANG_DELETE."\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?function=archive&amp;month=".$_GET["month"]."&amp;year=".$_GET["year"]."&amp;action=delete_article&amp;id=".$data["id"]."';\" />"; }
    
    if ($_GET["function"]=="my_content") { echo "<input type=\"button\" value=\"".LANG_EDIT."\" class=\"yellowbutton\" onclick=\"parent.location='./admin.php?function=edit_content&amp;tab=article&amp;id=".$data["id"]."';\" /><input type=\"button\" value=\"".LANG_DELETE."\" class=\"redbutton\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?function=my_content&amp;action=delete_article&amp;id=".$data["id"]."';\" />"; }
    
    if ($_GET["function"]=="unpublished_articles") { echo "<input type=\"button\" value=\"".LANG_EDIT."/".LANG_PUBLISH."\" class=\"yellowbutton\" onclick=\"parent.location='./admin.php?function=edit_content&amp;tab=article&amp;id=".$data["id"]."';\" /><input type=\"button\" value=\"".LANG_DELETE."\" class=\"redbutton\" onclick=\"if (!confirm('".LANG_ARE_YOU_SURE."')) return false; parent.location='./admin.php?function=unpublished_articles&amp;action=delete&amp;id=".$data["id"]."';\" />"; }
    
    if ($_GET["function"]=="unverified_articles") { echo "<input type=\"button\" value=\"".LANG_EDIT."\" class=\"yellowbutton\" onclick=\"parent.location='./admin.php?function=edit_content&amp;tab=article&amp;id=".$data["id"]."';\" /><input type=\"button\" value=\"".LANG_CONFIRM."\" class=\"greenbutton\" onclick=\"parent.location='./admin.php?function=unverified_articles&amp;action=confirm&amp;id=".$data["id"]."';\" />"; }
    
    echo "</td>
    </tr>\n";

    }
    
    if ($this->db->num_rows($sql)==0) { echo "<tr><td colspan=\"4\" style=\"text-align: center;\"><em>".LANG_NO_ARTICLES_FOUND."</em></td></tr>"; }
    
    echo "</table>";
    
  }
  
  //---[CZ]: přidání
  public function add() {
  
    if ($_POST["article_title"]!="" AND $_POST["article_link"]!="" AND $_POST["section"]!="" AND $_POST["article_type"]!="") {
  
    $check=$this->db->query("SELECT id FROM kcms_articles WHERE quick='".$this->db->escape($_POST["article_link"])."'");
    $check=$this->db->fetch_array($check);

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

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

    $sql=$this->db->query("INSERT INTO kcms_articles (quick, title, type, hp, complete, confirmed, discussion, section, perex, text, added, author, pereximg, top, show_article_info, keywords, poll) values (
    '".$this->db->escape($_POST["article_link"])."',
    '".$this->db->escape(htmlspecialchars($_POST["article_title"], ENT_NOQUOTES))."',
    ".intval($_POST["article_type"]).",
    ".intval($_POST["hp"]).",
    ".intval($_POST["complete"]).",
    ".intval($_POST["confirmed"]).",
    ".intval($_POST["discussion"]).",
    ".intval($_POST["section"]).",
    '".$this->db->escape($_POST["perex"])."',
    '".$this->db->escape($_POST["text"])."',
    ".time().",
    ".$_SESSION["user_id"].",
    '".$this->db->escape($pereximg)."',
    ".intval($_POST["top"]).",
    ".intval($_POST["show_article_info"]).",
    '".$this->db->escape($_POST["keywords"])."',
    ".intval($_POST["poll"])."
    )");

    $inserted=$this->db->query("SELECT id FROM kcms_articles ORDER BY id DESC");
    $inserted=$this->db->fetch_array($inserted);

    if ($this->login->check_access("publish_articles")==1 AND $_POST["publish_now"]==1) {
    $sql=$this->db->query("UPDATE kcms_articles SET published=".time().", publisher=".$_SESSION["user_id"]." WHERE id=".$inserted["id"]);
    }

    parent::$this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);

    } else {

    parent::$this->status_messages->print_error(LANG_THIS_ARTICLE_ALREADY_EXISTS);

    }
    
    } else {
    
    parent::$this->status_messages->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);
    
    }
    
  }
  
  //---[CZ]: smazání
  public function delete() {

    if ($_GET["id"]!="") {
    if ($this->login->check_access("archive")==0) { $sql=$this->db->query("DELETE FROM kcms_articles WHERE author=".$_SESSION["user_id"]." AND id=".intval($_GET["id"])); }
    if ($this->login->check_access("archive")==1) { $sql=$this->db->query("DELETE FROM kcms_articles WHERE id=".intval($_GET["id"])); }
    if ($sql) {
    $sql=$this->db->query("DELETE FROM kcms_comments WHERE article=".intval($_GET["id"]));
    }
    }
    
    parent::$this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
  }
  
  //---[CZ]: úprava článku
  public function update() {
  
    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)."',
    poll=".intval($_POST["poll"])."
    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 AND $_POST["publish"]==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"]));
    
    }

    parent::$this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);

    } else {

    parent::$this->status_messages->print_error(LANG_THIS_ARTICLE_ALREADY_EXISTS);

    }
    
    } else {
    
    parent::$this->status_messages->print_error(LANG_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_EDITATION);
    
    }
    
    } else {
    
    parent::$this->status_messages->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS);
    
    }
    
  }
  
  public function get($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 confirm() {
  
    $sql=$this->db->query("UPDATE kcms_articles SET confirmed=1 WHERE id=".intval($_GET["id"]));
    
  }
  
  public function publish_select_boxes() {
  
    if ($this->get("published")==0) { $date=getdate(time()); } else { $date=getdate($this->get("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;\" />";
    
    if ($this->get("published")==0) { echo "&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"publish\" value=\"1\" /> ".LANG_PUBLISH; } else { echo "<input type=\"hidden\" name=\"publish\" value=\"1\" />"; }
    
  }

}
?>