Skip to content

Commit

Permalink
Added possibility to set notification for tasks (and of course notifi…
Browse files Browse the repository at this point in the history
…cation templates)

Added two new events per tasks (new and update)
Added possibility to send spontaneous emails
Fixed various CS typos
Set version to 3.6.3
fixes #77
  • Loading branch information
tomolimo committed Jul 29, 2019
1 parent 217d605 commit e30ecf3
Show file tree
Hide file tree
Showing 19 changed files with 1,002 additions and 197 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

GLPI plugin that provides an interface with a customized ProcessMaker server (https://github.com/tomolimo/processmaker-server).

version 3.4.x is compatible with GLPI 9.2 and needs ProcessMaker either 3.0.1.8-RE-1.12 (https://github.com/tomolimo/processmaker-server/releases/tag/3.0.1.8-RE-1.12) or 3.3.0-RE-1.0 (https://github.com/tomolimo/processmaker-server/releases/tag/3.3.0-RE-1.0)
version 3.4.x is compatible with GLPI 9.2 and needs ProcessMaker either 3.0.1.8-RE-1.12 (https://github.com/tomolimo/processmaker-server/releases/tag/3.0.1.8-RE-1.12) or 3.3.0-RE-1.x (https://github.com/tomolimo/processmaker-server/releases/tag/3.3.0-RE-1.5)

version 3.5.x is compatible with GLPI 9.3 and needs ProcessMaker 3.3.0-RE-1.x (https://github.com/tomolimo/processmaker-server/releases/latest)

Expand Down
4 changes: 2 additions & 2 deletions ajax/dropdownProcesses.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

//if ($DB->numrows($result)) {
// while ($data = $DB->fetch_array($result)) {
if ($result->numrows()) {
//if ($result->numrows()) {
foreach ($result as $data) {
$process_entities = PluginProcessmakerProcess::getEntitiesForProfileByProcess($data["id"], $_SESSION['glpiactiveprofile']['id'], true);
$can_add = $data['max_cases_per_item'] == 0 || !isset($count_cases_per_item[$data["id"]]) || $count_cases_per_item[$data["id"]] < $data['max_cases_per_item'];
Expand All @@ -82,7 +82,7 @@
$count++;
}
}
}
//}

$ret['results'] = $processes;
$ret['count'] = $count;
Expand Down
4 changes: 2 additions & 2 deletions ajax/dropdownUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@
$count = 0;
//if ($DB->numrows($result)) {
// while ($data = $DB->fetch_assoc($result)) {
if ($res->numrows()) {
//if ($res->numrows()) {
foreach ($res as $data) {
$users[$data["id"]] = $dbu->formatUserName($data["id"], $data["name"], $data["realname"],
$data["firstname"]);
$logins[$data["id"]] = $data["name"];
}
}
//}

if (!function_exists('dpuser_cmp')) {
function dpuser_cmp($a, $b) {
Expand Down
2 changes: 1 addition & 1 deletion front/processmaker.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$case = new PluginProcessmakerCase;
if ($case->getFromGUID($resultCase->caseId)) {
$link = $case->getLinkURL();
$task = new PluginProcessmakerTask();
$task = new PluginProcessmakerTask($_POST['itemtype'].'Task');

$task->getFromDBByRequest([
'WHERE' => [
Expand Down
65 changes: 44 additions & 21 deletions inc/case.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,27 +265,50 @@ function reassignCase($delIndex, $taskGuid, $delThread, $users_id_source, $users
*/
public function reassignTask ($delIndex, $newDelIndex, $delThread, $newDelThread, $newTech) {
global $DB;
$res = $DB->request('glpi_plugin_processmaker_tasks', [
'AND' => [
'plugin_processmaker_cases_id' => $this->getID(),
'del_index' => $delIndex,
'del_thead' => $delThread
]
]);
//$query = "SELECT * FROM glpi_plugin_processmaker_tasks WHERE plugin_processmaker_cases_id={$this->getID()} AND del_index=$delIndex AND del_thread=$delThread; ";
//$res = $DB->query($query);
//if ($DB->numrows($res) > 0) {
// $row = $DB->fetch_array( $res );
if ($row = $res->next()) {
$glpi_task = new $row['itemtype'];
$glpi_task->getFromDB( $row['items_id'] );

$itilobject_itemtype = $this->fields['itemtype']; //str_replace( 'Task', '', $row['itemtype'] );

$dbu = new DbUtils;
$pm_task_row = $dbu->getAllDataFromTable(PluginProcessmakerTask::getTable(), ['plugin_processmaker_cases_id' => $this->getID(), 'del_index' => $delIndex, 'del_thread' => $delThread]);
if ($pm_task_row && count($pm_task_row) == 1) {
$pm_task_row = array_shift($pm_task_row);
$glpi_task = new $pm_task_row['itemtype'];
$glpi_task->getFromDB( $pm_task_row['items_id'] );

$itilobject_itemtype = $this->fields['itemtype'];
$foreignkey = getForeignKeyFieldForItemType( $itilobject_itemtype );

PluginProcessmakerProcessmaker::addWatcher( $itilobject_itemtype, $glpi_task->fields[ $foreignkey ], $newTech );

$glpi_task->update( [ 'id' => $row['items_id'], $foreignkey => $glpi_task->fields[ $foreignkey ], 'users_id_tech' => $newTech ]);
$donotif = PluginProcessmakerNotificationTargetProcessmaker::saveNotificationState(false); // do not send notification yet
$glpi_task->update( ['id' => $glpi_task->getID(), $foreignkey => $glpi_task->fields[$foreignkey], 'users_id_tech' => $newTech, 'update' => true] );
PluginProcessmakerNotificationTargetProcessmaker::restoreNotificationState($donotif);

// Notification management
// search if at least one active notification is existing for that pm task with that event 'task_update_'.$glpi_task->fields['taskcategories_id']
$res = PluginProcessmakerNotificationTargetTask::getNotifications('task_update', $glpi_task->fields['taskcategories_id'], $this->fields['entities_id']);
if ($res['notifications'] && count($res['notifications']) > 0) {
$pm_task = new PluginProcessmakerTask($pm_task_row['itemtype']);
$pm_task->getFromDB($pm_task_row['items_id']);
NotificationEvent::raiseEvent($res['event'],
$pm_task,
['plugin_processmaker_cases_id' => $this->getID(),
'itemtype' => $pm_task_row['itemtype'],
'task_id' => $glpi_task->getID(),
'old_users_id_tech' => $glpi_task->oldvalues['users_id_tech'],
'is_private' => isset($glpi_task->fields['is_private']) ? $glpi_task->fields['is_private'] : 0,
'entities_id' => $this->fields['entities_id'],
'case' => $this
]);
} else {
$item = new $itilobject_itemtype;
$item->getFromDB($glpi_task->fields[$foreignkey]);
NotificationEvent::raiseEvent('update_task',
$item,
['plugin_processmaker_cases_id' => $this->getID(),
'itemtype' => $pm_task_row['itemtype'],
'task_id' => $glpi_task->getID(),
'is_private' => isset($glpi_task->fields['is_private']) ? $glpi_task->fields['is_private'] : 0
]);
}

// then update the delIndex and delThread
//$query = "UPDATE glpi_plugin_processmaker_tasks SET del_index = $newDelIndex, del_thread = $newDelThread WHERE id={$row['id']}; ";
Expand All @@ -294,7 +317,7 @@ public function reassignTask ($delIndex, $newDelIndex, $delThread, $newDelThread
'del_index' => $newDelIndex,
'del_thread' => $newDelThread
], [
'id' => $row['id']
'id' => $pm_task_row['id']
]
);
}
Expand Down Expand Up @@ -694,14 +717,14 @@ static function showForItem(CommonITILObject $item) {
$condition[] = ['is_active' => 1];
if ($itemtype == 'Ticket') {
$condition[] = ['is_incident' => 1];
$is_itemtype = "AND is_incident=1";
//$is_itemtype = "AND is_incident=1";
if ($item->fields['type'] == Ticket::DEMAND_TYPE) {
$condition[] = ['is_request' => 1];
$is_itemtype = "AND is_request=1";
//$is_itemtype = "AND is_request=1";
}
} else {
$condition[] = ['is_'.strtolower($itemtype) => 1];
$is_itemtype = "AND is_".strtolower($itemtype)."=1";
//$is_itemtype = "AND is_".strtolower($itemtype)."=1";
}
PluginProcessmakerProcess::dropdown(['value' => 0,
'entity' => $item->fields['entities_id'],
Expand Down
3 changes: 0 additions & 3 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,6 @@ function parseUrl( url ) {
foreach ($res as $row) {
$pmGroups[ $row['CON_ID'] ] = $row['CON_VALUE'];
}
//foreach ($PM_DB->request( $query ) as $row) {
// $pmGroups[ $row['CON_ID'] ] = $row['CON_VALUE'];
//}
Dropdown::showFromArray( 'pm_group_guid', $pmGroups, ['value' => $config->fields['pm_group_guid']] );
} else {
echo "<font color='red'>".__('Not connected');
Expand Down
120 changes: 120 additions & 0 deletions inc/notificationtargetcase.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?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
]);
}
}
}

}
Loading

0 comments on commit e30ecf3

Please sign in to comment.