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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

Get current ID in control


Go to End


8 Posts   3570 Views

Avatar
quanto

Community Member, 91 Posts

27 July 2012 at 11:50pm

Edited: 27/07/2012 11:50pm

Maybe a stupid question which has been asked lots of times on the forum, but I can't find a solution.

In my template ProductOverzicht.ss I have the following code:

      
<% if Children %>
      <% control GetChildren %>
      <section class="blok">
        <article> 
          
          <% if Top.isNieuw %><div class="nieuw">nieuw</div><% else %>nope $ID<% end_if %>
          <div class="links"> 
          <% if MyFotos %><% control MyFotos %><% control Plaatje %><% if First %>$setWidth(188)<% end_if %><% end_control %><% end_control %><% else %>
          <img src="/themes/agrideals/images/no-image.png" alt="no image"/><% end_if %>
          </div>  
          <div class="rechts"><h2>$Title</h2>  
            $Content.FirstParagraph
          </div>
          <div class="bekijk"><a href="$Link">Bekijk product</a></div>
        </article>
      </section> 
      <% end_control %>
      <% end_if %>

I want to find out how I can get the current ID of the Child of Top.isNieuw

The function isNieuw:

   public function isNieuw(){    
    $aantalweken = 2;
    $tijd = time() - ((7 * 24 * 60 * 60) * $aantalweken);
    $obj = DataObject::get_one('Product', 'ID='.$this->ID.' AND UNIX_TIMESTAMP(Created) > '.$tijd);

    Debug::show($obj);
    if($obj) return false;
    else return true;  
    
  }

Currently I get the ID of the parent, not the child...

Avatar
Hattori

Community Member, 20 Posts

28 July 2012 at 6:42am

function isNieuw(){
...
...
...
return $obj; //replace if($obj) return false;else return true;  by this
}

in template:

<% control Top.isNieuw %>
$ID
<%end_control %>

Avatar
quanto

Community Member, 91 Posts

28 July 2012 at 11:43pm

Edited: 28/07/2012 11:44pm

Not exactly what i ment.

I'm trying to get the ID from the subpages wich are called by the control

Where do i have to put the isNieuw function? Just in Page.php?

<% control GetChildren %>
      
      <% control Top.isNieuw %>
            //gives the id from the main page, not the child.. how do I get the child ID?

      <% end_control %>
<% end_control %>

Avatar
Hattori

Community Member, 20 Posts

29 July 2012 at 5:15am

Edited: 29/07/2012 5:16am

Not sure what you want to do, it's better if have an image shows your site structure with descriptions.

Avatar
Willr

Forum Moderator, 5523 Posts

29 July 2012 at 8:17pm

how do I get the child ID?

In SS3.0 you can use $Up to move up the template scope. In 2.* you can't do that, you'll have to open and end multiple controls. Though by the sounds of it isNieuw returns a boolean so you should use if rather than control and this will not alter the template scope so you don't need to change your code.

<% control GetChildren %>

<% if Top.isNieuw %>
$ID
<% end_if %>
<% end_control %>
<% end_control %>

Avatar
quanto

Community Member, 91 Posts

29 July 2012 at 10:49pm

Edited: 29/07/2012 10:50pm

I recreated the script on some points, but still remaining the same problem.

Overview of the code:

ProductOverzicht.php

class ProductOverzicht_Controller extends Page_Controller {
...
  public function GetChildren($Limit = 5){
    return DataObject::get("Product", "ParentID = $this->ID", Null, Null, $Limit);
  }
      
  public function isNieuw(){    
    $aantalweken = 2;
    $tijd = time() - ((7 * 24 * 60 * 60) * $aantalweken);
    
    if (date('U',strtotime($this->Created)) > $tijd) return true;
    else return false;      
  }
...
}

ProductOverzicht.ss

...
<% if Children %>
  <% control GetChildren %>
      <% if isNieuw %>
           //do something
      <% else %>
           //do something else
      <% end_if %>
  <% end_control %>
<% end_if %>
...

The above code (isNieuw) doesn't put out anything. When I put in Top.isNieuw it works, but it gets the Created time of the top page in $this->Created, not of the current child in the control. I need the current time of that specific child into my function.

Avatar
Willr

Forum Moderator, 5523 Posts

30 July 2012 at 5:54pm

The above code (isNieuw) doesn't put out anything. When I put in Top.isNieuw it works, but it gets the Created time of the top page in $this->Created, not of the current child in the control. I need the current time of that specific child into my function.

So you need to use the Created date of the child? Then why not move the isNiew function to your actual child class (Product). Make sure you have this in your model record (Product) and not your controller.

Avatar
quanto

Community Member, 91 Posts

30 July 2012 at 7:08pm

Make sure you have this in your model record (Product) and not your controller.

No way! That easy? Damn I spend days to fix it. Thank you! You saved my day!