<?php
 
    class error{
        
        static $sys_groups = array(
            'sys' => 'Błąd systemowy',
            'class' => 'Funkcje, klasy i interfejsy',
            'smarty' => 'System szablonów',
            'mysql' => 'Baza danych'
        );
        
        static $instance , $i;
        public $items = array();
        public $error_html;
        
        public function __construct(){
            self::$instance = $this;
            self::$i = $this;
        }
        
        public static function system( $value , $group='sys' , $err_num=false , $err_type='ERR' ){
            
            switch( $err_type ){
                
                case 'ERR':
                    //page::$instance->htmlClear();
                    
                    $params = array(
                        'group' => self::$sys_groups[$group],
                        'num' => $err_num,
                        'type' => $err_type
                    );
                    
                    die( html::$instance->error( $value , $params , 'die' ) );
                    
                break;
                
                case 'WARN':
                
                    $params = array(
                        'group' => self::$sys_groups[$group],
                        'num' => $err_num,
                        'type' => $err_type
                    );
                    
                    page::$instance->val( html::$instance->error( $value , $params , 'sys' ) );
                break;
                
                case 'INFO':
                    logs::file( $value );
                break;
                
                default:
                    logs::file('Nieznany błąd!<br />' . join(' , ',debug_backtrace()) );
                break;
                
            }

               
        }
        
        public function put( $value , $name=false ){
            
            if(!empty(page::$instance->field_valid['fname']) AND !$this->items[page::$instance->field_valid['fname']]){
                $name = page::$instance->field_valid['fname'];
            }
            
            if( $name ) $this->items[$name] = $value;
            else array_push( $this->items , $value );
        }
        
        public function valid(){
            
            if( count( $this->items ) > 0 ) return false;
            else return true;
            
        }
        
        public function clear(){
            $this->items = array();
        }
        
        public function toContent( $add_params = false , $to_var = false ){
           if( $to_var ) $this->error_html =  html::$instance->error( $this->items , $add_params );
           else  page::$instance->val( html::$instance->error( $this->items , $add_params ) );
        }
        
        public function getAsLines( $glue = "<br />" ){
            return join( $glue , $this->items );
        }
        
        public function lightbox(){
            global $tpl;
            
            foreach( $this->items as $div ){
                $divs[] = '<div class="label">' . $div . '</div>';
            }
            
            $tpl->assign('divs',join('',$divs));
            page::$i->setHead( $tpl->fetch('tpl/script/error_lightbox.tpl') );
        }
        
    }


?>