<?php

class Firma extends Model {

  function Firma() {
  
    parent::Model();
  
  }
  
  function vyber($jazyk = "", $zacnina = 0, $maximum = 50, $kategoria = 0, $id = 0, $limituj = true, $search = '')
  {
    
    $sql = "
      SELECT    f.id                as firma_id,
                f.nazov             as firma_meno,
                f.logo              as firma_logo,
                f.obrazok           as firma_obrazok,
                f.datum             as firma_datum,
                f.link              as firma_link,
                f.web_akcie         as firma_web_akcie,
                f.web_cenniky       as firma_web_cenniky,
                f.adresa            as firma_adresa,
                f.popis             as firma_popis,
                f.zlava             as firma_zlava,
                f.kluce             as firma_kluce,
                f.clenska           as firma_clenska,
                f.cislo             as firma_cislo,
                k.nazov_".($jazyk == "" ? "sk" : $jazyk)." as firma_kategoria,
                f.kategoria_id      as firma_kategoria_id
      FROM firmy f
      JOIN kategorie k ON k.id = f.kategoria_id ";
    if ($search == '') {
      $sql .= "
        " . ($kategoria != 0 && $id == 0 ? "WHERE f.kategoria_id = $kategoria" : '') . ($id != 0 ? "WHERE f.id = $id" : '') . "
        ORDER BY f.datum DESC
        " . ($limituj ? "LIMIT $zacnina, $maximum" : '');
    } else {
      $search = strtolower($search);
      $patern = "";
      for($i=0;$i<strlen($search);$i++) {
        $char = $search[$i];
        switch ($char) {
          case 'a':
          case 'á':
          case 'ä':
            $patern .= "[aáäAÁÄ]";
          break;
          case 'e':
          case 'é':
          case 'ě':
            $patern .= "[eéěEÉĚ]";
          break;
          case 'i':
          case 'í':
            $patern .= "[iíIÍ]";
          break;
          case 'o':
          case 'ó':
          case 'ô':
          case 'ö':
          case 'ő':
            $patern .= "[oóôöőOÓÔŐÖ]";
          break;
          case 'u':
          case 'ú':
          case 'ü':
          case 'ű':
            $patern .= "[uúűüUÚŰÜ]";
          break;
          case 'y':
          case 'ý':
            $patern .= "[yýYÝ]";
          break;
          case 'l':
          case 'ľ':
          case 'ĺ':
            $patern .= "[lľĺLĽĹ]";
          break;
          case 'n':
          case 'ň':
            $patern .= "[nňNŇ]";
          break;
          case 'z':
          case 'ž':
            $patern .= "[zžZŽ]";
          break;
          case 'r':
          case 'ŕ':
          case 'ř':
            $patern .= "[rŕřRŘŔ]";
          break;
          case 't':
          case 'ť':
            $patern .= "[tťTŤ]";
          break;
          case 'd':
          case 'ď':
            $patern .= "[dďDĎ]";
          break;
          case 'c':
          case 'č':
            $patern .= "[cčCČ]";
          break;
          case 's':
          case 'š':
            $patern .= "[sšSŠ]";
          break;
          case '/':
          case '-':
          case ',':
          case '_':
          case '+':
          case ':':
          case '\\':
          case ';':
          case '<':
          case '>':
          break;
          default: $patern .= "[" . $search[$i] . "]"; break;
        }
      }
      
      $patern = $this->zjednodus($patern);
           
      $sql .= "
        ORDER BY f.datum DESC
      ";
  
    }
    
    $query = $this->db->query($sql);
    $return = $query->result_array();
    
    if (isset($patern)) {
      $out = array();
      foreach($return as $r) {
        if (eregi($patern, $this->zjednodus($r['firma_kluce'])) !== false ||
            eregi($patern, $this->zjednodus($r['firma_meno'])) !== false ||
            eregi($patern, $this->zjednodus($r['firma_adresa'])) !== false ||
            eregi($patern, $this->zjednodus($r['firma_popis'])) !== false ||
            eregi($patern, $this->zjednodus($r['firma_zlava'])) !== false) {
          $out[] = $r;    
        }
      }
      $return = $out;
    
    }
    
    return $return;
    
  }
  
