<?php
/*
# Fuer debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
#echo __LINE__."<br>";
*/

setlocale (LC_ALL, 'de_DE@utf8', 'de_DE', 'de', 'ge');
include_once 'classes/lg-on_Smarty.class.php';
$smarty = new lgon_Smarty();
require_once("config.inc.php");
require_once("func_rollenrechte.php");
$templatename = substr(basename($_SERVER['PHP_SELF']),0,-3)."html";
require_once "language/german.inc.php";




// Rechteüberprüfung
dbconnect();
if ($user_admin == ""){ require("index.php"); exit;} //Wenn man nicht angemeldet ist, darf man nicht auf die Seite
if(!rore($user_admin,'a_suche_si','RE')){require("lib/rechte.php");exit;}
// Rechteüberprüfung ende

if(isset($_GET['action'])){
  $action = $_GET['action'];
}else{
  $action = '';
}

if($action == ''){
}

if($action == 'suche'){

  $suchbegriff = strtoupper($_POST['suchbegriff']);
  $suchbegriff = preg_replace('/\s{2,}/sm',' ',$suchbegriff,PREG_SET_ORDER); //Mehr als zwei leerzeichen entfernen
  preg_match_all("/(\-{0,1}\+{0,1}\*{0,1}%{0,1}\"([^\"]+)\"\*{0,1}%{0,1}\+{0,1}\-{0,1}|\-{0,1}\+{0,1}\*{0,1}%{0,1}(\w+)\*{0,1}%{0,1}\+{0,1}\-{0,1})/u", $suchbegriff, $Worte);
  
  $sql2 = "(";
  
  for($i=0; $i < count($Worte[0]); $i++)
  {
      $suchbegriff = str_replace (array("\\", "ä", "ö", "ü", "ß", "Ä", "Ö", "Ü"), array("", "&auml;", "&OUML;", "&UUML;", "&SZLIG;", "&AUML;", "&OUML;", "&UUML;"),  $Worte[0][$i]);
      $suchwort = str_replace (array("+", "-", "\"", "\\"), array("", "", "", ""), $suchbegriff);         
      
      if($i == 0){
        $oper = "";
      }else{
        $oper = "AND";
      }
      
      if(strpos($Worte[0][$i],"+") === 0){           
        $sql2 .= "$oper UPPER(CONCAT(inhalt, textwort)) LIKE '%".$suchwort."%' ";
      } elseif(strpos($Worte[0][$i],"-") === 0){
        $sql2 .= "$oper UPPER(CONCAT(inhalt, textwort)) NOT LIKE '%".$suchwort."%' ";
      } elseif($Worte[0][$i] == ""){
        NULL;
      }else{
        $sql2 .= "$oper UPPER(CONCAT(inhalt, textwort)) LIKE '%".$suchwort."%' ";
      }
  }
  $sql2 .= ")";
  
  //Select zusammenbauen
  $sql1 = "SELECT count(*) as anz_treffer FROM quelle WHERE ";
  $sql3 = "$sql1$sql2";
  

  $result = $db->query( $sql3);
  $res = $result->fetch_array()
         or die ("Cannot execute query");
  
  function kurzer_text($string,$laenge){ 
      $origin=strlen($string);
      $stri_arr=explode(" ",$string);
      $anzzahl=count($stri_arr);
      $gekuerzt=0;
      $string="";
      while($gekuerzt<$anzzahl){ 
          $string_alt=$string;
          $string=$string." ".$stri_arr[$gekuerzt];
          $gekuerzt++;
          if(strlen($string)>$laenge){ 
              $gekuerzt=$anzzahl; 
              $string=$string_alt;
          } 
      } 
      if($laenge<$origin){ 
          $string=$string."<nobr>&nbsp;<b>...</b></nobr>";
      } 
      return $string; 
  }
  

  if($res['anz_treffer'] == '' or $res['anz_treffer'] == 0){
    $smarty->assign('suche_simp_nomatch', "1");
  }else{
    $smarty->assign('suche_simp_match', "1");
   
    dbconnect();
    $sql1 = "SELECT distinct lid, ueberschrift, textstelle, inhalt, textwort, stapgd, kid, ukid, DATE_Format(datum, '%d') tag, DATE_Format(datum, '%m') monat, DATE_Format(datum, '%Y') jahr, DATE_Format(datum, '%Y%m%d') date_sort
               FROM quelle
              WHERE";
    $sql4 =  "ORDER BY datum desc";
    $sql3 = "$sql1$sql2$sql4";
    
    $result = $db->query( $sql3);


    $ln_zaehler = 0;
    while ($row = $result->fetch_array()){
     $preview = kurzer_text(strip_tags($row['inhalt']),200);
     $wochentag = date("l", mktime(0,0,0,$row['monat'],$row['tag'],$row['jahr']));
     switch($wochentag)
     {
       case 'Sunday':
           $wochentag = "So.";
           break;
       case 'Monday':
           $wochentag = "Mo.";
           break;
       case 'Tuesday':
           $wochentag = "Di.";
           break;
       case 'Wednesday':
           $wochentag = "Mi.";
           break;
       case 'Thursday':
           $wochentag = "Do.";
           break;
       case 'Friday':
           $wochentag = "Fr.";
           break;
       case 'Saturday':
           $wochentag = "Sa.";
           break;
     }
     
     $result1 = $db->query( "SELECT bezeichnung FROM kategorie WHERE kid='$row[kid]'" );
     $row1 = $result1->fetch_array();
     
     $result2 = $db->query( "SELECT bezeichnung FROM unterkategorie WHERE ukid='$row[ukid]'" );
     $row2 = $result2->fetch_array();

     
     $ln_zaehler = $ln_zaehler + 1;
     $smarty->assign('anzahltreffer', "$res[anz_treffer]");
     
     $row['kategorie'] = $row1['bezeichnung'];
     $row['unterkategorie'] = $row2['bezeichnung'];
     $row['preview'] = $preview;
     $row['wochentag'] = $wochentag;
     $value[] = $row;
    }

    // Assign this array to smarty...
    $smarty->assign('table_data', $value);
   
  }

}

$smarty->assign('action', "$action");
$smarty->display("$template/$templatename");
?>