<?php
class Sections extends Admin {

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

  //---[CZ]: přidání sekce
  public function add_section() {
  
    if ($_POST["name"]!="") {

    $quick=$this->urls->transfer_to_seo($_POST["name"]);
    
    if (isset($_GET["id"])) { $higher=intval($_GET["id"]); } else { $higher=0; }

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

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

    if (isset($_GET["id"])) {
    $level=$this->db->query("SELECT level FROM kcms_sections WHERE id=".intval($_GET["id"]));
    $level=$this->db->fetch_array($level);
    $level=$level["level"]+1;
    } else { $level=0; }

    $position=$this->db->query("SELECT position FROM kcms_sections ORDER by position DESC");
    $position=$this->db->fetch_array($position);

    $position=$position["position"]+1;

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

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

    } else {

    $this->status_messages->print_error(LANG_THIS_SECTION_ALREADY_EXISTS);

    }

    } else {

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

    }
    
  }
  
  //---[CZ]: posunutí nahoru
  public function move_up() {
  
    if (isset($_GET["position"])) {
    
    if ($_GET["tab"]=="") { $table="kcms_sections"; $where=""; }
    if ($_GET["tab"]=="columns") { $table="kcms_columns"; $where=" col='".$this->db->escape($_GET["column"])."' AND"; }
  
    $sql=$this->db->query("UPDATE $table SET position=".(2*intval($_GET["position"])-1)." - position WHERE$where position IN ('".intval($_GET["position"])."', ".(intval($_GET["position"])-1).")");
    if ($sql) { $this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status_messages->print_error(LANG_AN_ERROR_OCCURED); }
    
    }
    
  }
  
  //---[CZ]: posunutí dolů
  public function move_down() {
  
    if (isset($_GET["position"])) {
    
    if ($_GET["tab"]=="") { $table="kcms_sections"; $where=""; }
    if ($_GET["tab"]=="columns") { $table="kcms_columns"; $where=" col='".$this->db->escape($_GET["column"])."' AND"; }
  
    $sql=$this->db->query("UPDATE $table SET position=".(2*intval($_GET["position"])+1)." - position WHERE$where position IN ('".intval($_GET["position"])."', ".(intval($_GET["position"])+1).")");
    if ($sql) { $this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status_messages->print_error(LANG_AN_ERROR_OCCURED); }
    
    }
    
  }
  
    
  //---[CZ]: smazání
  public function delete_section() {
  
    $sql=$this->db->query("DELETE FROM kcms_sections WHERE id=".intval($_GET["id"]));
    $this->delete_subsections(intval($_GET["id"]));
    
    if ($sql) { $this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status_messages->print_error(LANG_AN_ERROR_OCCURED); }
    
  }
  
  public function delete_subsections($higher) {
  
    $sql=$this->db->query("SELECT id FROM kcms_sections WHERE higher=$higher");
    while(list($id)=$this->db->fetch_row($sql)) {
    $sql=$this->db->query("DELETE FROM kcms_sections WHERE id=$id");
    $this->delete_subsections($id);
    }
    
  }
  
  //---[CZ]: smazání položky sloupce
  public function delete_column_item() {
  
    $sql=$this->db->query("DELETE FROM kcms_columns WHERE id=".intval($_GET["id"]));
    
    if ($sql) { $this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED); } else { $this->status_messages->print_error(LANG_AN_ERROR_OCCURED); }
    
  }
  
  //---[CZ]: editace sekce
  public function edit_section() {
  
    if ($_POST["name"]!="") {

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

    $old_quick=$this->db->query("SELECT quick FROM kcms_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_sections WHERE quick='".$this->db->escape($quick)."'");
    $check=$this->db->fetch_array($check);

    }
    
    //---[CZ]: definování proměnných
    if (!isset($_POST["highlight"])) { $_POST["highlight"]=0; }
    if (!isset($_POST["ntbl"])) { $_POST["ntbl"]=0; }
    if (!isset($_POST["hidden"])) { $_POST["hidden"]=0; }
    if (!isset($check["id"])) { $check["id"]=""; }

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

    $sql=$this->db->query("UPDATE kcms_sections SET quick='".$this->db->escape($quick)."', name='".$this->db->escape($_POST["name"])."', highlight=".intval($_POST["highlight"]).", outlink='".$this->db->escape($_POST["outlink"])."', ntbl=".intval($_POST["ntbl"]).", hidden=".intval($_POST["hidden"])." WHERE id=".intval($_GET["id"]));

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

    } else {

    $this->status_messages->print_error(LANG_THIS_SECTION_ALREADY_EXISTS);

    }

    } else {

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

    }
    
  }
  
  //---[CZ]: editace položky sloupce
  public function edit_column_item() {
  
    if ($_POST["name"]!="") {
    
    //---[CZ]: definování proměnných
    if (!isset($_POST["hidden"])) { $_POST["hidden"]=0; }

    $sql=$this->db->query("UPDATE kcms_columns SET name='".$this->db->escape($_POST["name"])."', content='".$this->db->escape($_POST["itemcontent"])."', hidden=".$this->db->escape($_POST["hidden"])." WHERE id=".intval($_GET["id"]));

    $this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
    } else { $this->status_messages->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS); }
    
  }
  
  // vypsání sekcí
  public function output_sections($higher) {
  
    $i=0;
  
    $sql=$this->db->query("SELECT id, name, highlight, outlink, position, level FROM kcms_sections WHERE higher=".intval($higher)." ORDER by position");
    while(list($id, $name, $highlight, $outlink, $position, $level)=$this->db->fetch_row($sql)) {

    $i++;

    echo "<tr>
    <td"; if ($level!=0) { echo " style=\"padding-left: ".($level*15)."px;\">&raquo;&nbsp;&nbsp;"; } else { echo ">"; } echo $this->db->unescape($name)."</td>
    <td style=\"text-align: center;\">";
    if ($i!=1) { echo "<a href=\"./admin.php?function=sections&amp;action=move_up&amp;position=".($position)."\" style=\"text-decoration: none;\">&uarr;</a> "; }
    if ($i<$this->db->num_rows($sql)) { echo " <a href=\"./admin.php?function=sections&amp;action=move_down&amp;position=".($position)."\" style=\"text-decoration: none;\">&darr;</a>"; }
    if ($i==1 AND $i==$this->db->num_rows($sql)) { echo "-"; }
    echo "</td>
    <td style=\"text-align: center;\">";
    if ($highlight==1) { echo LANG_YES; } else { echo LANG_NO; }
    echo "</td>
    <td style=\"text-align: center;\">";
    if ($outlink!="") { echo LANG_YES; } else { echo LANG_NO; }
    echo "</td>
    <td style=\"text-align: center;\"><input type=\"button\" class=\"yellowbutton\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?function=sections&amp;part=edit&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=sections&amp;action=delete&amp;id=$id';\" /></td>
    </tr>\n";

    $this->output_sections($id);

    }

  }
  
  //---[CZ]: získání dat pro editaci
  public function get($column) {
  
    if ($_GET["tab"]=="") { $table="kcms_sections"; }
    if ($_GET["tab"]=="columns") { $table="kcms_columns"; }
  
    $sql=$this->db->query("SELECT $column FROM $table WHERE id=".intval($_GET["id"]));
    $sql=$this->db->fetch_array($sql);
    return $this->db->unescape($sql["$column"]);
    
  }
  
  public function load_sections($higher, $current) {
  
    $sql=$this->db->query("SELECT id, name, level FROM kcms_sections WHERE higher=$higher ORDER BY position");
    while(list($id, $name, $level)=$this->db->fetch_row($sql)) {
    echo "<option value=\"$id\"";
    if ($id==$current) { echo " selected=\"selected\""; }
    echo ">";
    for ($i=0; $i<$level; $i++) {
    echo "&nbsp;&nbsp;&raquo;";
    }
    if ($level!=0) { echo "&nbsp;&nbsp;"; }
    echo $this->db->unescape($name)."</option>\n";
    $this->load_sections($id, $current);
    }
    
  }
  
  public function output_columns($column) {
  
    $i=0;
  
    $sql=$this->db->query("SELECT id, name, position FROM kcms_columns WHERE col='$column' ORDER by position");
    while($data=$this->db->fetch_array($sql)) {

    $i++;

    echo "<tr>
    <td>".$this->db->unescape($data["name"])."</td>
    <td style=\"text-align: center;\">";
    if ($i!=1) { echo "<a href=\"./admin.php?function=sections&amp;tab=columns&amp;action=move_up&amp;column=$column&amp;position=".$data["position"]."\" style=\"text-decoration: none;\">&uarr;</a> "; }
    if ($i<$this->db->num_rows($sql)) { echo " <a href=\"./admin.php?function=sections&amp;tab=columns&amp;action=move_down&amp;column=$column&amp;position=".$data["position"]."\" style=\"text-decoration: none;\">&darr;</a>"; }
    if ($i==1 AND $i==$this->db->num_rows($sql)) { echo "-"; }
    echo "<td style=\"text-align: center;\"><input type=\"button\" value=\"".LANG_EDIT."\" onclick=\"parent.location='./admin.php?function=sections&amp;tab=columns&amp;part=edit&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=sections&amp;tab=columns&amp;action=delete&amp;id=".$data["id"]."';\" /></td>
    </tr>\n";

    }

  }
  
  //---[CZ]: přidání položky do sloupce
  public function add_column_item() {
  
    if ($_POST["name"]!="") {

    $position=$this->db->query("SELECT position FROM kcms_columns WHERE col='".$this->db->escape($_POST["column"])."' ORDER by position DESC");
    $position=$this->db->fetch_array($position);

    $position=$position["position"]+1;

    $sql=$this->db->query("INSERT INTO kcms_columns (col, name, position) values ('".$this->db->escape($_POST["column"])."', '".$this->db->escape($_POST["name"])."', $position)");

    $this->status_messages->print_success(LANG_ACTION_WAS_SUCCESSFULLY_COMPLETED);
    
    } else { $this->status_messages->print_error(LANG_YOU_HAVE_NOT_FILLED_SOME_ITEMS); }
    
  }

}
?>