Jump to content
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 2

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VeiJari
      Hello forum!
      I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 
      Now here's the code for the hook:
      wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
        $page = $hook->arguments(0);
        $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
        wire()->log->message($page->name);
        $page->setAndSave('name', $newUrl);
      });
      I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 
      Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php
      and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
       
      What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.
      Thanks for the help!
    • By VeiJari
      Hello forum! I'm trying to learn how to time up my functions by using lazycron but I can't get it fired up. 
      Here's my code in site/init.php:
      function setEventToRepeat(HookEvent $e) { $wire->log->message("kutsutaan cron"); $events = $wire->pages->find('template=tapahtuma'); die(var_dump($events)); $wire->log->message('Kaikki tapahtumat haettu:' .$events); }   $wire->addHook('LazyCron::every30Seconds', null, 'setEventToRepeat'); I get nothing in the logs and can't echo or dump anything
      Thanks for the help!
    • By louisstephens
      So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?
      So after rereading my post, I believe it is a bit vague so I will try to explain more. 
      The Goal:
      Create a page with a template "one". Once the page is created/saved => create a new child page with the template of "two" If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page) The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.
    • By quickjeff
      Hi Guys,
      I am running a page template that contains a filter form that allows users to display results based on number of  comments. 
      Here is what I am thinking about doing: 
      -add new field called total_comments 
      -somehow populate this field every time a new comment is added or removed by admin with count($page->comments)
      -use form filter to display results that the selector form has found.
      Example: $selector = “total_comments>=$q”; 
       
      My question to everyone: Is this a good approach? If so how do I get another field to update based on the comment module adding or removing a comment? 
×
×
  • Create New...