maxf5

[SOLVED] close events with startdate and enddate

Recommended Posts

Hey guys,

i have some sort of events/campaigns which have a lifespan. To deactivate them i made a checkbox-field called closed and i have to deactivate them manually.
I would like to put two fields in the template, startdate and enddate, which activate/deactivate that checkbox field automatically.

Dou you have any idea how to bring that on the run?

Share this post


Link to post
Share on other sites

Install LazyCron (core module) and add a hook for a certain time period, say LazyCron::everyHour. Something like this should work:

wire()->addHookAfter('LazyCron::everyHour', function (HookEvent $e) {
    $closables = $e->pages->find('closed=0, enddate>now');
    foreach ($closables as $p) $p->setAndSave('closed', 1);
    
    $openables = $e->pages->find('closed=1, startdate>now, enddate<now');
    foreach ($openables as $p) $p->setAndSave('closed', 0);
});

 

  • Like 1

Share this post


Link to post
Share on other sites

hey @abdus

that's a great hint!

I installed LazyCron and made the /site/ready.php

<?php namespace ProcessWire;

  wire()->addHookAfter('LazyCron::every30Seconds', function (HookEvent $e) {
      $test = $e->pages->get('id=1');
      $test->setAndSave('footerhide',1);
      $test->setAndSave('meta_desc','123');
  });

.. for testing and nothing happens. In tracy debugger ($test->setAndSave('meta_desc','123');) it works fine..

Share this post


Link to post
Share on other sites

LazyCron hooks fire after PW finishes serving the request, so logging inside the hook will not work. Try setting random values to see if changes anything.

wire()->addHookAfter('LazyCron::every30Seconds', function (HookEvent $e) {
    $test = $e->pages->get(1);
    $test->setAndSave([
        'footerhide' => mt_rand(0, 1),
        'meta_desc' => '123' . time()
    ]);
});

If that doesnt work, try deleting site/assets/cache/LazyCron.cache

  • Thanks 1

Share this post


Link to post
Share on other sites

Ok, the Lazy.Cron.cache was guilty ;)

it's working fine now, thank you @abdus!

you just have to put: enddate<now for the closables ---  startdate<now, enddate>now for the openables

wire()->addHookAfter('LazyCron::every30Minutes', function (HookEvent $e) {

  $closables = $e->pages->find("closed=0, enddate<now");
  foreach ($closables as $p) $p->setAndSave('closed', 1);

  $openables = $e->pages->find("closed=1, startdate<now, enddate>now");
  foreach ($openables as $p) $p->setAndSave('closed', 0);

});

 

  • Like 1

Share this post


Link to post
Share on other sites
4 minutes ago, maxf5 said:

enddate<now for the closables ---  startdate<now, enddate>now for the openables

Yeah, it often takes me multiple tries to get date arithmetics right.

Share this post


Link to post
Share on other sites

know that feeling ;)

Maybe it isn't bad to add or modify:

  $openables = $e->pages->find("closed=1, enddate>now");
  foreach ($openables as $p) $p->setAndSave('closed', 0);

.. when the moderator don't fill in any startdate

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mustafa-Online
      I want to remove "Breadcrumbs" From Some Admin Pages .. for example (Modules/Profile) Pages.
      Aslo: How To Modify Page Headline .. Thanks
      .

    • By Mustafa-Online
      Hi Everyone, I have built until now two websites using ProcessWire & it's really powerful- the only thing that seems blurry to me is Hooks
      not as a concept, but i don't know which one to choose and when ??
      For example, i tried to change to login redirect URL :
      So why choosing "___loginSuccess" over "___afterLogin" ?
      There is a lack of tutorials related to this topic / and docs too 
    • By PawelGIX
      How can I override the url method to add an extra get parameter to each address? 
       
      Ex: About page url /about/   like this /about/?param=2 
    • By bmacnaughton
      I have a hook that creates a page for a subset of the pages on our site. It uses the saved page's name as part of the created page's name.
      The problem I am having is that my hook, attached to Pages::saved(), is being called even when the page save failed because of missing fields. Is there a way I can tell that the page save failed due to missing fields?
       
      Never mind - it does succeed; it just issues warnings about required fields.
       
    • By gebeer
      Hello,
      I noticed that the Pages::added hook gets called twice. PW 3.0.62.
      To test , add this to admin.php
      wire()->addHookAfter('Pages::added', function($event) { bardump('added'); // needs Tracy Debugger }); Can anyone confirm this?
      It gives me trouble when adding a hook that skips the page add step (for users), following Pete's concept. There will always be created 2 new pages which I need to avoid.
      Is it a feature or a bug?