<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('is_installed'))
{
	function is_installed($type='redirect')
	{

		if(!defined('IS_INSTALLED'))
		{
			$CI = get_instance();
			$file 		= './dbc_config/config.xml';
		   	$xmlstr 	= file_get_contents($file);
			$xml 		= simplexml_load_string($xmlstr);
			$config		= $xml->xpath('//config');
			$is_installed = $config[0]->is_installed;
			
			if(!defined('IS_INSTALLED'))
				define('IS_INSTALLED',$is_installed);
		}

		if(constant('IS_INSTALLED')=='no')
		{			
			if($type=='redirect')
			redirect(site_url('install'),'refresh');
			else
			return 'no';
		}
		else
		{
			if($type!='redirect')
			return 'yes';

		}
	}
}


if ( ! function_exists('dbc_url_title'))
{
	function dbc_url_title($str, $separator = 'dash', $lowercase = FALSE)
	{

		if ($separator == 'dash')
		{
			$search		= '_';
			$replace	= '-';
		}
		else
		{
			$search		= '-';
			$replace	= '_';
		}

		$trans = array(
						'&\#\d+?;'				=> '',
						'&\S+?;'				=> '',
						'\s+'					=> $replace,
						$replace.'+'			=> $replace,
						$replace.'$'			=> $replace,
						'^'.$replace			=> $replace,
						'\.+$'					=> ''
					);

		$str = strip_tags($str);

		foreach ($trans as $key => $val)
		{
			$str = preg_replace("#".$key."#i", $val, $str);
		}

		if ($lowercase === TRUE)
		{
			$str = strtolower($str);
		}

		return trim(stripslashes($str));
	}
}


if ( ! function_exists('default_lang'))
{
	function default_lang()
	{
		if(is_installed('return')!='no')
		{
			if(defined('DEFAULT_LANG'))
			{
				return constant('DEFAULT_LANG');
			}
			else
			{			
				$CI = get_instance();
				$CI->load->database();
				$query 		= $CI->db->get_where('options',array('key'=>'site_settings'));		
				if($query->num_rows()>0)
				{
					$row = $query->row();
					$settings = json_decode($row->values);
					$default_lang = $settings->site_lang;			
				}
				else
					$default_lang = 'en';

				if(!defined('DEFAULT_LANG'))
					define('DEFAULT_LANG',$default_lang);
				return $default_lang;						
			}
		}
		else
			return 'en';
	}
}

if ( ! function_exists('get_site_logo'))
{
	function get_site_logo()
	{
		$logo = get_settings('site_settings','site_logo',base_url('assets/images/logo/logo-default.png'));

		if ($logo == ''){
			$logo = base_url('assets/images/logo/logo-default.png');
		}
		return $logo;
	}
}

if ( ! function_exists('get_author_url'))
{
	function get_author_url()
	{		
		return 'http://dbcinfotech.net/app/envato/index.php/';
	}
}

if ( ! function_exists('get_current_page'))
{
	function get_current_page()
	{
		$uri =  uri_string();
		$segements = explode('/',$uri);
		$i=0;
		$url = '';
		foreach ($segements as $seg) {
			if($i>0)
			{
				$url .= $seg.'/';
			}
			$i++;
		}

		$CI = get_instance();
		$CI->load->model('show/show_model');
		$query = $CI->show_model->get_page_by_url(rtrim($url,"/"));
		if($query->num_rows()>0)
			return $query->row_array();
		else
			return array('error'=>'page_not_found');
	}
}

if ( ! function_exists('get_user_title_by_id'))
{
	function get_user_title_by_id($id)
	{
		$CI = get_instance();
		$CI->load->database();
		$query = $CI->db->get_where('users',array('id'=>$id));
		if($query->num_rows()>0)
		{
			$row = $query->row();
			if($row->first_name!='')
				return $row->first_name;
			else
				return $row->user_name;
		}
		else
			return 'N/A';
	}
}

if ( ! function_exists('get_user_email_by_id'))
{
    function get_user_email_by_id($id)
    {
        $CI = get_instance();
        $CI->load->database();
        $query = $CI->db->get_where('users',array('id'=>$id));
        if($query->num_rows()>0)
        {
            $row = $query->row();
            return $row->user_email;
        }
        else
            return 'N/A';
    }
}

