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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Showing this weeks birthdays


Reply


8 Posts   904 Views

Avatar
Wocker

Community Member, 6 Posts

4 November 2010 at 9:24am

As I am completely new to Silverstripe, I still have some problems understanding some of it's structure and functions. But still I am able to use the basics to create my site.

But now I want to show a listing of all the birthdays of this week... or better, the next 7 days or maybe two weeks on the homepage of my site.
I am thinking about creating a page with all the people profiles, that include the birthday. Than I would like to use a control to filter out the birthdays within, let's say, the next 7 days.

I would think that I would need to create a dataobject that contains all the persons.
But I am not sure how to create that. I can't seem to find very clear instructions on it.

Further more I would think I should create a <% control persons %> and than have some ability to filter them. But how?

I hope someone here on the forum can give me some more insight or guide me to some clear instructions on how to go about this task. Any code examples with explanation would be very welcome because I am still starting to learn Silverstripe.

Avatar
simon_w

Forum Moderator, 474 Posts

4 November 2010 at 10:14am

Edited: 04/11/2010 10:14am

All you'll need is a method in your controller that returns a DataObject::get() call. Something like:

public function UpcomingBirthdays() {
   return DataObject::get('Person', '"Birthday" >= CURDATE() AND "Birthday" < CURDATE() + INTERVAL 7 DAY');
}

Here, it's important that it's DAY, not DAYS.

Then, in your template, you can use <% control UpcomingBirthdays %>

Avatar
Ryan M.

Community Member, 309 Posts

4 November 2010 at 1:11pm

How to create a Person dataobject:

class Person extends DataObject {

static $db = array('FullName' => 'Varchar', 'Birthdate' => 'Date');

static $has_one = array();

function getCMSFields_forPopup() {
return new FieldSet(
new TextField('FullName', 'Full Name'),
new DateField('Birthdate')
);
}

}

Replace/add the fields with the ones you want.

Avatar
Wocker

Community Member, 6 Posts

5 November 2010 at 10:27am

Thank you so much for the help. I've managed to create a personDetails.php and a teamPage.php
And ithat works like a charm, although I still have to implement the birthday selector on the homepage... but now I am hungry for more :)

I've found some code to edit the DatePicker

$dateField = new DateField('Birthday');
$dateField->setConfig('showcalendar', true);
$dateField->setConfig('showdropdown', true);
$dateField->setConfig('dateformat', 'dd/MM/YYYY');

But as it is a birthday picker, it's quite a pain with the current way of selecting a date that's in the 1970's for example.
Is there a way that I can make it a bit easier/faster to select a year in the past?

Avatar
Wocker

Community Member, 6 Posts

5 November 2010 at 12:44pm

Unfortunately the method for getting the upcoming birthdays doesn't work. Because a birthday date is a date in the past ie 8-11-1977 for November 8th 1977. But the suggested method only filters for dates in the near future.
Isn't it possible to do something like: WHERE Month >= currentMonth and Day >= currentDay + INTERVAL 7 DAY

Any ideas?

Avatar
Wocker

Community Member, 6 Posts

6 November 2010 at 10:56pm

I can't seem to find the code to compare the current month and current day with a month and day stored in a birthdate
if I try to get the CURDATE().m (for the current month) it doesn't work.

After reading http://api.silverstripe.org/2.4/sapphire/model/Date.html
I thought I could do something like:

return DataObject::get('Person', '"Birthday".DayOfMonth ( ) >= CURDATE().DayOfMonth ( ) AND "Birthday".DayOfMonth ( ) <= CURDATE().DayOfMonth ( ) + INTERVAL 7 DAY ');

This doesn't work, but if it did, I would still have to check for the month. Including the state in which the current month December and the following month (with the next birthday) is in January.

Than I was thinking, this would probably be a lot easier if I could use the proposed solution by simon_w. But to be able to use that I should set the birthday in the future. The year in particular. Because if I could set 28-11-1977 to 28-11-2010 And then check it, this would be no problem. But how to do that within the get statement? I can't imagine I am the first person to try to make a birthday list in Silverstripe so I would assume others would have already cracked this problem. Otherwise this will be a nice post for others in the future if we can come up with the sollution :)

Avatar
Wocker

Community Member, 6 Posts

9 November 2010 at 6:44am

Just a bump, because I am certain someone on this forum must be able to help me tackle this problem :)

Avatar
simon_w

Forum Moderator, 474 Posts

9 November 2010 at 10:32pm

Yeah, I'm really slack at replying.

Your try wouldn't work because the Date object is strictly in PHP, and the DO::get() call is handling the WHERE clause in the underlying SQL. There's plans for SilverStripe 3.0 to get it so it'll all be done in the PHP layer, but that's a while off yet.

Something that may work is:

return DataObject::get('Person', '"Birthday" - INTERVAL YEAR("Birthday") YEAR >= CURDATE() - INTERVAL YEAR(CURDATE()) YEAR AND "Birthday" - INTERVAL YEAR("Birthday") YEAR < CURDATE() + INTERVAL 7 DAY - INTERVAL YEAR(CURDATE()) YEAR');

As for your DatePicker problem, currently it is not possible to have the year dropdown. I have just got to tidy up the modification I made and will submit it as a patch for inclusion in a later release.