Jump to content

Added Custom Action but it doesn't do nothing!


Xonox
 Share

Recommended Posts

Hi,

This is my first module development and I have a couple of questions. First to explain the objective of the module.

I want to set the site reservations (Pages with reservation template) as Payed or Not Payed, straight from the Page Tree View. So far, I was able to display the actions as I wanted, but can't do nothing. I was to build through a couple of posts I checked but there are some things that aren't working:

1.  The action is running but it's not working:

switch($action){

	case 'pay':
		$page->reservation_paid = 1;
		$page->save;
		$success = true;
		$message = 'Payment done!';
		break;

	case 'unpay':
		$page->reservation_paid = 0;
		$page->save;
		$success = true;
		$message = 'Payment removed!';
		break;

}

2. How can I make sure that it runs as ajax and updates the page? As you can see ajax is set to true but it doesn't seem to be working.

3. Do I need the protected function createActions($page)? Doesn't seems to be doing nothing.

Here's the full code. If someone could give me some insights, it would be greately appreciated. Thanks,

<?php

class CustomActions extends WireData implements Module {

	public static function getModuleInfo() {

		return array(
			'title' => 'Custom Actions to Page List',
			'version' => 1,
			'summary' => 'Adds Custom PageActions to Page List actions',
			'singular' => true, // Limit the module to a single instance
			'autoload' => true, // Load the module with every call to ProcessWire
		);
	}

	public function init() {
		$this->addHookAfter('ProcessPageListActions::getExtraActions', $this, 'addActions');
		$this->addHookBefore('ProcessPageListActions::processAction', $this, 'exeActions');
	}

	/**
	 * Add the new actions
	 */
	public function addActions($event) {
		$page = $event->arguments[0];
		$actions = $event->return;
		// We do not overwrite existing core actions
		$actions = array_merge($this->createActions($page), $actions);
		// $event->return = $actions // Shouldn't be needed
	}

	public function ready() {
		$process = wire('page')->process;
		if($process == 'ProcessPageList') {
			$this->addHookAfter("ProcessPageListRender::getPageActions", $this, 'hookPageListActions');
		}
	}

	public function hookPageListActions(HookEvent $event) {

		$actions = array();
		$page = $event->arguments[0];

		// Apply only to reservation pages
		if($page->template != 'reservation') return $actions;

		$actions = $event->return;
		if($page->reservation_paid) {

			$actions[] = array(
				'cn' => 'Unpay',
				'name' => 'Unpay',
				'url' => $this->config->urls->admin . "page/?action=unpay&id={$page->id}",
				'ajax' => true,
			);

		}
		else {

			$actions[] = array(
				'cn' => 'Pay',
				'name' => 'Pay',
				'url' => $this->config->urls->admin . "page/?action=pay&id={$page->id}",
				'ajax' => true,
			);

		}

		$event->return = $actions;

	}

	/**
	 * Create the actions
	 */
	protected function createActions($page){

		$actions = array();
		if($page->template != 'reservation') return $actions;
		//if($page->template->noSettings || !$page->editable('status', false)) return $actions;
		$adminUrl = $this->wire('config')->urls->admin . 'page/';

		if($this->wire('user')->hasPermission('page-edit', $page) && !$page->isTrash()) {
			$actions['page-edit'] = array(
				'cn'    => 'Pay',
				'name'  => 'Pay',
				'url'   => "$adminUrl?action=PageActionPay1&id=$page->id",
				'ajax' => true,
			);
		}

		return $actions;

	}

	/**
	 * This is run when an action is initiated
	 *
	 * This can only been called if a page is editable.
	 */
	public function exeActions($event) {

		list($page, $action) = $event->arguments;

		$actions = $this->createActions($page);

		// This way checking for roles or other access rules is not duplicated.
		// If the action is still created, it's also actionable.
		if(!isset($actions[$action]) || !$page->editable()) return;

		$success = false;
		$needSave = true;
		$message = '';
		$remove = false;
		$refreshChildren = 0;

		switch($action){

			case 'pay':
				$page->reservation_paid = 1;
				$page->save;
				$success = true;
				$message = 'Payment done!';
				break;

			case 'unpay':
				$page->reservation_paid = 0;
				$page->save;
				$success = true;
				$message = 'Payment removed!';
				break;

		}

		// Return if success, otherwise move on to the hook function
		if(!$success) return;
		else $event->replace = true;

		// Return information
		$event->return = array(
			'action' => $action,
			'success' => true, // Fails are managed later by hooked function
			'message' => $message,
			'updateItem' => $page->id, // id of page to update in output
			'remove' => $remove,
			'refreshChildren' => $refreshChildren,
			// also available: 'appendItem' => $page->id, which adds a new item below the existing
		);

	}

}

 

Link to comment
Share on other sites

Update

I managed to simplify the code to the minimum. I don't find any problem r why isn't working. Can someone shed some light on why the actions is not running through AJAX?

<?php

class PayActions extends WireData implements Module {

	// MODULE INFO
	public static function getModuleInfo() {

		return array(
			'title' => 'Quick Pay Method',
			'version' => 1,
			'summary' => 'Adds Pay/Unpay actions to pages with reservation template.',
			'singular' => true, // Limit the module to a single instance
			'autoload' => true, // Load the module with every call to ProcessWire
		);
	}

	// MODULE INITIALIZATION
	public function ready() {
		$process = wire('page')->process;
		if($process == 'ProcessPageList') {

			// Use getPageActions because I don't want to place in the extra (expandable) actions
			$this->addHookAfter('ProcessPageListRender::getPageActions', $this, 'addPageListActions');

			// Hook to the page actions
			$this->addHookBefore('ProcessPageListActions::processAction', $this, 'executeActions');

		}
	}

	// ADD ACTIONS
	public function addPageListActions(HookEvent $event) {

		// Initialize
		$page = $event->arguments[0];
		$actions = array();
		$actions = $event->return;

		// Apply only to reservation pages
		if($page->template != 'reservation') return $actions;

		if($page->reservation_paid) {
			$actions[] = array(
				'cn' => 'Unpay',
				'name' => 'Unpay',
				'url' => $this->config->urls->admin . "page/?action=unpay&id={$page->id}",
				'ajax' => true,
			);
		}
		else {
			$actions[] = array(
				'cn' => 'Pay',
				'name' => 'Pay',
				'url' => $this->config->urls->admin . "page/?action=pay&id={$page->id}",
				'ajax' => true,
			);
		}

		$event->return = $actions;

	}

	// EXECUTE ACTION
	public function executeActions($event) {

		if(isset($actions[$action]) && $page->editable()) {

			$success = true;
			$needSave = true;
			$message = '';
			$remove = false;
			$refreshChildren = 0;

			switch($action) {
				case 'pay':
					$page->set('reservation_paid', 1);
					$message = $this->_('Paid');
					break;
				case 'unpay':
					$page->set('reservation_paid', 0);
					$message = $this->_('Unpaid');
					break;
				default:
					$success = false;
					$action = 'unknown';
			}

			// Save page or catch errors
			if($success) try {
				if($needSave) $success = $page->save();
				if(!$success) $message = sprintf($this->_('Error executing: %s', $message));
			} catch(Exception $e) {
				$success = false;
				$message = $e->getMessage();
			}

		}
		else {

			$success = false;

		}

		$result = array(
			'action' => $action,
			'success' => $success,
			'message' => $message,
			'updateItem' => $page->id, // id of page to update in output
			'remove' => $remove,
			'refreshChildren' => $refreshChildren,
			// also available: 'appendItem' => $page->id, which adds a new item below the existing
		);

		return $result;

	}

}

 

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

×
×
  • Create New...