Example #1
0
 /**
  * Database table installation for the item type
  *
  * @param Migration $migration
  * @return boolean True on success
  */
 public static function install(Migration $migration)
 {
     $obj = new self();
     $table = $obj->getTable();
     // Create new table
     if (!TableExists($table)) {
         $migration->displayMessage("Installing {$table}");
         // Create questions table
         $query = "CREATE TABLE IF NOT EXISTS `{$table}` (\n                     `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n                     `plugin_formcreator_forms_id` int(11) NOT NULL,\n                     `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,\n                     `order` int(11) NOT NULL DEFAULT '0'\n                  )\n                  ENGINE = MyISAM\n                  DEFAULT CHARACTER SET = utf8\n                  COLLATE = utf8_unicode_ci;";
         $GLOBALS['DB']->query($query) or die($GLOBALS['DB']->error());
     } else {
         /**
          * Migration of special chars from previous versions
          *
          * @since 0.85-1.2.3
          */
         $query = "SELECT `id`, `name`\n                    FROM `{$table}`";
         $result = $GLOBALS['DB']->query($query);
         while ($line = $GLOBALS['DB']->fetch_array($result)) {
             $query_update = 'UPDATE `' . $table . '` SET
                            `name` = "' . plugin_formcreator_encode($line['name']) . '"
                          WHERE `id` = ' . $line['id'];
             $GLOBALS['DB']->query($query_update) or die($GLOBALS['DB']->error());
         }
     }
     // Migration from previous version => Remove useless target field
     if (FieldExists($table, 'plugin_formcreator_targets_id', false)) {
         $GLOBALS['DB']->query("ALTER TABLE `{$table}` DROP `plugin_formcreator_targets_id`;");
     }
     // Migration from previous version => Rename "position" into "order" and start order from 1 instead of 0
     if (FieldExists($table, 'position', false)) {
         $GLOBALS['DB']->query("ALTER TABLE `{$table}` CHANGE `position` `order` INT(11) NOT NULL DEFAULT '0';");
         $GLOBALS['DB']->query("UPDATE `{$table}` SET `order` = `order` + 1;");
     }
     // Migration from previous version => Update Question table, then create a "description" question from content
     if (FieldExists($table, 'content', false)) {
         $version = plugin_version_formcreator();
         $migration = new Migration($version['version']);
         PluginFormcreatorQuestion::install($migration);
         $table_questions = getTableForItemType('PluginFormcreatorQuestion');
         // Increment the order of questions which are in a section with a description
         $query = "UPDATE `{$table_questions}`\n                   SET `order` = `order` + 1\n                   WHERE `plugin_formcreator_sections_id` IN (\n                     SELECT `id`\n                     FROM {$table}\n                     WHERE `content` != ''\n                  );";
         $GLOBALS['DB']->query($query);
         // Create description from content
         $query = "INSERT INTO `{$table_questions}` (`plugin_formcreator_sections_id`, `fieldtype`, `name`, `description`, `order`)\n                     SELECT `id`, 'description' AS fieldtype, CONCAT('Description ', `id`) AS name,  `content`, 1 AS `order`\n                     FROM {$table}\n                     WHERE `content` != ''";
         $GLOBALS['DB']->query($query);
         // Delete content column
         $GLOBALS['DB']->query("ALTER TABLE `{$table}` DROP `content`;");
     }
     return true;
 }
Example #2
0
/**
 * Install all necessary elements for the plugin
 *
 * @return boolean True if success
 */
function plugin_formcreator_install()
{
    $version = plugin_version_formcreator();
    $migration = new Migration($version['version']);
    // Parse inc directory
    foreach (glob(dirname(__FILE__) . '/inc/*') as $filepath) {
        // Load *.class.php files and get the class name
        if (preg_match("/inc.(.+)\\.class.php/", $filepath, $matches)) {
            $classname = 'PluginFormcreator' . ucfirst($matches[1]);
            include_once $filepath;
            // If the install method exists, load it
            if (method_exists($classname, 'install')) {
                $classname::install($migration);
            }
        }
    }
    return true;
}
 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;
 }