if ( ! function_exists('get_user_fullname_by_id'))
{
    function get_user_fullname_by_id($id)
    {
        $CI = get_instance();
        $CI->load->database();
        $query = $CI->db->get_where('users',array('id'=>$id));
        if($query->num_rows()>0)
        {
            $row = $query->row();
            if($row->first_name!='')
                return $row->first_name.' '.$row->last_name;
            else
                return $row->user_name;
        }
        else
            return 'N/A';
    }
}

if ( ! function_exists('get_user_fullname_by_username'))
{
    function get_user_fullname_by_username($username)
    {
        $CI = get_instance();
        $CI->load->database();
        $query = $CI->db->get_where('users',array('user_name'=>$username));
        if($query->num_rows()>0)
        {
            $row = $query->row();
            if($row->first_name!='')
                return $row->first_name.' '.$row->last_name;
            else
                return $row->user_name;
        }
        else
            return 'N/A';
    }
}

if ( ! function_exists('is_banned'))
{
	function is_banned($user_email)
	{
		$CI = get_instance();
		$CI->load->database();
		$query = $CI->db->get_where('users',array('user_email'=>$user_email));
		if($query->num_rows()>0)
		{
			$row = $query->row();
			if($row->banned==1)
				return TRUE;
			else
				return FALSE;
		}
		else
			return TRUE;
	}
}

if ( ! function_exists('is_loggedin'))
{
	function is_loggedin()
	{
		$CI = get_instance();
		if($CI->session->userdata('user_name')=='')
			return FALSE;
		else
			return TRUE;
	}
}

if ( ! function_exists('is_admin'))
{
	function is_admin()
	{
		$CI = get_instance();
		if($CI->session->userdata('user_name')!='' && $CI->session->userdata('user_type')==1)
			return TRUE;
		else
			return FALSE;
	}
}

if ( ! function_exists('is_agent'))
{
	function is_agent()
	{
		$CI = get_instance();
		if($CI->session->userdata('user_name')!='' && $CI->session->userdata('user_type')==2)
			return TRUE;
		else
			return FALSE;
	}
}

if ( ! function_exists('check_config'))
{
	function check_config()
	{
		$files = array('user_uploads','user_uploads/plugins','dbc_config','application/config/database.php');
		$error = '';

		foreach ($files as $file) 
		{
			if(is_writable($file)==FALSE)
			{
				$error .= '<div class="alert alert-error">SITE_ROOT/'.$file.' is not writable.Please change it\'s permission before installing bookit.</div>';
			}
		}

		return $error;
	}
}

if ( ! function_exists('get_site_title'))
{
	function get_site_title()
	{
		$CI = get_instance();
		$CI->load->database();
		$query = $CI->db->get_where('options',array('key'=>'site_settings'));		
		if($query->num_rows()>0)
		{
			$row = $query->row();
			$values = json_decode($row->values);
			if(isset($values->site_title))
				return $values->site_title;
			else
				return 'Bookit';
		}
		else
			return 'Bookit';
	}
}

if ( ! function_exists('get_per_page_value'))
{
	function get_per_page_value()
	{
		return get_settings('site_settings','per_page',20);		
	}
}

if ( ! function_exists('translate'))
{
	function translate($html='')
	{
		preg_match_all("^\[(.*?)\]^",$html,$fields, PREG_PATTERN_ORDER);
		foreach ($fields[1] as $key) 
		{
			$res = lang_key($key);
			$html = str_replace('['.$key.']',$res,$html);
		}

		return $html;

	}
}



if ( ! function_exists('lang_key'))
{
	function lang_key($key='')
	{
		if(defined('LANG_ARRAY'))
		{
			$lang = json_decode(constant('LANG_ARRAY'));
			return (isset($lang->$key))?$lang->$key:$key;				
		}
		else
		{
			$CI = get_instance();
			$CI->load->database();
			$curr_lang 	= $CI->uri->segment(1);

			$default_lang = default_lang();
				
			if($curr_lang=='')
				$query = $CI->db->get_where('language',array('short_name'=>$default_lang));
			else
				$query = $CI->db->get_where('language',array('short_name'=>$curr_lang));

			if($query->num_rows()>0)
			{
				$row = $query->row();
				if(!defined('LANG_ARRAY'))
					define('LANG_ARRAY',$row->values);

				$lang = json_decode($row->values);
				return (isset($lang->$key))?$lang->$key:$key;				
			}
			else
				return $key;
			
		}
	}
}

