Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » HasManyFileManager: New CMS module/extension. Testers/review needed

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

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

Page: 1 2 3 4 5 6 7 8
Go to End
Author Topic: 104134 Views
  • erwanpia
    Avatar
    Community Member
    63 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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();
             }
          
       }

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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 these instuctions how to build your own custom file decorator class.

  • erwanpia
    Avatar
    Community Member
    63 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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 ;
       }

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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.

  • erwanpia
    Avatar
    Community Member
    63 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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/

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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)

  • erwanpia
    Avatar
    Community Member
    63 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    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");
             

  • erwanpia
    Avatar
    Community Member
    63 Posts
    104134 Views
Page: 1 2 3 4 5 6 7 8
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.