<?php
class News_model extends Model {

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

    function News_model()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->helper("log");
    }
	
	function getMaxNewsOrder($moduleId){
		$sql = "SELECT MAX(news_order) FROM news WHERE news_module_id=".$moduleId." and news_deleted=0";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(news_order)'];
		} else return 1;
	}
    
	function getnews($moduleId){
		//$this->db->order_by('news_order', 'desc');
		$this->db->order_by('ID_news', 'desc');
		return $this->db->get_where('news', array('news_module_id' => $moduleId, 'news_deleted' => 0));
	}
	
	function getsinglenews($id){
		$query = $this->db->get_where('news', array('ID_news' => $id));
		return $query->row();
	}

	function addnews($moduleId, $title, $title_url, $expiration_date, $text_short, $text){
		$order = $this->getMaxNewsOrder($moduleId) + 1;
		$exp_date = explode('/', $expiration_date);
		$data = array(
			'news_module_id' => $moduleId,
			'news_title' => $title,
			'news_title_url' => $title_url,
			'news_text_short' => $text_short,
			'news_text' => $text,
			'news_expiration_date' => $exp_date[2].'-'.$exp_date[1].'-'.$exp_date[0], //???
			'news_order' => $order,
			'news_deleted' => 0
		);
		$this->db->set('news_added', 'sysdate()', FALSE);
		$this->db->insert('news', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Dodawanie nowej aktualności', 'news', 'insert', $moduleId, $title);
		
		return $affRows;
	}	
	
	function getmoduleid($id){
		
	}
	
	function editnews($id, $title, $title_url, $expiration_date, $text_short, $text){
		$exp_date = explode('/', $expiration_date);
		$data = array(
			'news_title' => $title,
			'news_title_url' => $title_url,
			'news_text_short' => $text_short,
			'news_text' => $text,
			'news_expiration_date' => $exp_date[2].'-'.$exp_date[1].'-'.$exp_date[0]
		);
		$this->db->set('news_updated', 'sysdate()', FALSE);
		$this->db->where('ID_news', $id);
		$this->db->update('news', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Edycja aktualności', 'news', 'update', $id, $title);
		
		return $affRows;
	}
	
	function deletenews(){
		
	}
	
	
	function checkname_url($title_url){
		$sql="SELECT * FROM modules, news WHERE news_module_id=ID_modules and modules_user_id=".$this->session->userdata('uzytkownik_id')." and news_title_url='".$title_url."'";     
		$query = $this->db->query($sql);
		
		if($query->num_rows > 0){
			$i = 0;
			while(1){
				$i++;
				$sql="SELECT * FROM modules, news WHERE news_module_id=ID_modules and modules_user_id=".$this->session->userdata('uzytkownik_id')." and news_title_url='".$title_url."-".$i."'";     
				$query = $this->db->query($sql);
				if($query->num_rows > 0) {
					continue;
				} else return $title_url.'-'.$i;
			}
			
		} else return $title_url;
	}
 	
	function checkname_url_update($id, $title_url){
		$sql="SELECT * FROM modules, news WHERE ID_news!=".$id." and news_module_id=ID_modules and modules_user_id=".$this->session->userdata('uzytkownik_id')." and news_title_url='".$title_url."'";     
		$query = $this->db->query($sql);
		
		if($query->num_rows > 0){
			
			$i = 0;
			while(1){
				$i++;
				$sql="SELECT * FROM modules, news WHERE ID_news!=".$id." and news_module_id=ID_modules and modules_user_id=".$this->session->userdata('uzytkownik_id')." and news_title_url='".$title_url."-".$i."'";     
				$query = $this->db->query($sql);
				if($query->num_rows > 0) {
					continue;
				} else return $title_url.'-'.$i;
			}
			
			
		} else return $title_url;
	}
}

?>