예제 #1
0
 public static function install(Migration $migration)
 {
     $table = getTableForItemType(__CLASS__);
     if (!TableExists($table)) {
         $query = "CREATE TABLE IF NOT EXISTS `{$table}` (\n                     `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n                     `plugin_formcreator_forms_id` tinyint(1) NOT NULL,\n                     `itemtype` varchar(100) NOT NULL DEFAULT 'PluginFormcreatorTargetTicket',\n                     `items_id` int(11) NOT NULL DEFAULT 0,\n                     `name` varchar(255) NOT NULL DEFAULT ''\n                  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
         $GLOBALS['DB']->query($query) or die($GLOBALS['DB']->error());
         // Migration from previous version
     } elseif (!FieldExists($table, 'itemtype', false)) {
         // Migration from version 1.5 to 1.6
         if (!FieldExists($table, 'type', false)) {
             $query = "ALTER TABLE `{$table}`\n                      ADD `type` tinyint(1) NOT NULL default '2';";
             $GLOBALS['DB']->query($query);
         }
         // Add new column for link with target items
         $query = "ALTER TABLE `{$table}`\n                     ADD `itemtype` varchar(100) NOT NULL DEFAULT 'PluginFormcreatorTargetTicket',\n                     ADD `items_id` int(11) NOT NULL DEFAULT 0;";
         $GLOBALS['DB']->query($query);
         // Create ticket template for each configuration in DB
         $query = "SELECT t.`urgency`, t.`priority`, t.`itilcategories_id`, t.`type`, f.`entities_id`\n                   FROM `glpi_plugin_formcreator_targets` t, `glpi_plugin_formcreator_forms` f\n                   WHERE f.`id` = t.`plugin_formcreator_forms_id`\n                   GROUP BY t.`urgency`, t.`priority`, t.`itilcategories_id`, t.`type`, f.`entities_id`";
         $result = $GLOBALS['DB']->query($query) or die($GLOBALS['DB']->error());
         $i = 0;
         while ($ligne = $GLOBALS['DB']->fetch_array($result)) {
             $i++;
             $id = $ligne['urgency'] . $ligne['priority'] . $ligne['itilcategories_id'] . $ligne['type'];
             $template = new TicketTemplate();
             $template_id = $template->add(array('name' => 'Template Formcreator ' . $i, 'entities_id' => $ligne['entities_id'], 'is_recursive' => 1));
             $predefinedField = new TicketTemplatePredefinedField();
             // Urgency
             if (!empty($ligne['urgency'])) {
                 $predefinedField->add(array('tickettemplates_id' => $template_id, 'num' => 10, 'value' => $ligne['urgency']));
             }
             // Priority
             if (!empty($ligne['priority'])) {
                 $predefinedField->add(array('tickettemplates_id' => $template_id, 'num' => 3, 'value' => $ligne['priority']));
             }
             // Category
             if (!empty($ligne['itilcategories_id'])) {
                 $predefinedField->add(array('tickettemplates_id' => $template_id, 'num' => 7, 'value' => $ligne['itilcategories_id']));
             }
             // Type
             if (!empty($ligne['type'])) {
                 $predefinedField->add(array('tickettemplates_id' => $template_id, 'num' => 14, 'value' => $ligne['type']));
             }
             $_SESSION["formcreator_tmp"]["ticket_template"]["{$id}"] = $template_id;
         }
         // Prepare Mysql CASE For each ticket template
         $mysql_case_template = "CASE CONCAT(`urgency`, `priority`, `itilcategories_id`, `type`)";
         foreach ($_SESSION["formcreator_tmp"]["ticket_template"] as $id => $value) {
             $mysql_case_template .= " WHEN {$id} THEN {$value} ";
         }
         $mysql_case_template .= "END AS `tickettemplates_id`";
         // Create Target ticket
         $version = plugin_version_formcreator();
         $migration = new Migration($version['version']);
         require_once 'targetticket.class.php';
         PluginFormcreatorTargetTicket::install($migration);
         $table_targetticket = getTableForItemType('PluginFormcreatorTargetTicket');
         $query = "SELECT `id`, `name`, {$mysql_case_template}, `content` FROM `{$table}`;";
         $result = $GLOBALS['DB']->query($query);
         while ($line = $GLOBALS['DB']->fetch_array($result)) {
             // Insert target ticket
             $query_insert = "INSERT INTO {$table_targetticket} SET\n                              `name` = \"{$line['name']}\",\n                              `tickettemplates_id` = \"{$line['tickettemplates_id']}\",\n                              `comment` = \"{$line['content']}\"";
             $GLOBALS['DB']->query($query_insert);
             $targetticket_id = $GLOBALS['DB']->insert_id();
             // Update target with target ticket id
             $query_update = "UPDATE `{$table}` SET `items_id` = {$targetticket_id} WHERE `id` = {$line['id']}";
             $GLOBALS['DB']->query($query_update);
         }
         // Remove useless column content
         $GLOBALS['DB']->query("ALTER TABLE `{$table}` DROP `content`;");
     }
     return true;
 }