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 celfred
      Hello,
      I am getting nuts trying to understand hooks and I hope someone in the community will be able to help. This is deiving me crazy ! I have tested tens of possibilities to eventually reduce my code to this :
        bd('outside');   $wire->addHookAfter('Page::render', function($event) {     bd('inside');   }); And if someone could tell me why my bd('inside'); never triggers... I would be infinitely grateful !
      EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)
    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
       
      Thanks in advance !
    • By SwimToWin
      I have a website that allows users to create their personal "website" (a page with sub-pages).
      Users shall be able to:
      Log in (frontend and/or admin), Edit "their" page(s) - I am using the "Page Edit Per User"-module (https://modules.processwire.com/modules/page-edit-per-user/) to grant access to the relevant pages Create child pages - possible? Users shall not be able to see other pages in the admin interface - "Admin Restrict Page Tree" may do the trick (https://modules.processwire.com/modules/admin-restrict-page-tree/)? Frontend editing shall be possible - I am considering "Fredi" (https://modules.processwire.com/modules/fredi/) for this. The challenge is that it takes a lot of modules and configuration.
      Is there a way to set this up that doesn't require a lot of configuration for each new user?
    • By tarkvsg
      Hi!
      tell me, pls, how to execute this equality (from ready.php)
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {     if($event->object->hasField == 'FIELDNAME')     {         .....     } } - $hasField The Field object associated with this Inputfield (from definition parent class for InputfieldPage)
      the left side equation is the object entity, and the right side - the string 
      How it is?
    • By Robin S
      Another little admin helper module...
      Template Field Widths
      Adds a "Field widths" field to Edit Template that allows you to quickly set the widths of inputfields in the template.

      Why?
      When setting up a new template or trying out different field layouts I find it a bit slow and tedious to have to open each field individually in a modal just to set the width. This module speeds up the process.
      Installation
      Install the Template Field Widths module.
      Config options
      You can set the default presentation of the "Field widths" field to collapsed or open. You can choose Name or Label as the primary identifier shown for the field. The unchosen alternative will become the title attribute shown on hover. You can choose to show the original field width next to the template context field width.  
      https://github.com/Toutouwai/TemplateFieldWidths
      https://modules.processwire.com/modules/template-field-widths/
×
×
  • Create New...