<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/'); 
class Flashdata {
	protected $native = FALSE;
	protected $_inst = NULL;
	protected $_c = NULL;
	protected $_a = NULL;
	protected $data = array();
	
	function __construct() {	
		$php = phpversion();
		
		$version = explode(".",$php);
		if ($version[0] >= 5 && $version[1] >= 5 && $version[2] >= 0) {
			if (session_status() == PHP_SESSION_NONE) session_start();
		} else {
			if(session_id() == '') session_start();
		}
		unset($php, $version);
		
		if(!$this->native) {
			$this->_inst = & get_instance();
			$this->_c = strtolower($this->_inst->router->fetch_class());
			$this->_a = strtolower($this->_inst->router->fetch_method());
		}
		
		return $this;
	}
	
	public function setNative() {
		$this->native = TRUE;
		return $this;
	}
	
	public function unsetNative() {
		$this->native = FALSE;
		return $this;
	}

	function set($data, $key = NULL) {
		if(!$data) return;
		if($key) {
			$_SESSION['FLASHDATA'][$key] = $data;
			$this->data[$key] = $data;
			
			if(!$this->native) {
				$session = $this->_inst->session->userdata('flashdata');
				if(!$session) $session = array();
				$session[$key] = $data;
				
				$this->_inst->session->set_userdata('flashdata',$session);
			}
		} else {
			$_SESSION['FLASHDATA'][] = $data;
			$this->data[] = $data;
			
			if(!$this->native) {
				$session = $this->_inst->session->userdata('flashdata');
				if(!$session) $session = array();
				$session[] = $data;
				
				$this->_inst->session->set_userdata('flashdata',$session);
			}
		}
		
		return $this;
	}

	function clear($key = NULL) {
		if ($key === NULL) {
			unset($_SESSION['FLASHDATA']);
			$this->data = array();
			
			if(!$this->native) {
				if($this->_inst->session->userdata('flashdata')) $this->_inst->session->unset_userdata('flashdata');
			}
		} else {
			if(isset($_SESSION['FLASHDATA'][$key])) unset($_SESSION['FLASHDATA'][$key]);
			if(isset($this->data[$key])) unset($this->data[$key]);
			
			if(!$this->native) {
				$session = $this->_inst->session->userdata('flashdata');
				if(!$session) $session = array();
				$session[$key] = $data;
				
				if(isset($session[$key])) unset($session[$key]);
				
				if(!empty($session)) $this->_inst->session->set_userdata('flashdata',$session);
			}
		}
		
		return $this;
	}
	
	function get($key = NULL, $destroy = TRUE) {		
		if ($key === NULL) {
			$return = (isset($_SESSION['FLASHDATA'])) ? $_SESSION['FLASHDATA'] : NULL;
			if(!$return) $return = $this->data;			
			if(!$this->native) $return = $this->_inst->session->userdata('flashdata');
			
			if($destroy) $this->clear();
		} else {
			$return = (isset($_SESSION['FLASHDATA'][$key])) ? $_SESSION['FLASHDATA'][$key] : NULL;
			
			if(!$return) $return = (isset($this->data[$key])) ? $this->data[$key] : NULL;
			
			if(!$this->native) {
				$session = $this->_inst->session->userdata('flashdata');
				if(!$session) $session = array();
				
				$return = (isset($session[$key])) ? $session[$key] : NULL;
			}
			
			if($destroy) $this->clear($key);
		}
		return $return;
	}
	
}















?>