Putting uploaded files into a set automatically

Permalink 3 users found helpful
Had a need to put uploaded files (images) into a set automatically so an image gallery plugin would display them to visitors. Sharing what I did!

Create the set first, e.g. uploaded-image or something. I then copied the /concrete/blocks/form directory into /blocks. Then in the file /blocks/form/controller.php I added the following after line 265 (C5 V5.4.1.1):
$tmpFileIds[intval($row['msqID'])] = $resp->getFileID(); # line 265
$fs = FileSet::getByName('file-upload');
$fsf = $fs->addFileToSet($resp->getFileID());


The first line of the three *IS* line 265 so you can identify it. Note that 'file-upload' is the name of the set I created.

Hope this helps people trying to put uploads into sets! Would be very useful to have an 'Add uploads to set automatically' style option in the form UI.

G

surefyre
 
ScottC replied on at Permalink Reply
ScottC
there is an on_file_upload event, i haven't extended it, but you could take any instance of a file uploaded and add it to a set with our existing code.
melat0nin replied on at Permalink Reply
melat0nin
EDIT: stupid me got the set name wrong. That's what deadlines do to you!

Hi surefyre

I implemented your code but it doesn't seem to work - I get a blank screen when the form is submitted, and the file, although uploaded, isn't put into the correct set.

Any ideas? I have advanced permissions enabled. The set I'm trying to upload to is called file_uploads.
surefyre replied on at Permalink Reply
surefyre
The only thing I can think is that advanced permissions changes the flow
somehow, it was such a small simple hack to get it to do that, can you try
an ordinary test clean install of C5 without to see if the same happens? Oh,
did you create the set beforehand in filemanager? ;o)

G




On 12 April 2011 14:27, Concrete5 Community <discussions@concretecms.com>wrote:
gnyma replied on at Permalink Reply
gnyma
Hi G!

This is extremely useful, thank you!

What if I have different pages and galleries that I want people to upload to? Since we're modifying one block that will effect every upload, how can we make it so that we can assign different filesets for each page/image gallery?

Thanks in advance,
Amy
surefyre replied on at Permalink Reply
surefyre
Hi Amy

I've not tried this but I think it should work - bit of a kludge but if it gets you there... ;o)

In the blocks/form directory you could create a 'templates' directory. Copy the blocks/form/view.php script into the templates directory as e.g. gallery1.php

Edit gallery1.php to include a hidden field e.g.
<input type="hidden" name="galleryid" value="gallery1" />


In your controller.php you can then examine the galleryid value to decide which set to put the file into e.g.
switch($_REQUEST['galleryid']) {
    case 'gallery1':
       $mysetname = 'gallery_1';
       break ;
    case 'gallery2':
       $mysetname = 'gallery_2';
       break ;
    default:
   $mysetname = '';
}
if($mysetname != '') {
    // add file to set
    $fs = FileSet::getByName($mysetname);
    $fsf = $fs->addFileToSet($resp->getFileID());
}


Then create gallery2.php, etc in the templates directory and select those as custom templates for the relevant galleries.

Hope you get the idea of my line of thinking from that!

G
gnyma replied on at Permalink Reply
gnyma
Hi G!

Thanks for the reply and I'll have a go at your code -- I've figured it out as well, albeit using a different approach outlined here:http://www.concrete5.org/community/forums/chat/form-attachment...

It's great that there's different solutions to a problem. :)