GLPI version 9.5.0
⚠️ This documentation is written for a plugin calledperception
, you'll need to adapt function, array key, ... to you own plugin.
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);
}
}
}
}
In file setup.php
in function plugin_init_perception()
:
if (!in_array(PluginPerceptionModel::class, $CFG_GLPI["notificationtemplates_types"])){
$CFG_GLPI["notificationtemplates_types"][] = PluginPerceptionModel::class;
}
$params = [
'entities_id' => $_POST["id"],
'users_id' => $_POST['users_id']
];
NotificationEvent::raiseEvent(PluginPerceptionNotificationTargetModel::SEND_PERCEPTION, new PluginPerceptionModel(), $params);