if ( ! function_exists('checksavedlogin'))
{
	function checksavedlogin()
	{
		$CI = get_instance();
		$key = get_cookie('mycookie_key');
		$user = get_cookie('mycookie_user');
	
		if($user!=FALSE && $key!=FALSE)
		{
			$CI->load->model('auth_model');
			$CI->auth_model->check_cookie_val($user,$key);
		}
	}
}

if ( ! function_exists('get_username_by_id'))
{
	function get_username_by_id($id)
	{
		$CI = get_instance();
		$query = $CI->db->get_where('users',array('id'=>$id));
		$row = $query->row();
		return $row->user_name;
	}
}

if ( ! function_exists('get_id_by_username'))
{
	function get_id_by_username($user_name)
	{
		$CI = get_instance();
		$query = $CI->db->get_where('users',array('user_name'=>$user_name));
		$row = $query->row();
		return $row->id;
	}
}

if ( ! function_exists('get_plugins'))
{
	function get_plugins()
	{
		$CI = get_instance();
		$query = $CI->db->get_where('plugins',array('status'=>1));		
		return $query;
	}
}

if ( ! function_exists('configPagination'))
{
	function configPagination($url,$total_rows,$segment,$per_page=10)
	{


		$CI = get_instance();
		$CI->load->library('pagination');
		$config['base_url'] 		= site_url($url);
		$config['total_rows'] 		= $total_rows;
		$config['per_page'] 		= $per_page;
		$config['uri_segment'] 		= $segment;
		$config['num_tag_open'] 	= '<li>';
		$config['num_tag_close'] 	= '</li>';
		$config['cur_tag_open'] 	= '<li class="active"><a href="#">';
		$config['cur_tag_close']	= '</a></li>';
		$config['num_links'] 		= 3;
		$config['next_tag_open'] 	= "<li>";
		$config['next_tag_close'] 	= "</li>";
		$config['prev_tag_open'] 	= "<li>";
		$config['prev_tag_close'] 	= "</li>";
		
		$config['first_link'] 	= FALSE;
		$config['last_link'] 	= FALSE;
		$CI->pagination->initialize($config);
		
		return $CI->pagination->create_links();
	}
}



if ( ! function_exists('render_widget'))
{
	function render_widget($alias='')
	{
		$CI 		= get_instance();
		$CI->load->helper('inflector');	
		$CI->load->helper('file');
		$query = $CI->db->get_where('widgets',array('alias'=>$alias));
		if($query->num_rows()>0)
		{
			$row = $query->row();
			if($row->status==1)
			{
				if(read_file('./application/modules/widgets/'.$row->alias.'.php')!=FALSE)
					require_once'./application/modules/widgets/'.$row->alias.'.php';					
			}
			else if($row->status==0)
				echo '';
			else
				echo 'Module not found';				
		}
		else
		{
			echo 'module not found';
		}			
	}
}

if ( ! function_exists('render_widgets'))
{
	function render_widgets($position='')
	{
		$CI 		= get_instance();
		$CI->load->helper('inflector');	
		$CI->load->helper('file');
		$widgets 	= get_widgets_by_position($position);	
		if(!empty($widgets)){
			foreach($widgets as $row)
			{
				$query = $CI->db->get_where('widgets',array('alias'=>$row));
				if($query->num_rows()>0)
				{
					$row = $query->row();
					if($row->status==1)
					{
						if(read_file('./application/modules/widgets/'.$row->alias.'.php')!=FALSE)
							require_once'./application/modules/widgets/'.$row->alias.'.php';					
					}
					else if($row->status==0)
						echo 'Module not published';
					else
						echo 'Module not found';				
				}
				else
				{
					echo 'module not found';
				}
					
			}
		}
	}
}

if ( ! function_exists('get_widgets_by_position'))
{
	function get_widgets_by_position($pos='')
	{
		$CI = get_instance();
		$positions = get_option('positions');
		if(is_array($positions)==TRUE && isset($positions['error'])==TRUE)
		{
			$widgets = array();
		}
		else
		{
			$positions = json_decode($positions->values);
			$widgets = array();
			foreach($positions as $position)
			{
				if($position->name==$pos)
				{
					if(isset($position->widgets))
					$widgets = $position->widgets;
				}
			}			
		}
		return $widgets;
	}
}

