<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation {

    function 
__construct()
    {
        
parent::CI_Form_validation();
    }
    
    function 
_execute($row$rules$postdata NULL$cycles 0)
    {
    
//changed based on
    //http://codeigniter.com/forums/viewthread/123816/P10/#619868
        
if(isset($_FILES[$row['field']]['size']))
        {
// it is a file so process as a file

            
$postdata $_FILES[$row['field']];
            
            
$_in_array FALSE;        
        
            
// If the field is blank, but NOT required, no further tests are necessary
            
$callback FALSE;
            if ( ! 
in_array('file_required'$rules) AND $postdata['size']==0)
            {
                
// Before we bail out, does the rule contain a callback?
                
if (preg_match("/(callback_\w+)/"implode(' '$rules), $match))
                {
                    
$callback TRUE;
                    
$rules = (array('1' => $match[1]));
                }
                else
                {
                    return;
                }
            }        
            
            foreach(
$rules as $rule)
            {
                
/// COPIED FROM the original class
                
                // Is the rule a callback?            
                
$callback FALSE;
                if (
substr($rule09) == 'callback_')
                {
                    
$rule substr($rule9);
                    
$callback TRUE;
                }
                
                
// Strip the parameter (if exists) from the rule
                // Rules can contain a parameter: max_length[5]
                
$param FALSE;
                if (
preg_match("/(.*?)\[(.*?)\]/"$rule$match))
                {
                    
$rule    $match[1];
                    
$param    $match[2];
                }            
                
                
// Call the function that corresponds to the rule
                
if ($callback === TRUE)
                {
                    if ( ! 
method_exists($this->CI$rule))
                    {         
                        continue;
                    }
                    
                    
// Run the function and grab the result
                    
$result $this->CI->$rule($postdata$param);

                    
// Re-assign the result to the master data array
                    
if ($_in_array == TRUE)
                    {
                        
$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata $result;
                    }
                    else
                    {
                        
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata $result;
                    }
                
                    
// If the field isn't required and we just processed a callback we'll move on...
                    
if ( ! in_array('file_required'$rulesTRUE) AND $result !== FALSE)
                    {
                        return;
                    }
                }
                else
                {                
                    if ( ! 
method_exists($this$rule))
                    {
                        
// If our own wrapper function doesn't exist we see if a native PHP function does. 
                        // Users can use any native PHP function call that has one param.
                        
if (function_exists($rule))
                        {
                            
$result $rule($postdata);
                                                
                            if (
$_in_array == TRUE)
                            {
                                
$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata $result;
                            }
                            else
                            {
                                
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata $result;
                            }
                        }
                                            
                        continue;
                    }

                    
$result $this->$rule($postdata$param);

                    if (
$_in_array == TRUE)
                    {
                        
$this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata $result;
                    }
                    else
                    {
                        
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata $result;
                    }
                }
                                
                
// Did the rule test negatively?  If so, grab the error.
                
if ($result === FALSE)
                {            
                    if ( ! isset(
$this->_error_messages[$rule]))
                    {
                        if (
FALSE === ($line $this->CI->lang->line($rule)))
                        {
                            
$line 'Unable to access an error message corresponding to your field name.';
                        }                        
                    }
                    else
                    {
                        
$line $this->_error_messages[$rule];
                    }
                    
                    
// Is the parameter we are inserting into the error message the name
                    // of another field?  If so we need to grab its "field label"
                    
if (isset($this->_field_data[$param]) AND isset($this->_field_data[$param]['label']))
                    {
                        
$param $this->_field_data[$param]['label'];
                    }
                    
                    
// Build the error message
                    
$message sprintf($line$this->_translate_fieldname($row['label']), $param);

                    
// Save the error message
                    
$this->_field_data[$row['field']]['error'] = $message;
                    
                    if ( ! isset(
$this->_error_array[$row['field']]))
                    {
                        
$this->_error_array[$row['field']] = $message;
                    }
                    
                    return;
                }                
            }        
        }
    else
    {
        
parent::_execute($row$rules$postdata,$cycles);
    }
    }
    
    
    function 
file_required($file)
    {
        if(
$file['size']===0)
        {
            
$this->set_message('file_required','Uploading a file for %s is required.');
            return 
FALSE;
        }
        
        return 
true;
    }
    
    function 
file_size_max($file,$max_size)
    {
        if(
$file['size']>$max_size 1024)
        {
            
$this->set_message('file_size_max',"%s is too big. (max allowed is $max_size KB)");
            return 
FALSE;
        }
        return 
true;
    }
    
    function 
file_allowed_type($file,$type)
    {
        
/*
        three types allowed:
        1. mime type image/gif
        2. general mime type image application 
        3. file ext pdf,xls
        */
        
print_r($file);
        
        if(
strpos($file['type'],$type)===FALSE)
        {
            
$this->set_message('file_allowed_type',"%s cannot be {$file['type']}.(should be $type)");
            return 
false;        
        }
        else
        {
            return 
TRUE;
        }
    }
}



/* End of file form_validation.php */
/* Location: ./system/application/libraries/form_validation.php */