Jump to content
benbyf

trouble triggering lazycron hook

Recommended Posts

Hi, I'm trying to add a hook to lazycron in my module that finds all pages with a checkbox and then sets less the seconds passed to another field, but as yet have seen anything happen.
 

public function init() {
	$this->checkboxName = "pdp_pub_later_check";
	$this->fieldName = "pdp_pub_later";

	$this->addHook("LazyCron::everyMinute", $this, 'publishDefferedPages');
}
public function ___publishDefferedPages(HookEvent $e){
		// seconds since last lazycron
		$seconds = $e->arguments[0];

		// find all pages with deffered field
		$defferedPages = $this->pages->find("{$this->checkboxName}=true");

		// for each page decrease time for deffered field
		foreach ($defferedPages as $page) {

			// get current page time
			$timeTillPublish = $page->get($this->fieldName);

			// set time to time minus time past
			$timeLeft = $timeTillPublish - $seconds;

			// if time passed 0 or less then publish page
			$page->of(false);
			if($timeLeft <= 0){
				// remove flags and save
				$this->publish();

				$page->set($this->fieldName, 0);
			}else{
				$page->set($this->fieldName, $timeLeft);
			}
			$page->Save();
			$page->of(true);
		}
	}

 

Share this post


Link to post
Share on other sites

What's $this->publish()? Shouldn't it publish the page?

Edit: Also try compareing the checkbox to 1 instead of true.

Share this post


Link to post
Share on other sites

sorry my bad, i left out another function:

public function publish($page) {
		$page->removeStatus(Page::statusUnpublished);
		$page->removeStatus(Page::statusHidden);
		$page->Save();
	}

 

Screenshot 2016-08-11 15.37.15.png

Share this post


Link to post
Share on other sites

You're not supplying the page to your publish method?! Also is the updating of the integer working or are both things not triggering?

Share this post


Link to post
Share on other sites

now added the $page, but still cant seem to get cron to fire. is the a simple thing to do to check if LazyCron is even working? thanks

Share this post


Link to post
Share on other sites

Yeah, just write something to the log in your method. I'm on mobile, so no example unforunately. You could even simply die(); the whole thing. Is your module autoloaded?

  • Like 1

Share this post


Link to post
Share on other sites

Cron is working as it's printing to the logs :) so must be not finding the pages or something. Havent used $log before, and im an idiot for not, so thanks for the push in that direction for debugging.

Share this post


Link to post
Share on other sites

AAAHAHAHHAAHAAHHAH i think i know what it is, and boy is it annoying!!!! the pages im looking to target a UNPUBLISHED and therefore I cant grab them in $pages, any way of seraching for unpublished pages too? grrrrrrrrr

Share this post


Link to post
Share on other sites

Working, legend! It was the old Unpublished pages thing again (something that tripped me up when I first start learning PW), thanks again everything seems to be working and will be releasing deffered pages module soon. (wish the forum still made solved buttons)

  • Like 1

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 MarkE
      LazyCron operates on an elapsed time interval, but you may want it to operate at specified times.
      Assuming your website is going to be visited sufficiently frequently (either by real users or by a Cron job), you can do this by editing the file /assets/cache/LazyCron.cache. This file contains 21 timestamps – one for each of the LazyCron specified intervals (e.g. “everyMinute”). The timestamp denotes when that interval was last triggered. To make the trigger happen at the required time, change the timestamp in the matching row to be when the previous trigger should have occurred.
      To make this easier, I have done an Excel spreadsheet (“Lazycron_fill.xlsx”) to simplify the process – just enter the required benchmark timings and copy the result into the LazyCron.cache file.
      Lazycron_fill.xlsx
    • By Hardoman
      Hello community,
      we have a website running version 3.0.118. The owner would like to have a watermark merged to the images, that are being uploaded in the CKEditor as a requirement.
      Image upload besides the CKEditor within galleries and single images works as a charm already. We also use croppable image 3 there. (PIM2)
      To realize this requirement, I thought of using a hook in the admin area. So, I read a lot in our forums and tested this by adding a hook into the ready.php file.
      $this->addHookAfter('InputfieldFile::fileAdded',function(HookEvent$event){ wire('log')->save('test','Image upload works'); ... The log entry is being created correctly. But when I try to use the pim/watermark-function like in a template, he cannot find the watermark-image anymore. Furthermore, when I try to get the page-id, it does not seem to be accessible, because the application does not seem to know how to reference it, or I dont know the right way to do so…
      So my questions are:
       
      Is this the right attempt at all or will there be another, better workaround? It seems, I cannot access the page object (of the content page) within this scope or file but I would need it to save the processed image inside the right files/id folder Would it be better to place the hook into the admin-template? (or admin.php)
        Thanks for any hints in advance. 🙂
    • By martind
      hi,
      is it possible to change field parent_id from InputfieldPageListSelect to InputfieldPageAutocomplet by addHookAfter('ProcessPageEdit::buildFormContent')?
      thanks,
      martin
       
    • By Noel Boss
      👋 PW Pros…
      I have some hooks that I need to bind at the init phase (or even __construct) and I was wondering, and I couldn't find a good and simple way to determine if I'm in the admin. Would be nice if there is a reliable short option to do so, but I can't seem to find one… Is there a coherent way to tell this no matter where I am?
      Right now, I use the following method inside one of my modules:
      public function isAdmin($page = null) { if ( strpos($this->input->url, $this->urls->admin) !== false || $this->process instanceof ProcessPageList || $this->process instanceof ProcessPageEdit || ($page instanceof Page && $page->rootParent->id == $this->config->adminRootPageID) ) { return true; } return false; } @ryan wouldn't it be nice to have something like wire()->isAdmin(); like wire()->user->isLoggedin(); to tell if we are in admin – very early on (probably even in __construct() phase of modules?
    • 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!
×
×
  • Create New...