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

Auto-Relating uploaded files through ManyManyFileDataObjectManager ?


Go to End


5 Posts   1493 Views

Avatar
brice

Community Member, 52 Posts

3 September 2010 at 7:41am

I'm writing a Media Module that leverages DOM and Uploadify. The module has a MediaAlbumPage which calls a ManyManyFileDataObjectManager in its getCMSFields routine to manage videos. For simplicities sake; The ManyManyFileDataObjectManager is set to display all Videos and the checkbox to show "Related Only" is left unchecked.

When I upload a video through the add button at top of the DOM field, the video is shown in the list upon completion, but the checkbox relating it to the current MediaAlbumPage is left empty.

Is there a simple solution to auto-relate files through the ManyManyFileDataObjectManager (e.g. auto-check the box in the DOM to relate the asset)?

I see there's a trick in the ImageGallery Module that handles this by calling a class extending FileDataObjectManager instead of FileDataObjectManager itself, and injecting the ID of the parent gallery through the getUploadifyFields and getUploadFields method -- but was wondering if there's a different way to go w/o having to write a custom DOM class.

Thanks!

~ Brice

Avatar
UncleCheese

Forum Moderator, 4102 Posts

3 September 2010 at 8:39am

Yeah, I posted a tutorial on that somewhere in this forum..

Avatar
brice

Community Member, 52 Posts

3 September 2010 at 10:23am

Edited: 03/09/2010 10:30am

hmmm.. still searching but will post here when I find it! :)

Thought I had something in: http://silverstripe.org/archive/show/65501#post65501
($relationAutoSetting) ... but it does not work as expected :(

Avatar
brice

Community Member, 52 Posts

3 September 2010 at 10:36am

Edited: 03/09/2010 10:37am

I believe I found it;

http://silverstripe.org/dataobjectmanager-module-forum/show/290329#post290329

Will play around & see if I can come up w/ an elegant patch -- maybe favor the ImageGallery module approach.

Avatar
brice

Community Member, 52 Posts

3 September 2010 at 12:39pm

Below is a proposed patch to FileDataObjectManager to handle this automatically if you set the autoRelateUploads property on your ManyManyFileDataObjectManager or HasManyFileDataObjectMany to boolean: true or string: name of relation.

Index: www/dataobject_manager/code/FileDataObjectManager.php
===================================================================
--- www/dataobject_manager/code/FileDataObjectManager.php	(revision 14)
+++ www/dataobject_manager/code/FileDataObjectManager.php	(working copy)
@@ -41,6 +41,21 @@
 	
 	public $uploadifyField = "MultipleFileUploadField";
 	
+	// [start] bb: patch - auto relate uploaded asset (1 of 2)
+	/**
+	 * false: [default] do not attempt to auto-relate uploads
+	 * true: use plural name of uploaded class for relation component
+	 * String: specify has_many or many_many relationship name
+	 * @example 
+	 * 		$DOM = new ManyManyFileDataObjectManager;
+	 * 		$DOM->autoRelateUploads = true; 
+	 * @example 
+	 * 		$DOM = new HasManyFileDataObjectManager;
+	 * 		$DOM->autoRelateUploads = 'Videos'; 
+	 */
+	public $autoRelateUploads = false;
+	// [end] bb: patch - auto relate uploaded asset (1 of 2)
+	
 	public function __construct($controller, $name = null, $sourceClass = null, $fileFieldName = null, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") 
 	{
 		if(!class_exists("SWFUploadField"))
@@ -413,6 +428,21 @@
 				$this->updateDataObject($obj);
 				$obj->write();
 				$dataobject_ids[] = $obj->ID;
+				
+				// [start] bb: patch - auto relate uploaded asset (2 of 2)
+				if($this->autoRelateUploads && property_exists($this, 'RelationType'))
+				{
+					$parent = DataObject::get_by_id($this->controllerClassName, $this->controllerID);
+					$relationName = (is_string($this->autoRelateUploads)) ?
+						$this->autoRelateUploads : $object->plural_name();
+					$func = ($this->RelationType == 'ManyMany') ? 
+						'getManyManyComponents' : 'getComponents';
+						
+					$relations = $parent->$func($relationName);
+					$relations->add($obj);
+					$relations->write();
+				}
+				// [end] bb: patch - auto relate uploaded asset (2 of 2)
 			}
 			$_POST['uploaded_files'] = $dataobject_ids;					
 		}	

It does not include the JavaScript fix mentioned by Darren in http://silverstripe.org/dataobjectmanager-module-forum/show/290329#post290329

NOTE: this must be applied against version of DOM that include & use Uploadify