I've got an idea for a project and wondering if there's an already existing module that can achieve something similar. Basically I want to allow the users to upload/download files from their member profile. Users will only be able to view/download their own files. I have member profiles set up already, it's the file handling that I need to get working now.
I imagine it will be something like a gridfield displayed on the users profile. When they use an upload field it will save files in to assets/ folder. Which can only be viewed by that member (admins can view all in the /admin section of the CMS.
Are there any current modules that support something like this?
If I create an extension of the Member profile that allows uploading files, how could I implement the security for only users with the currentID to view files?
EDIT: OK So I've made some progress with this. The upload file function works. It saves it in to a folder which is named after the user. However, it saves the uploaded file in that directory, but also a blank file under the "Files" directory. Here is my code...
//User file upload function
public function UploadUserFile() {
$fields = new FieldList($field = new UploadField('UserFiles', 'Upload files'));
$field->setCanAttachExisting(false); // Block access to SilverStripe assets library
$field->setCanPreviewFolder(false); // Don't show target filesystem folder on upload field
$field->setFolderName('user-files/user-'.Member::currentUserID()); //Upload to a user specific folder
$actions = new FieldList(new FormAction('submit', 'Save Images'));
$form = new Form($this, 'UploadUserFile', $fields, $actions, null);
return $form;
}
public function submit($data, $form) {
$file = new File();
$form->saveInto($file);
$file->write();
return $this;
}
EDIT 2:
Turns out that the submit function was causing the extra file. Disregard. Still experiencing other problems now but will see if I can fix it first.
EDIT 3:
OK, I've had a go and I am able to display files on the front end. Here is my code...
//User file upload function
public function UploadUserFile() {
$fields = new FieldList($field = new UploadField('UserFiles', 'Upload files'));
$field->setCanAttachExisting(false); // Block access to SilverStripe assets library
$field->setCanPreviewFolder(false); // Don't show target filesystem folder on upload field
$field->setFolderName('User-Files/User-'.Member::currentUserID().'-'.Member::currentUser()->Surname.Member::currentUser()->FirstName); //Upload to a user specific folder
$field->setAutoUpload(false);
$actions = new FieldList(new FormAction('submit', 'Save Images'));
$form = new Form($this, 'UploadUserFile', $fields, $actions, null);
return $form;
}
//Refresh files function
public function submit($data, $form) {
return $this->redirectBack();
}
//Display users files
public function DisplayFiles() {
$arrayList = ArrayList::create();
$files = File::get()->filter(array(
"OwnerID" => Member::currentUserID(),
"ClassName:not" => 'Folder'
));
foreach($files as $file) {
if($file->canView()) {
$arrayList->push($file);
}
}
return $arrayList;
}
I'm trying to allow users to delete their file from the front end. What's the best way to handle this?
Thanks