<?php
class Admin_model extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';
	var $encryption_key = "i:Dvv%^uJKK9)_K<";

    function Admin_model()
    {
        // Call the Model constructor
        parent::Model();
        
        $this->load->helper("log");
    }
	
	function log_in_user($login, $pass, $auto){
		
		$data = array(
			'user_login' => $login,
            'user_pass' => sha1($this->encryption_key.$pass),
			'user_deleted' => 0
        );
		$query = $this->db->get_where('users', $data);
		if ($query->num_rows() > 0) {
			$row = $query->row();
			//return $row->ID_users;
			
			if($row->user_blocked == 1) {
				$stats = array(
					'stats_user_id' => $row->ID_users,
					'stats_login' => $row->user_login,
					'stats_ip' => $this->input->ip_address(),
					'stats_user_agent' => $this->input->user_agent(),
					'stats_type' => 'Logowanie ręczne',
					'stats_result' => 'Użytkownik zablokowany'
				);
				$this->db->set('stats_time', 'sysdate()', FALSE);
				$this->db->insert('stats', $stats);
				
				/* crm db load
				 * $this->db->close();
					
				$db2 = $this->load->database('crm', true);
				$db2->set('stats_time', 'sysdate()', FALSE);
				$db2->insert('stats', $stats);
				
				$db2->close();
				
				$this->load->database('default');*/
					
				return;
			}
			
			$stats = array(
				'stats_user_id' => $row->ID_users,
				'stats_login' => $row->user_login,
				'stats_ip' => $this->input->ip_address(),
				'stats_user_agent' => $this->input->user_agent(),
				'stats_type' => 'Logowanie ręczne',
				'stats_result' => 'Logowanie udane'
			);
			
			$this->db->set('stats_time', 'sysdate()', FALSE);
			$this->db->insert('stats', $stats);
			
			
			/* crm db load
			 * $this->db->close();
					
			$db2 = $this->load->database('crm', true);
			$db2->set('stats_time', 'sysdate()', FALSE);
			$db2->insert('stats', $stats);
			
			$db2->close();
			
			$this->load->database('default');*/
			
						
			$data = array(
                'uzytkownik'	=> $login,
				'uzytkownik_id'	=> $row->ID_users,
                'zalogowany' 	=> TRUE,
				'firma'			=> $row->user_nazwa_firmy,
				//'pagename'		=> $row->user_pagename,
				'admin'			=> $row->user_admin,
				'tekst'		=> $row->user_podstrony,
				//'ustawienia_strony'	=> $row->user_ustawienia_strony,
				'galeria'		=> $row->user_galeria,
				'wideo'			=> $row->user_wideo,
				'pa'			=> $row->user_polecane,
				'news'			=> $row->user_aktualnosci,
				'fz'			=> $row->user_formularz_zgloszeniowy,
				'fk'			=> $row->user_formularz_kontaktowy,
				'faq'			=> $row->user_faq,
				'pliki'			=> $row->user_pliki,
				'mapa'			=> $row->user_mapa,
				'baner'			=> $row->user_baner,
				'sl'			=> $row->user_slider,
			
				'bg'			=> $row->user_baner_glowny,
				'ag'			=> $row->user_aktualnosci_glowne,
				'dk'			=> $row->user_dane_kontaktowe,
				'l'				=> $row->user_logo,
				'nl'			=> $row->user_newsletter,
				'sg'			=> $row->user_slider_glowny
            );
			$this->session->set_userdata($data);
			if($auto == true) {
				$loginhash = sha1($row->ID_users.time());
				$this->db->where(array('ID_users' => $row->ID_users));
				$this->db->update('users', array('user_loginhash' => $loginhash));
				// zapisać hash do ciastka
				$cookie = array(
                   'name'   => 'autologowanie',
                   'value'  => $loginhash,
                   'expire' => '865000'
                   //'domain' => '.some-domain.com',
                   //'path'   => '/',
                   //'prefix' => 'admin_'
               );

				set_cookie($cookie); 
			}
		} else {
			$query = $this->db->get_where('users', array('user_login' => $login));
			if($query->num_rows() > 0){
				$row = $query->row();
				
				$stats = array(
					'stats_user_id' => $row->ID_users,
					'stats_login' => $row->user_login,
					'stats_ip' => $this->input->ip_address(),
					'stats_user_agent' => $this->input->user_agent(),
					'stats_type' => 'Logowanie ręczne',
					'stats_result' => 'Błędne hasło'
				);
				
				$this->db->set('stats_time', 'sysdate()', FALSE);
				$this->db->insert('stats', $stats);
				
				
				/* crm db load
				 * $this->db->close();
					
				$db2 = $this->load->database('crm', true);
				$db2->set('stats_time', 'sysdate()', FALSE);
				$db2->insert('stats', $stats);
				
				$db2->close();
				
				$this->load->database('default');*/
				
			} else {
				$stats = array(
					'stats_user_id' => '-1',
					'stats_login' => $login,
					'stats_ip' => $this->input->ip_address(),
					'stats_user_agent' => $this->input->user_agent(),
					'stats_type' => 'Logowanie ręczne',
					'stats_result' => 'Nieistniejący login'
				);
				
				$this->db->set('stats_time', 'sysdate()', FALSE);
				$this->db->insert('stats', $stats);
				
				
				/* crm db load
				 * $this->db->close();
					
				$db2 = $this->load->database('crm', true);
				$db2->set('stats_time', 'sysdate()', FALSE);
				$db2->insert('stats', $stats);
				
				$db2->close();
				
				$this->load->database('default');*/
			}
		}
	}
	
	function auto_log_in_user($loginhash) {
		$data = array('user_loginhash' => $loginhash, 'user_deleted' => 0);
		$query = $this->db->get_where('users', $data);
		if($query->num_rows() > 0) {
			$row = $query->row();
			
			if($row->user_blocked == 1) {
				$stats = array(
					'stats_user_id' => $row->ID_users,
					'stats_login' => $row->user_login,
					'stats_ip' => $this->input->ip_address(),
					'stats_user_agent' => $this->input->user_agent(),
					'stats_type' => 'Logowanie automatyczne',
					'stats_result' => 'Użytkownik zablokowany'
				);
				$this->db->set('stats_time', 'sysdate()', FALSE);
				$this->db->insert('stats', $stats);
				
				
				/* crm db load
				 * $this->db->close();
					
				$db2 = $this->load->database('crm', true);
				$db2->set('stats_time', 'sysdate()', FALSE);
				$db2->insert('stats', $stats);
				
				$db2->close();
				
				$this->load->database('default');*/
				
				return;
			}
			
			$stats = array(
				'stats_user_id' => $row->ID_users,
				'stats_login' => $row->user_login,
				'stats_ip' => $this->input->ip_address(),
				'stats_user_agent' => $this->input->user_agent(),
				'stats_type' => 'Logowanie automatyczne',
				'stats_result' => 'Logowanie udane'
			);
			$this->db->set('stats_time', 'sysdate()', FALSE);
			$this->db->insert('stats', $stats);
			
			
			/* crm db load
			 * $this->db->close();
					
			$db2 = $this->load->database('crm', true);
			$db2->set('stats_time', 'sysdate()', FALSE);
			$db2->insert('stats', $stats);
			
			$db2->close();
			
			$this->load->database('default');*/
			
			
			$data = array(
                'uzytkownik'	=> $row->user_login, //$this->input->post('login', TRUE),
				'uzytkownik_id'	=> $row->ID_users,
                'zalogowany' 	=> TRUE,
				'firma'			=> $row->user_nazwa_firmy,
				//'pagename'		=> $row->user_pagename,
				'admin'			=> $row->user_admin,
				'tekst'		=> $row->user_podstrony,
				//'ustawienia_strony'	=> $row->user_ustawienia_strony,
				'galeria'		=> $row->user_galeria,
				'wideo'			=> $row->user_wideo,
				'pa'			=> $row->user_polecane,
				'news'			=> $row->user_aktualnosci,
				'fz'			=> $row->user_formularz_zgloszeniowy,
				'fk'			=> $row->user_formularz_kontaktowy,
				'faq'			=> $row->user_faq,
				'pliki'			=> $row->user_pliki,
				'mapa'			=> $row->user_mapa,
				'baner'			=> $row->user_baner,
				'sl'			=> $row->user_slider,
			
				'bg'			=> $row->user_baner_glowny,
				'ag'			=> $row->user_aktualnosci_glowne,
				'dk'			=> $row->user_dane_kontaktowe,
				'l'				=> $row->user_logo,
				'nl'			=> $row->user_newsletter,
				'sg'			=> $row->user_slider_glowny
            );
			$this->session->set_userdata($data);
		}
	}
	
	function refresh_user_sess_vals($id) {		/*		sprawdzac czy nie jest usuniety?		*/
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows() > 0){
			$row = $query->row();
			$data = array(
				'uzytkownik'	=>	$row->user_login,
				'firma'			=>	$row->user_nazwa_firmy,
				'admin'			=>	$row->user_admin,
				'tekst'			=>	$row->user_podstrony,
				'galeria'		=>	$row->user_galeria,
				'wideo'			=>	$row->user_wideo,
				'pa'			=>	$row->user_polecane,
				'news'			=>	$row->user_aktualnosci,
				'fz'			=>	$row->user_formularz_zgloszeniowy,
				'fk'			=>	$row->user_formularz_kontaktowy,
				'faq'			=>	$row->user_faq,
				'pliki'			=>	$row->user_pliki,
				'mapa'			=>	$row->user_mapa,
				'baner'			=>	$row->user_baner,
				'sl'			=>	$row->user_slider,
			
				'bg'			=> $row->user_baner_glowny,
				'ag'			=> $row->user_aktualnosci_glowne,
				'dk'			=> $row->user_dane_kontaktowe,
				'l'				=> $row->user_logo,
				'nl'			=> $row->user_newsletter,
				'sg'			=> $row->user_slider_glowny
			);
			$this->session->set_userdata($data);
		}
	}
		
	function edituser($id, $login, $mail, $pagename/*, $contact_mail, $title, $description*/){
		$data = array(
			'user_login' => $login,
			'user_mail' => $mail,
			'user_pagename' => $pagename
			/*,
			'user_contact_mail' => $contact_mail
			/*'user_title' => $title,
			'user_description' => $description*/
		);
		$this->db->where('ID_users', $id);
		$this->db->update('users', $data);
		
		$affRows = $this->db->affected_rows();
		
		
		/* crm db load
		 * $this->db->close();
					
		$db2 = $this->load->database('crm', true);
		$db2->where('ID_users', $id);
		$db2->update('users', $data);
		
		$db2->close();
		
		$this->load->database('default');*/
					
		
		action_log('Edycja użytkownika', 'users', 'update', $id, $login);
		
		$data1 = array(
			'uzytkownik' => $login,
			'pagename' => $pagename
		);
		$this->session->set_userdata($data1);
		return $affRows;
	}
	
	function editmetadane($id, $title, $description, $lang_nr){
		if($lang_nr == ''){
			$data = array(
				'user_title' => $title,
				'user_description' => $description
			);
			$this->db->where('ID_users', $id);
			$this->db->update('users', $data);
			$affRows = $this->db->affected_rows();
			
			
			/* crm db load
			 * $this->db->close();
					
			$db2 = $this->load->database('crm', true);
			$db2->where('ID_users', $id);
			$db2->update('users', $data);
			
			$db2->close();
			
			$this->load->database('default');*/
			
			
			action_log('Edycja metadanych użytkownika', 'users', 'update', $id, $this->session->userdata('uzytkownik'));
			
			return $affRows;	
		} else {
			$data = array(
				'users_meta_user_id' => $id,
				'users_meta_lang_id' => $lang_nr,
				'users_meta_title' => $title,
				'users_meta_description' => $description
			);
			$query = $this->db->get_where('users_meta', array('users_meta_user_id' => $id, 'users_meta_lang_id' => $lang_nr));
			if($query && $query->num_rows() > 0){
				$this->db->where('users_meta_user_id', $id);
				$this->db->where('users_meta_lang_id', $lang_nr);
				$this->db->update('users_meta', $data);
				$affRows = $this->db->affected_rows();
				
				action_log('Edycja metadanych użytkownika w innej wersji językowej', 'users_meta', 'update', $id, '');
				
				return $affRows;
			} else {
				$this->db->insert('users_meta', $data);
				$affRows = $this->db->affected_rows();
				
				action_log('Dodanie metadanych użytkownika w innej wersji językowej', 'users_meta', 'insert', $id, '');
				
				return $affRows;
			}
		}
		
	}
	
	function checkpass($id, $pass){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			
			$newpass = sha1($this->encryption_key.$pass);
			if($newpass == $row->user_pass) return TRUE; else return FALSE;
			
		} else return FALSE;
	}
	
	function changepass($id, $pass){
		$this->db->where('ID_users', $id);
		$this->db->update('users', array('user_pass' => sha1($this->encryption_key.$pass)));
		
		$affRows = $this->db->affected_rows();
		$nazwa = $this->db->get_where('users', array('ID_users' => $id))->row()->user_login;
		
		/* crm db load
		 * $this->db->close();
					
		$db2 = $this->load->database('crm', true);
		$db2->where('ID_users', $id);
		$db2->update('users', array('user_pass' => sha1($this->encryption_key.$pass)));
		
		$db2->close();
		
		$this->load->database('default');*/
		
		
		action_log('Zmiana hasła', 'users', 'update', $id, $nazwa);
		return $affRows;
	}
	
	function checkblock($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			if($row->user_blocked == 1)	return TRUE;
			else return FALSE;
		} else return TRUE;
	}
	
	function sprawdz_blokade_stron($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows() > 0){
			$row = $query->row();
			if($row->user_block_adding_pages == 1) return TRUE;
			else return FALSE;
		} else return TRUE;
	}
	
	function getpagetemplate($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			return $row->user_template;
		} else return 1;
	}
	
	function setpagetemplate($id, $template){
		$this->db->where('ID_users', $id);
		$this->db->update('users', array('user_template' => $template));
		
		$affRows = $this->db->affected_rows();
		
		$nazwa = $this->db->get_where('users', array('ID_users' => $id))->row()->user_login;
		
		
		/* crm db load
		 * $this->db->close();
					
		$db2 = $this->load->database('crm', true);
		$db2->where('ID_users', $id);
		$db2->update('users', array('user_template' => $template));
		
		$db2->close();
		
		$this->load->database('default');*/
		
		
		action_log('Zmiana szablonu strony', 'users', 'update', $id, $nazwa);
		return $affRows;
	}
	
	function getarticletemplate($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			return $row->user_article_template;			
		} else return 0;
	}
	
	function setdefaultarticletemplate($id, $template) {
		$this->db->where('ID_users', $id);
		$this->db->update('users', array('user_article_template' => $template));
		
		$affRows = $this->db->affected_rows();
		
		$nazwa = $this->db->get_where('users', array('ID_users' => $id))->row()->user_login;
		
		
		/* crm db load
		 * $this->db->close();
					
		$db2 = $this->load->database('crm', true);
		$db2->where('ID_users', $id);
		$db2->update('users', array('user_article_template' => $template));
		
		$db2->close();
		
		$this->load->database('default');*/
			
		
		action_log('Zmiana domyślnego szablonu artykułu', 'users', 'update', $id, $nazwa);
		return $affRows;
	}
 
	function checkname_url($name_url){
		if($name_url == 'pl') $name_url .= '1';
		else {
			$langs = $this->getlangs();
			foreach($langs->result() as $lang){
				if($name_url == $lang->langs_code) {
					$name_url .= '1';
					break;
				}
			}
		} 
		
		
		$sql="SELECT * FROM tree, nodes WHERE tree_node_id=ID_nodes and tree_user_id=".$this->session->userdata('uzytkownik_id')." and nodes_name_url='".$name_url."'";     
		$query = $this->db->query($sql);
		
		if($query->num_rows > 0){
			$i = 0;
			while(1){
				$i++;
				$sql="SELECT * FROM tree, nodes WHERE tree_node_id=ID_nodes and tree_user_id=".$this->session->userdata('uzytkownik_id')." and nodes_name_url='".$name_url."-".$i."'";     
				$query = $this->db->query($sql);
				if($query->num_rows > 0) {
					continue;
				} else return $name_url.'-'.$i;
			}
			/*$row = $query->row();
			$query2 = $this->db->get_where('tree', array('tree_node_id' => $row->ID_nodes));
			if($query2->num_rows > 0){
				$row2 = $query2->row();
				if($row2->tree_user_id == $this->session->userdata('uzytkownik_id')) { 
					return TRUE; 
				} else return FALSE;
			}*/
			//return TRUE;
		} else return $name_url;
	}
 	
	function checkname_url_update($id, $name_url){
		if($name_url == 'pl') $name_url .= '1'; 
		else {
			$langs = $this->getlangs();
			foreach($langs->result() as $lang){
				if($name_url == $lang->langs_code) {
					$name_url .= '1';
					break;
				}
			}	
		}
		
		
		$sql="SELECT * FROM tree, nodes WHERE ID_tree!=".$id." and tree_node_id=ID_nodes and tree_user_id=".$this->session->userdata('uzytkownik_id')." and nodes_name_url='".$name_url."'";     
		$query = $this->db->query($sql);
		/*
		$query = $this->db->get_where('tree', array('ID_tree' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			$nodeId = $row->tree_node_id;
		} else return TRUE;
		
		$this->db->where('ID_nodes !=', $nodeId);
		$query = $this->db->get_where('nodes', array('nodes_name_url' => $name_url));*/
		if($query->num_rows > 0){
			
			$i = 0;
			while(1){
				$i++;
				$sql="SELECT * FROM tree, nodes WHERE ID_tree!=".$id." and tree_node_id=ID_nodes and tree_user_id=".$this->session->userdata('uzytkownik_id')." and nodes_name_url='".$name_url."-".$i."'";     
				$query = $this->db->query($sql);
				if($query->num_rows > 0) {
					continue;
				} else return $name_url.'-'.$i;
			}
			
			
			/*$row = $query->row();	
			if($row->ID_tree == $id) return FALSE;
			
			return TRUE;*/
		} else return $name_url;
	}
	
	function get_user_pagename($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows>0){
			$row = $query->row();
			return $row->user_pagename;
		}
	}
	
	function get_user_folder($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows>0){
			$row = $query->row();
			return $row->user_folder;
		}
	}
	
	function getuserlink($id){
		$query = $this->db->get_where('user_link', array('user_link_user_id' => $id));
		if($query->num_rows() > 0){
			return $query->row();
		}
		return NULL;
	}
	
	function getlangs(){
		$this->db->order_by('ID_langs', 'asc');
		return $this->db->get('langs');
	}
	
	function getlangcode($lang_nr){
		if($lang_nr == '') return '';
		else $query = $this->db->get_where('langs', array('ID_langs' => $lang_nr));
		return $query->row()->langs_code.'/';
	}
	
	function addlang($userId, $lang){
		if(!$this->checklanguser($userId, $lang)) return false;
		
		
		$user_template = $this->db->get_where('users', array('ID_users' => $userId))->row()->user_template;
		$user_template_folder = '';
		$query = $this->db->get_where('global_templates', array('ID_global_templates' => $user_template));
		if($query->num_rows > 0){
			$user_template_folder = $query->row()->global_templates_folder;
		} else $user_template_folder = NULL;
		
		$folder = $this->get_user_folder($userId);
		if($user_template == 0) $base_template = './pages/'.$folder.'/template/base/';
		else {
			$base_template = './pages/application/template/'.$user_template_folder.'/';
		}
		
		$lang_code = $this->db->get_where('langs', array('ID_langs' => $lang))->row()->langs_code;
		if(file_exists($base_template.'translate/translate.php') && !file_exists($base_template.'translate/translate_'.$lang_code.'.php')){
			copy($base_template.'translate/translate.php', $base_template.'translate/translate_'.$lang_code.'.php');
		}
		if(file_exists($base_template.'translate/img.php') && !file_exists($base_template.'translate/img_'.$lang_code.'.php')){
			copy($base_template.'translate/img.php', $base_template.'translate/img_'.$lang_code.'.php');
		}
		
		
		$order = $this->getmaxlangsorder($userId) + 1;
		$data = array(
			'langs_user_user_id' => $userId,
			'langs_user_langs_id' => $lang,
			'langs_user_order' => $order,
			'langs_user_deactivated' => 0
		);
		$this->db->insert('langs_user', $data);
		
		$data2 = array(
			'tree_user_id' => $userId,
			'tree_node_order' => '1',
			'tree_path' => 'root',
			'tree_lang' => $lang
		);
		$this->db->insert('tree', $data2);
		
		action_log('Dodawanie użytkownikowi języka','langs_user','insert','','');
		action_log('Dodawanie użytkownikowi elementu root w drzewie dla nowego języka','tree','insert','','');
		return true;
	}
	
	function getmaxlangsorder($userId){
		$sql = "SELECT MAX(langs_user_order) FROM langs_user WHERE langs_user_user_id=".$userId."";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(langs_user_order)'];
		} else return 1;
	}
	
	function getlangsuser($userId){
		$this->db->order_by('langs_user_order', 'asc');
		$this->db->select('*');
		$this->db->from('langs_user');
		$this->db->from('langs');
		$this->db->where('langs_user_langs_id', 'ID_langs', false);
		$this->db->where('langs_user_user_id', $userId);
		$this->db->where('langs_user_deactivated', 0);
		return $this->db->get();
	}
	
	function getlangsuser2($userId){
		$this->db->order_by('langs_user_order', 'asc');
		$this->db->select('*');
		$this->db->from('langs_user');
		$this->db->from('langs');
		$this->db->where('langs_user_langs_id', 'ID_langs', false);
		$this->db->where('langs_user_user_id', $userId);
		return $this->db->get();
	}
	
	function checkmultilanguser($userId){
		$query = $this->db->get_where('users', array('ID_users' => $userId));
		if($query && $query->num_rows() > 0){
			$row = $query->row();
			if($row->user_multilang == 1){
				return true;
			} else return false;
		}
		return false;
	}
	
	function checklanguser($userId, $lang){
		$query = $this->db->get_where('langs_user', array('langs_user_user_id' => $userId, 'langs_user_langs_id' => $lang));
		if($query && $query->num_rows() > 0){
			return false;
		}
		return true; 
	}
	
	function setlang($lang_nr){
		$cookie = array(
			'name'   => 'lang',
			'value'  => $lang_nr,
			'expire' => '865000'
		);
		set_cookie($cookie);
	}
	
	function setlangsorder($langs){
		$order = 0;
		foreach ($langs as $l){
			$order++;
			$this->db->where('ID_langs_user', $l);			
			$this->db->update('langs_user', array('langs_user_order' => $order));
			
			$sql = "select * from langs, langs_user where ID_langs=langs_user_langs_id and ID_langs_user=".$l."";
			$nazwa = $this->db->query($sql)->row()->langs_name;			
			
			action_log('Zmiana kolejności języków', 'langs_user', 'update', $l, $nazwa);
		}
	}
	
	function getcontactdata($id){
		$query = $this->db->get_where('dane_kontaktowe', array('dane_kontaktowe_user_id' => $id));
		if($query && $query->num_rows() > 0){
			return $query->row();
		} else {
			$row = null;
			$row->dane_kontaktowe_nazwa = '';
			$row->dane_kontaktowe_adres = '';
			$row->dane_kontaktowe_telefon = '';
			$row->dane_kontaktowe_mail = '';
			$row->dane_kontaktowe_info = '';
			return $row;
		}
	}
	
	function setcontactdata($id, $nazwa, $adres, $telefon, $mail, $info){
		$query = $this->db->get_where('dane_kontaktowe', array('dane_kontaktowe_user_id' => $id));
		if($query && $query->num_rows() > 0){
			$data = array(
				'dane_kontaktowe_nazwa' => $nazwa,
				'dane_kontaktowe_adres' => $adres,
				'dane_kontaktowe_telefon' => $telefon,
				'dane_kontaktowe_mail' => $mail,
				'dane_kontaktowe_info' => $info
			);
			$this->db->where('dane_kontaktowe_user_id', $id);
			$this->db->set('dane_kontaktowe_added', 'sysdate()', FALSE);
			$this->db->update('dane_kontaktowe', $data);
			
			action_log('Aktualizacja danych kontaktowych','dane_kontaktowe','update',$id,$nazwa);
			
		} else {
			$data = array(
				'dane_kontaktowe_user_id' => $id,
				'dane_kontaktowe_nazwa' => $nazwa,
				'dane_kontaktowe_adres' => $adres,
				'dane_kontaktowe_telefon' => $telefon,
				'dane_kontaktowe_mail' => $mail,
				'dane_kontaktowe_info' => $info
			);
			$this->db->set('dane_kontaktowe_added', 'sysdate()', FALSE);
			$this->db->insert('dane_kontaktowe', $data);
			
			action_log('Dodawanie danych kontaktowych','dane_kontaktowe','insert',$id,$nazwa);
		}
	}
	
	function getlogo($id){
		$query = $this->db->get_where('logo', array('logo_user_id' => $id));
		
		if($query && $query->num_rows() > 0){
			return $query->row()->logo_name;
		} else {
			return null;
		}
	}
	
	function setlogo($file, $id){
		$plik = $_FILES['plik'];
		$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
		
		$query = $this->db->get_where('logo', array('logo_user_id' => $id));
		if($query && $query->num_rows() > 0){
			$row = $query->row();
			unlink('pages/'.$folder.'/images/'.$row->logo_name);
			$this->db->delete('logo', array('ID_logo' => $row->ID_logo));
		}
    	
    	if(in_array($plik['type'], array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))){
    		$n = $plik['name'];
    		$r = explode(".", $n);
    		$nazwa_pliku = 'logo-'.time().'.'.$r[count($r)-1];
    		
    		if(@move_uploaded_file($plik['tmp_name'], 'pages/'.$folder.'/images/'.$nazwa_pliku)){
    			
    			    			
    			
    			
    			$data = array(
    				'logo_user_id' => $id,
    				'logo_name' => $nazwa_pliku
    			);
    			
    			$this->db->insert('logo', $data);
    			action_log('Ustawianie logo', 'logo', 'insert', $this->db->insert_id(), $nazwa_pliku);
    		}
    		return true;
    	} else {return false;}
	}
	
	function getglobaltemplates(){
		$this->db->order_by('ID_global_templates', 'asc');
		return $this->db->get_where('global_templates', array('global_templates_enabled' => 1));
	}
	
	function getarticletemplates($id){
		$this->db->order_by('ID_article_templates', 'asc');
		$this->db->where('article_templates_user_id', 0);
		$this->db->or_where('article_templates_user_id', $id);
		return $this->db->get('article_templates');
	}
	
	function deactivatelang($id){
		$this->db->where('ID_langs_user', $id);
		$this->db->set('langs_user_deactivated', 1);
		$this->db->update('langs_user');
	}
	
	function activatelang($id){
		$this->db->where('ID_langs_user', $id);
		$this->db->set('langs_user_deactivated', 0);
		$this->db->update('langs_user');
	}
	
	function terminplatnosci($id){
		$this->db->select('*,  (TO_DAYS(user_kolejna_platnosc)-TO_DAYS(NOW())) as czas');
		return $this->db->get_where('users', array('ID_users' => $id))->row()->czas;
	}
	
	
	
	
	
}
?>