<?php
class User_model extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function User_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function getlang($userId, $lang_code){
    	if($lang_code == 'pl') return '';
    	$q = $this->db->get_where('users', array('ID_users' => $userId));
    	if($q && $q->num_rows() > 0){
    		$r = $q->row();
    		if($r->user_multilang == 0){
    			return -1;
    			//echo $r->user_multilang;
    		}
    	}
    	$query = $this->db->get_where('langs', array('langs_code' => $lang_code));
    	if($query && $query->num_rows() > 0) {
    		$row = $query->row();
    		$query2 = $this->db->get_where('langs_user', array('langs_user_user_id' => $userId, 'langs_user_langs_id' => $row->ID_langs));
    		if($query2 && $query2->num_rows() > 0) {
    			return ''.$row->ID_langs;
    		} else return '';
    	} else return '';
    }
    
	function getlangsuser($userId){
    	$q = $this->db->get_where('users', array('ID_users' => $userId));
    	if($q && $q->num_rows() > 0){
    		$r = $q->row();
    		if($r->user_multilang == 0){
    			return NULL;
    			//echo $r->user_multilang;
    		}
    	}
		$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);
		//$this->db->where('langs_user_langs_id=ID_langs', NULL, false);
		return $this->db->get();
	}
    
	function getmodule($id, $userId) {
		return $this->db->get_where('modules', array('ID_modules' => $id, 'modules_user_id' => $userId, 'modules_deleted' => 0));
	}
	
	function getModules($id, $userId) {
		$this->db->order_by('modules_order', 'asc');
		return $this->db->get_where('modules', array('modules_tree_id' => $id, 'modules_user_id' => $userId, 'modules_deleted' => 0));
	}
	
	function getPhotos($moduleId){
		$this->db->order_by('photos_order', 'asc');
		return $this->db->get_where('photos', array('photos_module_id' => $moduleId));
	}
	
	function getBanerPhotos($moduleId){
		$this->db->order_by('baner_order', 'asc');
		return $this->db->get_where('baner', array('baner_module_id' => $moduleId));
	}
	
	function getSliderPhotos($moduleId){
		$this->db->order_by('slider_order', 'asc');
		return $this->db->get_where('slider', array('slider_module_id' => $moduleId));
	}
	
	function getTextModule($moduleId){
		return $this->db->get_where('text', array('text_module_id' => $moduleId));
	}
	
	function getVideos($moduleId){
		$this->db->order_by('videos_order', 'asc');
		return $this->db->get_where('videos', array('videos_module_id' => $moduleId));
	}
	
	function getvideosize($userId) {
		return $this->db->get_where('videos_config', array('videos_config_user_id' => $userId));
	}
	
	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 || $row->user_deleted == 1)	return TRUE;
			else return FALSE;
		} else return TRUE;
	}
	
	function getpagename($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			return $row->user_pagename;
		} else return NULL;
	}
	
	function getfolder($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			return $row->user_folder;
		} else return NULL;
	}
	
	function gettitle($id, $lang_nr){
		if($lang_nr == ''){
			$query = $this->db->get_where('users', array('ID_users' => $id));
			if($query->num_rows > 0){
				$row = $query->row();
				return $row->user_title;
			} else return NULL;
		} else {
			$this->db->select('*, users_meta_title as user_title, users_meta_description as user_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){
				$row = $query->row();
				return $row->user_title;
			} else return NULL;
		}
	}
	
	function getmail($id){
		$query = $this->db->get_where('users', array('ID_users' => $id));
		if($query->num_rows > 0){
			$row = $query->row();
			return $row->user_mail;
		} else return NULL;
	}
	
	function getcontactmail($modId){
		$query = $this->db->get_where('modul_kontakt', array('modul_kontakt_module_id' => $modId));
		if($query->num_rows > 0){
			$row = $query->row();
			return $row->modul_kontakt_mail;
		} else return NULL;
	}
	
	function getdescription($id, $lang_nr){
		if($lang_nr == ''){
			$query = $this->db->get_where('users', array('ID_users' => $id));
			if($query->num_rows > 0){
				$row = $query->row();
				return $row->user_description;
			} else return NULL;
		} else {
			$this->db->select('*, users_meta_title as user_title, users_meta_description as user_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){
				$row = $query->row();
				return $row->user_title;
			} else return NULL;
		}
	}
	
	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 getpagetemplatefolder($templateId){
		$query = $this->db->get_where('global_templates', array('ID_global_templates' => $templateId));
		if($query->num_rows > 0){
			return $query->row();
		} else return NULL;
	}
	
	function getarticletemplate($templateId){
		return $this->db->get_where('article_templates', array('ID_article_templates' => $templateId))->row();
	}
	
	function getIdByName($name, $userId){
		$sql = "select * from nodes, tree where tree_node_id=ID_nodes and nodes_name_url='".$name."' and tree_user_id=".$userId." and tree_deleted=0"; 
		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			$row = $query->row();
			return $row->ID_tree;
		} else return 0;
		
	}
	
	function getuserplugins($id){		
		$sql = 'select * from plugins, plugins_user where plugins_user_plugin_id=ID_plugins and plugins_user_user_id='.$id.' order by ID_plugins asc;';
		
		return $this->db->query($sql);
	}
	
	function getRecommended($moduleId, $userId){
		$query = $this->db->get_where('polecane', array('polecane_module_id' => $moduleId));
		$p_tab = NULL;
		if($query->num_rows() > 0){
			$row = $query->row();
			if($row->polecane_latest == -1){
				$polecane = explode("/", $row->polecane_val);
				$p_tab = NULL;
				foreach ($polecane as $p){
					if((int)$p > 0) $p_tab[] = $p;
				}
				return $p_tab;
			} else if($row->polecane_latest > 0){
				
				$kategorie = explode("/", $row->polecane_val);
				
				$path='';
				foreach ($kategorie as $k){
					if((int)$k > 0) {
						$path .= " or tree_path REGEXP '.".$k.".((\.)([0-9]+))?$' ";
					}
				}
				$sql = 'select * from tree, nodes where tree_user_id=1 '.$path.' having    tree_deleted=0 and    ID_nodes=tree_node_id and nodes_published=1 order by ID_tree desc limit '.$row->polecane_latest.' ';
				
				$rec = $this->db->query($sql);
				foreach ($rec->result() as $r) {
					$p_tab[] = $r->ID_tree;
				}
			}
		}
		return $p_tab;
	}
	
	
	
	function getFaq($moduleId){

		$faqs = NULL;
		
		$this->db->order_by('faq_order', 'asc');
		$query = $this->db->get_where('faq', array('faq_module_id' => $moduleId, 'faq_deleted' => 0));
		$i = 0;
		foreach ($query->result() as $faq){
			$newFaq = new newFaq();
			$newFaq->name = $faq->faq_name;
			$newFaq->content = NULL;
			
			$this->db->order_by('faq_qa_order', 'asc');
			$query2 = $this->db->get_where('faq_qa', array('faq_qa_faq_id' => $faq->ID_faq, 'faq_qa_deleted' => 0));
			
			foreach ($query2->result() as $row){
				$newFaq->content[] = $row;
			}
			
			$faqs[$i++] = $newFaq;
		}
		return $faqs;
	}
	
	function getPliki($moduleId){
		$this->db->order_by('pliki_order', 'asc');
		return $this->db->get_where('pliki', array('pliki_module_id' => $moduleId, 'pliki_deleted' => 0));
	}
	
	function getNews($moduleId){
		$this->db->order_by('ID_news', 'desc');
		return $this->db->get_where('news', array('news_module_id' => $moduleId, 'news_deleted' => 0));
	}
	
	function getNewsGlowny($userId){
		$this->db->select('*, ID_news_glowny as ID_news, news_glowny_user_id as news_user_id, news_glowny_title as news_title, news_glowny_title_url as news_title_url, news_glowny_text_short as news_text_short, news_glowny_text as news_text, news_glowny_expiration_date as news_expiration_date, news_glowny_order as news_order, news_glowny_added as news_added, news_glowny_updated as news_updated, news_glowny_deleted as news_deleted, news_glowny_lang as news_lang');
		$this->db->order_by('ID_news_glowny', 'desc');
		return $this->db->get_where('news_glowny', array('news_glowny_user_id' => $userId, 'news_glowny_deleted' => 0));
	}
	
	function getSingleNews($userId, $node_name, $id, $name){
		$sql = 'select * from news, modules, nodes, tree where 
			modules_tree_id=ID_tree and tree_node_id=ID_nodes and nodes_name_url="'.$node_name.'" 
			and modules_user_id='.$userId.' and ID_modules=news_module_id and ID_news='.$id.' and news_title_url="'.$name.'"';
		return $this->db->query($sql);
	}
	
	function getSingleNewsGlowny($userId, $node_name, $id, $name, $lang_nr){
		$this->db->select('*, ID_news_glowny as ID_news, news_glowny_user_id as news_user_id, news_glowny_title as news_title, news_glowny_title_url as news_title_url, news_glowny_text_short as news_text_short, news_glowny_text as news_text, news_glowny_expiration_date as news_expiration_date, news_glowny_order as news_order, news_glowny_added as news_added, news_glowny_updated as news_updated, news_glowny_deleted as news_deleted, news_glowny_lang as news_lang');
		return $this->db->get_where('news_glowny', array('ID_news_glowny' => $id, 'news_glowny_user_id' => $userId, 'news_glowny_lang' => $lang_nr));
	}
	
	function getLocations($moduleId){
		return $this->db->get_where('mapa', array('mapa_module_id' => $moduleId, 'mapa_deleted' => 0));
	}
	
	function getContactData($userId){
		$this->db->select('*, dane_kontaktowe_nazwa as name, dane_kontaktowe_adres as address, dane_kontaktowe_telefon as phone, dane_kontaktowe_mail as mail, dane_kontaktowe_info as info');
		$query = $this->db->get_where('dane_kontaktowe', array('dane_kontaktowe_user_id' => $userId));
		if($query && $query->num_rows() > 0){
			return $query->row();
		} else {
			$row = null;
			$row->name = '';
			$row->address = '';
			$row->phone = '';
			$row->mail = '';
			$row->info = '';
			return $row;
		}
	}
	
	function getLogo($userId){
		$query = $this->db->get_where('logo', array('logo_user_id' => $userId));
		
		if($query && $query->num_rows() > 0){
			return $query->row()->logo_name;
		} else {
			return null;
		}
	}
	
	function getBaner($userId){
		$this->db->select('*, ID_baner_glowny as ID_baner, baner_glowny_name as baner_name, baner_glowny_header as baner_header, baner_glowny_order as baner_order, baner_glowny_added as baner_added ');
		$this->db->order_by('baner_glowny_order', 'asc');
		$query = $this->db->get_where('baner_glowny', array('baner_glowny_user_id' => $userId));
		if($query && $query->num_rows() > 0){
			return $query;
		} else {
			return null;
		}		
	}
	
	function getSlider($userId){
		$this->db->select('*, ID_slider_glowny as ID_slider, slider_glowny_name as slider_name, slider_glowny_header as slider_header, slider_glowny_order as slider_order, slider_glowny_added as slider_added ');
		$this->db->order_by('slider_glowny_order', 'asc');
		$query = $this->db->get_where('slider_glowny', array('slider_glowny_user_id' => $userId));
		if($query && $query->num_rows() > 0){
			return $query;
		} else {
			return null;
		}
	}
	
	function checkBaner($userId, $mainId, $id){
		$query = $this->db->get_where('baner_glowny_config', array('baner_glowny_config_user_id' => $userId));
		if($query && $query->num_rows() > 0){
			$row = $query->row();
			if($row->baner_glowny_config_allpages == 1) return true; else {
				if($mainId == $id) return true; else return false;
			}
		} else {
			return true;
		}
	}
	
	function checkSlider($userId, $mainId, $id){
		$query = $this->db->get_where('slider_glowny_config', array('slider_glowny_config_user_id' => $userId));
		if($query && $query->num_rows() > 0){
			$row = $query->row();
			if($row->slider_glowny_config_allpages == 1) return true; else {
				if($mainId == $id) return true; else return false;
			}
		} else {
			return true;
		}
	}
	
	function getGACode($userId){
		return $this->db->get_where('users', array('ID_users' => $userId))->row()->user_ga_code;
	}
	
	
	
	function send_contact($id, $imie, $nazwisko, $mail, $info, $strona, $modId){
		$this->load->helper('email');
		$recipientEmail = $this->getcontactmail($modId);
		if(valid_email($recipientEmail)){
			$config['protocol'] = 'mail';
			$config['charset'] = 'utf-8';
			$config['wordwrap'] = TRUE;
			$config['mailtype'] = 'html';
			$config['priority'] = '1';
			
			$pagename = reduce_double_slashes(prep_url($this->getpagename($id)));
			
			$this->email->initialize($config);
			$this->email->from($mail, $nazwisko.' '.$imie);
			$this->email->to($recipientEmail);
			$this->email->subject('Wiadomość ze strony '.$pagename);
			$data['strona'] = $pagename;//$strona;
			$data['imie'] = $imie;
			$data['nazwisko'] = $nazwisko;
			$data['mail'] = $mail;
			$data['info'] = $info;
			$html = $this->load->view('mail', $data, true);
			$this->email->message($html);
			
			if($this->email->send()){
				
				return TRUE;
			} else return FALSE;
			
		} else return FALSE;
	}
	
	function send_contact_fk($id, $fk, $fk_name, $mail, $strona, $modId){
		$this->load->helper('email');
		$recipientEmail = $this->getcontactmail($modId);
		
		if(valid_email($recipientEmail)){
			$config['protocol'] = 'mail';
			$config['charset'] = 'utf-8';
			$config['wordwrap'] = TRUE;
			$config['mailtype'] = 'html';
			$config['priority'] = '1';
			
			$pagename = reduce_double_slashes(prep_url($this->getpagename($id)));
			
			$this->email->initialize($config);
			$this->email->from($mail, $mail);
			$this->email->to($recipientEmail);
			$this->email->subject('Wiadomość ze strony '.$pagename);
			$data['strona'] = $pagename;//$strona;
			/*$data['imie'] = $imie;
			$data['nazwisko'] = $nazwisko;
			$data['mail'] = $mail;
			$data['info'] = $info;*/
			$data['fk'] = $fk;
			$data['fk_name'] = $fk_name;
			$data['mail'] = $mail;
			$html = $this->load->view('mail2', $data, true);
			$this->email->message($html);
			
			if($this->email->send()){
				
				return TRUE;
			} else return FALSE;
			
		} else return FALSE;
		
		
		
		/*foreach ($fk as $key => $val){
			echo $fk_name[$key].' '.$fk[$key].' || ';
		}
	
		return true;*/
	}
	
	function add_to_newsletter($user_id, $email){
	    $query = $this->db->get_where('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_wypisany' => 0, 'newsletter_email' => $email));
    	if($query && $query->num_rows() > 0){
    		return 0;
    	} else {
    		$this->db->insert('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_email' => $email, 'newsletter_hash' => sha1($user_id.$email.$user_id)));
    		return 1;
    	}
	}
	
	function cancel_newsletter($user_id, $hash){
		$this->db->delete('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_hash' => $hash));
	}
	
	function check_cache($id){
		return $this->db->get_where('users', array('ID_users' => $id))->row()->user_cache;
	}
	
	function szukaj($userId, $szukaj){
		//$this->db->from('users');
		/*$this->db->from('tree');
		$this->db->from('nodes');
		$this->db->from('modules');
		$this->db->from('text');
		$this->db->from('news');
		
		//$this->db->where('id_users', $userId);
		$this->db->where('tree_user_id', $userId);
		$this->db->where('ID_nodes', 'tree_node_id', false);
		$this->db->where('modules_user_id', $userId);*/
		
		//$this->db->where();	// wpisać wszystkie warunki, np, id usera, id drzewa, LIKE $szukaj itp...
		
		$data = array();
		
		$sql = 'select DISTINCT * from tree, nodes where tree_user_id='.$userId.' and tree_node_id=ID_nodes and (nodes_name LIKE "%'.$szukaj.'%" or nodes_name_url LIKE "%'.$szukaj.'%" or nodes_title LIKE "%'.$szukaj.'%" or nodes_description LIKE "%'.$szukaj.'%" or nodes_text LIKE "%'.$szukaj.'%") and tree_deleted=0';
		$data['nodes'] = $this->db->query($sql);
		
		$sql1 = 'select DISTINCT * from modules, text, tree, nodes where modules_user_id='.$userId.' and ID_modules=text_module_id and modules_tree_id=ID_tree and ID_nodes=tree_node_id and text.text_content LIKE "%'.$szukaj.'%" and tree_deleted=0 and modules_deleted=0 and text_deleted=0';
		$data['text'] = $this->db->query($sql1);
		
		$sql2 = 'select DISTINCT * from modules, news, tree, nodes where modules_user_id='.$userId.' and ID_modules=news_module_id and modules_tree_id=ID_tree and ID_nodes=tree_node_id and (news_title LIKE "%'.$szukaj.'%" or news_title_url LIKE "%'.$szukaj.'%" or news_text_short LIKE "%'.$szukaj.'%" or news_text LIKE "%'.$szukaj.'%") and tree_deleted=0 and modules_deleted=0 and news_deleted=0';
		$data['news'] = $this->db->query($sql2);
		
		$sql3 = 'select DISTINCT * from news_glowny where news_glowny_user_id='.$userId.' and (news_glowny_title LIKE "%'.$szukaj.'%" or news_glowny_title_url LIKE "%'.$szukaj.'%" or news_glowny_text_short LIKE "%'.$szukaj.'%" or news_glowny_text LIKE "%'.$szukaj.'%") and news_glowny_deleted=0';
		$data['news_glowny'] = $this->db->query($sql3);
		
		return $data;
		//return $this->db->get();
	}

	
}

class newFaq{
	public $name;
	public $content;
}




?>