Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Two questions regarding Uploadify


Go to End


8 Posts   2770 Views

Avatar
Xurk

Community Member, 50 Posts

5 January 2011 at 4:27am

Edited: 05/01/2011 4:31am

I've installed Uploadify today and am trying it out - as usual, it looks great and works like a charm! I've implemented it on the front-end of the website, where certain decorated Members can add images to specific profile details. Now that the basics are there, I want to fine-tune the page a bit more... so here come the questions :)

First off, an easy one. Does anyone know if there is a list somewhere containing all of the allowed key-value pairs for the setVar() method? I've used this to alter the text of the "Browse..." button, but I'd like to know if it's also possible to alter the text "Attached files", "No files attached", etc.

Another thing I've been trying to implement without much success, is somehow passing the currently stored files / images to the (Multiple)ImageUploadField in the form. Seeing as the form edits details of two different DataObject class instances at once, I haven't used "$Form->loadDataFrom()". I don't know if normally, that would be sufficient? In my case, I want to manually add the currently saved images to their form fields on page load. It seems like the most logical way to enable users to remove / replace them and add new images. As it is, the text "No files attached" could be misleading and cause them to think that their images haven't successfully been uploaded.

If this isn't possible, would the "next best thing" be to separate displaying currently stored images from the form and display them using a separate function on the template and - on form submission - deleting them (before saving the new images) if the user selected new images to upload?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

5 January 2011 at 4:50am

Hi, Xurk,

Your first question is easy... both of those strings have entries in the lang file, so you can just override Uploadify.ATTACHEDFILES or Uploadify.NOFILES, etc..

As for your second query, I read it 4 times, and I still don't understand what you want. Maybe give an example?

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
Xurk

Community Member, 50 Posts

5 January 2011 at 9:25pm

Edited: 05/01/2011 9:27pm

Hi UncleCheese, thanks for your response :)

On the commute home yesterday evening I suddenly realized that the answer to my first question was probably going to be related to the language file like you said, it just hadn't dawned on me in time. Thanks for confirming that for me! I needed the text to be in Dutch, so I've added a "nl_NL.php" file to the lang directory of Uploadify and SS automatically picks that up. So that one can be scratched off the list.

As for my other question... I was afraid that my post was going to be tough to decipher as I was struggling to explain myself, but I'll try and make a long story short this time - hopefully, that will be clearer. I'll break it down into the steps I want the user to take.

1. Visit the form page which includes two Uploadify fields (ImageUploadField and MultipleUploadField)
2. Complete the form and select some images to upload, submit the form
3. Return to the form page again later (or after submission - redirectBack() on success)
4. Auto-populate the form with all the information the user previously stored

Step four is where I'm stuck at the moment. Auto-populating the regular form fields (e.g. TextField, CheckboxField) is no problem, but it's not clear to me how to auto-populate the "Attached files" part of the Uploadify fields with the files which have been uploaded previously. I'm not sure if that is even a default feature though?
The reasons why I'd like to display the files there is so that the user can see which images he / she has uploaded and can use the Uploadify controls to remove them and upload new ones. This opposed to creating a default function for displaying / removing existing files on the form page.

Hopefully, I've managed to articulate my question better this time around. I'm eager to hear your thoughts on this!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

6 January 2011 at 4:27am

Hi, Xurk,

A lot of care and attention went into making sure Uploadify fields work like normal form fields and don't suffer all of the shortcomings that file upload fields usually do. Specifically, they can validate and save their state.

I recommend you look at this tutorial for more information on how to use Uploadify fields on the frontend.

http://www.leftandmain.com/silverstripe-tutorials/2010/08/30/using-uploadify-in-frontend-forms/

You're on the right track with loadDataFrom(). Uploadify fields use their native loadDataFrom($formData) method to set their default value intelligently.

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
Xurk

Community Member, 50 Posts

7 January 2011 at 10:58pm

Thanks again for helping out, UncleCheese. I agree with you that the Uploadify fields seamlessly fit into the rest of SilverStripe (not unlike DataObjectManager) and don't feel tacked on.

The tutorial you linked to is the one that I followed while implementing the fields on my frontend form; I read through it again but still couldn't quite find the solution. So I decided to look through the UploadifyField class code and soon found what I was looking for. It turned out to be the "setValue()" function - I guess that's what you were hinting towards? :) Anyway, adding that in equaled instant success!

I noticed that the "removefile()" doesn't do anything beside removing the attached file from the page - which makes sense, as that's what it's supposed to do seeing as the FileUploadField isn't normally used for viewing previously uploaded files. I also noticed that there is in fact a "deletefile()" function in the UploadifyField class, but couldn't figure out how to hook this into a frontend form as it seems to be there for the CMS side.

So what I've done is extend my FormAction function to check whether or not a FileID was passed and if not (or if the passed FileID differs from the previously stored ID), manually deletes any file which was associated with the DataObject record. There is probably a neater way to achieve this, but it works for now :)

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 January 2011 at 4:12am

Hi, Xurk,

Deleting files is not available for frontend fields. You can only remove them from the form itself -- not hte database. Destructive actions to the database and filesystem should be behind authentication as a best practice. Otherwise, you could have anonymous users deleting arbitrary files.

/YourForm/field/YourUploadField/deletefile/123
/YourForm/field/YourUploadField/deletefile/124
/YourForm/field/YourUploadField/deletefile/125

Not good. :)

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
Xurk

Community Member, 50 Posts

8 January 2011 at 4:42am

You're completely right :) However, this form is only available for a certain group of Members who are logged in. And I've set the deletion function up so that they should only be able to delete their own files. If anyone manages to "hack" that, at least we'll know whodunnit ;)

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 January 2011 at 5:07am

I suppose on the frontend you could be limited to only deleting files that have your OwnerID. That's definitely a feature I'll consider!