<?php

 
    class admin{
        
        static $instance , $i;
        public $url , $dir;
        public $menu = array(
        );
        
        public function __construct(){
            
            self::$instance = $this;
            self::$i = $this;
            
            $this -> dir = _cfg::$i->admin->dir . '/';
            $this -> url = @end( explode( "/" , $_SERVER['SCRIPT_NAME'] ) );
            
            if( user::$i->mode == 'mod' ){
                $this->menu = array();
            }
            
        }
        
        public function saveSettings( $array , $empty_field = true ){
            
            $el = 0;
            $up = 0;
            $err_keys = array();
            
            foreach( $array as $key => $val ){
                
                if(stristr($key,'_')){
                    $el++;
                    list( $name , $subname , $vmethod ) = explode("_",$key);
                    
                    if( $vmethod ){
                        
                        if( eval( 'valid::'.$vmethod.'( '.$val.' )') ){
                          core::$i->saveConfig( $name , $subname , $val );
                          $up++;  
                        } else array_push( $err_keys , $key );
                        
                    } else {
                        if( $empty_field ){
                            core::$i->saveConfig( $name , $subname , $val );
                            $up++;
                        } else {
                            if(!empty($val)){
                                core::$i->saveConfig( $name , $subname , $val );
                                $up++;
                            } else {
                                array_push( $err_keys , $key );
                            }
                        }
                    }
                }
                
            }
            
            return array(
                'el' => $el,
                'up' => $up,
                'err_count' => count($err_keys),
                'err_keys' => $err_keys,
                'err' => ($el==$up ? false : true)
            );
            
        }
        
        public function STDsaveSettings( $arr , $empty_field ){
            $params = admin::$i->saveSettings( $arr , $empty_field );
            if( !$params['err'] ){
                page::$i->setOk(I_SAVED);
            } else {
                page::$i->val( html::$i->error('Nie zapisano <b>' . $params['err_count'] . '</b> z <b>' . $params['el'] . '</b> ustawień. Sprawdź czy pola nie są pusta oraz czy mają odpowiedni format danych' , $params['err_keys'] , 'lightbox') ); 
            }
        }
        
        public function useThis(){
            require_once $this->dir . core::$i->mode . '/' . core::$i->function . '.php';
        }

        /**
         * Włącz (wyświetl) moduł
         * @return void
         */
        public function on(){
            global $tpl;
            if( core::AJAX() ){
                print( js::$instance->getJSON() );
            } else {
                page::$instance->val( $tpl->fetch( $this->dir . core::$i->mode . '/' . core::$i->function . '.tpl' ) );
            }
            
            
        }
        
        public function createMenu( $menu ){
            $html = '';
            foreach( $menu AS $key => $val ){
                $html .= '<li>';
                
                if( is_array( $val ) ){
                    $html .= '<a href="'.(is_array($val) ? "javascript:;" : ( (stristr( $val , "LINK" ) ? (str_replace("LINK:","",$val)) : (admin::$i->url . '?' . $val)) )).'" class="nesting">'.$key.'</a>';
                    $html .= '<ul>';
                    $html .= $this->createMenu( $val );
                    $html .= '</ul>';
                    
                } else $html .= '<a href="'.( stristr( $val , "LINK:" ) ? (str_replace("LINK:","",$val)) : (admin::$i->url . '?' . $val) ).'">'.$key.'</a>';
                $html .= '</li>';
            }
            return $html;
        }
        
        public static function parse_listing_options( $options_array ){
            
            global $tpl;
            
            $links = array();
            
            foreach( $options_array as $key => $data ){
                
                if( isset( $data['url'] ) AND !stristr( $data['url'] , admin::$i->url ) ){
                    $data['url'] = admin::$i->url . $data['url'];
                }
                
                switch( $key ){
                    
                    // Element Delete
                    case l('Usuń'):

                        if( !isset($data['ico_class']) ) $data['ico_class'] = 'fa fa-remove';

                        if( isset($data['url']) ){

                            $a = array(
                                'html' => $key,
                                'ico_class' => $data['ico_class'],
                                'params' => array(
                                    'onclick' => 'confirmation(' . "'"  . $data['url'] . "','" . l( ( $data['delete_text'] ? $data['delete_text'] : ('Czy na pewno chcesz usunąć?') ) ) . "')" . ''
                                )
                            );
                            
                        } else {

                            $delete_url = admin::$i->url . '?mode=' . $data['mode'] . '&function='.( $data['function'] ? $data['function'] : 'delete' ).'&id=' . $data['id'];
                            
                            $a = array(
                                'html' => $key,
                                'ico_class' => $data['ico_class'],
                                'params' => array(
                                    'onclick' => 'confirmation(' . "'"  . $delete_url . "','" . l( ( $data['delete_text'] ? $data['delete_text'] : ('Czy na pewno chcesz usunąć?') ) ) . "')" . ''
                                )
                            );
                        
                        }
                        
                    break;

                    // Element Edit
                    case l('Edytuj'):

                        if( !isset($data['ico_class']) ) $data['ico_class'] = 'fa fa-edit';

                        if( isset($data['url']) ){

                            $a = array(
                                'url' => $data['url'],
                                'html' => $key,
                                'ico_class' => $data['ico_class']
                            );
                            
                        } else {

                            $edit_url = admin::$i->url . '?mode=' . $data['mode'] . '&function='.( isset($data['function']) ? $data['function'] : 'edit' ).'&id=' . $data['id'];
                            
                            $a = array(
                                'url' => $edit_url,
                                'html' => $key,
                                'ico_class' => $data['ico_class']
                            );
                        
                        }
                        
                    break;
                    
                    case l('Aktywuj'):

                        if( isset($data['url']) ){

                            $a = array(
                                'url' => $data['url'],
                                'html' => $key,
                                'ico_class' => 'fa fa-play'
                            );
                            
                        } else {

                            $option_url = admin::$i->url . '?mode=' . $data['mode'] . '&function='.( isset($data['function']) ? $data['function'] : 'activate' ).'&id=' . $data['id'];
                            
                            $a = array(
                                'url' => $option_url,
                                'html' => $key,
                                'ico_class' => 'fa fa-play'
                            );
                        
                        }
                        
                    break;
                    
                    case l('Deaktywuj'):

                        if( !isset($data['ico_class']) ) $data['ico_class'] = 'fa fa-stop';

                        if( isset($data['url']) ){

                            $a = array(
                                'url' => $data['url'],
                                'html' => $key,
                                'ico_class' => $data['ico_class']
                            );
                            
                        } else {

                            $option_url = admin::$i->url . '?mode=' . $data['mode'] . '&function='.( isset($data['function']) ? $data['function'] : 'deactivate' ).'&id=' . $data['id'];
                            
                            $a = array(
                                'url' => $option_url,
                                'html' => $key,
                                'ico_class' => $data['ico_class']
                            );
                        
                        }
                        
                    break;
                    
                    case l('Szczegóły'):

                        // View details in modal box
                        if( isset($data['class']) AND $data['class'] == 'row_details' ){
                            
                            $a = array(
                                'html' => $key,
                                'ico_class' => ( $data['ico_class'] ? $data['ico_class'] : 'fa fa-search' ),
                                'params' => array(
                                    'data-table' => $data['mode'],
                                    'data-id' => $data['id']
                                )
                            );
                            
                        // Normal URL
                        } else {

                            $a = array(
                                'html' => $key,
                                'ico_class' => ( $data['ico_class'] ? $data['ico_class'] : 'fa fa-search' ),
                                'params' => array(
                                    'data-table' => $data['mode'],
                                    'data-id' => $data['id']
                                )
                            );
                            
                        }


                        
                    break;

                                       
                    default:
                        
                        $a = array(
                            'html' => $key,
                            'url' => $data['url'],
                            'ico_class' => $data['ico_class'],
                            'params' => ( count($data['params']) ? $data['params'] : false )
                        );
                        

                        
                    break;
                    
                }
                
                if( !empty( $data['class'] ) ) $a['class'] = $data['class'];
                
                
                // Params overwrite
                if( isset( $data['params'] ) ){
                    $a['params'] = $data['params'];
                } 
                
                array_push( $links , $a );
                
            }
            
            
            
            $tpl->assign(array(
                'options' => $links
            ));
            
            return $tpl->fetch('admin/options.tpl');
            
        }
        
    }

?>