<?php
class AppliException extends Exception {

  public $level;
  public $log_id;
  
  public $die = false;
  
  protected $getter = array('message', 'level', 'log_id', 'action');
  

/**
 * Getter
 * @param $key
 * @return unknown_type
 */
  public function __get($key) {
    if (array_search($key, $this->getter) !== false) {
      return $this->$key;
    }
    else {
      die ("Hodnotu $key nelze číst!");
    }
  }

/**
 * Třída pro výjimky aplikace 
 * @param $level - hodnota 1 - drobná chyba  až 5 - chyba ohrožující běh aplikace 
 * @param $message
 * @param $code
 */
  function __construct($level, $message, $code = 2, $die = false, $log_id = '') {
    $this->die	     = $die;

    if (empty($code)) {
  		$code = 2;
  	}

    if (empty($log_id)) {
    	$log_id = Logger::error($code, $message);
    }
    $this->log_id    = $log_id;
    $this->code   	 = $code;
    $this->level     = $level;
    parent::__construct($message);

  }

  function checkLevel($max_level, $appli = false) {

    if ($this->level == 5) {
      throw new AppliException($this->level, $this->message, $this->code, false,  $this->log_id);
    }
    elseif ($this->level > $max_level) {
      throw new AppliException($this->level, $this->message, $this->code,  false, $this->log_id);
    }
		$registry = Registry::getInstance();
		
		if ($appli) {
			$registry->application->errDisplay($this, array(998, $this->log_id, $this->message));
		}
		else {
      switch($this->level) {
  			case 1:
  				$registry->to_message = $this->getMessage();
  				break;
  
  			case 2:
  				$registry->to_message = $this->getMessage();
  				$registry->to_message_class = CSS_ERR;
  				break;
  
  			default:
  			case 3:
  				$registry->application->errDisplay($this, array(998, $this->log_id, $this->message));
  				break;
  		}
		}
  }

}
?>