Jump to content
benbyf

[SOLVED]saving user and field in cron hook module

Recommended Posts

Hello, having trouble getting PW to save my user or field in cron hook.

It seems to report the same field info each time unchanged. It should be counting down in seconds.

	public function init() {

		// set to every30Seconds in settings
		$this->defaultInterval = $this->cron_check;

		// add hooks to CRON
	    $this->addHookAfter("LazyCron::{$this->defaultInterval}", $this, 'checkUserSubscription');
	}

	public function ___checkUserSubscription(HookEvent $e){

		// seconds since last lazycron
		$seconds = $e->arguments[0];

		$log = wire('log');

		$field = $this->exclusiveRole;

		// users with specific role and field
		$users = $this->users->find("roles=$field");
		foreach ($users as $user) {

			// get stored time
			$currentTime = $user->get($this->fieldName);

			// if time not null
			if($currentTime){

				// evalute stored time minus time past
				$resultTime = intval($currentTime) - intval($seconds);

				$log->message("result ".$resultTime);

				// save user page
				$user->set($this->fieldName, $resultTime);
				$this->users->save($user);
		}
	}
}

 

Share this post


Link to post
Share on other sites
19 minutes ago, benbyf said:

$resultTime = intval($currentTime) - intval($seconds);

$log->message("result ".$resultTime); // save user page

$user->set($this->fieldName, $resultTime);

$this->users->save($user); }

think i fixed it! adding

$user->setOutputFormatting(false);

GRRRRRRRRRRRRR OF() trips me up everytime. is there a good reason why this is here?? can't it default to not always being a pain :(

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 Macaco
      Hi there, 
      I have two templates that should talk to each other. One is for events which should access a list of artists (eventually add a new artist) and their images related to that event; the second is the artist page, which the user should be able to add or remove events that they participated, also mentioning which images are related to each event.
      Some sort of feedback loop between the two.
      I have tried searching around the community and google, but maybe I'm not formulating the question properly. 
      Thank you all for the amazing work with Processwire and the community support. I have made a few websites with PCW yet I'm still quite between beginner and intermediate.
    • By Robin S
      Thanks to @Macrura for the idea behind this module.
      Page Field Info
      Adds information about options in Page Reference fields. Supports InputfieldSelect and inputfields that extend InputfieldSelect:
      InputfieldSelect InputfieldRadios InputfieldSelectMultiple InputfieldCheckboxes InputfieldAsmSelect Requires ProcessWire >= 3.0.61 and AdminThemeUikit.
      Screenshots
      Field config

      Example of changes to inputfield

      Example of info field filled out in Page Edit

      Installation
      Install the Page Field Info module.
      Configuration
      In the Input tab of the settings for a Page Reference field...
      Tick the "Add info tooltips to options" checkbox to enable tooltips for the options in the field. Tooltips are not possible for Select or AsmSelect inputfield types so for those types you would want to tick the next option. Tick the "Append info about selected options" checkbox to append information about the selected options to the bottom of the inputfield. If the Page Reference field is a "multiple pages" field then the info for each selected option will be prefixed with the option label (so the user will know what option each line of info relates to). In the "Info field" dropdown select a text field that will contain information about the page, to be used in the tooltips and appended info. Of course this field should be in the template(s) of the selectable pages for the Page Reference field. Hook
      In most cases the "Info field" will supply the text for the tooltips and appended info, but for advanced usages you can hookPageFieldInfo::getPageInfo() to return the text. For example:
      $wire->addHookAfter('PageFieldInfo::getPageInfo', function(HookEvent $event) { $page = $event->arguments(0); // The page $inputfield = $event->arguments(1); // InputfieldPage $field = $event->arguments(2); // The Page Reference field $info = $event->return; // Text from the info field, if any // Set some custom text as the $event->return... });  
      https://github.com/Toutouwai/PageFieldInfo
      https://modules.processwire.com/modules/page-field-info/
    • By AndZyk
      Hello,
      this maybe a simple question, but it bothers me for a while now:
      There used to be a warning, if two or more user try to edit the same page. But ever since AdminThemeUIkit I haven't seen it any more.
      Is this a bug or are there special circumstances under which the warning will be displayed?
      If I am logged-in in the same browser with one default and one privat window, I don't see this warning. But also different browsers on different machines in the same network don't see this warning.
      I was looking for the blog post on which this feature was announced, but couldn't find it anymore.
      We had the case, that two people were writing a text for the same page, but after person A saved the page first, the text of the person B was lost. I know we could have used ProDrafts for this case, but it shouldn't happen in the first place.
      I would appreciate some feedback. 😀
      Regards, Andreas
    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
    • By gregory
      Hi guys. I need to create a field (link field) that allows me to select the internal pages to use as links. Do you have any suggestions? Thank you.
×
×
  • Create New...