Hide list item after date

Permalink
Hi,

I like to hide a list item "after" a specific date (using attribute: date).

My pagelist template:
<?php  
   defined('C5_EXECUTE') or die("Access Denied.");
   $textHelper = Loader::helper("text");
   $imgHelper = Loader::Helper('image');
   if (count($cArray) > 0) { ?>
   <?php  
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      $target = $cobj->getAttribute('nav_target');
      $title = $cobj->getCollectionName();
      $c = $cobj->getCurrentPage();
      $date = $cobj->getCollectionDatePublic('j M Y'); 
   ?>
   <div class="block">
      <h1><?php  echo $title?></h1>


I found this code, but don't know how to implement it. And if I am right it hides on the day itself, I like to hide it after the date.
getCollectionName();
          if($date->getSystemDateTime($cobj->
    getCollectionAttributeValue('datum'))  >  
    $date->getLocalDateTime('now')) {
    ?>


Hope someone can help.

Thanks,
Maarten

 
studio4graphics replied on at Permalink Reply
Bump.

Does someone have any idea. I looks like it would pretty easy for a programmer to implement this, but I'm no programmer so could be wrong :-).
mnakalay replied on at Permalink Reply
mnakalay
Hello,
That code does (no hiding yet) things when the date saved in the page attribute datum is greater than now which means as long as datum is in the future.

That's not what you want to do. You want something to happen when datum becomes the past.

What's more, because the dates are not formatted, the difference between both dates will be compared at the seconds level. One second difference and things start happening.

You want your function to hide the link the day after datum. You need to format both dates to keep only year, month, and day.

Finally you need to skip the link if the date is actually past. We use the command 'continue' to tell the 'for' loop you have at the beginning to skip a turn.

Also, since you are dealing with dates, you need to load the date helper.

So first you need to modify your second bit of code to look like this:
if($date->getSystemDateTime($cobj->getCollectionAttributeValue('datum'), 'Y-m-d') < $date->getLocalDateTime('now', 'Y-m-d')) {
  continue;
}


then at the top of your long bit of code, where you have:
$textHelper = Loader::helper("text");
$imgHelper = Loader::Helper('image');


add the line:
$date = Loader::helper('date');


That's the call to the date helper.

So now you have
$textHelper = Loader::helper("text");
$imgHelper = Loader::Helper('image');
$date = Loader::helper('date');


Now, final step, take the bit of code you modified earlier (the if) and paste it in your code right after

$cobj = $cArray[$i];


so now you have
$cobj = $cArray[$i];
if($date->getSystemDateTime($cobj->getCollectionAttributeValue('datum'), 'Y-m-d') < $date->getLocalDateTime('now', 'Y-m-d')) {
  continue;
}


And you should be good to go :)
studio4graphics replied on at Permalink Reply
Hi,

Thanks for your fast reply. I put in the code but it gives me an error:
Fatal error: Call to a member function getSystemDateTime() on a non-object in /home/xxx/domains/xxx.nl/public_html/voorbeeld/c5-2/blocks/page_list_plus/templates/blog_index_thumbnail2-date.php on line 12


<?php  
   defined('C5_EXECUTE') or die("Access Denied.");
   $textHelper = Loader::helper("text");
   $imgHelper = Loader::Helper('image');
   $date = Loader::helper('date');
   if (count($cArray) > 0) { ?>
   <?php  
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      if($date->getSystemDateTime($cobj->getCollectionAttributeValue('datum'), 'Y-m-d') < $date->getLocalDateTime('now', 'Y-m-d')) {
        continue;
      }
      $target = $cobj->getAttribute('nav_target');
      $title = $cobj->getCollectionName();
      $c = $cobj->getCurrentPage();


Any idea why this is not working?
mnakalay replied on at Permalink Reply
mnakalay
My guess would be that your $cobj->getCollectionAttributeValue('datum') is the source of the problem.

To test that, try replacing that with a simple date like '2013-05-12' and see if it works.

If I'm right then it means you attribute datum might be empty or not set or the date is not formatted correctly.