Giter Club home page Giter Club logo

processadminactions's Introduction

Admin Actions

Processwire module for running various admin actions

Support forum:

https://processwire.com/talk/topic/14921-admin-actions/

Introduction

Admin Actions lets you quickly create actions in the admin that you can use over and over and even make available to your site editors (permissions for each action are assigned to roles separately so you have full control over who has access to which actions).

Included Actions

It comes bundled with a several actions and I will be adding more over time (and hopefully I'll get some PRs from you guys too). You can browse and sort and filter based on the content of all columns.

Convert Fields to Multi-Language Converts fields to their matching multi-language fieldtype.

Copy Content To Other Field This action copies the content from one field to another field on all pages that use the selected template.

Copy Field Content To Other Page Copies the content from a field on one page to the same field on another page.

Copy Repeater Items To Other Page Add the items from a Repeater field on one page to the same field on another page.

Copy Table Field Rows To Other Page Add the rows from a Table field on one page to the same field on another page.

Create Users Batcher Allows you to batch create users. This module requires the Email New User module and it should be configured to generate a password automatically.

Delete Unused Fields Deletes fields that are not used by any templates.

Delete Unused Templates Deletes templates that are not used by any pages.

Email Batcher Lets you email multiple addresses at once.

Field Set Or Search And Replace Set field values, or search and replace text in field values from a filtered selection of pages and fields.

FTP Files to Page Add files/images from a folder to a selected page.

Page Active Languages Batcher Lets you enable or disable active status of multiple languages on multiple pages at once.

Page Manipulator Uses an InputfieldSelector to query pages and then allows batch actions on the matched pages.

Page Table To Repeater Matrix Fully converts an existing (and populated) PageTable field to either a Repeater or RepeaterMatrix field.

Template Fields Batcher Lets you add or remove multiple fields from multiple templates at once.

Template Roles Batcher Lets you add or remove access permissions, for multiple roles and multiple templates at once.

User Roles Permissions Batcher Lets you add or remove permissions for multiple roles, or roles for multiple users at once.

Creating a New Action

Custom Site Actions

You can create new actions and place them in /site/templates/AdminActions/ so they don't get lost on module updates. These modules will appear under a new "Site" tab on the actions table.

Shared Site Actions

If you create a new action that you think others would find useful, please create a new AdminActions module and submit to the ProcessWire modules directory.

Shared actions to be shared must include a PW module file. There are two requirements:

  1. It's classname must start with "AdminActions"
  2. It must have 'requires' => 'ProcessAdminActions'

This is the entire contents needed in a file named AdminActionsMySharedAction.module

Once this module is installed on a site, AdminActions will detect it and allow users to configure and use it like any other action.

<?php namespace ProcessWire;

class AdminActionsMySharedAction extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Admin Actions My Shared Action',
            'summary' => 'My new action does really cool stuff',
            'version' => '0.1.0',
            'author' => 'John Doe',
            'href' => 'https://github.com/JohnDoe/AdminActionsMySharedAction',
            'requires' => 'ProcessAdminActions'
        );
    }

}

A good example of an installable action is @Toutouwai's Unordered List to Pages

Custom action examples

A new action file can be as simple as this:

<?php namespace ProcessWire;

class UnpublishAboutPage extends ProcessAdminActions {

    protected function executeAction() {
        $p = $this->wire('pages')->get('/about/');
        $p->addStatus(Page::statusUnpublished);
        $p->save();
        return true;
    }

}

Each action:

  • class must extend "ProcessAdminActions" and the filename must match the class name and end in ".action.php" like: UnpublishAboutPage.action.php
  • the action method must be: executeAction()

As you can see there are only a few lines needed to wrap the actual API call, so it's really worth the small extra effort to make an action.

Obviously that example action is not very useful. Here is another more useful one that is included with the module. It includes $description, $notes, and $author variables which are used in the module table selector interface. It also makes use of the defineOptions() method which builds the input fields used to gather the required options before running the action.

<?php namespace ProcessWire;

class DeleteUnusedFields extends ProcessAdminActions {

    protected $description = 'Deletes fields that are not used by any templates.';
    protected $notes = 'Shows a list of unused fields with checkboxes to select those to delete.';
    protected $author = 'Adrian Jones';
    protected $authorLinks = array(
        'pwforum' => '985-adrian',
        'pwdirectory' => 'adrian-jones',
        'github' => 'adrianbj',
    );

    protected function defineOptions() {

        $fieldOptions = array();
        foreach($this->wire('fields') as $field) {
            if ($field->flags & Field::flagSystem || $field->flags & Field::flagPermanent) continue;
            if(count($field->getFieldgroups()) === 0) $fieldOptions[$field->id] = $field->label ? $field->label . ' (' . $field->name . ')' : $field->name;
        }

        return array(
            array(
                'name' => 'fields',
                'label' => 'Fields',
                'description' => 'Select the fields you want to delete',
                'notes' => 'Note that all fields listed are not used by any templates and should therefore be safe to delete',
                'type' => 'checkboxes',
                'options' => $fieldOptions,
                'required' => true
            )
        );

    }


    protected function executeAction($options) {

        $count = 0;
        foreach($options['fields'] as $field) {
            $f = $this->wire('fields')->get($field);
            $this->wire('fields')->delete($f);
            $count++;
        }

        $this->successMessage = $count . ' field' . _n('', 's', $count) . ' ' . _n('was', 'were', $count) . ' successfully deleted';
        return true;

    }

}

