<?php
class Modul_model extends Model {

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

    function Modul_model()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->helper("log");
    }

	function getMaxModuleOrder($treeId, $userId){
		$sql="SELECT MAX(modules_order) FROM modules WHERE modules_tree_id='".$treeId."' and modules_user_id={$userId} and modules_deleted=0";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(modules_order)'];
		} else return 1;
	}
	
	function addmodule($name, $type, $treeId, $userId, $lang_nr, $name_show) {
		$order = $this->getMaxModuleOrder($treeId, $userId) + 1;
		if($name_show == true) $ns=1; else $ns=0;
		$data = array(
			'modules_name' => $name,
			'modules_name_show' => $ns,
			'modules_type' => $type,
			'modules_tree_id' => $treeId,
			'modules_order' => $order,
			'modules_user_id' => $userId,
			'modules_lang' => $lang_nr
		);
		$this->db->set('modules_added', 'sysdate()', FALSE);
		$this->db->insert('modules', $data);
		
		$insID = $this->db->insert_id();
		
		action_log('Dodawanie nowego modułu', 'modules', 'insert', $insID, $name);
		
		return $insID;
	}
	
	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 setmodulesorder($modules){
		$order = 0;
		foreach ($modules as $module){
			$order++;
			$this->db->where('ID_modules', $module);			
			$this->db->update('modules', array('modules_order' => $order));
			$nazwa = $this->db->get_where('modules', array('ID_modules' => $module))->row()->modules_name;
			action_log('Zmiana kolejności modułów', 'modules', 'update', $module, $nazwa);
		}
	}
	
	function removemodule($id, $type) {
		//usunąć elementy podpięte do tego - chyba jednak nie
		
		if($type == url_title("Tekst")) {
			//$this->db->where('text_module_id', $id);
			//$this->db->delete('text');
			//action_log('Usuwanie modułu tekstowego', 'text', 'delete', $id);
			$this->db->where('text_module_id', $id);
			$this->db->update('text', array('text_deleted' => 1));
			
		} elseif($type == url_title("Galeria")){
			//$this->db->where('gallery_config_module_id', $id);
			//$this->db->delete('gallery_config');
			//action_log('Usuwanie modułu galeria', 'gallery_config', 'delete', $id);
			
			$photos = $this->getphotos($id);
			if($photos && $photos->num_rows()>0){
				foreach($photos->result() as $photo){
					$this->removephoto($photo->ID_photos, $photo->photos_name);
					action_log('Usuwanie modułu galeria', 'photos', 'delete', $photo->ID_photos, $photo->photos_name);
				}
			}
		} elseif($type == url_title("Galeria wideo")){
			//$this->db->where('videos_config_module_id', $id);
			//$this->db->delete('videos_config');
			//action_log('Usuwanie modułu galeria wideo', 'videos_config', 'delete', $id);
			
			$videos = $this->getvideos($id);
			if($videos && $videos->num_rows()>0){
				foreach($videos->result() as $video){
					$this->removevideo($video->ID_videos, $video->videos_name, $video->videos_type);
					action_log('Usuwanie modułu galeria wideo', 'videos', 'delete', $video->ID_videos, $video->videos_header);
				}
			}
		} elseif($type == url_title("Polecane artykuły")){
			//$this->db->where('polecane_module_id', $id);
			//$this->db->delete('polecane');
			//action_log('Usuwanie modułu polecane', 'polecane', 'delete', $id);
			$this->db->where('polecane_module_id', $id);
			$this->db->update('polecane', array('polecane_deleted' => 1));
			
		} elseif($type == url_title("Aktualności")){
			//$this->db->where('news_module_id', $id);
			//$this->db->delete('news');
			//action_log('Usuwanie modułu aktualności', 'news', 'delete', $id);
			$this->db->where('news_module_id', $id);
			$this->db->update('news', array('news_deleted' => 1));
			
		} elseif($type == "FAQ"){
			$query = $this->db->get_where('faq', array('faq_module_id' => $id));
			if($query->num_rows() > 0){
				foreach($query->result() as $row){
					$this->removefaq($row->ID_faq);
				}
			}
		} elseif($type == "Formularz zgłoszeniowy"){
			//
			
		} elseif($type == "Formularz kontaktowy"){
			//
			
		} elseif($type == "Pliki"){
			
			$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
			$query = $this->db->get_where('pliki', array('pliki_module_id' => $id));
			if($query->num_rows() > 0){
				foreach($query->result() as $row){
					$this->removefile($row->ID_pliki, $folder);
				}
			}
		} elseif($type == "Mapa"){
			$this->db->where('mapa_module_id');
			$this->db->update('mapa', array('mapa_deleted' => 1));
			
		} elseif($type == "Baner"){
			$photos = $this->getbanerphotos($id);
			if($photos && $photos->num_rows()>0){
				foreach($photos->result() as $baner){
					$this->removebanerphoto($baner->ID_baner, $baner->baner_name);
					action_log('Usuwanie modułu baner', 'baner', 'delete', $baner->ID_baner, $baner->baner_name);
				}
			} else {}
		} elseif($type == "Slider"){
			$photos = $this->getsliderphotos($id);
			if($photos && $photos->num_rows()>0){
				foreach($photos->result() as $slider){
					$this->removesliderphoto($slider->ID_slider, $slider->slider_name);
					action_log('Usuwanie modułu slider', 'slider', 'delete', $slider->ID_slider, $slider->slider_name);
				}
			} else {}
		}
		
		$this->db->where('ID_modules', $id);
		//$this->db->delete('modules');
		$this->db->update('modules', array('modules_deleted' => 1));
		$affRows = $this->db->affected_rows();
		$nazwaModulu = $this->db->get_where('modules', array('ID_modules' => $id))->row()->modules_name;
		action_log('Usuwanie modułu', 'modules', 'delete', $id, $nazwaModulu);
		
		return $affRows;
	}
	
	function getmodule($id, $userId) {
		return $this->db->get_where('modules', array('ID_modules' => $id, 'modules_user_id' => $userId, 'modules_deleted' => 0));
	}
		
	function editmodulename($moduleId, $name, $name_show){
		if($name_show == true) $ns=1; else $ns=0;
		$this->db->where('ID_modules', $moduleId);
		$this->db->set('modules_updated', 'sysdate()', FALSE);
		$this->db->update('modules', array('modules_name' => $name, 'modules_name_show' => $ns));
		
		$affRows = $this->db->affected_rows();
		
		action_log('Edycja modułu - zmiana nazwy', 'modules', 'update', $moduleId, $name);
		
		return $affRows;
	}
		
	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 removephoto($id, $name){
		$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
		unlink('pages/'.$folder.'/foto/'.$name);
		unlink('pages/'.$folder.'/foto/thumb_'.$name);
		$this->db->where('ID_photos', $id);
		$this->db->delete('photos');
		$affRows = $this->db->affected_rows();
		
		action_log('Usuwanie zdjęcia', 'photos', 'delete', $id, $name);
		
		return $affRows;
	}
	
	function removebanerphoto($id, $name){
		$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
		unlink('pages/'.$folder.'/foto/baner/'.$name);
		$this->db->where('ID_baner', $id);
		$this->db->delete('baner');
		$affRows = $this->db->affected_rows();
		
		action_log('Usuwanie zdjęcia baneru', 'baner', 'delete', $id, $name);
		
		return $affRows;
	}
	
	function removesliderphoto($id, $name){
		$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
		unlink('pages/'.$folder.'/foto/slider/'.$name);
		$this->db->where('ID_slider', $id);
		$this->db->delete('slider');
		$affRows = $this->db->affected_rows();
		
		action_log('Usuwanie zdjęcia slidera', 'slider', 'delete', $id, $name);
		
		return $affRows;
	}
	
	function removevideo($id, $name, $type){
		if($type=="file"){
			unlink('pages/'.$folder.'/video/'.$name);
		}					
		$this->db->where('ID_videos', $id);
		$this->db->delete('videos');
		
		$affRows = $this->db->affected_rows();
		
		action_log('Usuwanie wideo', 'videos', 'delete', $id, $name);
		
		return $affRows;
	}
	
	function getParentOfModule($id) {
		$query = $this->db->get_where('modules', array('ID_modules' => $id));
		$row = $query->row();
		return $row->modules_tree_id;
	}
		
	function getvideos($moduleId){
		$this->db->order_by('videos_order', 'asc');
		return $this->db->get_where('videos', array('videos_module_id' => $moduleId));
	}
		
	function removefaqqa($id){
		//$this->db->delete('faq_qa', array('ID_faq_qa' => $id));
		$this->db->where('ID_faq_qa', $id);
		$this->db->update('faq_qa', array('faq_qa_deleted' => 1));
		$affRows = $this->db->affected_rows();
		
		$nazwa = $this->db->get_where('faq_qa', array('ID_faq_qa' => $id))->row()->faq_qa_q;
		action_log('Usuwanie Q/A', 'faq_qa', 'delete', $id, $nazwa);
		
		return $affRows;
	}
	
	function removefaq($id){
		$query = $this->db->get_where('faq_qa', array('faq_qa_faq_id' => $id));
		foreach($query->result() as $row){
			$this->removefaqqa($row->ID_faq_qa);
		}
		
		//$this->db->delete('faq', array('ID_faq' => $id));
		$this->db->where('ID_faq', $id);
		$this->db->update('faq', array('faq_deleted' => 1));
		
		$affRows = $this->db->affected_rows();
		
		$nazwa = $this->db->get_where('faq', array('ID_faq' => $id))->row()->faq_name;
		action_log('Usuwanie FAQ', 'faq', 'delete', $id, $nazwa);
		
		return $affRows;
	}
	
	function getmoduletype($id){
		$query = $this->db->get_where('modules', array('ID_modules' => $id));
		return $query->row()->modules_type;
	}
	
	function getmailforcontactform($id){
		$query = $this->db->get_where('modul_kontakt', array('modul_kontakt_module_id' => $id));
		if($query->num_rows() > 0){
			$row = $query->row();
			return $row->modul_kontakt_mail;
		} else return '';
	}
	
	function setmailforcontactform($id, $mail){
		$query = $this->db->get_where('modul_kontakt', array('modul_kontakt_module_id' => $id));
		if($query->num_rows > 0){
			$data = array('modul_kontakt_mail' => $mail);
			$this->db->where('modul_kontakt_module_id', $id);
			$this->db->update('modul_kontakt', $data);
		} else {
			$data = array(
				'modul_kontakt_module_id' => $id,
				'modul_kontakt_mail' => $mail
			);
			$this->db->insert('modul_kontakt', $data);
		}
	}
	
	function removenews($id){
		$this->db->where('ID_news', $id);
		$this->db->update('news', array('news_deleted' => 1));
		$affRows = $this->db->affected_rows();
		
		$nazwa = $this->db->get_where('news', array('ID_news' => $id))->row()->news_name;
		action_log('Usuwanie aktualności', 'news', 'delete', $id, $nazwa);
		
		return $affRows;
	}
	
	function removefile($id, $folder){
		$name = $this->db->get_where('pliki', array('ID_pliki' => $id))->row()->pliki_filename;
				
		unlink('pages/'.$folder.'/uploads/'.$name);
		
		$this->db->where('ID_pliki', $id);
		$this->db->delete('pliki');
		$affRows = $this->db->affected_rows();
			
		action_log('Usuwanie pliku', 'pliki', 'delete', $id, $name);
		
		return $affRows;
	}
	
	function removelocation($id){
		$nazwa = $this->db->get_where('mapa', array('ID_mapa' => $id))->row()->mapa_tytul;
		
		$this->db->where('ID_mapa', $id);
		$this->db->update('mapa', array('mapa_deleted' => 1));
		$affRows = $this->db->affected_rows();
		
		action_log('Usuwanie pozycji na mapie', 'mapa', 'delete', $id, $nazwa);
		
		return $affRows;
	}
	
}




?>