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.

All other Modules /

Discuss all other Modules here.

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

Image_Gallery Formating Date Value


Go to End
Reply


10 Posts   1794 Views

Avatar
jdi

Community Member, 34 Posts

1 November 2009 at 1:18am

Hey Crew,

I'm just tweaking the Image_Gallery a little and I'm gonna put what could be a very novice question out there:

I've added:

ImageGalleryAlbum.php:

static $db = array (
      'AlbumName' => 'Varchar(255)',
      'Description' => 'Text',
      'AlbumDate' => 'Date'
   );
   
   static $has_one = array (
      'CoverImage' => 'Image',
      'AlbumHead' => 'Image',
      'ImageGalleryPage' => 'ImageGalleryPage',
      'Folder' => 'Folder'
   );
   
   static $has_many = array (
      'GalleryItems' => 'ImageGalleryItem'
   );
   

   
   public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('AlbumName', _t('ImageGalleryAlbum.ALBUMTITLE','Album Title')),
         new TextareaField('Description', _t('ImageGalleryAlbum.DESCRIPTION','Description')),
         new CalendarDateField('AlbumDate', _t('ImageGalleryAlbum.ALBUMDATE','Date of Event')),
         new ImageField('CoverImage',_t('ImageGalleryAlbum.COVERIMAGE','Album Thumdnail')),
         new ImageField('AlbumHead',_t('ImageGalleryAlbum.ALBUMHEAD','Album Header'))
      );
   }

ImageGalleryPage.php:

public function EventDate()
   {
      return $this->CurrentAlbum()->AlbumDate;
   }

When I call this to the template ImageGalleryPage_album.ss as $EventDate it calls the date just fine e.g 2009-12-01

As soon as I format the date, $EventDate.Long for example, it no longer comes up with a value $EventDate doesn't print.

Can anyone tell me what I'm missing here?

Cheers.

Avatar
UncleCheese

Forum Moderator, 4094 Posts

1 November 2009 at 5:02am

Never modify the core components of a module, because now you'll have to reconcile your changes every time you upgrade. Silverstripe provides the tools for extending classes cleanly, so you should use them.

You should do all of this work in your /mysite/code, or theme directory, if you're using one.

MyImageGalleryPage.php

MyImageGalleryPage extends ImageGalleryPage
{
protected $albumClass = "MyImageGalleryAlbum";
}

MyImageGalleryAlbum.php

static $db = array (
'AlbumDate' => 'Date'
);

static $has_one = array (
'AlbumHead' => 'Image'
);

public function getCMSFields_forPopup()
{
$f = parent::getCMSFields_forPopup();
$f->push(new DatePickerField('AlbumDate','Album date'));
$f->push(new ImageField('AlbumHead','Album head'));
return $f;
}

/dev/build

Change your current imagegallery page to a MyImageGalleryPage type (note: MyImageGalleryPage is just an example name for whatever you want to call it)

Now you've insulated your changes by extending the class, and you've allowed the module to be, well, modular -- agnostic of the specific needs for your site.

If you want to create custom templates for anything, just create MyImageGalleryPage.ss in your theme dir /templates/Layout, or MyImageGalleryPage_album.ss, etc...

And for that date, there are a number of ways to format dates. All of this is in the SS documentation.

You can use $MyDateField.Nice, or $MyDateField.Format(m-d-Y), etc.. See /sapphire/core/fieldtypes/Date.php for all the functions available to you.

Avatar
jdi

Community Member, 34 Posts

4 November 2009 at 11:40pm

Edited: 04/11/2009 11:59pm

Thanks for the assist!

I've implemented everything that you've said. I do have another question for you if that is ok:

This is in ImageGalleryPage.php

$manager = new DataObjectManager(
            $this,
            'Albums',
            $this->albumClass,
            array('AlbumName' => _t('ImageGalleryAlbum.ALBUMNAME','Album Name'), 'Description' => _t('ImageGalleryAlbum.DESCRIPTION','Description')),
            'getCMSFields_forPopup',
            "ImageGalleryPageID = {$this->ID}"
         );

