Skip to content

Conversation

@kainhofer
Copy link
Contributor

This is a first proof-of-concept to add a proper messaging system with hooks, filters and actions to Admidio, similar to the actions/filters of Wordpress. This is not yet meant for immediate merge, but to get some feedback for the final, polished implementation.

The changes to User.php, common.php and changelog.php show how this can be used inside the existing Admidio code.

  1. Add a filter in the core code to let other code modify the default behavior (e.g. modify a displayed text/block, change a display, or prevent certain parts from being displayed altogether, e.g. hiding certain menu items selectively):
    use Admidio\Hooks\Hooks;
    $headline =  Hooks::apply_filters('changelog_headline', $headline);

Filters can modify the objects passed to them, but are not expected to have many side-effects.

All filters registered for 'changelog_headline' will be given a chance to modify the passed object(s). E.g. anywhere in a PHP file that is loaded on startup (plugin, other module, ...), you can register a filter. This example simply wraps "HOOKED: {........}" around the original headline:

Hooks::add_filter('changelog_headline', function ($value) {
    return "HOOKED: {" . $value . "}";
}, priority: 5, accepted_args: 1);
  1. Actions cannot modify the objects they are passed, but they are intended for their side-effects. E.g. when a user is created/modified/deleted, other parts of the code can react to it and e.g. sync the changes to a different server, send out notification emails or update newsletter subscriptions. Example in User.php:
    use Admidio\Hooks\Hooks;
        if ($newRecord) {
            Hooks::do_action('user_created', $this, $gCurrentUser);
        }

To add a listener to the action, simply register a function with add_action:

Hooks::add_action('user_created', function ($user, $actorId) {
    global $gLogger;
    $gLogger->warning('User Created: ', array('user' => $user, 'actorID' => $actorId));
    // log, sync, enqueue job, etc.
}, priority: 20, accepted_args: 2);

Of course, to be able to properly make use of this functionality, one needs to go through all the Admidio code and add lots of generic hooks and actions all over the place to make Admidio as flexible as possible.

My first target is to hook into the User creation/modification/deletion to sync the user data to Nextcloud via SCIM (as basis for the SSO). But other uses I envision is to modify the sidebar menu depending on the user groups.

This would also allow us to entangle some code in Admidio, e.g. by moving the user modification notification mails out from the User.php class into their own listeners in a separated file (in the future, even in a separate plugin, once a proper plugin system is implemented).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant