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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

IF Childrens LastEdited Date is over 10 days


Go to End


9 Posts   2418 Views

Avatar
web2works

Community Member, 50 Posts

19 September 2010 at 4:04am

Hi I would like to have an if statement in the template that check the Childrens LastEdit date is less than 10 days ago to display a latest graphic. I dont think I will be able to do this inside of the template but how would I get the children in the controller.

Thanks Ben

Avatar
Willr

Forum Moderator, 5523 Posts

19 September 2010 at 12:10pm

I dont think I will be able to do this inside of the template but how would I get the children in the controller.

In the controller you can do $this->Children(); I think you're correct in thinking it doesn't work in the template. Doesn't support > < for one.

Avatar
web2works

Community Member, 50 Posts

19 September 2010 at 8:29pm

Thanks I managed to intercept the children in the controller but what am I doing wrong to access the LastEdited? Is there a way in Silverstripe to print all of the variables being sent to th page?

Thanks again Ben

public function Websites(){

$websites = $this->Children();

if(!$websites) return null;

$date = $websites->LastEdited();

$websites->sort('LastEdited', 'DESC');

return $websites;
}

Avatar
dhensby

Community Member, 253 Posts

19 September 2010 at 9:46pm

Edited: 19/09/2010 9:51pm

$this->Children will return a ComponentSet (a bit like a DataObjectSet). So accessing $websites->LastEdited won't work as your not accessing the last date of a particular child.

If you want to know if the page is recently updated i would add a function to the Page model like so:

function getIsRecent() {
    return strtotime($this->LastEdited) > strtotime('-10 days');
}

This then allows you to do:

<% control Children %>
<% if IsRecent %>
NEW POST
<% end_if %>
<% end_control %>

Also, to pull out children that are sorted by the last edited date, do this:

function Websites() {
    $tenDaysAgo = strtotime('-10 days');
    return $this->Children(null,'LastEdited ASC');
}

Avatar
web2works

Community Member, 50 Posts

19 September 2010 at 10:04pm

Hi thanks for the help. Although I doesnt seem to be getting to the getIsRecent function.

This is what I have in my template:
<% control Children %>
<li>
<a href="$Link" title="Go to: $Title.XML Portfolio Webpage" class="webbrowseImage">
<% control Images %>
$Attachment.SetWidth(407)
<% end_control %></a>
<h3><a href="$Link" title="Learn more about: $Title.XML">$Title.XML</a></h3>
<p>$Content.LimitWordCountXML(15)<a href="$Link" title="Read more: $Title.XML">More</a></p>
<a href="$Link" title="Go to: $Title.XML Portfolio Webpage" class="button">More</a>

<% if IsRecent %><div class="recentsite"></div><% end_if %>
</li>
<% end_control %>

And in the PHP page
<?php
class WebsitesBrowse extends Page {
static $db = array();

static $default_child = 'WebsitePage';

static $allowed_children = array(
'WebsitePage' => 'WebsitePage'
);

static $has_many = array(
'WebsitePage' => 'WebsitePage'
);

static $singular_name = 'Website Overview';
static $plural_name = 'Websites Overview';

static $icon = "cms/images/treeicons/web";

public function getCMSFields(){
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Content.Main", "Content");
return $fields;
}

}

class WebsitesBrowse_Controller extends Page_Controller {

function getIsRecent() {
return strtotime($this->LastEdited) > strtotime('-10 days');
}

}

Avatar
dhensby

Community Member, 253 Posts

19 September 2010 at 10:46pm

You need to put the getIsRecent function in the model of the child object (WebsitePage class) not the controller (WebsitePage_Controller class).

Avatar
web2works

Community Member, 50 Posts

19 September 2010 at 11:18pm

Edited: 19/09/2010 11:50pm

Getting somewhere now it returns true for every child even after I change the LastEdit in the sitetree of the database.

I ran the following die to test the variables
die('LastEdit=' . strtotime($this->LastEdited) . '<br />-10days=' . strtotime('-10 days'));

LastEdit=1284830332
-10days=1284032978

Database sitetree.LastEdited = 2010-12-08 08:18:45

Sorry about the dumb questions I am starting to get more adventurous with sivlerstripe
Thanks BEn

Avatar
web2works

Community Member, 50 Posts

23 September 2010 at 4:24am

Can anybody help with this I am really stuck at the moment.
Thanks Ben

Go to Top