<?php
defined('PRTK') or die('Pratik Aktif Değil');
function GLOBALIZE($admin=false)
{
	if($admin)
	{
		if(!defined('VIEWPATH'))define('VIEWPATH', PRTK.'/theme'.'/'.ADMIN);
		if(!defined('VIEWURL'))define('VIEWURL', SUBFOLDER.'/theme'.'/'.ADMIN);
	} else {
		if(!defined('VIEWPATH'))define('VIEWPATH', PRTK.'/theme'.'/'.THEME);
		if(!defined('VIEWURL'))define('VIEWURL', SUBFOLDER.'/theme'.'/'.THEME);	
	}
}
function getSubUrl($url='',$admin=false)
{
	if($admin)
	{
		$q = parse_url($url, PHP_URL_QUERY);
		$l = parse_url($url, PHP_URL_PATH);
		$url = $l.'?'.$q.'&padmin';
	}
	$url =trim($url,'/');
	return SUBFOLDER.'/index.php/'.$url;
}
function redirect2($url='')
{
	//die($url);
	echo '<script> window.location = "'.$url.'";</script>';
	die('Yönlendiriliyorsunuz...');
}
/**
Ortak tema parçası yüklamak için
*/
function getCommonPart($part, $data=array())
{
	if( file_exists(VIEWPATH.'/Views/common/'.$part) )
	{
		include_once(VIEWPATH.'/Views/common/'.$part);
	} else {
		 log_message('info', VIEWPATH.'/Views/common/'.$part.' Bulunamadı'); 
	}
}
function getHead($data=array())
{
	getCommonPart('head.tpl', $data);
}
function getHeader($data=array())
{
	getCommonPart('header.tpl', $data);
}
function getFooter($data=array())
{
	getCommonPart('footer.tpl', $data);
}
function getPageHeader($data=array(), $title='')
{
	$data['title'] = $title;
	getCommonPart('page_header.tpl', $data);
}
function getSideBar($data=array())
{
	$suffix = isset($_SESSION['permission'])?$_SESSION['permission']:'0';
	getCommonPart('sidebar-'.$suffix.'.tpl',$data);
}
function getAlerts( )
{
	if(isset($_SESSION['alerts']))
	{
		foreach($_SESSION['alerts'] as $alert)
		{
			echo '<div class="alert alert-'.$alert['class'].' fade in block">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <i class="icon-info"></i>'.$alert['message'].'</div>';
		}
		unset($_SESSION['alerts']);
		} else {
		return false;
	}
}
function addAlert($code='', $mesage='', $class )
{
	$_SESSION['alerts'][]=array('code'=>$code, 'message'=>$mesage, 'class'=>$class);
}
function getUrlImg($img, $w='',$h='', $class='', $extra='')
{
	$width = '';
	$height = '';
	if($w!='') $width = 'width="'.$w.'px"';
	if($h!='') $height = 'height="'.$h.'px"';
	echo $img;
	if(!file_exists($img))
	{
		$img='no-image.png';
	}
	return '<img src="'.$img.'" alt="Pratik" '.$width.' '.$height.' class="'.$class.'" '.$extra.'>';
}
function getImg($img='', $w='',$h='', $class='', $extra='')
{
	$width = '';
	$height = '';
	if($w!='') $width = 'width="'.$w.'px"';
	if($h!='') $height = 'height="'.$h.'px"';
	if($img=='')
	{
	}
	if(!file_exists(WRITEPATH.'uploads/images/'.$img)|| $img=='')
	{
		$img='no-image.png';
	}
	return '<img src="'.WRITEURL.'uploads/images/'.$img.'" alt="Pratik" '.$width.' '.$height.' class="'.$class.'" '.$extra.'>';
}
function getThemeImg($img, $w='',$h='', $class='')
{
	$width = '';
	$height = '';
	if($w!='') $width = 'width="'.$w.'px"';
	if($h!='') $height = 'height="'.$h.'px"';
	if(!file_exists(VIEWPATH.'/assets/images/'.$img))
	{
		$img='no-image.png';
	}
	return '<img src="'.VIEWURL.'/assets/images/'.$img.'" alt="Pratik" '.$width.' '.$height.' class="'.$class.'" '.$class.'>';
}
function getUserImg($user, $w='30', $h='30', $class='')
{
	$file = 'users/user-'.$user['basic']['id'].'.jpg';
	return getImg($file, $w,$h, $class);
}
function formToken( $module )
{
	$formtoken = $_SESSION['formtoken']??md5(time());
	$_SESSION['formtoken'] = $formtoken;
	echo '<input type="hidden" name="module" value="'.$module.'"><input type="hidden" name="formtoken" value="'.$formtoken.'">';
}
function basicId( $data=false )
{
	if( isset($data['basic']))
	{
		$id = $data['basic']['id']['val']??'0';
		echo '<input type="hidden" class="basic_id" name="id" value="'.$id.'">';
	}
}
function theBasicId( $data=false )
{
	$id=false;
	if( isset($data['basic']))
	{
		$id = $data['basic']['id']['val']??'0';
	}
		return $id;
}
function getMenuClass( $item, $data=array() )
{
	if( isset($data['menu']))
	{
		if(in_array($item,$data['menu']))
		{
			return 'active';
		}
	}
	return '';
}
function ALC()
{
	if(isset($_SESSION['ALC']))
	{
		return $_SESSION['ALC'];
	} else {
		return false;
	}
}
function UID()
{
	if(isset($_SESSION['user']) && isset($_SESSION['user']['basic']))
	{
		return $_SESSION['user']['basic']['id'];
	} else {
		return false;
	}
}
function t_($text)
{
	if(!defined('LANG'))
	{
		return '*'.$text;
	}
	if(!defined('LANGLOADED') )
	{
	 	include_once PRTK.'/pratik/Languages/pratik_'.LANG.'.php';
		$_SESSION['text'] = $lang;
		define('LANGLOADED',TRUE);
	}
	if(isset($_SESSION['text'][$text]))
	{
		return $_SESSION['text'][$text];		
	} else {
		return $text;				
	}
}
function menuclass($item, $menu=array())
{
	if(in_array($item, $menu))
	{
		return 'active';
	}
}
function submenuclass($item, $menu=array())
{
	if(!in_array($item, $menu))
	{
		//return 'hidden';
	}
}
function sanitize($string,$limit=false,$url=false) 
{
	if(is_array($string)) $string = current($string);
	if($limit)
	{
		$string = substr($string,0,(int)$limit);
	}
	if($url){$pattern='/[^A-Za-z0-9\-\/]/';} else {$pattern='/[^A-Za-z0-9\-]/';}
	$aranan=array('Ğ','Ü','Ş','İ','Ö','Ç','ğ','ü','ş','ı','ö','ç');
	$konan=array('g','u','s','i','o','c','g','u','s','i','o','c');
	$string = str_replace($aranan, $konan , $string); // Replaces all spaces with hyphens.		
	$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
	$string = preg_replace($pattern, '', $string); // Removes special chars.
	$string = strtolower($string); // Replaces all spaces with hyphens.
	return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

