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.

Archive /

Our old forums are still available as a read-only archive.

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

HasManyFileManager: New CMS module/extension. Testers/review needed


Go to End


62 Posts   109893 Views

Avatar
bummzack

Community Member, 904 Posts

2 August 2008 at 11:13pm

Edited: 03/08/2008 10:38pm

Hello Forum

I'm currently working on two websites, where i'm in need for a FormField that allows me to attach multiple Images/Files to a SiteTree instance. The last time i encountered this problem, i solved it like described in this post: http://www.silverstripe.com/site-builders-forum/flat/5853 (creating a folder for every page and put files in there).
However, this solution doesn't satisfy my needs and lacks a possibility to easily sort the items attached to a Page. Therefore i decided to write my own custom FormField for my needs.

I think the module (or CMS extension) has now reached a stage where i can show it to you and let you comment and test it.
Since this is my first extension to the SilverStripe CMS and i'm not yet quite familiar with all the concepts and possibilities of the framework, the code may seem rather "hackish" here and there :)
That's why i'm posting this here. To get as many feedback and testers as possible!

Please note: This is a beta release at best. Use it in a productive environment at your own risk!

Installation
- Download the filemanager.zip
- Unzip to <your_site_directory>/filemanager
- Run /db/build?flush=1

That should be all. When you enter the CMS, you'll have a new page type named "File Page". Create a new File Page. You should have two additional tabs named "Files" and "Images" in the CMS. Now you can add Files to these tabs. These items then "belong" to this Page.
Items can be sorted using Drag'n'Drop.

Have a look at the code/FilePage.php File to see an example of how to add the HasManyFileManager FormField to your own Pages.

This module has been tested in Safari3 (mac), Firefox3 (mac), IE7 (win), IE6 (win). In IE6 there's a bug when dragging items (weird offset).

And here's a screenshot how this looks in the CMS.

Any feedback is welcome. Thanks for reading.

Edit: fixed formatting...
Edit: Ouch. File attachement didn't seem to work.
Edit: Fixed some bugs. New sources to be found here: filemanager_0.2.zip

Avatar
Sean

Forum Moderator, 922 Posts

4 August 2008 at 7:43pm

Edited: 04/08/2008 7:51pm

This looks awesome.

It would be even better if you could add this onto any type of page type, instead of just a FilePage, so you could pretty much use it like a standard FormField in the CMS or even a form on the front end (possibly a lot more work to achieve this?). Alternatively even attaching it onto 'Page' using the DataObjectDecorator by default could be an option? This would mean you'd get the added benefit of being able to attach multiple images to already existing standard pages.

It would mean you'd need to move those methods you've currently made on FilePage over to HasManyFileManager, e.g. "deleteAttached" and "Files" - or, it's possible they could go on the decorator class instead if they don't fit on HasManyFileManager.

Nice work on it though, you appear to have touched on the more advanced concepts of SilverStripe, like the DataObjectDecorator, as seen with AttachedFile, so it's nice to see a solid implementation of it!

Sean

Avatar
bummzack

Community Member, 904 Posts

4 August 2008 at 7:58pm

Hi Sean

Thanks for the Reply.
You're right, i'll modify the FormField to make it work on any Type of Page inside the CMS. That was actually my intention :)
Is it possible to have a onBeforeDelete method on a FormField, and will it be called when the parent SiteTree element is being deleted? Or how would you implement said methods?

Avatar
Sean

Forum Moderator, 922 Posts

4 August 2008 at 8:09pm

Edited: 04/08/2008 8:12pm

Very good point. I don't know enough about the DataObjectDecorator to understand if that you call delete() on it's owner class it knows about/calls onBeforeDelete() on the decorator class. I guess you'd also need to call $this->owner instead of $this on the decorator if that works.

And as for the FormField class, I don't believe it's possible to implement a check for deletion of it's SiteTree parent, because it doesn't have the responsibility to do so.

Perhaps you could try adding onBeforeDelete() to your decorator, and see if it's called when you delete an instance of the decorated class?

Avatar
bummzack

Community Member, 904 Posts

4 August 2008 at 8:14pm

Allright. Thanks!
I'll look into it and post here what i come up with.

Avatar
bummzack

Community Member, 904 Posts

5 August 2008 at 12:28am

I considered the input from Sean and modified my code accordingly. Some Bugs have been eliminated too.

Get the latest sources: filemanager_0.3.zip

Sadly, a onBeforeDelete Method on a Decorator doesn't apply to or overrides a existing implementation in the decorated class. That's why i now created a new Decorator for the SiteTree Object which adds 2 methods to any SiteTree instance. These two methods are:
AttachedFiles A method you can call inside your Templates. Let's assume you have a HasManyFileManager FormField in your Page Class and you "named" it "MyImages". Now you could add the following Code to the Page.ss template to output files attached to the MyImages HasManyFileManager.

<% control AttachedFiles(MyImages) %>
... item output. for example $CroppedImage(100,100)
<% end_control %>

deleteAttachedFiles Delete all files attached to this SiteTree instance. You are encouraged to add a call to this Method inside your Pages onBeforeDelete method.
Example (in Page.php)

public function onBeforeDelete(){
	parent::onBeforeDelete();

	/** @see SiteTreeFileHandler.php */
	$this->deleteAttachedFiles();
}

These changes make it quite simple to add the HasManyFileManager to your own Page Classes. Here's a very basic example for a custom Page that has a ManyFileManager. Let's call it ImagePage.php

<?php
class ImagePage extends Page
{
	static $has_many = array(
		'Images' => 'Image'
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();

		$manager = new HasManyFileManager(
			$this,
			'MyImages', // name -> will be used for file grouping
			'Images' // relation name defined in $has_many
		);

		$fields->addFieldToTab('Root.Content.Images', $manager);
		return $fields;
	}

	public function onBeforeDelete(){
		parent::onBeforeDelete();

		/** @see SiteTreeFileHandler.php */
		$this->deleteAttachedFiles();
	}
}

That's all. Now you can add Images to this Page and output them as described above.
Please note, that there is still a FilePage.php Class inside the filemanager/code Directory. You can safely delete it, it's just there for demonstration purposes.

Avatar
Fuzz10

Community Member, 791 Posts

15 August 2008 at 11:27pm

Hi Banal,

That is an awesome piece of work you did !

I'm currently using your module in 1 of our projects , it works like a charm !

Thank you for that ...

I think this could very well be integrated into the main CMS system (without being a separate module) !

Avatar
bummzack

Community Member, 904 Posts

15 August 2008 at 11:37pm

Hi Fuzz10

Thanks for the feedback. Glad it's useful to you!
It still has some quirks i wasn't yet able to resolve to my satisfaction. Mainly because the SilverStripe Image Class has some bugs, or let's call it "room for improvements".
When you upload different files, but with the same Filename, the mini-thumbnails get messed up. The creation of folders and putting files into the right directory doesn't work as expected too... i filed that in the BugTracker.

As of now, it works quite nicely here but i had to modify the Image Class (ss core).
Of course it would be awesome, if the SilverStripe Team decides to put this "module" into a future release ;)

Go to Top