if ( ! function_exists('theme_url'))
{
	function theme_url($theme='')
	{
		$CI 	= get_instance();
		if($theme=='')
		$theme 	= get_active_theme();
		return base_url('application/modules/themes/views/'.$theme.'/');	
	}
}


if ( ! function_exists('load_view'))
{
	function load_view($view='',$data=array(),$buffer=FALSE,$theme='')
	{
		$CI 	= get_instance();
		if($theme=='')
		$theme 	= get_active_theme();
		if($buffer==FALSE)
		{
			if(@file_exists(APPPATH."modules/themes/views/".$theme."/".$view.".php"))
			$CI->load->view('themes/'.$theme.'/'.$view,$data);
			else
			$CI->load->view('themes/default/'.$view,$data);	
		}
		else
		{
			if(@file_exists(APPPATH."modules/themes/views/".$theme."/".$view.".php"))
			$view_data = $CI->load->view('themes/'.$theme.'/'.$view,$data,TRUE);
			else
			$view_data = $CI->load->view('themes/default/'.$view,$data,TRUE);	
			return $view_data;
		}
	}
}

if ( ! function_exists('load_template'))
{
	function load_template($data=array(),$theme='',$tmpl='template_view')
	{
		$row 	= get_option('site_settings');
		if(is_array($row) && isset($row['error']))
		{
			echo 'Site settings not found.error on : epbase_helper';
			die();
		}
		else
		{
			$values 		= json_decode($row->values);
			$data['title'] 	= $values->site_title;
		}

		load_view($tmpl,$data);
	}
}


if ( ! function_exists('get_active_theme_woc'))
{
	function get_active_theme_woc()
	{
		
		$row = get_option('active_theme');
		if(is_array($row) && isset($row['error']))
		{
			$theme = 'default';
		}
		else
			$theme = $row->values;			
	
		
		return $theme;
		
	}
}

if ( ! function_exists('get_active_theme'))
{
	function get_active_theme()
	{
		if(defined('ACTIVE_THEME'))
		{

			return constant('ACTIVE_THEME');
		}
		else
		{
			
			$row = get_option('active_theme');
			if(is_array($row) && isset($row['error']))
			{
				$theme = 'default';
			}
			else
				$theme = $row->values;			
		
			if(!defined('ACTIVE_THEME'))
			{
				define('ACTIVE_THEME',$theme);
			}
			return $theme;
		}
	}
}

if ( ! function_exists('get_settings'))
{
	function get_settings($option='',$key='',$default='Yes')
	{
		$settings = get_option($option);
		if(is_array($settings)==FALSE)
		{
			$settings = (array)json_decode($settings->values);
			$val = (isset($settings[$key]))?$settings[$key]:$default;
		}
		else
			$val = $default;

		return $val;
	}
}


if ( ! function_exists('get_option'))
{
	function get_option($key='')
	{
		$defined = 0;
		if(defined('OPTIONS_ARRAY'))
		{						
			$options = (array)json_decode(constant('OPTIONS_ARRAY'));
			if(isset($options[$key]))
			{
				$defined = 1;
				return $options[$key];
			}
		}


		if($defined==0)
		{
			$CI = get_instance();
			$CI->load->database();
			$query = $CI->db->get_where('options',array('key'=>$key,'status'=>1));		
			if($query->num_rows()>0)
				$option = $query->row();
			else
				$option = array('error'=>'Key not found');

			$options[$key] = $option;
			if(!defined('OPTIONS_ARRAY'))
				define('OPTIONS_ARRAY',json_encode($options));

			return $option;
		}
	}
}

if ( ! function_exists('update_option'))
{
	function update_option($key='',$values=array())
	{
		$CI = get_instance();
		$data['values'] = json_encode($values);
		$query = $CI->db->update('options',$data,array('key'=>$key));		
	}
}

