<?php

    class wagi
    {

        public static $sql_table = 'wagi';        

        public static $own_listing = false;
        public static $own_listing_parse = false;
        public static $own_options = true;
        
        public function __construct()
        {
        }
        
        public static function edit( $request , $id )
        {
            
            $Obj = self::get( $id );
            
            if( !$Obj->id )
            {
                statement::$i->set( 'Element o takim identyfikatorze nie istnieje w bazie danych' , 'danger' );
                return;
            }
            
            // Nazwa
            if( !empty( $request['waga'] ) AND $request['waga'] != $Obj->waga )
            {
                $sql['waga'] = esc( $request['waga'] );
            }
            if( count( $sql ) )
            {
                if( db::$i->update( self::$sql_table , $sql , $id ) )
                {
                    statement::$i->set( 'update' );
                } 
                else 
                {
                    statement::$i->set( 'mysql' );
                }
            } 
            else 
            {
                statement::$i->set( 'Nie dokonano żadnych zmian' );
            }
            
        }
        
        public static function getList( $params )
        {
            
            $where = array();
            
            if( isset( $params['proces_id'] ) )
            {
                $where[] = "`proces_id`='".esc( $params['proces_id'] )."'";
            }
            
            if( isset( $params['kryterium_id'] ) )
            {
                $where[] = "`kryterium_id`='".esc( $params['kryterium_id'] )."'";
            }
            
            if( !count( $where ) ) return;
            
            $query = db::$i->q("
            SELECT * 
            FROM `".( self::$sql_table )."` 
            WHERE ".( join(' AND ',$where) )." 
            ORDER BY `id` ASC
            " , true );
            
            if( db::$i->rows )
            {
                
                $Wagi = new stdClass;
                $Wagi->Count = db::$i->rows;
                
                while( $item = db::$i->f( $query ) )
                {
                    $Wagi->Items[] = self::parse( $item );
                }
                
                    return $Wagi;
            }
            
                return;
        }
        
        public static function parse( $item )
        {
            return core::object( $item );
        }
        
        /**
         * Pobierz wagę
         * @param int $id
         * @return object $Waga
         */
        public static function get( $id )
        {
            if( !$id ) return;
            
            $data = db::$i->row("
            SELECT * 
            FROM `".( self::$sql_table )."` 
            WHERE `id`='".( $id )."'
            ");
            
                return core::object( $data );
        }
        
        public static function options( $item )
        {
                
                $options[l('Szczegóły')] = array(
                        'mode' => 'wagi',
                        'id' => $item['id'],
                        'class' => 'row_details'
                );
                
                
                $options[l('Edytuj')] = array(
                        'mode' => 'wagi',
                        'id' => $item['id']
                );

                                                
                $options[l('Usuń')] = array(
                        'mode' => 'wagi',
                        'id' => $item['id']
                );
                
                return $options; 
        }   
        
        public static function details_parse()
        {
            
        }
        
    }
    
?>