Search the Community
Showing results for tags 'custom actions'.
-
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 ); } }