Esempio n. 1
0
 private function _handle_file_upload($field = null, $params = array())
 {
     require_once ENDO_PACKAGES_ROOT . 'VerotUpload' . DS . 'class.upload.php';
     // TODO add 'delete-checkbox' functionality
     if (!empty($_FILES)) {
         // defaults
         $path = array_get($params, 'path', 'assets');
         $allowed = array_get($params, 'allowed', 'image');
         $options = array_get($params, 'options', array());
         $file = new Upload($_FILES[$field]);
         if ($file->uploaded) {
             // path
             $path = 'uploads' . DS . AppInflector::pluralize(get_class($this)) . DS . $path . DS . $this->id . DS;
             $folder = WEB_ROOT . $path;
             // settings
             if ($allowed == 'image') {
                 $file->allowed = array('image/gif', 'image/jpg', 'image/jpeg', 'image/png', 'image/bmp');
                 // defaults
                 if (!array_get($options, 'width') && !array_get($options, 'height')) {
                     $options = array_merge(array('width' => 100), $options);
                 }
                 // resize
                 $file->image_max_pixels = 1000000;
                 $file->image_resize = true;
                 $file->image_convert = 'jpg';
                 // determine method
                 if (array_get($options, 'width') && !array_get($options, 'height')) {
                     // width only
                     $file->image_x = $options['width'];
                     $file->image_ratio_y = true;
                 } elseif (!array_get($options, 'width') && array_get($options, 'height')) {
                     // height only
                     $file->image_y = $options['height'];
                     $file->image_ratio_x = true;
                 } else {
                     // width and height
                     $file->image_x = $options['width'];
                     $file->image_y = $options['height'];
                     // crop?
                     if (array_get($options, 'crop')) {
                         $file->image_ratio_crop = true;
                     } else {
                         $file->image_ratio = true;
                     }
                 }
             } elseif (is_array($allowed)) {
                 $file->allowed = $allowed;
             }
             // process!
             $file->Process($folder);
             // success?
             if ($file->processed) {
                 // remove old file?
                 @unlink($folder . str_replace(DS . $path, '', $this->{$field . '_old'}));
                 $file->Clean();
                 $this->{$field} = DS . $path . $file->file_dst_name;
                 return parent::save();
             } else {
                 // TODO add endo form error message
                 echo 'ERROR: ' . $file->error;
                 return false;
             }
         }
     }
     return true;
 }