<?php
class Polecane_model extends Model {

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

    function Polecane_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 setrecommended($module_id, $latest, $val){
		$query = $this->db->get_where('polecane', array('polecane_module_id' => $module_id));
		$data = array(
			'polecane_module_id'	=>	$module_id,
			'polecane_latest'		=>	$latest,
			'polecane_val'			=>	$val
		);
		if($query->num_rows() > 0){
			
			$row = $query->row();
			$this->db->set('polecane_updated', 'sysdate()', FALSE);
			$this->db->where('ID_polecane', $row->ID_polecane);
			$this->db->update('polecane', $data);
			$affRows = $this->db->affected_rows();
			action_log('Zmiana polecanych artykułów', 'polecane', 'update', $row->ID_polecane);
		} else {
			
			$this->db->set('polecane_added', 'sysdate()', FALSE);
			$this->db->insert('polecane', $data);
			//$affRows = $this->db->affected_rows();
			$affRows = $this->db->insert_id();
			action_log('Zmiana polecanych artykułów', 'polecane', 'insert', $affRows);
		}
		return $affRows;		
	}
	
	function getrecommended($module_id){
		$query = $this->db->get_where('polecane', array('polecane_module_id' => $module_id));
		if($query->num_rows() > 0){
			return $query->row();
		} else {
			$r->polecane_latest = NULL;
			$r->polecane_val = NULL;
			return $r;
		}
	}
		
}
?>