Jump to content

Cannot access $users inside of a module


easynowbaby
 Share

Recommended Posts

Hello all,

for the first time I need help from the great people of this great forum! :D I am trying to add new functionality to admin, namely sending emails from the edit page. I want to add an extra "Send email" button, which I did using this snippet. Here is my modified code:

<?php

class HookPageEdit extends WireData implements Module {

	public static function getModuleInfo() {

		return array(
			'title' => 'Hook PageEdit', 
			'version' => 1, 
			'summary' => '',
			'singular' => true, 
			'autoload' => true, 
			);
	}	

	public function init() {
		$this->addHookAfter('ProcessPageEdit::buildForm', $this, 'addInputs');
		$this->addHookBefore('ProcessPageEdit::processInput', $this, 'sendMail');
	}

	public function addInputs($event) {
		$page = $event->object->getPage();

		if($page->template == "email"){
			$form = $event->return; 

			$accept = $this->modules->get('InputfieldSubmit');
			$accept->attr('id+name', 'hook_accept_application');
			$accept->class .= ' ui-priority-secondary head_button_clone';
			$accept->attr('value', $this->_('Send email'));

			$f = $this->modules->get('InputfieldCheckbox');
      $f->attr('name', 'checkbox_all_users');
      $f->label = 'Send to all';
      $f->attr('value', $this->checkbox_all_users ? $this->checkbox_all_users : 0 );
      $f->attr('checked', $this->checkbox_all_users === 1 ? 'checked' : '' );      

			$form->insertBefore($accept, $form->get("id"));
			$form->insertBefore($f, $form->get("id"));
		}
	}
	
	public function sendMail($event) {
		if($this->input->post->hook_accept_application){
			$page = $event->object->getPage();
			$event->replace = true;

			if(!$page->id){
				$this->error("Invalid application to be accepted."); 
				return;
			}

			$toAll = $this->input->post->checkbox_all_users;
			
			if ($toAll) {
				$users = $users->find("roles=member,guest");				
			}
			
		}
	}
	
}

But I get an error saying that $users is null. Anyone knows how to access users in my module? Also, is this a good way of doing what I need?

Thanks a lot!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...