Override default behaviour

Posted:
Thu Mar 05, 2009 2:12 am
by knackaerts
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

Posted:
Thu Mar 05, 2009 9:30 am
by shannah
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

Posted:
Thu Mar 05, 2009 11:09 am
by knackaerts
Truly impressive...
Thank's for this great, great product!

Posted:
Thu Mar 05, 2009 1:35 pm
by shannah
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');
}
}