Jump to content

How To Create An "Action" In An Admin Page


Gazley
 Share

Recommended Posts

Hi there,

I have a template/page that uses hooks to achieve various things. When the admin user has logged on to the admin site and goes to the page in question (customer), she might want to simply click a button to send a specific email to the customer in question. Right now, I have a "send email" checkbox in the template and in the "before saved" hook, it detects the selection and sends the email.

In reality, quite often, the admin user ticks the "send email" checkbox and forgets to click the page's "saved" button. This isn't really surprising because it's a little counter-intuitive. The "send email" job is an action and I'm currently treating it like a data change/save process.

Is there a way to create an action button in a page when in PW's admin mode? 

Many thanks.

Link to comment
Share on other sites

Good Day,

If I am understanding you correctly, you could have a text or textarea field that is only viewable by the Admin.  From there you could use a Hanna Code to have a button "Send Email" that sends out the email.  That's just one way to achieve this.

i'm sure there are others that have a simpler, more streamlined or innovative solution to your problem.  The great thing about PW is that there's multiple ways of getting things done. Good luck.

  • Like 2
Link to comment
Share on other sites

@gazley,

One possibility would be a process module that would handle the sending; for example, you would have a button on the page which would for example link to that process, even by ajax, and send the email.

another way, in preserving your current method, would be to run some JS when the checkbox is checked, and display an alert "you have selected to send email on save - would you like to save the page now to initiate this action?" and then run a click action on the save button depending on their response, or even just run the click action on the save button after they select that checkbox - since it would save the page, i can't see any drawback to that...

  • Like 4
Link to comment
Share on other sites

Gazley,

I think it's important to have something like a page save, or other button that actually triggers the send. You could trigger it to happen via ajax when the user checks the "send email" checkbox, but that doesn't give them an opportunity to change their mind. The send would be immediate, unless of course you ask for a confirmation in a modal. 

  • Like 2
Link to comment
Share on other sites

You might be able to get some ideas from my Table CSV Import / Export module - even if you don't have Profields Table, you should still be able to grab the bits of code you need - take a look at the "Export as CSV" button on the screenshot in the first post: https://processwire.com/talk/topic/7905-profields-table-csv-importer-exporter/

In the repo you'll notice there is a helper Process module (ProcessTableCsvExport.module) for handling the action when that button is clicked.

Hope that helps a little, although keep in mind that it uses a hidden iframe technique to spawn the download and as noted by others above, you will probably need an AJAX approach in this case. 

  • Like 3
Link to comment
Share on other sites

@macrura 

One possibility would be a process module that would handle the sending; for example, you would have a button on the page which would for example link to that process, even by ajax, and send the email.

I can't see how you can add a field to the template that is a button - is there such a field type? If there was a way to add a button to the page, I'm not at all sure how to add JS/Ajax to an "admin" type of page. Obviously, there is a way to do this but I haven't done this before - I've only added Ajax to pages that render on the public facing site, not the "admin" pages. For sure, I have things to learn about customising admin pages! :)

@adrian - Thanks for this information. I do have the Profield modules so can take a look at your approach.

Link to comment
Share on other sites

In the specific case of the checkbox to send the email I think the best is to add a note instructing people to save the page. Something like "send email after saving this page. Don't forget to save the page or the email will not be sent".

A quick hack would be to use Admin custom files module to add some JS magic (read "hack") to do this instantly. You could for instance capture the change event on the checkbox to trigger the save button:

$(".checkbox").change(function() {
    if(this.checked) {
        $(".save-button").trigger("click");
    }
});

...or even replace the checkbox by a copy of the save button and change the text to "send email". The possibilities are infinite.

--

PS: notice my first sentence. I still would prefer the simple informative solution.

  • Like 4
Link to comment
Share on other sites

You could just alter this to your needs. This adds a second button to the pageEdit, which then triggers an action if clicked.

<?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, 'addButtons');
		$this->addHookBefore('ProcessPageEdit::processInput', $this, 'addButtonsMethods');
	}

	/**
	 *	Add a button besides "Save", which lets the user accept the application
	 *
	 */
	public function addButtons($event) {
		$page = $event->object->getPage();

		if($page->template == "application"){
			$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->_('Accept Application'));

			$form->insertBefore($accept, $form->get("id"));
		}
	}

	/**
	 *	Triggers the pageAction of "PageActionAcceptApplication" if the page was
	 *	submitted via the added button. This won't save the page, because that only
	 *	happens if the button is named "submit_save" or "submit_published"
	 *
	 */
	public function addButtonsMethods($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;
			}

			$accept = $this->modules->get("PageActionAcceptApplication");
			$accept->action($page);
		}
	}
	
}

  • Like 9
Link to comment
Share on other sites

This is great LostKobrakai!!!

Before the new action is called, I call $page->save() to save the current input (I think that an user probably thinks that such a button also saves, only with extra functionality).

However this seems to trigger this new event twice, only when the page has been edited. Once before the save event, and once after. How can I escape the first event, so that it will only be triggered after having latest information?

Many thanks!

*** EDIT ***

I'm utterly stumped with this. How can I add a simple "<a href="####" target="_blank"></a>" link/button as an element to the form. I've looked at the 'Hello World' example, but I just don't get it... I need to fetch the ID of the page and use it to reference to another URL..

Link to comment
Share on other sites

You don't want to save the page, as the hook, which runs the added action is executed before changes are evaluated. If you need the latest information you need to change the hook:

$this->addHookBefore('ProcessPageEdit::processInput', $this, 'addButtonsMethods');
// to
$this->addHookAfter('ProcessPageEdit::processInput', $this, 'addButtonsMethods');

As for your plain link: Where exactly do you have problems? Most likely it will be kinda similar to my code, but you'd use InputfieldMarkup to add custom markup instead of a button.

Link to comment
Share on other sites

this...is...great!

As for your plain link: Where exactly do you have problems? Most likely it will be kinda similar to my code, but you'd use InputfieldMarkup to add custom markup instead of a button.

I just don't get it... How do you know that? :-) I also looked inside the wire/modules/input folder, but I just didn't knew which module was the right one...

Link to comment
Share on other sites

As seen from my post count I'm around the forums for quite some time already, which helps with sorting out issues. Also everyone here was at the point of having no clue about how the whole cms works. So I eventually had the same problem when I started using the form api.

  • Like 1
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...