<?php
/****************************************************************************
** Knjižnica, kjer so zbrane funkcije za izpis različnih formatov podatkov **
** Author: Dave @ zabec.net	 												 			**
** Date: 2007 						 												 			**
****************************************************************************/

/* funkcija skrbi za pretvarjanje imen v povezave (pretvorba nedovoljenih znakov) */
/**********************************************************************************/
	/*function convertToLink($link)
	{

		$link = strtolower($link);
		$link = str_replace(' ', '_', $link);
		$link = str_replace('č', 'c', $link);
		$link = str_replace('ž', 'z', $link);
		$link = str_replace('š', 's', $link);
		$link = str_replace(',', '_', $link);
		$link = str_replace('Č', 'C', $link);
		$link = str_replace('Ž', 'Z', $link);
		$link = str_replace('Š', 'S', $link);
		$link = str_replace('"', '_', $link);
		$link = str_replace("'", '_', $link);
		$link = str_replace(".", '_', $link);
		$link = str_replace(';', '_', $link);
		$link = str_replace(':', '_', $link);
		$link = str_replace('?', '_', $link);
		$link = str_replace('ö', '_', $link);
		$link = str_replace('ń', '_', $link);
		$link = str_replace('Ś', '_', $link);
		$link = str_replace('ó', '_', $link);
		$link = str_replace("ė", '_', $link);
		
		$link = str_replace('á', '_', $link);
		$link = str_replace('ö', '_', $link);
		$link = str_replace('Ö', '_', $link);
		$link = str_replace("ğ", '_', $link);
		
		$link = str_replace('ņ', '_', $link);
		$link = str_replace('ē', '_', $link);
		$link = str_replace('ā', '_', $link);
		$link = str_replace("ó", '_', $link);
		
		$link = str_replace('ł', '_', $link);
		$link = str_replace('Ł', '_', $link);
		$link = str_replace('ě', '_', $link);
		$link = str_replace("ý", '_', $link);
		
		$link = str_replace('ę', '_', $link);
		$link = str_replace('ę', '_', $link);
		$link = str_replace('ě', '_', $link);
		$link = str_replace("ý", '_', $link);
		$link = str_replace("’", '_', $link);
		
		$link = str_replace("(", '_', $link);
		$link = str_replace(")", '_', $link);
		$link = str_replace("`", '_', $link);
		$link = str_replace("!", '_', $link);
		$link = str_replace("&", '', $link);
		$link = str_replace("amp", '_', $link);
		$link = str_replace("#039", '', $link);
		$link = strtolower($link);
		return $link;
	}*/
	
	function convertToLink($link)
	{
		$replace = array(
			'Š' => 'S',
			'š' => 's',
			'Č' => 'C',
			'č' => 'c',
			'Ž' => 'Z',
			'ž' => 'z'
		);
		
		$link = str_replace(array_keys($replace), $replace, $link);
		
		return strtolower(preg_replace("/[^a-z0-9-]{1}/iu", '_', $link));
	}

	function decodeFromLink($link)
	{
		$link = strtolower($link);
		$link = str_replace('_', '%', $link);
		$link = str_replace('c', 'c', $link);
		$link = str_replace('s', 's', $link);
		$link = str_replace('z', 'z', $link);
		$link = str_replace('C', 'C', $link);
		$link = str_replace('S', 'S', $link);
		$link = str_replace('Z', 'Z', $link);
		return $link;
	}
	function decodeFromLink_filter($link)
	{
		$link = strtolower($link);
		$link = str_replace('_', '%', $link);
/*
		$link = str_replace('c', '%', $link);
		$link = str_replace('s', '%', $link);
		$link = str_replace('z', '%', $link);
		$link = str_replace('C', '%', $link);
		$link = str_replace('S', '%', $link);
		$link = str_replace('Z', '%', $link);
*/
		return $link;
	}
	function normalize_unicode($str)
	{
		# Quotes cleanup
		$str = ereg_replace( chr(ord("`")), "'", $str );        # `
		$str = ereg_replace( chr(ord("´")), "'", $str );        # ´
		$str = ereg_replace( chr(ord("„")), ",", $str );        # „
		$str = ereg_replace( chr(ord("`")), "'", $str );        # `
		$str = ereg_replace( chr(ord("´")), "'", $str );        # ´
		$str = ereg_replace( chr(ord("“")), "\"", $str );        # “
		$str = ereg_replace( chr(ord("”")), "\"", $str );        # ”
		$str = ereg_replace( chr(ord("´")), "'", $str );        # ´

		$unwanted_array = array(    'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
									'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
									'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
									'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
									'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'č'=>'c', 'Č'=>'c');
		$str = strtr( $str, $unwanted_array );

		# Bullets, dashes, and trademarks
		$str = ereg_replace( chr(149), "&#8226;", $str );    # bullet •
		$str = ereg_replace( chr(150), "&ndash;", $str );    # en dash
		$str = ereg_replace( chr(151), "&mdash;", $str );    # em dash
		$str = ereg_replace( chr(153), "&#8482;", $str );    # trademark
		$str = ereg_replace( chr(169), "&copy;", $str );    # copyright mark
		$str = ereg_replace( chr(174), "&reg;", $str );        # registration mark

		return $str;
	}
	
/* funkcija skrbi za pretvorbo datuma (in časa) iz baze za prikaz */
/******************************************************************/
	function formatDateFromDB($variable, $type = "")
	{
		$datetime = explode(" ",$variable);
		$date = explode("-",$datetime[0]);
		$time = explode(":",$datetime[1]);

		/* vrnjemo željeni tip (datetime, dateshorttime, date, time, shorttime) */
		switch($type)
		{
			case "dateshorttime":
				/* primer izpisa 07.12.2007 09:16 */
				return "$date[2].$date[1].$date[0] $time[0]:$time[1]";
			break;
			case "date":
				/* primer izpisa 07.12.2007 */
				return "$date[2].$date[1].$date[0]";
			break;
			case "year":
				/* primer izpisa 07.12.2007 */
				return "$date[0]";
			break;
			case "time":
				/* primer izpisa 09:16:30 */
				return $datetime[1];
			break;
			case "shorttime":
				/* primer izpisa 09:16 */
				return "$time[0]:$time[1]";
			break;
			default:
				/* primer izpisa 07.12.2007 09:16:30 */
				return "$date[2].$date[1].$date[0] $datetime[1]";
			break;
		}
	}

/* funkcija skrbi za pretvorbo datuma za vpis v bazo */
/*****************************************************/
	function formatDateToDB($variable, $type = "")
	{
		//preverimo če je spremenljivka veljavna
		if(checkStr($variable, "datum"))	{
			$date = explode(".",$variable);
			return $date[2]."-".$date[1]."-".$date[0];
		}
		//spremenljivka ni veljavna vrnemo trenutni datum
		else {
			return date("Y-m-d");
		}
	}
	
	function DanTeden($num){
		switch($num){
			case 1:
				return "Ponedeljek";
			break;
			case 2:
				return "Torek";
			break;
			case 3:
				return "Sreda";
			break;
			case 4:
				return "Četrtek";
			break;
			case 5:
				return "Petek";
			break;
			case 6:
				return "Sobota";
			break;
			case 7:
				return "Nedelja";
			break;
		}
	}
	
	function toLower($text){
		$return = strtolower($text);
		$return = str_replace('Č', 'č', $return);
		$return = str_replace('Š', 'š', $return);
		$return = str_replace('Ž', 'ž', $return);
		return $return;
	}
	
	function ShortenText($text,$chars) { 
	
	// Change to the number of characters you want to display 
	
	$text = $text." "; 
	$text = substr($text,0,$chars); 
	$text = substr($text,0,strrpos($text,' ')); 
	$text = $text."..."; 
	
	return $text; 
	} 

/* Funkcija odstrani končnico iz imena datoteke. 
	Uporabna za prikaz imena datoteke brez končnice  */
	
	function removeFileExtension($file){
		
		$newFile = spec_mb_ucfirst($file,"UTF-8",true);
		$dot = strrpos($newFile,".");
		$newFile = substr($newFile,0,$dot);
		
		return $newFile;	
	}
	
	function spec_mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false) {
      $first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
      $str_end = "";
      if ($lower_str_end) {
	$str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
      }
      else {
	$str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
      }
      $str = $first_letter . $str_end;
      return $str;
    }
	
	/* Funkcija prikaže velikost datoteke v Kb ali Mb */
	
	/* informacije o datoteki */
	function sizeOfFile($file)
	{
		$velikost = filesize($file);
		$velikost = round($velikost/1024);
		$koncnica = " Kb";

		//prikaz v MB
		if($velikost > 1024)
		{
			$velikost = round($velikost/1024, 2);
			$koncnica = " Mb";
		}

		return $velikost.$koncnica;
	}
	
	function getDateDiff($timestamp_start, $timestamp_end=''){
		if(!$timestamp_end){
			$difference = time()-strtotime($timestamp_start);
		}
		else{
			$difference = strtotime($timestamp_end)-strtotime($timestamp_start);
		}
		
		// Seconds in: 
		$Ysec = 60 * 60 * 24 * 365; 
		$Msec = 60 * 60 * 24 * 30; 
		$Wsec = 60 * 60 * 24 * 7; 
		$Dsec = 60 * 60 * 24; 
		$Hsec = 60 * 60; 
		$Minsec = 60;
		
		$Y = (int)($difference / $Ysec); 
		$difference = $difference % $Ysec;
		
		$MON = (int)($difference / $Msec); 
		$difference = $difference % $Msec;
		
		$W = (int)($difference / $Wsec); 
		$difference = $difference % $Wsec;
		
		$D = (int)($difference / $Dsec); 
		$difference = $difference % $Dsec;
		
		$H = (int)($difference / $Hsec); 
		$difference = $difference % $Hsec;
		
		$MIN = (int)($difference / $Minsec); 
		$difference = $difference % $Minsec;
		
		$SEC = $difference;
		
		$out = "";
		if($Y > 0 ) {
				if($Y == 1) $out .= "$Y leto"; 
				else $out .= "$Y let";
			}
		elseif($MON > 0) {
				if($MON == 1)	$out .= "$MON mesec"; 
				else $out .= "$MON mesecev";
			}
		elseif($W > 0) {
				if($W == 1)	$out .= "$W teden"; 
				else $out .= "$W tednov";
			} 
		elseif($D > 0) {
				if($D == 1)	$out .= "$D dan"; 
				else $out .= "$D dni";
			}
		elseif($H > 0) {
				if($H == 1)	$out .= "$H ura"; 
				else $out .= "$H ur";
			} 
		elseif($MIN > 0) {
				if($MIN == 1)	$out .= "$MIN minuta"; 
				else $out .= "$MIN minut";
			} 
		else {
				if($SEC == 1)	$out .= "$SEC sekunda"; 
				else $out .= "$SEC sekund";
			}
			
		return $out;
	}
	
?>