/** * process any uploads * * @return void * @author Andy Bennett */ public static function upload_listener() { if (!Event::$data instanceof ORM) { throw new Exception('Model not passed to ' . __CLASS__ . '/' . __FUNCTION__); } $model = Event::$data; // do the uploading foreach ($_FILES as $file => $file_data) { if ($file_data['error'] == 0) { $udata = uploads::get_upload_data($file, $file_data); $n = str_replace('form_', '', $file); $id = (is_object($model->{$n}) and $model->{$n}->id > 0) ? $model->{$n}->id : 0; if (isset($udata['image_width']) and empty($udata['image_width'])) { unset($udata['image_width']); } if (isset($udata['image_height']) and empty($udata['image_height'])) { unset($udata['image_height']); } // if there is preview data set if (is_array($udata['preview'])) { // add the preview data into the upload table $preview = ORM::factory('upload'); foreach ($udata['preview'] as $k => $v) { if ($k == 'preview') { continue; } $preview->{$k} = $v; } $preview->parent_model = $model->object_name; $preview->parent_id = $model->id; $preview->save(); // and assign the id to the row $udata['preview_id'] = $preview->id; } unset($udata['preview']); $o = ORM::factory('upload', $id); if (!$o->loaded) { $o = ORM::factory('upload'); } foreach ($udata as $k => $v) { $o->{$k} = $v; } $o->parent_model = $model->object_name; $o->parent_id = $model->id; $o->save(); // insert the upload into the uploads database // $_POST[$file] = $o->id; $f = str_replace('form_', '', $file); $model->{$f} = $o->id; $model->save(); } } }