<?php

require_once("_backend.php");

class Admin extends Backend {

  function Admin() {
    
    parent::Backend();
    
    $this->load->model('user');
    
    $this->_prepareLangPanel("admin");
    
    if (!$this->user->isLogedIn() && $this->uri->segment(2) != 'login')
      redirect('admin/login/' . $this->jazyk);
    
    $this->_assign("header_lang", $this->language->GetLang("admin_head"));
    
    $this->_prepareLangPanel("admin");
  }
  
  function index() {
    $this->_display();
  }
  
  function login() {
    if ($this->uri->segment(4) == 'login' && isset($_POST['submit']))
    {
      if ($this->user->prihlas($_POST['login_name'], $_POST['login_pass']))
        redirect('admin/index/' . $this->jazyk);
    }
    
    $this->_assign('page_class', 'login');
    
    $this->_setBody('login');
    
    $login_lang = $this->language->GetLang("admin_login");
    
    $this->_assign("login_lang", $login_lang);
    
    $this->_setHeader($login_lang['header_login']);
  
    $this->_display();
  }
  
  function logout() {
    $this->user->odhlas();
    
    redirect("admin/login/" . $this->jazyk);
  }
  
  function settings() 
  {
    if (isset($_POST["submit_settings"]))
    {
      //Priprava nastaveni na vlozenie
      $set = array();
      $set[] = array('nazov' => 'hlavny_jazyk', 'hodnota' => $_POST['hlavny_jazyk']);
      $set[] = array('nazov' => 'firiem_na_stranku', 'hodnota' => $_POST['firiem_na_stranku']);
      $set[] = array('nazov' => 'strankovanie_pocet_listov', 'hodnota' => $_POST['strankovanie_pocet_listov']);
      $set[] = array('nazov' => 'meno_admina', 'hodnota' => $_POST['meno_admina']);
      $set[] = array('nazov' => 'heslo_admina', 'hodnota' => (empty($_POST['heslo_admina']) ? $this->nastavenie->vyber('heslo_admina') : ($_POST['heslo_admina'] == $_POST['heslo_admina_overenie'] ? sha1($_POST['heslo_admina']) : $this->nastavenie->vyber('heslo_admina'))));
      
      $this->nastavenie->zapis($set);
      redirect("admin/settings/" .$this->jazyk);
    }
    
    $this->_assign('page_class', 'settings');
    
    $this->_setBody('settings');
    
    $lang = $this->language->GetLang("admin_settings");
    
    $this->_assign("settings_lang", $lang);
    
    $this->_setHeader($lang['header_settings']);
    $this->_assign("settings", $this->nastavenie->vyber());
    
    $this->_display();
  }
  
  function firms()
  {
    $this->load->model('firma');
    
    if (isset($_POST['delete']))
    { 
      //$this->_debug($_POST);
      if (isset($_POST['firm_id']))
      {
        $this->firma->odstran($_POST['firm_id']);
      }
    }
    
    $this->_assign('page_class', 'firms');
    
    $this->_setBody('firms_list');
    
    $this->_assign('firms', $this->firma->vyber($this->jazyk,0,50,0,0,false));
    $lang = $this->language->GetLang("admin_firms");
    $this->_assign("firms_lang", $lang);
    
    $this->_setHeader($lang['header_firms']);
    
    $this->_display();
  }
  
  function firm_edit()
  {
    $this->_assign('page_class', 'firms');
    
    $this->load->model('firma');
    $this->load->model('kategoria');
    
    if (isset($_POST["submit_firm"]))
    {
      $this->firma->uprav($this->uri->segment(4), $_POST);
      redirect("admin/firms/".$this->jazyk);
    }
    
    $firma = $this->firma->vyber($this->jazyk, 0, 50, 0, $this->uri->segment(4));
    if (count($firma) > 0)
      $firma = $firma[0];
    
    $this->_setBody('firm_form');
    $this->_assign('action', base_url() . 'admin/firm_edit/' . $this->jazyk .'/'. $this->uri->segment(4));
    $this->_assign('firm', $firma);
    $this->_assign('category', $this->kategoria->dataFiltru($this->jazyk));
    
    $lang = $this->language->GetLang("admin_firms");
    $this->_assign("firms_lang", $lang);
    
    $this->load->model("images");
    
    $billboards = $this->images->getBillboards();
    $logos = $this->images->getLogos();
    
    $this->_assign("billboards", $billboards);
    $this->_assign("logos", $logos);
    
    $this->_setHeader($lang['header_firms_edit']);
    
    $this->_display();
  }
  
