<?php

/**
 * Automatic SQL Row Parser
 * @copyright 0arkcode
 * @author 0arkcode@gmail.com
 * @license 0arkcode
 */

    class row_details{
        
        public static $hidden_map = array(
            'config',
            'lin_id',
            'wektor'
        );

        public static $hidden_fields = array(
            'pass',
            'lin_id'
        );
                
        public static $class_map = array(
            '' => ''
        );

        

        public static $name_map = array(
            'id' => 'ID',
            'uid' => 'Użytkownik',
            'crt' => 'Utworzone',
            'nazwa' => array(
                'default' => 'Nazwa',
                'zdjecia' => 'Grupa'
            ),
            'mdf' => 'Modyf.',
            'lin' => 'Lingwistyczna',
            'mode' => 'Typ',
            'firstname' => 'Imię',
            'lastname' => 'Nazwisko',
            'user' => 'Decydent',
            'therapist' => 'Terapeuta',
            'temat_id' => 'Temat',
            'proces_id' => 'Proces',
            'typ_id' => 'Typ kryterium',
            'wartosc' => array(
                'default' => 'Wartość',
                'kryteria' => 'Grupa wartości'
            ),
            'wartosc_lin' => 'Wartość Lin.',
            'proces_details' => '',
            'end' => 'Zakończenie',
            'kryterium_id' => 'Kryterium',
            'waga' => 'Waga/Wartość',
            'predefined' => 'Wart. Predefiniowana',
            'image' => 'Obraz',
            'src' => 'Obraz'
        );
        
        public $all_data = false;
        public $options = false;
        public $table;
        public $id;
        public $html_rows = array();
        
        public function __construct( $table , $id ){
            
            $this->table = $table;
            $this->id = $id;
            
            $this->all_data = db::$i->byid( $table , $id );
            
            // Parse data
            if( $this->all_data ){
                
                $this->parse();
            }

            
        }
        
        public function parse(){
            
            foreach( $this->all_data as $key => $field ){
                
                unset( $values );
                
                if( !is_numeric( $key ) and !in_array( $key , self::$hidden_fields ) ){

                    
                    $values['value'] = $field;
                    
                    switch( $key ){

                        // ID
                        case 'id':

                            $values['html'] = $field;
                            
                        break;
                        
                        
                                
                        // Table
                        case 'table':
                            
                            switch( $this->table ){
                                
                                case  'report':
                                
                                    $values['html'] = report::type_name( $field );
                                
                                break;
                                
                                default:
                                    
                                    $values['html'] =  $field;
                                    
                                break;
                                
                            }
                            
                        break;
                                                
                        // Hash
                        case 'hash':

                            $values['html'] = $field;
                            
                        break;
                        
                        case 'typ_id':
                            list( $values['html'] ) = db::$i->row("SELECT `nazwa` FROM `typ_kryterium` WHERE `id`='".esc( $field )."'");
                        break;
                        
                        case 'temat_id':
                            
                           list( $values['html'] ) = db::$i->row("SELECT `nazwa` FROM `".( tematy::$sql_table )."` WHERE `id`='".esc( $field )."'");
                            
                        break;

                        case 'kryterium_id':
                            
                           list( $values['html'] ) = db::$i->row("SELECT `nazwa` FROM `".( kryteria::$sql_table )."` WHERE `id`='".esc( $field )."'");
                            
                        break;

                        case 'proces_id':
                            
                           list( $values['html'] ) = db::$i->row("SELECT `nazwa` FROM `".( procesy::$sql_table )."` WHERE `id`='".esc( $field )."'");
                            
                        break;
                                                                        
                        case 'therapist':
                        case 'user':
                            
                            list( $imie, $nazwisko , $email ) = db::$i->row("SELECT `firstname`,`lastname`,`email` FROM `".( user::$sql_table )."` WHERE `id`='".esc( $field )."'");
                            
                            $values['html'] = ( $imie ? ( $imie . ( $nazwisko ? ( ' ' . $nazwisko ) : '' ) ) : $email );
                            
                        break;
                        
                        case 'mode':
                            
                            $values['html'] = user::ModeName( $field );
                            
                        break;
                        
                        // Referral
                        case 'ref':

                            $values['html'] = listing::row_parse_html( array(
                                'html' => '#'.$field,
                                'params' => array(
                                    'data-uid' => $field,
                                    'data-table' => 'user',
                                    'data-id' => $field,
                                    'class' => 'row_details'
                                )
                            ));
                            
                        break;

                        case 'refcount':
                        
                            $values['html'] = $field;
                            
                        break;
                        
                        // ROW User
                        case 'uid':
                            
                            $values['html'] = '<a target="_blank" href="'.( admin::$i->url ).'?mode=user&function=details&id='.( $field  ).'" data-uid="'.( $field ).'">#'.( $field ).'</a>';
                            
                            
                        break;


                        
                        // Money
                        case 'cash':
                            
                            switch( $this->table ){
                                
                                case 'user':
                                    $values['html'] = config::$currency_symbol . parse::decimal_noround_third_small( $field );
                                break;
                                
                                default:
                                    $values['html'] = display::cash( $field );
                                break;
                                
                            }
                            
                        break;

                        case 'total':
                        case 'total_sharing':
                            
                            $values['html'] = config::$currency_symbol . parse::decimal_noround_third_small( $field );
                            
                        break;
                        
                        case 'fee_user':
                        case 'fee_ref':
                            
                            $values['html'] = config::$currency_symbol . parse::decimal_noround_third_small( $field );
                            
                        break;
                        
                        case 'price':
                        case 'invest':                        
                        case 'from_balance':
                        case 'cash_lock':
                            
                            $values['html'] = display::cash( $field );
                            
                        break;
                        
                        case 'price_exchange':
                        
                            $values['html'] = config::$currency_symbol . $field;
                        
                        break;
                        
                        case 'processor':
                            
                            $values['html'] = ( payment::$available_processor[$field] ? payment::$available_processor[$field] : $field );
                            
                        break;
                        
                        case 'status':
                        
                            switch( $this->table ){
                                
                                case 'user':
                                    $values['html'] = display::user_status( $field );
                                break;
                                
                                default:
                                    $values['html'] = display::status_name( $field );
                                break;
                            
                            }                  
                        
                        break;
                        
                        case 'toactive':
                            
                            $values['html'] = ( ( new DateTime( $this->all_data['active'] ) > new DateTime(crt())  ) ? l('Yes') : l('No') ) ;
                            
                        break;
                        
                        case 'verify':
                            
                            switch( $this->table ){
                                
                                case 'user':
                                    $values['html'] = display::verify_status( $field );
                                break;   
                                
                                default:
                                    $values['html'] = $field;
                                break;
                                
                            }
                            
                        break;
                        
                        case '2fa_secret':
                            
                            switch( $this->table ){
                                
                                case 'user':
                                    $values['html'] = $field ? 'YES' : 'NO';
                                break;   
                                
                                default:
                                    $values['html'] = $field;
                                break;
                                
                            }
                            
                        break;

                        case 'display':
                        
                            $values['html'] = display::status_01( $field );
                        
                        break;

                              
                        // GEO
                        case 'geo_continent':
                            
                            $values['html'] = geo::get_continent_name( $field );
                            
                        break;
                                    
                        case 'geo_country':
                        
                            $values['html'] = geo::get_country_name( $field );
                            
                        break;
                        
                        case 'geo_state':
                            
                            $values['html'] = geo::get_state_name( $field );
                            
                        break;
                        
                        case 'geo_city':
                            
                            $values['html'] = geo::get_city_name( $field );
                            
                        break;
                                                
                        default:
                        
                            // Other fee fields
                            if( stristr( $key , 'fee' ) ){
                                
                                $values['html'] = display::cash( $field );
                            
                            // Others
                            } elseif( empty( $field ) ){
                                
                                $values['html'] = '-';
                            
                            } else {
                                
                                $values['html'] = $field;
                            }
                            
                        
                        break;
                        
                    }
                    
                    
                    // Name 
                    $values['key'] = $key;
                    //$values['name'] = ( array_key_exists( $key , self::$name_map ) ?  l(self::$name_map[$key]) : ucfirst( $key ) );
                    
                    if( array_key_exists( $key , self::$name_map ) ){
                        
                        if( is_array( self::$name_map[$key] ) ) $values['name'] = ( isset( self::$name_map[$key][$this->table] ) ? self::$name_map[$key][$this->table] : self::$name_map[$key]['default'] );
                        else $values['name'] = self::$name_map[$key];
                        
                    } else $values['name'] = ucfirst( $key );
                    
                    if( stristr( $values['name'] , '_' ) ){
                        $values['name'] = join( ' ' , array_map( 'ucfirst' , explode( "_" , $values['name'] ) ) );
                    }
                    
                    //array_push( $this->html_rows , $values );
                    // 2017-03-05 modification
                    $this->html_rows[$key] = $values;
                    
                }
            }
            
            
            
            // Additional parsing if required
            if( array_key_exists( $this->table , self::$class_map ) ){
                $class_name = self::$class_map[$this->table];
                $this->html_rows = $class_name::details_parse( $this->html_rows );
                
                $this->options = admin::parse_listing_options( $class_name::options( $this->all_data ) );
            }
            
        }
        
        /**
         * Tab name or tab row visibility in html table
         * @param string $field_name - Simplate field name like 'id' or 'name' etc.
         * @return bool
         */      
        public static function is_visibility( $field_name ){
                
                if( in_array( $field_name , self::$hidden_fields ) ) return false;
                
                return true;
                   
        }
        
    }

?>