Skip to content

Commit

Permalink
Fixed int(10) signed int to int(11) unsigned int
Browse files Browse the repository at this point in the history
fixed purged ticket that didn't purge message_id

fixes #62
fixes #64
  • Loading branch information
tomolimo committed Mar 11, 2022
1 parent 4b2d2c6 commit eeae77c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
67 changes: 46 additions & 21 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ function plugin_mailanalyzer_install() {

if (!$DB->tableExists("glpi_plugin_mailanalyzer_message_id")) {
$query = "CREATE TABLE `glpi_plugin_mailanalyzer_message_id` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`message_id` VARCHAR(255) NOT NULL DEFAULT '0',
`ticket_id` INT(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE INDEX `message_id` (`message_id`),
INDEX `ticket_id` (`ticket_id`)
)
COLLATE='utf8_general_ci'
ENGINE=innoDB;
";
`id` INT(11) NOT NULL AUTO_INCREMENT,
`message_id` VARCHAR(255) NOT NULL DEFAULT '0',
`tickets_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE INDEX `message_id` (`message_id`),
INDEX `tickets_id` (`tickets_id`)
)
COLLATE='utf8_general_ci'
ENGINE=innoDB;
";

$DB->query($query) or die("error creating glpi_plugin_mailanalyzer_message_id " . $DB->error());
} else {
Expand All @@ -28,6 +28,16 @@ function plugin_mailanalyzer_install() {
}
}

if (!$DB->fieldExists('glpi_plugin_mailanalyzer_message_id', 'tickets_id')) {
// then we must change the name and the length of id and ticket_id to 11
$query = "ALTER TABLE `glpi_plugin_mailanalyzer_message_id`
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT FIRST,
CHANGE COLUMN `ticket_id` `tickets_id` INT(11) NOT NULL DEFAULT '0' AFTER `message_id`,
DROP INDEX `ticket_id`,
ADD INDEX `ticket_id` (`tickets_id`);";
$DB->query($query) or die('Cannot alter glpi_plugin_mailanalyzer_message_id table! ' . $DB->error());
}

return true;
}

Expand All @@ -45,6 +55,9 @@ function plugin_mailanalyzer_uninstall() {
}


/**
* Summary of PluginMailAnalyzer
*/
class PluginMailAnalyzer {

/**
Expand Down Expand Up @@ -117,7 +130,7 @@ public static function plugin_pre_item_add_mailanalyzer($parm) {
[
'AND' =>
[
'ticket_id' => ['!=', 0],
'tickets_id' => ['!=', 0],
'message_id' => $messageId
]
]
Expand All @@ -143,20 +156,20 @@ public static function plugin_pre_item_add_mailanalyzer($parm) {
'glpi_plugin_mailanalyzer_message_id',
['AND' =>
[
'ticket_id' => ['!=',0],
'tickets_id' => ['!=',0],
'message_id' => $messages_id
],
'ORDER' => 'ticket_id DESC'
'ORDER' => 'tickets_id DESC'
]
);
if ($row = $res->next()) {
// TicketFollowup creation only if ticket status is not closed
$locTicket = new Ticket();
$locTicket->getFromDB((integer)$row['ticket_id']);
$locTicket->getFromDB((integer)$row['tickets_id']);
if ($locTicket->fields['status'] != CommonITILObject::CLOSED) {
$ticketfollowup = new ITILFollowup();
$input = $parm->input;
$input['items_id'] = $row['ticket_id'];
$input['items_id'] = $row['tickets_id'];
$input['users_id'] = $parm->input['_users_id_requester'];
$input['add_reopen'] = 1;
$input['itemtype'] = 'Ticket';
Expand All @@ -172,7 +185,7 @@ public static function plugin_pre_item_add_mailanalyzer($parm) {
'glpi_plugin_mailanalyzer_message_id',
[
'message_id' => $messageId,
'ticket_id' => $input['items_id']
'tickets_id' => $input['items_id']
]
);

Expand All @@ -187,7 +200,7 @@ public static function plugin_pre_item_add_mailanalyzer($parm) {

} else {
// ticket creation, but linked to the closed one...
$parm->input['_link'] = ['link' => '1', 'tickets_id_1' => '0', 'tickets_id_2' => $row['ticket_id']];
$parm->input['_link'] = ['link' => '1', 'tickets_id_1' => '0', 'tickets_id_2' => $row['tickets_id']];
}
}
}
Expand Down Expand Up @@ -215,7 +228,7 @@ public static function plugin_item_add_mailanalyzer($parm) {
global $DB;
if (isset($parm->input['_mailgate'])) {
// this ticket have been created via email receiver.
// update the ticket ID for the message_id only for newly created tickets (ticket_id == 0)
// update the ticket ID for the message_id only for newly created tickets (tickets_id == 0)

// Are 'Thread-Index' or 'Refrences' present?
$messages_id = self::getMailReferences($parm->input['_message']);
Expand All @@ -224,14 +237,14 @@ public static function plugin_item_add_mailanalyzer($parm) {
$DB->update(
'glpi_plugin_mailanalyzer_message_id',
[
'ticket_id' => $parm->fields['id']
'tickets_id' => $parm->fields['id']
],
[
'WHERE' =>
[
'AND' =>
[
'ticket_id' => 0,
'tickets_id' => 0,
'message_id' => $messages_id
]
]
Expand All @@ -243,7 +256,7 @@ public static function plugin_item_add_mailanalyzer($parm) {

/**
* Summary of getMailReferences
* @param Laminas\Mail\Storage\Message $message
* @param Laminas\Mail\Storage\Message $message
* @return array
*/
private static function getMailReferences(Laminas\Mail\Storage\Message $message) {
Expand All @@ -267,5 +280,17 @@ private static function getMailReferences(Laminas\Mail\Storage\Message $message)

return $messages_id;
}


/**
* Summary of plugin_item_purge_mailanalyzer
* @param mixed $item
*/
static function plugin_item_purge_mailanalyzer($item) {
Global $DB;
// the ticket is purged, then we are going to purge the matching rows in glpi_plugin_mailanalyzer_message_id table
// DELETE FROM glpi_plugin
$DB->delete('glpi_plugin_mailanalyzer_message_id', ['tickets_id' => $item->getID()]);
}
}

2 changes: 1 addition & 1 deletion mailanalyzer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</authors>
<versions>
<version>
<num>2.0.2</num>
<num>2.1.0</num>
<compatibility>9.5</compatibility>
</version>
<version>
Expand Down
7 changes: 6 additions & 1 deletion setup.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

define ("PLUGIN_MAILANALYZER_VERSION", "2.0.2");
define ("PLUGIN_MAILANALYZER_VERSION", "2.1.0");

/**
* Summary of plugin_init_mailanalyzer
Expand All @@ -21,6 +21,11 @@ function plugin_init_mailanalyzer() {
$PLUGIN_HOOKS['item_add']['mailanalyzer'] = [
'Ticket' => ['PluginMailAnalyzer', 'plugin_item_add_mailanalyzer']
];

$PLUGIN_HOOKS['item_purge']['mailanalyzer'] = [
'Ticket' => ['PluginMailAnalyzer', 'plugin_item_purge_mailanalyzer']
];

}


Expand Down

0 comments on commit eeae77c

Please sign in to comment.