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.

Archive

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

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

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


Reply

62 Posts   104229 Views

Avatar
erwanpia

10 October 2008 at 3:58am Community Member, 63 Posts

OK thks again

I have it working now but my problem is in the template : I can't access custom fields Description and DestLink

I tried DataObject::add_extension('Image', 'AttachedFile'); in _config without success

         <% if AttachedFiles(MyImages) %>
         <div id="our_work">
         <ul >
            <% control AttachedFiles(MyImages) %>
               <li><a href="$DestLink">$SetWidth(200) dqsfqsdfqsd $Description</a></li>
            <% end_control %>
         </ul>
         </div>
   <div id="worm"></div>
         <% end_if %>

AttachedFile customization :

class AttachedFile extends DataObjectDecorator
{
   // get the additional DB Fields
   public function extraDBFields() {
      return array(
         'db' => array(
            'Grouping' => 'Varchar(255)',
            'Description'=> 'Varchar(255)'
         ),
         'has_one' => array(
            'SiteTree' => 'SiteTree',
            "LinkTo" => "SiteTree"
         ),
         
      );
   }

   /**
    * check wheather or not this is a image
    * @return bool
    */
   public function IsImage(){
      return $this->owner instanceof Image;
   }

   static $default_sort = 'Sort';

   // set the container class here. usually SiteTree will do.
   // This should be the same class as in the has_one relation
   static $containerClass = 'SiteTree';

   /**
    * This is needed in the FileManager. We just allow the user to edit the
    * File Title by adding it here.
    *
    * FIXED: Name is changed to getMyCMSFields 'cause there s an existing
    * getCMSFields function in the File-Class.
    *
    * @return FieldSet fields that should be editable by the user
    */
   public function getMyCMSFields(){
      /**
       * FIXED: Value is only shown if theres an existing entry.
       * Otherwise we get some crappy stuff in the Title
       */
      $items = DataObject::get('SiteTree');
$values = array();$values[0] = "";
foreach($items as $item){
$values[$item->ID] = $item->Title;
}
      $fields = new FieldSet(
         new TextField('Title', _t('AttachedFile_Uploader.TITLE', 'Title'),($this->owner->ID) ? $this->owner->Title : ""),
         new TextareaField('Description', "Description",5,5,($this->owner->ID) ? $this->owner->Description : ""),
         new DropdownField(   
                  "LinkToID",
                  "Lien",   $values,$this->owner->LinkToID
               )
      );
      return $fields;
   }
   
   public function testDecoClass(){
      Debug::show('i am decorated');
   }

   public function getContainerClass(){
      return self::$containerClass;
   }
   function DestLink() {
   
         $linkTo = $this->LinkToID ? DataObject::get_by_id("SiteTree", $this->LinkToID) : null;
         if($linkTo) {
            return $linkTo->Link();
         }
      
   }

Avatar
banal

10 October 2008 at 4:09am Community Member, 901 Posts

When you're accessing properties from a object that is extended by a Decorator class you should always use $this->owner->property.
E.g. $this->owner->LinkToID (you got that wrong in your DestLink method).

The custom properties and methods work just fine in a template. At least they do for me. Please read [url=http://doc.silverstripe.com/doku.php?id=modules:hasmanyfilemanager#create_your_custom_file_class]these instuctions[/url] how to build your own custom file decorator class.

Avatar
erwanpia

10 October 2008 at 4:55am Community Member, 63 Posts

Thks and sorry for coming back

for some reason the decorator object does not return custom variables, even via the owner object.

   function DestLink() {
    return $this->owner->LinkToID ;
   }

Avatar
banal

10 October 2008 at 5:02am Community Member, 901 Posts

Did you even rebuild your database (db/build?flush=1) ?

Please have a look at your File table and check if you got a "LinkToID" Field there and if it's values are being populated when you save files in the CMS.

Avatar
erwanpia

10 October 2008 at 5:17am Community Member, 63 Posts

Hi see you' re getting anxious...

Yes I rebuilt the DB ten times, and yes values are being saved in the file table

that's why I was wondering why data is seen in the admin (getMyCMSFields is populating the modify form with the saved values from the this->owner default values)

but they can't be seen in the front end even via functions such as DestLink.

If I solve this I promise I publish an extension that does the image /rollover effect from this page

http://kyanmedia.com/

Avatar
banal

10 October 2008 at 5:39am (Last edited: 10 October 2008 5:42am), Community Member, 901 Posts

Hm. I tested your code and it works for me (SilverStripe 2.2.2). Here's the full code listing:

<?php
// file mysite/code/MyFile.php
class MyFile extends DataObjectDecorator
{
   public function extraDBFields() {
      return array(
         'db' => array(
            'Description'=> 'Varchar(255)'
         ),
         'has_one' => array(
            "LinkTo" => "SiteTree"
         )
      );
   }
   
   public function getMyCMSFields(){
      $items = DataObject::get('SiteTree');
      $values = array();$values[0] = "";
      
      foreach($items as $item){
         $values[$item->ID] = $item->Title;
      }
      
      $fields = new FieldSet(
         new TextField('Title', _t('AttachedFile_Uploader.TITLE', 'Title'),($this->owner->ID) ? $this->owner->Title : ""),
         new TextareaField('Description', "Description",5,5,($this->owner->ID) ? $this->owner->Description : ""),
         new DropdownField("LinkToID","Lien", $values,$this->owner->LinkToID)
      );
      return $fields;
   }
   
   
   function DestLink() {
      $linkTo = $this->owner->LinkToID ? DataObject::get_by_id("SiteTree", $this->owner->LinkToID) : null;
      
      if($linkTo) {
         return $linkTo->Link();
      } else {
         return "no LINK";
      }
   }
}

And in the _config.php

// file mysite/_config.php
DataObject::add_extension('File', 'MyFile');

Template:

<% if AttachedFiles(MyImages) %>
   <ul>
      <% control AttachedFiles(MyImages) %>
      <li>$CMSThumbnail <u>$Description</u> <i>$DestLink</i></li>
      <% end_control %>
   </ul>
<% end_if %>

EDIT: Since this is a decorator in its own class, you can remove the Grouping and SiteTree DB relations and just add the new fields (Description and LinkTo)

Avatar
erwanpia

10 October 2008 at 8:56pm Community Member, 63 Posts

OK I suppose I'll have to reinstall silverstripe I must have messed up somewhere

anyway here goes my implementation of the nice rollover effect

takes images from HasManyFileManager, text description and link to a page of the site

requires jquery

         <% if AttachedFiles(MyImages) %>
         <div id="our_work">
         <ul >
            <% control AttachedFiles(MyImages) %>
            
               <li><a href="$DestLink">$SetWidth(200) $Description $Name $Title</a></li>
            <% end_control %>
         </ul>
         </div>
   <div id="worm"></div>
         <% end_if %>

//mysite/code/Page.php / function init of controller

      Requirements::themedCSS("slideTextGallery");
      Requirements::javascript("jsparty/javascript/jquery-1.2.6.min.js");
      Requirements::javascript("jsparty/slideTextGallery.js");
         

Avatar
erwanpia

10 October 2008 at 8:57pm Community Member, 63 Posts

plus file below