  function new_firm()
  {
    $this->_assign('page_class', 'firms');
    
    $this->load->model('firma');
    $this->load->model('kategoria');
    
    if (isset($_POST["submit_firm"]))
    {
      $this->firma->vloz($_POST);
      redirect("admin/firms/".$this->jazyk);
    }
    
    $lang = $this->language->GetLang("admin_firms");
    $this->_assign("firms_lang", $lang);
    
    $this->_setHeader($lang['header_firms_new']);
    
    $this->_setBody('firm_form');
    $this->_assign('category', $this->kategoria->dataFiltru($this->jazyk));
    
    $this->load->model("images");
    
    $billboards = $this->images->getBillboards();
    $logos = $this->images->getLogos();
    
    $this->_assign("billboards", $billboards);
    $this->_assign("logos", $logos);
    
    $this->_display();
  }
  
  function categories()
  {
    $this->_assign('page_class', 'categories');
    
    $this->load->model('kategoria');
    
    if (isset($_POST['hide_selected']))
    {
      if (isset($_POST['category_id']))
        $this->kategoria->skryOznacene($_POST['category_id']);
    }
    
    if (isset($_POST['show_selected']))
    {
      if (isset($_POST['category_id']))
        $this->kategoria->zobrazOznacene($_POST['category_id']);
    }
    
    $lang = $this->language->getLang("admin_categories");
    
    $this->_setHeader($lang["header_categories"]);
    $this->_assign("category_lang", $lang);
    
    $this->_assign("categories", $this->kategoria->dataFiltru($this->jazyk));
    
    $this->_setBody('categories_list');
    
    $this->_display();
  }
  
  function category_edit() {
    $this->_assign('page_class', 'categories');
    
    $this->load->model('kategoria');
    
    if (isset($_POST['submit_category_form']))
    {
      $this->kategoria->uprav($this->uri->segment(4), $_POST);
      redirect("admin/categories/".$this->jazyk);
      die();
    }
    
    $lang = $this->language->getLang("admin_categories");
    
    $this->_setHeader($lang["header_category_edit"]);
    $this->_assign("category_lang", $lang);
    
    $this->_assign("category", $this->kategoria->vyberId($this->uri->segment(4)));
    $this->_assign("action", base_url() . "admin/category_edit/" . $this->jazyk . "/". $this->uri->segment(4));
    
    $this->_setBody('category_form');
    
    $this->_display();
  }
  
  function category_new() {
    $this->_assign('page_class', 'categories');
    
    $this->load->model('kategoria');
    
    if (isset($_POST['submit_category_form']))
    {
      $this->kategoria->vloz($_POST);
      redirect("admin/categories/".$this->jazyk);
      die();
    }
    
    $lang = $this->language->getLang("admin_categories");
    
    $this->_setHeader($lang["header_category_new"]);
    $this->_assign("category_lang", $lang);
    
    $this->_assign("action", base_url() . "admin/category_new/" . $this->jazyk);
    
    $this->_setBody('category_form');
    
    $this->_display();
  }
  
  function articles()
  {
    $this->_assign('page_class', 'articles');
    
    $this->load->model("clanok");
    
    if (isset($_POST['articles_submit']))
    {
      $this->clanok->uloz($this->uri->segment(4), $_POST);
    }
    
    $lang = $this->language->getLang('admin_articles');
    $this->_setHeader($lang['header_articles']);
    $this->_assign('articles_lang', $lang);
    
    $this->_assign("articles", $this->clanok->vyberVsetko());
    
    $this->_setBody("articles_form");
    
    $this->_display();
  }
  
  function select_image()
  {
    $images = array();
    $search = "";
    
    if (isset($_POST['submit_search']))
    {
      $this->load->model("images");
      
      $search = $_POST['search'];
      $images = $this->images->getBillboards($search);
    }
  
    $lang = $this->language->GetLang("admin_firms");
    $this->_assign("firms_lang", $lang);
  
    $this->_assign('imgs', $images);
    $this->_assign('path', 'firms_images');
    $this->_assign('action', base_url().'admin/select_image/'.$this->jazyk);
    $this->_assign('search', $search);
    $this->_assign('element', 'obrazok');
    
    $this->_setBody('img_select');
    
    $this->_displayPopup();
  }
  
  function select_logo()
  {
    $images = array();
    $search = "";
    
    if (isset($_POST['submit_search']))
    {
      $this->load->model("images");
      
      $search = $_POST['search'];
      $images = $this->images->getLogos($search);
    }
  
    $lang = $this->language->GetLang("admin_firms");
    $this->_assign("firms_lang", $lang);
  
    $this->_assign('imgs', $images);
    $this->_assign('path', 'firms_logos');
    $this->_assign('action', base_url().'admin/select_logo/'.$this->jazyk);
    $this->_assign('search', $search);
    $this->_assign('element', 'logo');
    
    $this->_setBody('img_select');
    
    $this->_displayPopup();
  }
}

?>