<?php
class Urls {

  private $db;

  public function __construct() {
    $this->db = new Db_layer();
  }
  
  //---[CZ]: vytvoření SEO URL
  public function transfer_to_seo($data) {
    $transfer=array("á"=>"a", "ä"=>"a", "č"=>"c", "ď"=>"d", "é"=>"e", "ě"=>"e", "ë"=>"e", "í"=>"i", "&#239;"=>"i", "ň"=>"n", "ó"=>"o", "ö"=>"o", "ř"=>"r", "š"=>"s", "ť"=>"t", "ú"=>"u", "ů"=>"u", "ü"=>"u", "ý"=>"y", "&#255;"=>"y", "ž"=>"z", "Á"=>"A", "Ä"=>"A", "Č"=>"C", "Ď"=>"D", "É"=>"E", "Ě"=>"E", "Ë"=>"E", "Í"=>"I", "&#207;"=>"I", "Ň"=>"N", "Ó"=>"O", "Ö"=>"O", "Ř"=>"R", "Š"=>"S","Ť"=>"T", "Ú"=>"U", "Ů"=>"U", "Ü"=>"U", "Ý"=>"Y", "&#376;"=>"Y", "Ž"=>"Z");

    $data=strtr($data, $transfer);
    $data=strtolower($data);
    $data=preg_replace("/[^[:alpha:][:digit:]]/", "-", $data);
    $data=trim($data, "-");
    $data=preg_replace("/[-]+/", "-", $data);
    return $data;
  }
  
  //---[CZ]: vypsání zanoření do sekce
  public function section_name($higher, $i, $path) {
    global $path;
    $sql=$this->db->query("SELECT id, quick, name, higher FROM kcms_sections WHERE id=$higher ORDER BY position");
    while(list($id, $quick, $name, $higher)=$this->db->fetch_row($sql)) {
    $i++;
    $path="$name &raquo; $path";
    $this->section_name($higher, $i, $path);
    }
    return substr($path, 0, -9);
  }
  
  //---[CZ]: sekce
  public function section($i) {
    
    if (URL_TYPE=="static") { $url="/sections/$i/"; }
    if (URL_TYPE=="dynamic") { $url="./index.php?section=$i"; }
    
    return $url;
    
  }
  
  //---[CZ]: galerie
  public function gallery($i) {
    
    if (URL_TYPE=="static") { $url="/galleries/$i/"; }
    if (URL_TYPE=="dynamic") { $url="./index.php?gallery=$i"; }
    
    return $url;
    
  }
  
  //---[CZ]: článek
  public function article($i) {
    
    if (URL_TYPE=="static") { $url="/articles/$i/"; }
    if (URL_TYPE=="dynamic") { $url="./index.php?article=$i"; }
    
    return $url;
    
  }
  
  //---[CZ]: profil
  public function profile($i) {
    if (URL_TYPE=="static") { $url="/profiles/$i/"; }
    if (URL_TYPE=="dynamic") { $url="./index.php?profile=$i"; }
    return $url;
  }
  
  //---[CZ]: profil
  public function root() {
    if (URL_TYPE=="static") { $url="/"; }
    if (URL_TYPE=="dynamic") { $url="./"; }
    return $url;
  }
  
  //---[CZ]: profil
  public function rootJS() {
    if (URL_TYPE=="static") { $url="../../"; }
    if (URL_TYPE=="dynamic") { $url="./"; }
    return $url;
  }
  
  //---[CZ]: funkce
  public function fction($i) {
    if (URL_TYPE=="static") { $url="/$i/"; }
    if (URL_TYPE=="dynamic") { $url="./index.php?function=$i"; }
    return $url;
  }
  
  //---[CZ]: vyhledávání
  public function search() {
  
    if (URL_TYPE=="static") { header("Location: /search/?string=".rawurlencode($_POST["search"])); }
    if (URL_TYPE=="dynamic") { header("Location: ./index.php?function=search&string=".rawurlencode($_POST["search"])."/"); }
    return $url;
    
  }
  
  //---[CZ]: odkaz na profil autora
  public function author_link($id) {
  
    $sql=$this->db->query("SELECT login, realname FROM ".TABLE_USERS." WHERE id=$id");
    $sql=$this->db->fetch_array($sql);
    
    if ($sql["realname"]!="") { return "<a href=\"".$this->profile($sql["login"])."\" target=\"_blank\">".$sql["realname"]."</a>"; }
    if ($sql["realname"]=="") { return "<a href=\"".$this->profile($sql["login"])."\" target=\"_blank\">".$sql["login"]."</a>"; }
  
  }

}
?>