diff --git a/src/Pluf/Form/Field/File.php b/src/Pluf/Form/Field/File.php index 1cccc3c..0b47b98 100644 --- a/src/Pluf/Form/Field/File.php +++ b/src/Pluf/Form/Field/File.php @@ -24,7 +24,7 @@ class Pluf_Form_Field_File extends Pluf_Form_Field { public $widget = 'Pluf_Form_Widget_FileInput'; - public $move_function = 'Pluf_Form_Field_moveToUploadFolder'; + public $move_function = 'Pluf_Form_Field_File_moveToUploadFolder'; public $max_size = 2097152; // 2MB public $move_function_params = array(); @@ -81,7 +81,7 @@ class Pluf_Form_Field_File extends Pluf_Form_Field /** * Default move function. The file name is sanitized. * - * In the extra parameters, 2 can be used so that this function is + * In the extra parameters, options can be used so that this function is * matching most of the needs: * * * 'upload_path': The path in which the uploaded file will be @@ -91,6 +91,13 @@ class Pluf_Form_Field_File extends Pluf_Form_Field * * * 'upload_overwrite': Set it to true if you want to allow overwritting. * + * * 'file_name': Force the file name to this name and do not use the + * original file name. If this name contains '%s' for + * example 'myid-%s', '%s' will be replaced by the + * original filename. This can be used when for + * example, you want to prefix with the id of an + * article all the files attached to this article. + * * If you combine those options, you can dynamically generate the path * name in your form (for example date base) and let this upload * function create it on demand. @@ -99,7 +106,7 @@ class Pluf_Form_Field_File extends Pluf_Form_Field * @param array Extra parameters. If upload_path key is set, use it. (array()) * @return string Name relative to the upload path. */ -function Pluf_Form_Field_moveToUploadFolder($value, $params=array()) +function Pluf_Form_Field_File_moveToUploadFolder($value, $params=array()) { $name = Pluf_Utils::cleanFileName($value['name']); $upload_path = Pluf::f('upload_path', '/tmp'); @@ -111,6 +118,13 @@ function Pluf_Form_Field_moveToUploadFolder($value, $params=array()) throw new Pluf_Form_Invalid(__('An error occured when creating the upload path. Please try to send the file again.')); } } + if (isset($params['file_name'])) { + if (false !== strpos($params['file_name'], '%s')) { + $name = sprintf($params['file_name'], $name); + } else { + $name = $params['file_name']; + } + } $dest = $upload_path.'/'.$name; if ((!isset($params['upload_overwrite']) or $params['upload_overwrite'] == false) and file_exists($dest)) { throw new Pluf_Form_Invalid(sprintf(__('A file with the name "%s" has already been uploaded.'), $name));