if ( ! function_exists('add_option'))
{
	function add_option($key='',$values='')
	{
		$CI = get_instance();
		$data['values'] = $values;
		$result = get_option($key);
		if(is_array($result) && isset($result['error']))
		{
			$data['key'] = $key;
			$query = $CI->db->insert('options',$data);					
		}
		else
		{
			$query = $CI->db->update('options',$data,array('key'=>$key));		
		}
	}
}

if ( ! function_exists('get_widgets'))
{
	function get_widgets()
	{
		$CI = get_instance();
		$widgets = array(
				array('lib'=>'gi_relatedpost','function'=>'get_related_posts','param'=>array('sc mondal'))
			);
		
		foreach ($widgets as $widget) 
		{
			$CI->load->library($widget['lib']);
			$CI->$widget['lib']->$widget['function']($widget['param']);
		}
	}
}


if ( ! function_exists('is_active_menu'))
{
	function is_active_menu($url)
	{
		if(is_array($url))
		{
			foreach ($url as $menu) {
				if(strpos(uri_string(),$menu))
					return 'active';
			}					
		}
		else
		{
			if(uri_string()=='' && $url=='')
				return 'active';

			if(uri_string()=='' || $url=='')
				return '';
			return (strpos(uri_string(),$url))?'active':'';			
		}
	}
}

if ( ! function_exists('get_alias_by_url'))
{
	function get_alias_by_url()
	{
		$uri =  uri_string();
		$segements = explode('/',$uri);
		$i=0;
		$url = '';
		foreach ($segements as $seg) {
			if($i>0)
			{
				$url .= $seg.'/';
			}
			$i++;
		}

		$CI = get_instance();
		$CI->load->model('show/show_model');
		$alias = $CI->show_model->get_alias_by_url($url);
		return $alias;
	}
}

if ( ! function_exists('truncate'))
{
	function truncate($s, $l, $e = '...', $isHTML = false){
		$i = 0;
		$tags = array();
		if($isHTML){
			preg_match_all('/<[^>]+>([^<]*)/', $s, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
			foreach($m as $o){
				if($o[0][1] - $i >= $l)
					break;
				$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
				if($t[0] != '/')
					$tags[] = $t;
				elseif(end($tags) == substr($t, 1))
					array_pop($tags);
				$i += $o[1][1] - $o[0][1];
			}
		}
		return substr($s, 0, $l = min(strlen($s),  $l + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . (strlen($s) > $l ? $e : '');
	}
}

if ( ! function_exists('encode_html'))
{
	function encode_html($html){
		$html = str_replace('<','&lt;', $html);
		$html = str_replace('>','&gt;', $html);
		return $html;
	}
}

if ( ! function_exists('get_page_layout'))
{
	function get_page_layout($alias)
	{
		$CI = get_instance();
		$CI->load->model('show/show_model');
		$query = $CI->show_model->get_page_by_alias($alias);
		if($query->num_rows>0)
		{
			$row = $query->row();
			return $row->layout;
		}
		else
			return  get_settings('site_settings','default_layout',1);
	}
}

if ( ! function_exists('create_log'))
{
	function create_log($user_name)
	{		
		$row = get_option('purchase_key');
		$purchase_key = (isset($row->values))?$row->values:'1';

		$row = get_option('item_id');
		$item_id = (isset($row->values))?$row->values:'2';

		$domain = base_url();
		if (!strpos('-'.$domain, "http://localhost/"))
		{
			//set POST variables
			$url = get_author_url().'admin/verify/checkproductkey';
			$fields = array(
								'purchase_key' => urlencode($purchase_key),
								'item_id' => urlencode($item_id),
								'domain' => urlencode($domain),
								'item'		=> 'realcon'
							);


			$fields_string = '';
			//url-ify the data for the POST
			foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
			rtrim($fields_string, '&');

			//open connection
			$ch = curl_init();

			//set the url, number of POST vars, POST data
			curl_setopt($ch,CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, 0);  
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
			curl_setopt($ch,CURLOPT_POST, count($fields));
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);


			//execute post
			$result = curl_exec($ch);
			
			curl_close($ch);
			
			$CI = get_instance();
			if($result!=1)		
			{
				$CI->session->set_flashdata('msg', '<div class="alert alert-danger">Please verify your purchase.</div>');
				redirect(site_url('admin/purchase/regdomain'));
			}					
		}
	}
}

/* End of file array_helper.php */
/* Location: ./system/helpers/array_helper.php */