|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------- |
| 5 | + * |
| 6 | + * GLPI - Gestionnaire Libre de Parc Informatique |
| 7 | + * |
| 8 | + * http://glpi-project.org |
| 9 | + * |
| 10 | + * @copyright 2015-2025 Teclib' and contributors. |
| 11 | + * @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 12 | + * @var DBmysql $DB |
| 13 | + * @var Migration $migration |
| 14 | + * |
| 15 | + * --------------------------------------------------------------------- |
| 16 | + * |
| 17 | + * LICENSE |
| 18 | + * |
| 19 | + * This file is part of GLPI. |
| 20 | + * |
| 21 | + * This program is free software: you can redistribute it and/or modify |
| 22 | + * it under the terms of the GNU General Public License as published by |
| 23 | + * the Free Software Foundation, either version 3 of the License, or |
| 24 | + * (at your option) any later version. |
| 25 | + * |
| 26 | + * This program is distributed in the hope that it will be useful, |
| 27 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | + * GNU General Public License for more details. |
| 30 | + * |
| 31 | + * You should have received a copy of the GNU General Public License |
| 32 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 33 | + * |
| 34 | + * --------------------------------------------------------------------- |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * @var DBmysql $DB |
| 39 | + * @var Migration $migration |
| 40 | + */ |
| 41 | + |
| 42 | +use function Safe\json_encode; |
| 43 | + |
| 44 | +$migration->log('Group managed olas', false); |
| 45 | + |
| 46 | +add_groups_id_field_in_olas($migration); |
| 47 | +create_items_olas_table($migration); |
| 48 | +migrate_items_olas_data($migration); |
| 49 | +remove_olas_fields_in_tickets($migration); |
| 50 | +update_crontask($migration, $DB); |
| 51 | + |
| 52 | +$migration->executeMigration(); |
| 53 | +return; |
| 54 | + |
| 55 | +// --- functions |
| 56 | + |
| 57 | +function add_groups_id_field_in_olas(Migration $migration): void |
| 58 | +{ |
| 59 | + $migration->addField( |
| 60 | + OLA::getTable(), |
| 61 | + Group::getForeignKeyField(), |
| 62 | + 'fkey', |
| 63 | + [ |
| 64 | + 'value' => '0', |
| 65 | + 'null' => false, |
| 66 | + 'after' => 'slms_id', |
| 67 | + ] |
| 68 | + ); |
| 69 | + |
| 70 | + // addKey requires the table to exist -> execute migration before |
| 71 | + $migration->executeMigration(); |
| 72 | + $migration->addKey(OLA::getTable(), Group::getForeignKeyField()); |
| 73 | +} |
| 74 | + |
| 75 | +function remove_olas_fields_in_tickets(Migration $migration): void |
| 76 | +{ |
| 77 | + $fields_to_remove = [ |
| 78 | + 'ola_waiting_duration', |
| 79 | + 'olas_id_tto', |
| 80 | + 'olas_id_ttr', |
| 81 | + 'olalevels_id_ttr', |
| 82 | + 'ola_tto_begin_date', |
| 83 | + 'ola_ttr_begin_date', |
| 84 | + 'internal_time_to_resolve', |
| 85 | + 'internal_time_to_own', |
| 86 | + ]; |
| 87 | + |
| 88 | + foreach ($fields_to_remove as $field) { |
| 89 | + $migration->dropField(Ticket::getTable(), $field); |
| 90 | + } |
| 91 | +} |
| 92 | +function create_items_olas_table(Migration $migration): void |
| 93 | +{ |
| 94 | + $charset = DBConnection::getDefaultCharset(); |
| 95 | + $collation = DBConnection::getDefaultCollation(); |
| 96 | + $pk_sign = DBConnection::getDefaultPrimaryKeySignOption(); |
| 97 | + |
| 98 | + $query = "CREATE TABLE IF NOT EXISTS `glpi_items_olas` ( |
| 99 | + `id` int {$pk_sign} NOT NULL AUTO_INCREMENT, |
| 100 | + `itemtype` varchar(255) NOT NULL, |
| 101 | + `items_id` int unsigned NOT NULL, |
| 102 | + `olas_id` int unsigned NOT NULL, |
| 103 | + `ola_type` tinyint NOT NULL, -- 1: TTO, 2: TTR |
| 104 | + `start_time` timestamp NULL DEFAULT NULL, |
| 105 | + `due_time` timestamp NULL DEFAULT NULL, |
| 106 | + `end_time` timestamp NULL DEFAULT NULL, |
| 107 | + `waiting_time` int NOT NULL DEFAULT 0, |
| 108 | + `waiting_start` timestamp, |
| 109 | + `is_late` tinyint NOT NULL DEFAULT 0, |
| 110 | + PRIMARY KEY (`id`) |
| 111 | + ) ENGINE=InnoDB DEFAULT CHARSET=$charset COLLATE=$collation ROW_FORMAT=DYNAMIC;"; |
| 112 | + $migration->addPreQuery($query); |
| 113 | + $migration->executeMigration(); |
| 114 | + |
| 115 | + $migration->addKey('glpi_items_olas', 'olas_id'); |
| 116 | + $migration->addKey('glpi_items_olas', ['itemtype', 'items_id'], 'item'); |
| 117 | +} |
| 118 | + |
| 119 | +function migrate_items_olas_data(Migration $migration): void |
| 120 | +{ |
| 121 | + $_ticket = new Ticket(); |
| 122 | + if (!$_ticket->isField('olas_id_tto')) { |
| 123 | + // olas_id_tto field is removed : considere migration as done |
| 124 | + return; |
| 125 | + } |
| 126 | + $tickets_with_ola = $_ticket->find(['OR' |
| 127 | + => [ |
| 128 | + ['NOT' => ['olas_id_tto' => null]], |
| 129 | + ['NOT' => ['olas_id_ttr' => null]], |
| 130 | + ]]); |
| 131 | + |
| 132 | + foreach ($tickets_with_ola as $ticket) { |
| 133 | + if ($ticket['olas_id_tto'] !== 0) { |
| 134 | + $io = new Item_Ola(); |
| 135 | + |
| 136 | + $_data = [ |
| 137 | + 'itemtype' => Ticket::class, |
| 138 | + 'items_id' => $ticket['id'], |
| 139 | + 'olas_id' => $ticket['olas_id_tto'], |
| 140 | + 'start_time' => $ticket['ola_tto_begin_date'], |
| 141 | + 'due_time' => $ticket['internal_time_to_own'], |
| 142 | + 'waiting_time' => $ticket['ola_waiting_duration'], |
| 143 | + 'waiting_start' => null, |
| 144 | + ]; |
| 145 | + |
| 146 | + if (!$io->add($_data)) { |
| 147 | + throw new Exception('Failed to migrate OLA TTO data: ' . json_encode($_data)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if ($ticket['olas_id_ttr'] !== 0) { |
| 152 | + $io = new Item_Ola(); |
| 153 | + |
| 154 | + $_data = [ |
| 155 | + 'itemtype' => Ticket::class, |
| 156 | + 'items_id' => $ticket['id'], |
| 157 | + 'olas_id' => $ticket['olas_id_ttr'], |
| 158 | + 'start_time' => $ticket['ola_ttr_begin_date'], |
| 159 | + 'due_time' => $ticket['internal_time_to_resolve'], |
| 160 | + 'waiting_time' => $ticket['ola_waiting_duration'], |
| 161 | + ]; |
| 162 | + |
| 163 | + if (!$io->add($_data)) { |
| 164 | + throw new Exception('Failed to migrato OLA TTO data: ' . json_encode($_data)); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +function update_crontask(Migration $migration, DBmysql $DB): void |
| 171 | +{ |
| 172 | + // find if cron task already exists to choose against adding it or updating it |
| 173 | + $crontask = $DB->request([ |
| 174 | + 'SELECT' => ['id'], |
| 175 | + 'FROM' => CronTask::getTable(), |
| 176 | + 'WHERE' => [ |
| 177 | + 'name' => 'olaticket', |
| 178 | + ], |
| 179 | + ]); |
| 180 | + $id = $crontask->current() ? $crontask->current()['id'] : null; |
| 181 | + |
| 182 | + if (is_null($id)) { |
| 183 | + // add new crontask |
| 184 | + $migration->insertInTable( |
| 185 | + 'glpi_crontasks', |
| 186 | + [ |
| 187 | + 'itemtype' => 'Item_Ola', |
| 188 | + 'name' => 'olaticket', |
| 189 | + 'frequency' => 5 * MINUTE_TIMESTAMP, |
| 190 | + 'param' => null, |
| 191 | + 'state' => CronTask::STATE_WAITING, |
| 192 | + 'mode' => CronTask::MODE_INTERNAL, |
| 193 | + 'lastrun' => null, |
| 194 | + 'logs_lifetime' => 30, |
| 195 | + 'hourmin' => 0, |
| 196 | + 'hourmax' => 24, |
| 197 | + ] |
| 198 | + ); |
| 199 | + } else { |
| 200 | + // update existing crontask |
| 201 | + if (false === $DB->doQuery($DB->buildUpdate('glpi_crontasks', ['itemtype' => 'Item_Ola'], ['id' => $id]))) { |
| 202 | + throw new Exception('Failed to update crontask itemtype'); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments