Changeset 1658

Show
Ignore:
Timestamp:
01/02/08 00:34:15 (6 months ago)
Author:
Shadowhand
Message:

Fixing up Form_Upload a little.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/modules/forge/controllers/forge_demo.php

    r1657 r1658  
    4646                $form = new Forge; 
    4747                $form->input('hello')->label(TRUE); 
    48                 $form->upload('file')->label(TRUE)->rules('required|allow[jpg,png,gif]|size[2M]'); 
     48                $form->upload('file')->label(TRUE)->rules('required|size[100KB]|allow[jpg,png,gif]'); 
    4949                $form->submit('Upload'); 
    5050 
  • trunk/modules/forge/i18n/en_US/forge.php

    r1522 r1658  
    44( 
    55        'invalid_input' => 'Invalid input type requested: %s', 
    6         'address_unit' => 'Unit #', 
    7         'address_city' => 'City', 
    8         'address_state' => 'State', 
    9         'address_zip' => 'Zip' 
    106); 
  • trunk/modules/forge/libraries/Form_Upload.php

    r1657 r1658  
    1111        protected $protect = array('type', 'label', 'value'); 
    1212 
     13        // Upload data 
    1314        protected $upload; 
     15 
     16        // Upload directory 
    1417        protected $directory; 
    1518 
     
    4952                // Make sure the upload director is valid and writable 
    5053                if ($dir === '/' OR ! is_dir($dir) OR ! is_writable($dir)) 
    51                         throw new Kohana_Exception('forge.upload.unwritable', $dir); 
     54                        throw new Kohana_Exception('upload.not_writable', $dir); 
    5255 
    5356                $this->directory = $dir; 
     
    5962                empty($this->directory) and $this->directory(); 
    6063 
    61                 if ($status = parent::validate()) 
     64                // By default, there is no uploaded file 
     65                $filename = ''; 
     66 
     67                if ($status = parent::validate() AND $this->upload['error'] === UPLOAD_ERR_OK) 
    6268                { 
    63                         // No filename means an invalid upload 
    64                         $filename = ''
     69                        // Set the filename to the original name 
     70                        $filename = $this->upload['name']
    6571 
    66                         if ($this->upload['error'] === UPLOAD_ERR_OK
     72                        if (Config::item('upload.remove_spaces')
    6773                        { 
    68                                 // Set the filename to the original name 
    69                                 $filename = $this->upload['name']; 
    70  
    71                                 if (Config::item('upload.remove_spaces')) 
    72                                 { 
    73                                         // Remove spaces, due to global upload configuration 
    74                                         $filename = preg_replace('/\s+/', '_', $this->data['value']); 
    75                                 } 
    76  
    77                                 // Move the uploaded file to the upload directory 
    78                                 move_uploaded_file($this->upload['tmp_name'], $filename = $this->directory.$filename); 
     74                                // Remove spaces, due to global upload configuration 
     75                                $filename = preg_replace('/\s+/', '_', $this->data['value']); 
    7976                        } 
    8077 
     78                        // Move the uploaded file to the upload directory 
     79                        move_uploaded_file($this->upload['tmp_name'], $filename = $this->directory.$filename); 
     80                } 
     81 
     82                if ( ! empty($_POST[$this->data['name']])) 
     83                { 
    8184                        // Reset the POST value to the new filename 
    8285                        $this->data['value'] = $_POST[$this->data['name']] = $filename; 
     
    9699        public function rule_allow() 
    97100        { 
    98                 if (empty($this->upload['tmp_name'])
     101                if (empty($this->upload['tmp_name']) OR count($types = func_get_args()) == 0
    99102                        return; 
    100103 
     
    122125                $allow = FALSE; 
    123126 
    124                 foreach (func_get_args() as $type) 
     127                foreach ($types as $type) 
    125128                { 
    126129                        if (in_array($mime, Config::item('mimes.'.$type))) 
     
    134137                if ($allow === FALSE) 
    135138                { 
    136                         $this->errors['allow'] = TRUE; 
     139                        $this->errors['invalid_type'] = TRUE; 
    137140                } 
    138141        } 
     
    142145                $bytes = (int) $size; 
    143146 
    144                 switch (substr($size, -1)) 
     147                switch (substr($size, -2)) 
    145148                { 
    146                         case 'G': $bytes *= 1024; 
    147                         case 'M': $bytes *= 1024; 
    148                         default:  $bytes *= 1024; 
     149                        case 'GB': $bytes *= 1024; 
     150                        case 'MB': $bytes *= 1024; 
     151                        case 'KB': $bytes *= 1024; 
     152                        default: break; 
    149153                } 
    150154 
    151155                if (empty($this->upload['size']) OR $this->upload['size'] > $bytes) 
    152156                { 
    153                         $this->errors['size'] = $size
     157                        $this->errors['max_size'] = array($size)
    154158                } 
    155159        } 
    156160 
    157         public function html() 
     161        protected function html_element() 
    158162        { 
    159163                return form::upload($this->data);