I can't wrap my head around trying to extend it in my version of MyImageGalleryPage.php? I would like to add two values into the array 'Photographer' and 'Venue'. Both of these I have in my version of MyImageGalleryAlbum.php.

Also:

I have my template files in themes/main/templates/Layout/. But all my browser outputs on the gallery page is:

<html>
   <head>
      <script id="injection_graph_func" src="chrome://skype_ff_toolbar_win/content/injection_graph_func.js" charset="utf-8">1Filtered chrome url chrome://skype_ff_toolbar_win/content/injection_graph_func.js</script>
   </head>
   <body>MyImageGalleryPage</body>
</html>

It completely ignores my templates structure inherited from Page.ss. Can anyone tell me why this is?

Avatar
UncleCheese

Forum Moderator, 4094 Posts

5 November 2009 at 2:07am

So why can't you just add Photographer and Venue to your MyImageGalleryAlbum $db array, and then add those fields to the getCMSFields_forPopup function, like you did with AlbumDate and AlbumHead?

Avatar
jdi

Community Member, 34 Posts

5 November 2009 at 6:18am

The intention of this is to have the Photographer and Venue values displayed listed on the "Albums" tab, before the popup is brought up. Or am I thinking about this wrong?

Avatar
jdi

Community Member, 34 Posts

7 November 2009 at 4:14am

Hey Guys,

Ive worked my way through the solution above. I have both MyImageGalleryPage.php and MyImageGalleryAlbum.php working. I have solved my issue with the templates by making sure to add the constructor class to the MyImageGalleryPage.php structure.

Unfortunately after all of this it still doesn't solve my initial problem!

$AlbumDate doesnt call to the MyImageGalleryPage_album.ss template, nor do any of the other values that Ive managed to add to my new code files. They all work fine in the back end and CMS, this is great, but theyre no good to me if I cant call them to the template...?

Any help would be so much appreciated :D

Cheers.

Avatar
UncleCheese

Forum Moderator, 4094 Posts

7 November 2009 at 4:42am

Post your template?

Avatar
jdi

Community Member, 34 Posts

10 November 2009 at 7:05am

Edited: 10/11/2009 7:18am

Hey,

Sorry about the late reply here I didn't have a netz while I was in Venice :S

MyImageGalleryPage.php:

<?php
/**
* Defines the MyImagegalleryPage page type
*/

class MyImageGalleryPage extends ImageGalleryPage {

   protected $albumClass = "MyImagegalleryAlbum";

   public function PostPhotographer()
   {
      return $this->CurrentAlbum()->Photographer;
   }
   
      public function PostVenue()
   {
      return $this->CurrentAlbum()->Venue;
   }
   
}

class MyImageGalleryPage_Controller extends Page_Controller {

   public function init()
   {

      parent::init();
      Requirements::css('image_gallery/css/ImageGallery.css');
   }
   
   
   protected function adjacentAlbum($dir)
{
$t = $dir == "next" ? ">" : "<";
$sort = $dir == "next" ? "ASC" : "DESC";
return DataObject::get_one(
$this->albumClass,
"ImageGalleryPageID = {$this->ID} AND SortOrder $t {$this->CurrentAlbum()->SortOrder}",
false,
"SortOrder $sort"
);
}   

public function NextAlbum()
   {
      return $this->adjacentAlbum("next");
   }

   public function PrevAlbum()
   {
      return $this->adjacentAlbum("prev");
   }
   
}
?>

MyImageGalleryAlbum.php:

<?php
/**
* Defines the MyImageGalleryAlbum page type
*/

class MyImageGalleryAlbum extends ImageGalleryAlbum {
static $db = array (
      'AlbumDate' => 'Date',
      'Photographer' => 'Text',
      'Venue' => 'Text'
   );

   static $has_one = array (
      'EventHeader' => 'Image'
   );

