Skip to content

Instantly share code, notes, and snippets.

@antoine1003
Last active May 9, 2024 12:17
Show Gist options
  • Save antoine1003/3b887f7e68e66b1e21a592694584d1f1 to your computer and use it in GitHub Desktop.
Save antoine1003/3b887f7e68e66b1e21a592694584d1f1 to your computer and use it in GitHub Desktop.
Some documentation on how did I add custom notification in a GLPI plugin. Feel free to add some infos.

Custom notification in GLPI

GLPI version 9.5.0

⚠️ This documentation is written for a plugin called perception, you'll need to adapt function, array key, ... to you own plugin.

Steps

Create notification class

In your plugin (folder inc) create a notification class, in my case : notificationtargetmodel.class.php.

<?php
if (!defined('GLPI_ROOT')) {
    die("Sorry. You can't access directly to this file");
}

// Class NotificationTarget
/**
 * Class PluginBadgesNotificationTargetBadge
 */
class PluginPerceptionNotificationTargetModel extends NotificationTarget {

    const REQUESTER = 30;
    // My events
    const SEND_PERCEPTION    = "SendPerception";
    const REVIVE_PERCEPTION  = "RevivePerception";


    /**
     * @return array
     */
    function getEvents() {
        return [
            self::SEND_PERCEPTION      => __('Send perception', 'perception'),
            self::REVIVE_PERCEPTION  => __('Revive perception', 'perception')
        ];
    }

    /**
     * Get additionnals targets for Tickets
     *
     * @param string $event
     */
    function addAdditionalTargets($event = '') {
        if ($event == self::SEND_PERCEPTION || $event == self::REVIVE_PERCEPTION) {
            $this->addTarget(self::REQUESTER, __("Requester"));
        }
    }

    function addSpecificTargets($data, $options) {
        //Look for all targets whose type is Notification::ITEM_USER
        switch ($data['type']) {
            case Notification::USER_TYPE:
                switch ($data['items_id']) {
                    case 30 :
                        $usertype = self::GLPI_USER;
                        $user = new User();
                        $user->getFromDB($options['users_id']);
                        // Send to user without any check on profile / entity
                        // Do not set users_id
                        $data = ['name'     => $user->getName(),
                            'email'    => $user->getDefaultEmail(),
                            'language' => $user->getField('language'),
                            'users_id' => $user->getID(),
                            'usertype' => $usertype];
                        $this->addToRecipientsList($data);
                }
        }
    }
}

Add notification class to notificationtemplates_types

In file setup.php in function plugin_init_perception() :

if (!in_array(PluginPerceptionModel::class, $CFG_GLPI["notificationtemplates_types"])){
    $CFG_GLPI["notificationtemplates_types"][] = PluginPerceptionModel::class;
}

Raise event

$params =  [
    'entities_id'  => $_POST["id"],
    'users_id' => $_POST['users_id']
];
NotificationEvent::raiseEvent(PluginPerceptionNotificationTargetModel::SEND_PERCEPTION, new PluginPerceptionModel(), $params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment