Override default behaviour

A place for users and developers of the Xataface to discuss and receive support.

Override default behaviour

Postby knackaerts » Thu Mar 05, 2009 2:12 am

Dear all,

Is it possible to override a default behaviour? I would like the user to be able to upload images. widget:type = file is OK, but I want the file copied to a specific directory, the filename changed and only the filename stored in MySQL...

Where to start?

Kris
knackaerts
 
Posts: 2
Joined: Thu Mar 05, 2009 2:08 am

Postby shannah » Thu Mar 05, 2009 9:30 am

Check out http://xataface.com/documentation/how-t ... le-uploads

What you are looking for is the container field.

E.g.

[myfield]
Type=container

This will save the file in the file system and store the name in the 'myfield' field. You can specify an alternate directory for storage using the 'url' and 'savepath' directives.

If you want to change the file name on upload you can define an afterSave() trigger:

Code: Select all
function afterSave(&$record){
    if ( $record->valueChanged('myfield') ){
        $fieldDef =& $record->_table->getField('myfield');
        $savepath = $fieldDef['savepath'];
        $filename = basename($record->val('myfield'));
        rename($savepath.'/'.$filename, $savepath.'/mynewname');
    }
}


-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby knackaerts » Thu Mar 05, 2009 11:09 am

Truly impressive...

Thank's for this great, great product!
knackaerts
 
Posts: 2
Joined: Thu Mar 05, 2009 2:08 am

Postby shannah » Thu Mar 05, 2009 1:35 pm

One correction. It is probably best to use the beforeSave handler instead of the afterSave handler so that you can change the file name in the database as well.

Code: Select all
function beforeSave(&$record){
    if ( $record->valueChanged('myfield') ){
        $fieldDef =& $record->_table->getField('myfield');
        $savepath = $fieldDef['savepath'];
        $filename = basename($record->val('myfield'));
        rename($savepath.'/'.$filename, $savepath.'/mynewname');
        $record->setValue('myfield', 'mynewname');
    }
}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 20 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved