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 dragan
      $this->addHookBefore('InputfieldTextarea::render', function($event) { $field = $event->object; if($field->name == 'body_offer') { $del = ''; foreach($this->wire->pages->get(11229)->textblocks as $item) { $field->entityEncodeText = false; $body = str_replace(PHP_EOL, '', $item->body); $body = addslashes($body); $title = $item->title; $field->description .= $del . "<a href=\"#\" class=\"ckesnippet\" data-snippet=\"$body\">$title</a>"; $del = ' | '; } } $js = wire('config')->urls->templates . 'scripts/ckesnippets.js'; $event->return = str_replace("</html>", "\n<script type='text/javascript' src='$js'></script>\n</html>", $event->return); }); I'm trying to add custom text-blocks from repeater fields into CKEditor. I've stolen the whole idea (and code) from @bernhard

      Problem is: The links are actually being rendered, but the Javascript is not loaded, i.e. the $event->return line doesn't seem to work. If I inspect the HTML, my additional script-tag is nowhere to be seen. I don't get any errors either. Running latest PW dev + PHP 7.1.19.
    • By cosmicsafari
      Hi all,
      I have noticed that LazyCorn seems to run maybe once or twice and then just stops.
      Has anybody else encountered this, I have the following within 2 different modules.
      public function init(){ $this->addHookAfter('ProcessModule::executeEdit', $this, 'configurableButtons'); wire()->addHook('LazyCron::every15Minutes', $this, 'autoGenerate'); } public function init(){ wire('forms')->addHookAfter('ProcessModule::executeEdit', $this, 'configurableButtons'); wire()->addHook('LazyCron::everyHour', $this, 'getExport'); } Does having multiple LazyCrons setup cause any issues?
    • By cosmicsafari
      Hi all,
      I am using the formSubmitSuccess form builder hook to send a copy of the form submission to a third party.
      However I couldn't figure out how to get the submissions form type at this point, the reason being is that I only want to send the data onto the third party for certain form types.
      I don't have the submission details being passed into the hook, wasn't sure how to achieve this so at the moment I have it doing a DB query to pull the latest submission details for that form type so I'm rather stumped about how I can go about achieving this.
      Hope thats makes sense.
    • By joe_g
      Hi there,
      I'm trying to limit a specific user can only add pages as a child (under) a page created by themselves.  
      The discussion in this thread gets close, it's a working solution on how to only allow editing on pages you created yourself. I'm using this and it works well. But I'm not sure how to modify this for my purposes. 
       
      I'm suspecting I need to do to something like
      $this->addHookBefore("Page::added", $this, 'added');
      then in "added" check if parent is created by the current user. But I'm a bit lost on how exactly to do this.
      thank you! /J
       
    • By suntrop
      Sometimes I need pages in the site tree that are basically just a reference to another page – to redirect to the original page or display the same content under the new page url.
      I was surprised I couldn't find anything like that in the forum or as a module already. However, there is a new module by @Robin S but not exactly what I need.
      Example
      Home News Press Newsroom Media Services About Newsroom (virtual page for /news/newsroom/) Contact Example I: The page /about/newsroom/ should act as /news/newsroom/ – so, wehen I loop all pages for a sitemap both pages should have the URL /news/newsroom/.
      Example II: Another example (not often used) would be tu display the exact same content on /about/newsroom/ – although the page itself has no content on its own.
      Those examples don't have much in common, but they are just virtual or fake pages in the site tree.
      While the second one should be an easy task with wireRenderFile(), the first example isn't quite obvious to me. I think I have to use hooks and I have no idea where to start. I really appreciate you guys come up with a solution for everything, but I need to learn this myself  So just some hints to start would be great!
      Do I put the code into a module or some of the _init.php, ready.php etc. files? Or do I have to use a template file (e.g. virtual-page.php)? When I look into Captain Hook  – how do I find the appropriate point to hook into? (/wire/core/ Page.php and loaded()?) Do I need to create/manipulate each field separately (path, title, summary)? Sorry for the long read