How can i get the fileobject in event 'on_file_add'

Permalink
i have trouble to get the File Object in the Event Class bei using 'on_file_add'.

I thought it works like this:
class FileEvents extends Object {
   public function setupContent($f, $fv) {
      //...do anything like
      $file['title']    = $fv->getTitle();
      $file['path']    = $f->getPath();
      $file['type']   = $fv->getExtension();
   }
 }


By the way, the event is sent successfully when uploading a file.
Events::extend('on_file_add', 'FileEvents', 'setupContent', 'models/file_events.php');

Is there also an event for replacing a file?

Thanks for your help!

Asseco
 
12345j replied on at Permalink Reply
12345j
hmm, that is the way it works. have you tried using the event tester addon?
Asseco replied on at Permalink Reply
Asseco
Meanwhile, I can restrict the problem further.
The file object is passed. The problem is that not all functions are available.

The following works:
$f->getURL();
$f->getFileID();


And that does not work:
$f->getTitle();
$f->getPath();
$fv->getExtension();


But i need exactly this functions :(

Even a re-import does not work:
$newF = File::getByID($f->getFileID());
Asseco replied on at Permalink Reply
Asseco
Is there any solution?
Please, i need help!
JohntheFish replied on at Permalink Reply
JohntheFish
I suspect you may have a chicken and egg problem, in that what you are looking for may not exist at the time the event is triggered, and is only available after the file is added, not while you are adding it.

(This is by no means a definitive statement. It may be a completely wrong assumption, so please check before proceeding further on my assumptions. It is based on observing similar issues with other add processes in C5).

To get round this sort of problem,
- modify the core code to trigger an event after the add has completed.
- in your own code, simulate/predict what the add is doing and work with that info.

Neither are easy to do.
Asseco replied on at Permalink Reply
Asseco
At first I thought that too. After I've looked at the core code, I was not sure anymore:

public static function add($filename, $prefix, $data = array()) {
      $db = Loader::db();
      $dh = Loader::helper('date');
      $date = $dh->getSystemDateTime(); 
      $uID = 0;
      $u = new User();
      if (isset($data['uID'])) { $uID = $data['uID'];}          
      else if ($u->isRegistered()) {$uID = $u->getUserID();}
      $db->Execute('insert into Files (fDateAdded, uID) values (?, ?)', array($date, $uID));
      $fID = $db->Insert_ID();
      $f = File::getByID($fID);
      $fv = $f->addVersion($filename, $prefix, $data);
      Events::fire('on_file_add', $f, $fv);   
      return $fv;
   }
JohntheFish replied on at Permalink Reply
JohntheFish
I see what you mean. It looks like there should be usable objects passed to the event handler.

Maybe try var_dumping $f and $fv in your event handler and see what is actually in them. As 12345J noted, Mnkras' event tester addon is also an excellent tool for seeing what an event is giving you.

Are there any other handlers for the same event that could be messing with these?