Currently the IMCE editor has the option to replace an existing file with the new one in the settings located at:
admin/settings/imce
The feature works correctly but while working on a project it was requested of me to have the editor prompt the user to confirm that they would like to overwrite the existing file before actually overwriting the file. I was able to do this by modifying the javascript and using drupal_add_js and drupal_to_js to make the "imce_settings_replace" variable available to the javascript layer. I have created a patch that modifies inc/imce.pages.inc and js/imce.js to acheive this result.
After thinking about this request it seemed like something that might be useful to the community and might be good as default functionality of the IMCE module so I am attaching the patch to this issue for review.
Comment | File | Size | Author |
---|---|---|---|
#1 | imce_prompt.patch | 126.2 KB | blischalk |
imce_prompt.patch | 1.58 KB | blischalk | |
Comments
Comment #1
blischalk CreditAttribution: blischalk commentedUpon further testing it was discovered that the prompt was not functioning in IE properly. I have updated the patch so that it functions properly in both IE and Firefox. I am attaching the updated patch with this post.
Comment #2
blischalk CreditAttribution: blischalk commentedSetting thread to "Needs review" status.
Comment #3
mostou CreditAttribution: mostou commentedSubscribe
Comment #4
ufku CreditAttribution: ufku commentedYou don't have to hack IMCE for this. You just need to define hook_form_alter for imce upload form and put a checkbox(Overwrite existing?) and add a validator to the form. The validator should check the value of checkbox and change the value of $GLOBALS['conf']['imce_settings_replace'] accordingly.
Comment #5
ufku CreditAttribution: ufku commentedComment #6
johns996 CreditAttribution: johns996 commentedAny chance we could get a version 7.x version of this patch? Or even more details about about how to make ufku's changes?
Comment #7
frodopwns CreditAttribution: frodopwns commentedI agree a 7.x version would be nice.
Comment #8
frodopwns CreditAttribution: frodopwns commentedThis pretty well does it for me in D7
Comment #9
johns996 CreditAttribution: johns996 commentedWhere did you add that code?
Comment #10
frodopwns CreditAttribution: frodopwns commentedI created a new module named "imcevalid"
so the folder imcevalid contains:
imcevalid.info
imcevalid.module
imcevalid.info:
imcevalid.modul contains just the function i posted above (without
tags):
Upload this module and enable it...should be good to go....
Comment #11
frodopwns CreditAttribution: frodopwns commentedLet me know if you need more info.
Also...if your file names aren't already lowercase you may need to change the javascript to do that before comparing the new file with the existing ones.
Comment #12
johns996 CreditAttribution: johns996 commentedFor some reason I can't get this new module to work. I've created the files as you described, enabled the module and changed my IMCE common settings to "Replace the existing file with the new one" but I don't get a prompt or anything else when trying to upload a file already on the server.
I use CamelCase for my file names, so because of that I tried changing
var newfile = fname.substring(12).toLowerCase();
to
var newfile = fname.substring(12);
but this still didn't fire the prompt. Am I missing something?
One final note, I had to keep in the opening php tag in the module. Without it, it just dropped the function at the head of my pages. (I kept the closing one off, however.)
Comment #13
johns996 CreditAttribution: johns996 commentedAfter playing around with this script for a bit, this is what I got to finally work the way I was expecting it to.
What was happening was that the .substring(12) was cutting off the first 12 characters of the imported file name so the comparison would never match. I changed that to .substring(0) and then pulled out the .toLowerCase() since my files are not all lower case. Now, things work as expected.
Thanks for your help with this. My users are going to love it.
Comment #14
frodopwns CreditAttribution: frodopwns commentedI had trouble splitting the filename because I was getting something like "C:\fakepath\filename.ext" then when i did str,split("\\") it failed so I just did the substring to get rid of the fake path.
Also this script may need some tweaking for cross browser support because the value returned from the file input differs.
in chrome C:\fakepath\filename
in firefox its just filename so i did this:
if (jQuery.browser.mozilla){
var newfile = fname.toLowerCase();
}
else {
var newfile = fname.substring(12).toLowerCase();
}
Comment #15
johns996 CreditAttribution: johns996 commentedNaturally, I didn't check other browsers to see what they were doing. After checking Chrome, I see the fakepath stuff you mentioned and your tweak worked. I'm a little afraid to see what IE 8 and 9 do to the path.
Comment #16
frodopwns CreditAttribution: frodopwns commentedIE9 appears to be the same as Chrome..not sure about other versions. Hooray for jQuery hacks!
Comment #17
johns996 CreditAttribution: johns996 commentedIE8 also works like Chrome/IE9. I'm calling that good and pushing this live. Thanks again my Tolkien inspired friend.