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

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 picarica
      so i am trying to put CustomHooksForVariations.module, a custom module, i am placing it into site/modules direcotry yet my modules page in admin panel gives me errors
      so this is the screen show when i refresh modules, i dont know why the shole hook is written on top of the page :||

      and this next image is when i try to install it, i saw that it is not defiuned modules.php but it shouldnt need to be ?, any ways i dont want to edit site's core just to make one moulde work there has to be a way

    • By Andi
      Continuing my journey into PW hooks, I'm trying to find a way to retrieve all images from a page that explicitly *do not* have a certain tag (or tags) attached to them.
      Found this post from 2015
      But I'm wondering if there's a more elegant way to go about this.
      Let's say I have a multi-image field called "images_header" and instead of
      $page->images_header->findTag('mytag'); I would like to do this:
      $page->images_header->excludeTag('mytag'); So I'd be able to do
      // find images that don't have the tag "mytag" $images = $page->images_header->excludeTag('mytag'); // check if there's any images if (count($images)>0) { // do something.. } Would this be possible by hooking into Pagefiles somehow?
      There's this bit in /wire/core/Pagefiles.php Line 626 that I'd basically just need to reverse (or at least in my mind 😄 )
      public function findTag($tag) { $items = $this->makeNew(); foreach($this as $pagefile) { if($pagefile->hasTag($tag)) $items->add($pagefile); } return $items; } Any ideas on how this could be done in a graceful manner?
      Thanks in advance!
    • By Andi
      Getting a little deeper into the ProcessWire state-of-mind here. I seriously think I wouldn't have come back to webdev if it wasn't for this wonderful little gem of a CMS.
      I have an "Options" field added to all users on a site. If the user has anything other then "default" selected, I would like to show a permanent message in the admin like the one in the screenshot, only so that the user can't close it. As a friendly reminder that he changed that option from default to something crazy 🙂
      I've read up on how to send messages to users, but where would I hook into to make this show up all the time in the backend?
      https://processwire.com/api/ref/wire/message/
      Thanks in advance!

    • By celfred
      Hello,
       
      Here's what I'm trying to achieve : I have a textarea field that is frontend editable on any page I want to. If a user with a specific role updates it, I want to tick a checkbox field on the parent page where the textarea resides. So I tried to hook as follows in my ready.php file :

        $this->addHookBefore('Fieldtype::savePageField', function(HookEvent $event) {       $page = $event->argument[0];       $field = $event->argument[1];       if($this->user->hasRole('teacher')) {      $page->alertBox = 1;       } // DEBUG HERE (bd($page),  l($field)... ????   }); Of course (!) it doesn't work (yet !) but the thing is I have no idea how to debug this since my bd() never triggers. I've tried the 'Event interceptor' of Tracy debugger which helped me setting up my hook. I guess my $page and $field are correct... but how could I go any further ?
      The road is long to become a dev (when you're not one 🙂 )...
      For further information in case someone wonders : I'd like to set up a textarea that is modified by a user (having a 'player' role, ie 'student'). When front-end modified, I wish it would automatically alert me (the teacher) by ticking a box. So when I log in with my teacher role, I get a list of all textareas I have to read over. When I read/correct/update them (front-end), I would like my hook to automatically untick the box to remove it from my list. In other words, the 'textarea' status should go back and forth according to who modified it last. For the time being, I have managed to make it work with a checkbox that the user has to manually tick/untick, but I've noticed many kids forget to tick the box so they edit their text and I don't get a notice 😞
      Thanks if anyone takes time to try and help me on that one !
    • By celfred
      Hello,
      I'm struggling with hooks to try and do this : adding user page to a page field when visiting a particular page on my website. In fact, I want to have a list of connected users on a particular page (called meeting-hall).
      I've tried all kinds of things (which I don't really understand, sorry) but the latest is this :

        $wire->addHookBefore('Page::render', function($e) {     $page = $e->arguments(0);     if ($page->is("name=meeting-hall")) {       $page->connected->add($user); // connected is a Page field       $page->save();     } else {       $meeting = wire("pages")->get("name=meeting-hall");       $meeting->connected->remove($user);       $meeting->save();     }   }); I put this in my _init.php file but nothing works...
      The best I managed was updating my 'connected' field when the user loads the 'meeting hall' page by having this on the page template :

          if ($user->isLoggedin()) {       $page->connected->add($user);       $page->of(false);       $page->save();       $page->of(true);     } But I wanted to remove the user when he or she leaves the page... hence my thought about using hooks...
      If you can give me a hint, I'd greatly appreciate !
×
×
  • Create New...