  function strankujVystup($stranka = 0, $krokuj_po = 50, $pocet_listov = 5, $kategoria = 0)
  {
    $result = array();
    
    if ($kategoria != 0)
    {
      $this->db->where("kategoria_id = $kategoria");
    }
    $query = $this->db->get("firmy");
    
    $riadkov = $query->num_rows();
    
    $zacni_na = $stranka * $krokuj_po;
    
    $stranok_max = ceil($riadkov / $krokuj_po);
    
    $pocet_pred = floor($zacni_na / $krokuj_po);
    $pocet_po = ceil(($riadkov - ($zacni_na + $krokuj_po)) / $krokuj_po);
    $pocet_pred = ($pocet_pred > $pocet_listov ? $pocet_listov : $pocet_pred);
    $pocet_po = ($pocet_po > $pocet_listov ? $pocet_listov : $pocet_po);
    
    $result["povolit_dozadu"] = ($stranka > 0 ? 1 : 0);
    $result["povolit_dopredu"] = ($stranka < ($stranok_max - 1) ? 1 : 0);
    $result["smer_dozadu"] = $stranka - 1;
    $result["smer_dopredu"] = $stranka + 1;
    $result["smer_uplne_dozadu"] = 0;
    $result["smer_uplne_dopredu"] = $stranok_max - 1;
    
    $listy = array();
    for ($i=$pocet_pred;$i>0;$i--)
    {
      $listy[] = array("cislo_stranky" => ($stranka - $i + 1), "stranka" => ($stranka - $i));
    }
    $result["list_pred"] = $listy;
    
    $listy = array();
    for ($i=1;$i<=$pocet_po;$i++)
    {
      $listy[] = array("cislo_stranky" => ($stranka + $i + 1), "stranka" => ($stranka + $i));
    }
    $result["list_po"] = $listy;
    
    return $result;
  }
  
  function uprav($id, $data)
  {
    $sql = "UPDATE firmy SET nazov = '".$data['nazov']."', kategoria_id = '".$data['kategoria_id']."', logo = '".$data['logo']."', obrazok = '".$data['obrazok']."', link = '".$data['link']."', web_cenniky='".$data['web_cenniky']."', web_akcie='".$data['web_akcie']."', adresa='".$data['adresa']."', popis='".$data['popis']."', zlava='".$data['zlava']."', kluce='".$data['kluce']."', clenska='".$data['clenska']."', cislo='".intval($data['cislo'])."' WHERE id = $id";
    
    $query = $this->db->query($sql);
    
    if ($this->db->affected_rows() == 1)
      return true;
    
    return false;
  }
  
  function vloz($data)
  {
    $sql = "INSERT INTO firmy SET nazov = '".$data['nazov']."', kategoria_id = '".$data['kategoria_id']."', logo = '".$data['logo']."', obrazok = '".$data['obrazok']."', link = '".$data['link']."', datum = '".date("U")."', web_cenniky='".$data['web_cenniky']."', web_akcie='".$data['web_akcie']."', adresa='".$data['adresa']."', popis='".$data['popis']."', zlava='".$data['zlava']."', kluce='".$data['kluce']."', clenska='".$data['clenska']."', cislo='".intval($data['cislo'])."'";
    
    $query = $this->db->query($sql);
    
    if ($this->db->affected_rows() == 1)
      return true;
    
    return false;
  }
  
  function odstran($data) {
    $where = "";
    
    $pa = array();
    
    foreach($data as $k => $v)
    {
      $pa[] = "id = " . $v;
    }
    
    $where = implode(" OR ", $pa);
  
    $this->db->query("DELETE FROM firmy WHERE " . $where);
  }
  
  function zjednodus($text) {
    $trans = array(
    "ľ" => "l",
    "š" => "s",
    "č" => "c",
    "ť" => "t",
    "ý" => "y",
    "á" => "a",
    "í" => "i",
    "é" => "e",
    "ú" => "u",
    "ä" => "a",
    "ô" => "o",
    "ó" => "o",
    "ö" => "o",
    "ő" => "o",
    "ü" => "u",
    "ű" => "u",
    "ů" => "u",
    "ř" => "r",
    "ň" => "n",
    "ĺ" => "l",
    "ě" => "e",
    "ž" => "z",
    "Ľ" => "L",
    "Š" => "S",
    "Č" => "C",
    "Ť" => "T",
    "Ž" => "Z",
    "Ý" => "Y",
    "Á" => "A",
    "Í" => "I",
    "É" => "E",
    "Ú" => "U",
    "Ů" => "U",
    "Ř" => "R",
    "Ď" => "D",
    "Ĺ" => "L",
    "Ě" => "E",
    "Ž" => "Z",
    "Ô" => "O",
    "Ó" => "O",
    "Ő" => "O",
    "Ö" => "O",
    "Ű" => "U",
    "Ü" => "U"
    );
    
    return strtr($text, $trans);
  }
}

?>