Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Auto-Relating uploaded files through ManyManyFileDataObjectManager ?

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1026 Views
  • brice
    Avatar
    Community Member
    51 Posts

    Auto-Relating uploaded files through ManyManyFileDataObjectManager ? Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Auto-Relating uploaded files through ManyManyFileDataObjectManager ? Link to this post

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

  • brice
    Avatar
    Community Member
    51 Posts

    Re: Auto-Relating uploaded files through ManyManyFileDataObjectManager ? Link to this post

    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

  • brice
    Avatar
    Community Member
    51 Posts

    Re: Auto-Relating uploaded files through ManyManyFileDataObjectManager ? Link to this post

    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.

  • brice
    Avatar
    Community Member
    51 Posts

    Re: Auto-Relating uploaded files through ManyManyFileDataObjectManager ? Link to this post

    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

    1026 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.