<?php
class Faq_model extends Model {

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

    function Faq_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 addfaq($moduleId, $name){
		$order = $this->getMaxFaqOrder($moduleId) + 1;
		$data = array(
			'faq_module_id' => $moduleId,
			'faq_name' => $name,
			'faq_order' => $order
		);
		$this->db->set('faq_added', 'sysdate()', FALSE);
		$this->db->insert('faq', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Dodawanie nowej kategorii FAQ', 'faq', 'insert', $moduleId, $name);
		
		return $affRows;
	}
	
	function getfaqs($moduleId){
		$this->db->order_by('faq_order', 'asc');
		return $this->db->get_where('faq', array('faq_module_id' => $moduleId, 'faq_deleted' => 0));
	}

	function getParentOfFaq($id){
		$query = $this->db->get_where('faq', array('ID_faq' => $id));
		$row = $query->row();
		
		$query2 = $this->db->get_where('modules', array('ID_modules' => $row->faq_module_id));
		$row2 = $query2->row();
		return $row2->modules_tree_id;
	}
	
	function getfaq($id/*, $userId*/) {
		return $this->db->query('select * from faq, modules where ID_faq='.$id.' and faq_module_id=ID_modules and faq_deleted=0 and modules_deleted=0');
	}
	
	function getfaqid($id){
		$query = $this->db->get_where('faq_qa', array('ID_faq_qa' => $id));
		$row = $query->row();
		return $row->faq_qa_faq_id;
	}
	
	function getMaxFaqOrder($moduleId){
		$sql = "SELECT MAX(faq_order) FROM faq WHERE faq_module_id=".$moduleId." and faq_deleted=0";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(faq_order)'];
		} else return 1;
	}

	function getMaxFaqQAOrder($faqId){
		$sql = "SELECT MAX(faq_qa_order) FROM faq_qa WHERE faq_qa_faq_id=".$faqId." and faq_qa_deleted=0";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(faq_qa_order)'];
		} else return 1;
	}
	
	function addfaqqa($faqId, $q, $a){
		$order = $this->getMaxFaqQAOrder($faqId) + 1;
		$data = array(
			'faq_qa_faq_id' => $faqId,
			'faq_qa_q' => $q,
			'faq_qa_a' => $a,
			'faq_qa_order' => $order	
		);
		$this->db->insert('faq_qa', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Dodawanie Q/A do FAQ', 'faq_qa', 'insert', $faqId, $q);

		return $affRows;
	}
	
	function updatefaqqa($id, $q, $a){
		$data = array(
			'faq_qa_q' => $q,
			'faq_qa_a' => $a
		);
		$this->db->where('ID_faq_qa', $id);
		$this->db->update('faq_qa', $data);
		$affRows = $this->db->affected_rows();
		
		action_log('Edycja Q/A do FAQ', 'faq_qa', 'insert', $id, $q);
		
		return $affRows;
	}
	
	function getfaqsqa($faqId){
		$this->db->order_by('faq_qa_order', 'asc');
		return $this->db->get_where('faq_qa', array('faq_qa_faq_id' => $faqId, 'faq_qa_deleted' => 0));
	}
	
	function getfaqqa($id){
		$this->db->where('ID_faq_qa', $id);
		$this->db->where('faq_qa_deleted', 0);
		return $this->db->get('faq_qa');
	}
	
	function setfaqsorder($faqs){
		$order = 0;
		foreach ($faqs as $faq){
			$order++;
			$this->db->where('ID_faq', $faq);			
			$this->db->update('faq', array('faq_order' => $order));
			$nazwa = $this->db->get_where('faq', array('ID_faq' => $faq))->row()->faq_name;
			action_log('Zmiana kolejności FAQ', 'faq', 'update', $faq, $nazwa);
		}
	}
	
	function setfaqsQAorder($faqs){
		$order = 0;
		foreach ($faqs as $faq_qa){
			$order++;
			$this->db->where('ID_faq_qa', $faq_qa);			
			$this->db->update('faq_qa', array('faq_qa_order' => $order));
			$nazwa = $this->db->get_where('faq_qa', array('ID_faq_qa' => $faq_qa))->row()->faq_qa_q;
			action_log('Zmiana kolejności Q/A', 'faq_qa', 'update', $faq_qa, $nazwa);
		}
	} 
	
	function editfaqname($id, $name){
		$this->db->where('ID_faq', $id);
		$this->db->set('faq_updated', 'sysdate()', FALSE);
		$this->db->update('faq', array('faq_name' => $name));
		
		$affRows = $this->db->affected_rows();
		
		action_log('Edycja faq - zmiana nazwy', 'faq', 'update', $id, $name);
		
		return $affRows;
	}
	
	
	
	
}
?>