Finally we use $options array in the executeAction() method to get the values entered into those options fields to run the API script to remove the checked fields.

There is one additional method that I didn't outline called: checkRequirements() - you can see it in action in the PageTableToRepeaterMatrix action. You can use this to prevent the action from running if certain requirements are not met.

At the end of the executeAction() method you can populate $this->successMessage, or $this->failureMessage which will be returned after the action has finished.

Populating options via URL parameters

You can also populate the option parameters via URL parameters. You should split multiple values with a "|" character.

You can either just pre-populate options:

http://mysite.dev/processwire/setup/admin-actions/options?action=TemplateFieldsBatcher&templates=29|56&fields=219&addOrRemove=add

or you can execute immediately:

http://mysite.dev/processwire/setup/admin-actions/run?action=TemplateFieldsBatcher&templates=29|56&fields=219&addOrRemove=add

Note the "options" vs "run" as the last path segment before the parameters.

Calling an action via the API

You can call an action easily via the API. Here's an example of copying content of a field from one page to another.

Simply load the module, and call the action name as a method while passing the required options as an array:

$options = array(
   'field' => 99,
   'sourcePage' => 1131,
   'destinationPage' => 1132
);
$modules->get("ProcessAdminActions")->CopyFieldContentToOtherPage($options);

Automatic Backup / Restore

Before any action is executed, a full database backup is automatically made. You have a few options to run a restore if needed:

  • Follow the Restore link that is presented after an action completes
  • Use the "Restore" submenu: Setup > Admin Actions > Restore
  • Move the restoredb.php file from the /site/assets/cache/AdminActions/ folder to the root of your site and load in the browser
  • Manually restore using the AdminActionsBackup.sql file in the /site/assets/cache/AdminActions/ folder

License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

(See included LICENSE file for full license text.)

processadminactions's People

Contributors

adrianbj avatar arjenblokzijl avatar daun avatar jlahijani avatar jmartsch avatar toutouwai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

processadminactions's Issues

Error after updating to 0.3.4

Running ProcessWire 3.0.45, after updating to AdminActions 0.3.4, I get this error:

Unsupported operand types (line 347 in /home/*/public_html/site/modules/ProcessAdminActions/ProcessAdminActions.module)ย 

(Tried logging out/in, clearing compilated files and updating module files.)

Unwanted labels for config checkboxes?

This might be intentional and not a bug, but the labels for the checkboxes in the "In menu" column look like they are maybe not meant to be there.

2019-02-24_094009

And I noticed from a screenshot in the support thread that the labels didn't used to be there.

586a779b1633b_screenshot2017-01-02at7_53_40am thumb png d0b5d97d7933409de398064d20f6850e

UI detail

A minor, non-urgent thing to save for a rainy day...

The spacing around the notifications is a bit uneven in AdminThemeUikit:
2018-05-05_124916

AdminThemeDefault for comparison:
2018-05-05_124925

Unpublished pages don't get modified

Let's say I want to make a bunch of unpublished pages be published using the Page Manipulator action. It won't work because the action doesn't use 'include=all' in the selector to find unpublished pages.

PHP8 deprecation warning

PHP Deprecated: ucfirst(): Passing null to parameter #1 ($string) of type string is deprecated in [...\ProcessPageFieldSelectCreator\ProcessPageFieldSelectCreator.module.php:125]

Rename execute query parameter to prevent firewall block

On sites that have the 7G firewall installed, requests get blocked when executing any action. The reason is that the firewall blocks URLs that have exec in the query string.

Could the query parameter be renamed from execute to something like run? Would this have any adverse effects?

Currently, I've removed the rule from the firewall, but maybe it's a small change that will make life easier for other users of those htaccess rules.

Happy to submit a PR.

filemtime() warning on install

Hi Adrian,

When installing the module Tracy picks up the following error:

PHP Warning: filemtime(): stat failed for .../site/assets/cache/AdminActions/AdminActionsBackup.sql in ...\ProcessAdminActions.module.php:390

Namespace error

Hi Adrian,

We discussed this in the support topic a few years ago but we didn't find a resolution.

The issue is that there's an error message if an action includes the PW namespace. Besides needing to have the namespace in order to get proper code analysis in an IDE, I also think that as a general principle we should be avoiding the PW file compiler whenever possible. The compiler made sense early on to ease the transition from PW2 to PW3 but coming up on 8 years later I think it's more bad than good and it's okay for module authors to now drop PW2 support.

I've attached a couple of test actions that demonstrate the error - one an installable module and the other for /site/templates/AdminActions/.

AdminActionsTestOne.zip
TestTwo.action.zip

And these are the errors:
2024-02-06_104439
2024-02-06_104731

[Feature Request] Allow fields inside repeater templates to be copied for the "Copy Content to Other Field" action

The "Copy Content to Other Field" action template dropdown does not include the templates that repeaters are actually based on (ie: 'repeater_myfield1', 'repeater_myfield2', etc.).

Because of this, you can't copy the content of a field INSIDE a repeater from one of its fields to another.

It only requires the following simple change in the action file:

Line 26 from:
"sort=name, flags!=".Template::flagSystem

to:
"sort=name,pageClass=''|RepeaterPage|RepeaterMatrixPage"

I've tested it out on real data and it works fine.

I think this is very useful to have as part of this action as its more comprehensive.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.