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 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? 
    • By Ken Muldrew
      I'm trying to get a short routine to run once per day that will look at some pages and send a reminder email when that customer's subscription (yearly) is about to expire. When I run the code in a template then it works without issue, but inside my lazycron service routine, I get an "Error: Uncaught Error: Call to a member function get() on null" as if the database cannot be found. My autoload module is just the sample HelloWorld module included with ProcessWire, editted to perform this task. The whole of it is included below (I've stripped out the code that generates the email because it never gets past $pages->find):
       
      <?php namespace ProcessWire;
      /**
       * ProcessWire 'LazyCronLoad'  module
       *
       */
      class LazyCronLoad extends WireData implements Module {
          public static function getModuleInfo() {
              return array(
                  'title' => 'LazyCronLoad', 
                  'version' => 1, 
                  'summary' => 'Just loads a lazy cron callback.',
                  'singular' => true, 
                  'autoload' => true, 
                  );
          }
          public function init() {
              // initialize the hook in the AutoLoad module
              $this->addHook('LazyCron::everyDay', $this, 'myHook');
              
          }
          public function myHook(HookEvent $e) {
              // called once per day
              wire('log')->save('user_activities',' lazy cron service routine');
                  $transport_pages = $pages->find("template=aggregate-entry, aggregate_type.title='Transport'");
                  foreach ($transport_pages as $page) {
                      if (($page->purchase_date + 30325800 < time()) && ($page->purchase_date + 30412600 > time())) { // between 351 and 352 days
                          wire('log')->save('user_activities', $page->id . ' email reminder sent');
                          // send email
                  }
              }
          }        
          
      }
      The first wire('log') shows up but the second one doesn't (the purchase_date condition is met (as demonstrated by running the code in a template close in time to when the lazycron routine executes)). The error log gives the Uncaught Error shown above. 
      I think this is a beginner's mistake with something obvious being missed and would be grateful for any assistance in fixing it.
    • By Mustafa-Online
      I got a field with a type of "files" named: course_file .. how can I upload it to custom directory??
    • 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?