andrewminton/enhanced_upload_field — pull request #2 by nitriques (2013-01-07 01:03:24+00:00)
Title: Wooooooooooo ! It's finally here!!!

After two days of work, (well, if one day of work is 4 hours since I am on vacations) I have cracked the case! Let me explain to you why to was soooo long to do achieve.

Reusing code that was not meant to be reuse can be a pain. But since php leverage the uses of arrays, this opens a lot of possibilities to clever developer. The thing is, if you mess up with data you need to serve it back the way it is expected. See the `revertData` method for an example. I really need to read and understand the code from the `FieldUpload` class to be able to figure out what was going on. This as been a BIG gotcha since this fields uses the `$data` variable both as a string and an array. Another thing I had to do was to rename the input field, in order to make saving the entry with a file (not touching it) work.

I had to hack through the javascript code to be able to make the select disappear and re-appear. If no that of a hack since the jQuery's event can be catch easily. I got rid of the CSS file since I did not needed anymore. I used the core css in order to style the field. If you wish to style the select box, feel free to to it, but you do not have to copy the upload field's css anymore ;)

I made the select box hidden if there is only one choice in it. If you delete the file, the subdirectory used if already chosen. If there is a file in the field, the select box is remove totally, so it won't affect the POST data.

Playing with this made me realize that you now can re-upload the same file! It just checks a new name, with and underscore and a numeral before the extension. Yeah! And if you change the directory, everything works perfectly!

I am really happy with the result. Having extended the functionality, I can be sure that more test cases have already been covered. I though about path traversal check. Went into the source code of the parent class, and it turns out it's already there! Nice!!! +1 for code reuse again! And if they find a bug in this check, you extension will benefit from the update without having to do anything. Brilliant.

Hope you like. I am really trilled about the fact that the field is about 300 LOC, with LOTS of comments. I hope those will help you understand what I did. Have fun!

P.S. You have to fix a bug in Symphony 2.3.1 in order to make it work properly. This bug is already resolve in the `integration` branch of Symphony, but it was shipped with 2.3.1. See the code for full details, I made a comment for this.

P.S.S If you have some free time, can you please check my [css3_filters extension](https://github.com/DeuxHuitHuit/css3_filters)? That would be neat!