Skip to content

Commit

Permalink
Merge pull request #132 from tomolimo/4.0/bugfixes
Browse files Browse the repository at this point in the history
Fixed issues with notifications
  • Loading branch information
tomolimo authored Jul 30, 2021
2 parents a07c962 + 14e7e61 commit 3ba25a6
Show file tree
Hide file tree
Showing 6 changed files with 709 additions and 674 deletions.
257 changes: 137 additions & 120 deletions inc/notificationtargetcase.class.php
Original file line number Diff line number Diff line change
@@ -1,120 +1,137 @@
<?php
/*
* @version $Id: notificationtargettaskcategory.class.php tomolimo $
-------------------------------------------------------------------------
*/

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

// Class NotificationTarget
class PluginProcessmakerNotificationTargetCase extends PluginProcessmakerNotificationTargetProcessmaker {

// type
const EMAIL_RECIPIENTS = 200;

// user type
const RECIPIENTS = 1;

/**
* Summary of getEvents
* @return string[]
*/
public function getEvents() {
return ['send_email' => __('Send email', 'processmaker')];
}


/**
* Summary of addAdditionalTargets
* @param mixed $event
*/
function addAdditionalTargets($event = '') {
$this->notification_targets = [];
$this->notification_targets_labels = [];
$this->addTarget(self::RECIPIENTS, __('eMail recipients', 'processmaker'), self::EMAIL_RECIPIENTS);
}


/**
* Summary of addSpecificTargets
* @param mixed $data
* @param mixed $options
*/
function addSpecificTargets($data, $options) {

// test if we are in the good notification
// then in this case add the targets from the ['recipients']
if (isset($options['glpi_send_email'])) {
// normalize $options['glpi_send_email'] to an array of email parameters
$options['glpi_send_email'] = isset($options['glpi_send_email']['notifications_id']) ? [$options['glpi_send_email']] : $options['glpi_send_email'];

foreach($options['glpi_send_email'] as $params) {
if (isset($params['notifications_id'])
&& $params['notifications_id'] == $data['notifications_id']) {
//Look for all targets whose type is Notification::ITEM_USER
switch ($data['type']) {
case self::EMAIL_RECIPIENTS:

switch ($data['items_id']) {

case self::RECIPIENTS :
$this->addUsers($params);
break;
}
}
}
}
}

// if no target is added to $this, then the notification will not be sent.

}


/**
* Add users from $options['glpi_send_email']['to']
*
* @param array $email_param should contain 'recipients'
*
* @return void
*/
function addUsers($email_param = []) {
global $DB, $CFG_GLPI;

if (isset($email_param['recipients'])) {
$id_list = []; // for users with ids
$email_list = []; // for standalone emails

// normalize into array the recipient list
$email_param['recipients'] = is_array($email_param['recipients']) ? $email_param['recipients'] : [$email_param['recipients']];
foreach ($email_param['recipients'] as $user) {
if (is_numeric($user)) {
$id_list[] = intval($user);
} else {
$email_list[] = $user;
}
}

$query = $this->getDistinctUserSql()."
FROM `glpi_users` ".
$this->getProfileJoinSql()."
WHERE `glpi_users`.`id` IN (".implode(',', $id_list).")";

foreach ($DB->request($query) as $data) {
$this->addToRecipientsList($data);
}

foreach($email_list as $email){
$this->addToRecipientsList([
'email' => $email,
'language' => $CFG_GLPI["language"],
'users_id' => -1
]);
}
}
}

}
<?php
/*
* @version $Id: notificationtargettaskcategory.class.php tomolimo $
-------------------------------------------------------------------------
*/

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

// Class NotificationTarget
class PluginProcessmakerNotificationTargetCase extends PluginProcessmakerNotificationTargetProcessmaker {

// type
const EMAIL_RECIPIENTS = 200;

// user type
const RECIPIENTS = 1;

/**
* Summary of getEvents
* @return string[]
*/
public function getEvents() {
return ['send_email' => __('Send email', 'processmaker')];
}


/**
* Summary of addAdditionalTargets
* @param mixed $event
*/
function addAdditionalTargets($event = '') {
$this->notification_targets = [];
$this->notification_targets_labels = [];
$this->addTarget(self::RECIPIENTS, __('eMail recipients', 'processmaker'), self::EMAIL_RECIPIENTS);
}


/**
* Summary of addSpecificTargets
* @param mixed $data
* @param mixed $options
*/
function addSpecificTargets($data, $options) {

// test if we are in the good notification
// then in this case add the targets from the ['recipients']
if (isset($options['glpi_send_email'])) {
// normalize $options['glpi_send_email'] to an array of email parameters
$options['glpi_send_email'] = isset($options['glpi_send_email']['notifications_id']) ? [$options['glpi_send_email']] : $options['glpi_send_email'];

foreach($options['glpi_send_email'] as $params) {
if (isset($params['notifications_id'])
&& $params['notifications_id'] == $data['notifications_id']) {
//Look for all targets whose type is Notification::ITEM_USER
switch ($data['type']) {
case self::EMAIL_RECIPIENTS:

switch ($data['items_id']) {

case self::RECIPIENTS :
$this->addUsers($params);
break;
}
}
}
}
}

// if no target is added to $this, then the notification will not be sent.

}


/**
* Add users from $email_param['recipients']
*
* @param array $email_param should contain 'recipients'
*
* @return void
*/
function addUsers($email_param = []) {
global $DB, $CFG_GLPI;

if (isset($email_param['recipients'])) {
$id_list = []; // for users with ids
$email_list = []; // for standalone emails

// normalize into array the recipient list
$email_param['recipients'] = is_array($email_param['recipients']) ? $email_param['recipients'] : [$email_param['recipients']];
foreach ($email_param['recipients'] as $user) {
if (is_numeric($user)) {
$id_list[] = intval($user);
} else {
$email_list[] = $user;
}
}

$user = new User();
foreach ($id_list as $users_id) {
if ($user->getFromDB($users_id)) {

$author_email = UserEmail::getDefaultForUser($user->fields['id']);
$author_lang = $user->fields["language"];
$author_id = $user->fields['id'];

if (empty($author_lang)) {
$author_lang = $CFG_GLPI["language"];
}
if (empty($author_id)) {
$author_id = -1;
}

$user = [
'language' => $author_lang,
'users_id' => $author_id
];
if ($this->isMailMode()) {
$user['email'] = $author_email;
}
$this->addToRecipientsList($user);
}
}

foreach($email_list as $email){
$this->addToRecipientsList([
'email' => $email,
'language' => $CFG_GLPI["language"],
'users_id' => -1
]);
}
}
}

}
Loading

0 comments on commit 3ba25a6

Please sign in to comment.