Jump to:

23575 Posts in 19402 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » IF Childrens LastEdited Date is over 10 days

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

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

Page: 1 2
Go to End
Author Topic: 1578 Views
  • web2works
    Avatar
    Community Member
    50 Posts

    IF Childrens LastEdited Date is over 10 days Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

    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.

  • web2works
    Avatar
    Community Member
    50 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

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

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

    $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');
    }

  • web2works
    Avatar
    Community Member
    50 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

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

    }

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

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

  • web2works
    Avatar
    Community Member
    50 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

    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

  • web2works
    Avatar
    Community Member
    50 Posts

    Re: IF Childrens LastEdited Date is over 10 days Link to this post

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

    1578 Views
Page: 1 2
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.