21299 Posts in 5735 Topics by 2603 members
| Go to End | Next > | |
| Author | Topic: | 1157 Views |
-
IF Childrens LastEdited Date is over 10 days

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
-
Re: IF Childrens LastEdited Date is over 10 days

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.
-
Re: IF Childrens LastEdited Date is over 10 days

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;
} -
Re: IF Childrens LastEdited Date is over 10 days

19 September 2010 at 9:46pm Last edited: 19 September 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');
} -
Re: IF Childrens LastEdited Date is over 10 days

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');
}}
-
Re: IF Childrens LastEdited Date is over 10 days

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).
-
Re: IF Childrens LastEdited Date is over 10 days

19 September 2010 at 11:18pm Last edited: 19 September 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=1284032978Database sitetree.LastEdited = 2010-12-08 08:18:45
Sorry about the dumb questions I am starting to get more adventurous with sivlerstripe
Thanks BEn -
Re: IF Childrens LastEdited Date is over 10 days

23 September 2010 at 4:24am
Can anybody help with this I am really stuck at the moment.
Thanks Ben
| 1157 Views | ||
| Go to Top | Next > |



