<?php
class News_glowny_model extends Model {

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

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

	function getMaxNewsOrder($userId, $lang_nr){
		$sql = "SELECT MAX(news_glowny_order) FROM news_glowny WHERE news_glowny_user_id=".$userId." and news_glowny_lang='".''.$lang_nr."' and news_glowny_deleted=0";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(news_glowny_order)'];
		} else return 1;
	}
	
	function getnewsglowny($userId, $lang_nr){
		//$this->db->order_by('news_order', 'desc');
		$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, 'news_glowny_lang' => ''.$lang_nr));
	}
	
	function getsinglenewsglowny($id){
		$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');
		$query = $this->db->get_where('news_glowny', array('ID_news_glowny' => $id));
		return $query->row();
	}
    
	function checkname_url($title_url){
		$sql="SELECT * FROM news_glowny WHERE news_glowny_user_id=".$this->session->userdata('uzytkownik_id')." and news_glowny_title_url='".$title_url."'";     
		$query = $this->db->query($sql);
		
		if($query->num_rows > 0){
			$i = 0;
			while(1){
				$i++;
				$sql="SELECT * FROM news_glowny WHERE news_glowny_user_id=".$this->session->userdata('uzytkownik_id')." and news_glowny_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 news_glowny WHERE ID_news_glowny!=".$id." and news_glowny_user_id=".$this->session->userdata('uzytkownik_id')." and news_glowny_title_url='".$title_url."'";     
		$query = $this->db->query($sql);
		
		if($query->num_rows > 0){
			
			$i = 0;
			while(1){
				$i++;
				$sql="SELECT * FROM news_glowny WHERE ID_news_glowny!=".$id." and news_glowny_user_id=".$this->session->userdata('uzytkownik_id')." and news_glowny_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 addnewsglowny($userId, $title, $title_url, $expiration_date, $text_short, $text, $lang_nr){
		$order = $this->getMaxNewsOrder($userId, ''.$lang_nr) + 1;
		$exp_date = explode('/', $expiration_date);
		$data = array(
			'news_glowny_user_id' => $userId,
			'news_glowny_title' => $title,
			'news_glowny_title_url' => $title_url,
			'news_glowny_text_short' => $text_short,
			'news_glowny_text' => $text,
			'news_glowny_expiration_date' => $exp_date[2].'-'.$exp_date[1].'-'.$exp_date[0], //???
			'news_glowny_order' => $order,
			'news_glowny_deleted' => 0,
			'news_glowny_lang' => ''.$lang_nr.''
		);
		$this->db->set('news_glowny_added', 'sysdate()', FALSE);
		$this->db->insert('news_glowny', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Dodawanie nowej aktualności głównej', 'news_glowny', 'insert', $userId, $title);
		
		return $affRows;
	}
	
	function editnewsglowny($id, $title, $title_url, $expiration_date, $text_short, $text){
		$exp_date = explode('/', $expiration_date);
		$data = array(
			'news_glowny_title' => $title,
			'news_glowny_title_url' => $title_url,
			'news_glowny_text_short' => $text_short,
			'news_glowny_text' => $text,
			'news_glowny_expiration_date' => $exp_date[2].'-'.$exp_date[1].'-'.$exp_date[0]
		);
		$this->db->set('news_glowny_updated', 'sysdate()', FALSE);
		$this->db->where('ID_news_glowny', $id);
		$this->db->update('news_glowny', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Edycja aktualności', 'news_glowny', 'update', $id, $title);
		
		return $affRows;
	}
	
	function removenewsglowny($id){
		$this->db->where('ID_news_glowny', $id);
		$this->db->update('news_glowny', array('news_glowny_deleted' => 1));
		$affRows = $this->db->affected_rows();
		
		$nazwa = $this->db->get_where('news_glowny', array('ID_news_glowny' => $id))->row()->news_glowny_title;
		action_log('Usuwanie aktualności', 'news_glowny', 'delete', $id, $nazwa);
		
		return $affRows;
	}
		
}
?>