   public function getCMSFields_forPopup()   {
   
      $f = parent::getCMSFields_forPopup();
      $f->push(new CalendarDateField('AlbumDate','Date of Event'));
      $f->push(new TextField('Photographer','Name of Photographer'));
      $f->push(new TextField('Venue','Event Venue'));
      $f->push(new ImageField('EventHeader','Event Header', null, null, null, 'assets/Uploads/'));
      
      
      return $f;
   }

}

?>

themes/main/templates/Layout/MyImageGalleryPage_album.ss:

$EventHeader
<div id="Content" class="typography">
<div id="ContentPad">
   <% if Albums %>
   <% end_if %>
      <h5>$AlbumTitle<span class="album-info"> by $PostPhotographer</span></h5>
      <p class="articleDate"><strong>Venue:</strong> $PostVenue</p>
<p class="articleDate">$AlbumDate.Long</p>
      $GalleryLayout
      <div class="album-nav">
         <ul>
         <% if PrevAlbum %>
            <% control PrevAlbum %>
               <li class="prev">
                  <div class="album-nav-img"><a href="$Link" title="<% sprintf(_t('GOTOALBUM','Go to the %s album'),$AlbumName) %>">$CoverImage.SetWidth(50)</a></div>
                  <div class="album-nav-desc">
                     <h4><% _t('PREVIOUSALBUM','Previous Album') %>:</h4>                     
                     <a href="$Link" class="album-link">$AlbumName</a>
                  </div>
               </li>
            <% end_control %>
         <% end_if %>
         <% if NextAlbum %>
            <% control NextAlbum %>
               <li class="next">
                  <div class="album-nav-img"><a href="$Link" title="<% sprintf(_t('GOTOALBUM','Go to the %s album'),$AlbumName) %>">$CoverImage.SetWidth(50)</a></div>
                  <div class="album-nav-desc">
                     <h4><% _t('NEXTALBUM','Next Album') %>:</h4>
                     <a href="$Link" class="album-link">$AlbumName</a>
                  </div>
               </li>
            <% end_control %>
         <% end_if %>
         </ul>
      </div>
      <% if GalleryItems.MoreThanOnePage %>
         <ul id="pagination-imagegallery">      
            <% if GalleryItems.NotFirstPage %>
               <li class="previous"><a title="<% _t('VIEWPREVIOUSPAGE','View the previous page') %>" href="$GalleryItems.PrevLink">&laquo;<% _t('PREVIOUS','Previous') %></a></li>            
            <% else %>   
               <li class="previous-off">&laquo;<% _t('PREVIOUS','Previous') %></li>
            <% end_if %>
            <% control GalleryItems.Pages %>
               <% if CurrentBool %>
                  <li class="active">$PageNum</li>
               <% else %>
                  <li><a href="$Link" title="<% sprintf(_t('VIEWPAGENUMBER','View page number %s'),$PageNum) %>">$PageNum</a></li>            
               <% end_if %>
            <% end_control %>
            <% if GalleryItems.NotLastPage %>
               <li class="next"><a title="<% _t('VIEWNEXTPAGE', 'View the next page') %>" href="$GalleryItems.NextLink"><% _t('NEXT','Next') %> &raquo;</a></li>
            <% else %>
               <li class="next-off"><% _t('NEXT','Next') %> &raquo;</li>            
            <% end_if %>
         </ul>       
      <% end_if %>
   <% if Albums %>
   <% end_if %>
</div>
</div>
<div id="ContentFoot">
<div id="backNav"><a href="#">Return to Main Photos Page</a></div>
</div>

Sorry about the massive post.

What I have found is that when I call the value $AlbumDate in themes/main/templates/Includes/AlbumList.php (included in MyImageGalleryPage.ss) then the value is called to the page perfectly. But when I try to do it to MyImageGalleryPage_album.ss it won't work unless I create the function in the relative MyImageGalleryPage.php file as shown above.

EDIT: I've just stumbled across this right now. If I used

<% control Albums %><% end_control %>

I can call the values correctly. But of course it calls all the values for all of the albums...

Go to Top