<?php
class Wideo_model extends Model {

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

    function Wideo_model()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->helper("log");
    }
	/*
	function log($info, $table, $operation, $id, $name=''){
		$data = array(
			'action_log_user_id' => $this->session->userdata('uzytkownik_id'),
			'action_log_login' => $this->session->userdata('uzytkownik'),
			'action_log_info' => $info.'. Tabela '.$table.', operacja '.$operation.', ID pozycji '.$id.', nazwa '.$name
		);
		$this->db->set('action_log_time', 'sysdate()', FALSE);
		$this->db->insert('action_log', $data);
	}
	*/
	
	function getvideosize($userId) {
		return $this->db->get_where('videos_config', array('videos_config_user_id' => $userId));
	}
		
	function editvideosize($userId, $x, $y){
		$size = $this->getvideosize($userId);
		if($size && $size->num_rows()>0){
			$data = array('videos_config_size_x' => $x, 'videos_config_size_y' => $y);
			$this->db->where('videos_config_user_id', $userId);
			$this->db->update('videos_config', $data);

			$affRows = $this->db->affected_rows();
			$s = $size->row();
			action_log('Zmiana rozmiaru wideo', 'videos_config', 'update', $s->ID_videos_config);
		} else {
			$data = array('videos_config_user_id' => $userId, 'videos_config_size_x' => $x, 'videos_config_size_y' => $y);
			$this->db->insert('videos_config', $data);

			$affRows = $this->db->affected_rows();
			action_log('Zmiana rozmiaru wideo', 'videos_config', 'insert', $this->db->insert_id());
		}
		return $this->db->affected_rows();
	}
			
	function setvideosorder($videos){
		$order = 0;
		foreach ($videos as $video){
			$order++;
			$this->db->where('ID_videos', $video);			
			$this->db->update('videos', array('videos_order' => $order));
			$nazwa = $this->db->get_where('videos', array('ID_videos' => $video))->row()->videos_header;
			action_log('Zmiana kolejności wideo', 'videos', 'update', $video, $nazwa);
		}
	}
	
	function getvideo($id){
		$query = $this->db->get_where('videos', array('ID_videos' => $id));
		if($query->num_rows()>0){
			$row = $query->row();
			return $row;
		} else return NULL;
	}
	
	function getMaxVideoOrder($moduleId){
		$sql="SELECT MAX(videos_order) FROM videos WHERE videos_module_id='".$moduleId."' ";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(videos_order)'];
		} else return 1;
	}
	
	function addvideolink($moduleId, $type, $name, $header, $description){
		$order = $this->getMaxVideoOrder($moduleId) + 1;
		$data = array(
			'videos_module_id' => $moduleId,
			'videos_type' => $type,
			'videos_name' => $name,
			'videos_header' => $header,
			'videos_description' => $description,
			'videos_order' => $order
		);
		$this->db->set('videos_added', 'sysdate()', FALSE);
		$this->db->insert('videos', $data);
		
		$affRows = $this->db->affected_rows();
		action_log('Dodawanie wideo z serwisu zewnętrznego', 'videos', 'insert', $this->db->insert_id(), $header);
		
		return $affRows;
	}
	
	function addvideofile($moduleId, $type, $header, $description, $file){
		
		$plik=$_FILES['plik'];
	
		if(/*in_array($plik['type'], array('video/mpeg', 'video/x-msvideo', 'application/octet-string'))*/ 1 == 1){		///   kombinować z typami plików
			$n=$plik['name'];
			$r=explode(".",$n);
			$nazwa_pliku=time().'.'.$r[count($r)-1];
			$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
			if(@move_uploaded_file($plik['tmp_name'], 'pages/'.$folder.'/video/'.$nazwa_pliku)){
				
								
				$order = $this->getMaxVideoOrder($moduleId) + 1;
				
				$data = array(
					'videos_module_id' => $moduleId,
					'videos_type' => $type,
					'videos_name' => $nazwa_pliku,
					'videos_header' => $header,
					'videos_description' => $description,
					'videos_order' => $order
				);
				$this->db->set('videos_added', 'sysdate()', FALSE);
				$this->db->insert('videos', $data);
				action_log('Dodawanie wideo z dysku', 'videos', 'insert', $this->db->insert_id(), $header);
				//echo ' '.$nazwa_pliku.' ';
			} 
			return true;
		} else {return false;}
	}
	
	function addvideo($moduleId, $type, $header, $description, $file){
		$order = $this->getMaxVideoOrder($moduleId) + 1;
		$data = array(
			'videos_module_id' => $moduleId,
			'videos_type' => $type,
			'videos_name' => $file,
			'videos_header' => $header,
			'videos_description' => $description,
			'videos_order' => $order
		);
		$this->db->set('videos_added', 'sysdate()', FALSE);
		$this->db->insert('videos', $data);
		
		$affRows = $this->db->affected_rows();
		
		action_log('Dodawanie wideo', 'videos', 'insert', $this->db->insert_id(), $header);
		
		return $affRows;
	}
	
	function edytujvideo($id, $header, $desc){
		
		$data = array(
			'videos_header' => $header,
			'videos_description' => $desc
		);
		$this->db->where("ID_videos", $id);
		$this->db->update('videos', $data);
		action_log('Edycja opisu filmu', 'videos', 'update', $id, $header);
	}

}
?>