From bfc83e838d4b812c89e5f451dc4e7646cbff8b06 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Fri, 23 Nov 2018 19:35:48 +0100 Subject: [PATCH] feat(install): add force instal or upgrade modes Signed-off-by: Thierry Bugier --- hook.php | 4 +- install/install.php | 86 +- install/update_0.0_2.5.php | 1323 ---------------- install/update_2.5_2.6.php | 196 --- install/update_2.6.2_2.6.3.php | 88 -- install/update_2.6_2.7.php | 228 --- install/upgrade_to_2.5.php | 1328 +++++++++++++++++ ...ate_2.6_2.6.1.php => upgrade_to_2.6.1.php} | 61 +- install/upgrade_to_2.6.3.php | 90 ++ install/upgrade_to_2.6.php | 201 +++ install/upgrade_to_2.7.php | 235 +++ scripts/cliinstall.php | 10 +- 12 files changed, 1951 insertions(+), 1899 deletions(-) delete mode 100644 install/update_0.0_2.5.php delete mode 100644 install/update_2.5_2.6.php delete mode 100644 install/update_2.6.2_2.6.3.php delete mode 100644 install/update_2.6_2.7.php create mode 100644 install/upgrade_to_2.5.php rename install/{update_2.6_2.6.1.php => upgrade_to_2.6.1.php} (51%) create mode 100644 install/upgrade_to_2.6.3.php create mode 100644 install/upgrade_to_2.6.php create mode 100644 install/upgrade_to_2.7.php diff --git a/hook.php b/hook.php index 6022fb66a..1bb1758c8 100644 --- a/hook.php +++ b/hook.php @@ -42,7 +42,9 @@ function plugin_formcreator_install() { $migration = new Migration($version['version']); require_once(__DIR__ . '/install/install.php'); $install = new PluginFormcreatorInstall(); - if (!$install->isPluginInstalled()) { + if (!$install->isPluginInstalled() + || isset($_SESSION['plugin_formcreator']['cli']) + && $_SESSION['plugin_formcreator']['cli'] == 'force-install') { return $install->install($migration); } return $install->upgrade($migration); diff --git a/install/install.php b/install/install.php index ccc3eb481..15e5e8b26 100644 --- a/install/install.php +++ b/install/install.php @@ -38,6 +38,24 @@ class PluginFormcreatorInstall { protected $migration; + /** + * array of upgrade steps key => value + * key is the version to upgrade from + * value is the version to upgrade to + * + * Exemple: an entry '2.0' => '2.1' tells that versions 2.0 + * are upgradable to 2.1 + * + * @var array + */ + private $upgradeSteps = [ + '0.0' => '2.5', + '2.5' => '2.6', + '2.6' => '2.6.1', + '2.6.1' => '2.6.3', + '2.6.3' => '2.7', + ]; + /** * Install the plugin * @param Migration $migration @@ -66,43 +84,21 @@ public function install(Migration $migration) { */ public function upgrade(Migration $migration) { $this->migration = $migration; - $fromSchemaVersion = $this->getSchemaVersion(); - - $this->installSchema(); + if (isset($_SESSION['plugin_formcreator']['cli']) && $_SESSION['plugin_formcreator']['cli'] == 'force-upgrade') { + // Might return false + $fromSchemaVersion = array_search(PLUGIN_FORMCREATOR_SCHEMA_VERSION, $this->upgradeSteps); + } else { + $fromSchemaVersion = $this->getSchemaVersion(); + } - // All cases are run starting from the one matching the current schema version - switch ($fromSchemaVersion) { - case '0.0': - require_once(__DIR__ . '/update_0.0_2.5.php'); - plugin_formcreator_update_2_5($this->migration); - - case '2.5': - require_once(__DIR__ . '/update_2.5_2.6.php'); - plugin_formcreator_update_2_6($this->migration); - - case '2.6': - require_once(__DIR__ . '/update_2.6_2.6.1.php'); - plugin_formcreator_update_2_6_1($this->migration); - - require_once(__DIR__ . '/update_2.6.2_2.6.3.php'); - plugin_formcreator_update_2_6_3($this->migration); - - require_once(__DIR__ . '/update_2.6_2.7.php'); - plugin_formcreator_update_2_7($this->migration); - - default: - // Must be the last case - if ($this->endsWith(PLUGIN_FORMCREATOR_VERSION, "-dev")) { - if (is_readable(__DIR__ . "/update_dev.php") && is_file(__DIR__ . "/update_dev.php")) { - include_once __DIR__ . "/update_dev.php"; - $updateDevFunction = 'plugin_formcreator_update_dev'; - if (function_exists($updateDevFunction)) { - $updateDevFunction($this->migration); - } - } - } + while ($fromSchemaVersion && isset($this->upgradeSteps[$fromSchemaVersion])) { + $this->upgradeOneStep($this->upgradeSteps[$fromSchemaVersion]); + $fromSchemaVersion = $this->upgradeSteps[$fromSchemaVersion]; } + $this->migration->executeMigration(); + // if the schema contains new tables + $this->installSchema(); $this->configureExistingEntities(); $this->createRequestType(); $this->createDefaultDisplayPreferences(); @@ -112,6 +108,28 @@ public function upgrade(Migration $migration) { return true; } + /** + * Proceed to upgrade of the plugin to the given version + * + * @param string $toVersion + */ + protected function upgradeOneStep($toVersion) { + ini_set("max_execution_time", "0"); + ini_set("memory_limit", "-1"); + + $suffix = str_replace('.', '_', $toVersion); + $includeFile = __DIR__ . "/upgrade_to_$toVersion.php"; + if (is_readable($includeFile) && is_file($includeFile)) { + include_once $includeFile; + $updateClass = "PluginFormcreatorUpgradeTo$suffix"; + $this->migration->addNewMessageArea("Upgrade to $toVersion"); + $upgradeStep = new $updateClass(); + $upgradeStep->upgrade($this->migration); + $this->migration->executeMigration(); + $this->migration->displayMessage('Done'); + } + } + /** * Find the version of the plugin * diff --git a/install/update_0.0_2.5.php b/install/update_0.0_2.5.php deleted file mode 100644 index 9a43a22ad..000000000 --- a/install/update_0.0_2.5.php +++ /dev/null @@ -1,1323 +0,0 @@ -. - * --------------------------------------------------------------------- - * @author Thierry Bugier - * @author Jérémy Moreau - * @copyright Copyright © 2011 - 2018 Teclib' - * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ - * @link https://github.com/pluginsGLPI/formcreator/ - * @link https://pluginsglpi.github.io/formcreator/ - * @link http://plugins.glpi-project.org/#/plugin/formcreator - * --------------------------------------------------------------------- - */ - -/** - * Upgrade any version of Formcreator < 2.5.0 to 2.5.0 - * @param Migration $migration - */ -function plugin_formcreator_update_2_5(Migration $migration) { - global $DB; - - $migration->displayMessage("Upgrade to schema version 2.5"); - - plugin_formcreator_updateAnswer_2_5($migration); - plugin_formcreator_updateCategory_2_5($migration); - plugin_formcreator_updateForm_Answer_2_5($migration); - plugin_formcreator_updateForm_Profile_2_5($migration); - plugin_formcreator_updateFormValidator_2_5($migration); - plugin_formcreator_updateForm_2_5($migration); - plugin_formcreator_updateHeader_2_5($migration); - plugin_formcreator_updateIssue_2_5($migration); - plugin_formcreator_updateQuestionCondition_2_5($migration); - plugin_formcreator_updateQuestion_2_5($migration); - plugin_formcreator_updateSection_2_5($migration); - plugin_formcreator_updateTarget_2_5($migration); - plugin_formcreator_updateTargetChange_2_5($migration); - plugin_formcreator_updateTargetChange_Actor_2_5($migration); - plugin_formcreator_updateTargetTicket_Actor_2_5($migration); - plugin_formcreator_updateTargetTicket_2_5($migration); - plugin_formcreator_updateTitle_2_5($migration); - - $migration->executeMigration(); -} - -function plugin_formcreator_updateAnswer_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Answers - $migration->displayMessage("Upgrade glpi_plugin_formcreator_answers"); - // Update field type from previous version (Need answer to be text since text can be WYSIWING). - $query = "ALTER TABLE `glpi_plugin_formcreator_answers` CHANGE `answer` `answer` text;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - $query = "SELECT `id`, `answer` FROM `glpi_plugin_formcreator_answers`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_answers` SET - `answer` = '".addslashes($line['answer'])."' - WHERE `id` = ".$line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - //rename foreign key, to match table plugin_formcreator_forms_answers name - $migration->changeField('glpi_plugin_formcreator_answers', - 'plugin_formcreator_formanwers_id', - 'plugin_formcreator_forms_answers_id', - 'integer'); - - $migration->addKey('glpi_plugin_formcreator_answers', 'plugin_formcreator_forms_answers_id'); - $migration->addKey('glpi_plugin_formcreator_answers', 'plugin_formcreator_question_id'); - - $migration->migrationOneTable('glpi_plugin_formcreator_answers'); -} - -function plugin_formcreator_updateCategory_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Categories - $migration->displayMessage("Upgrade glpi_plugin_formcreator_categories"); - - if ($DB->tableExists('glpi_plugin_formcreator_cats')) { - $query = "INSERT IGNORE INTO `glpi_plugin_formcreator_categories` (`id`, `name`) - SELECT `id`,`name` FROM glpi_plugin_formcreator_cats"; - $DB->query($query); - $DB->query("DROP TABLE glpi_plugin_formcreator_cats"); - } - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - $query = "SELECT `id`, `name`, `comment` FROM `glpi_plugin_formcreator_categories`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_categories` SET - `name` = '".plugin_formcreator_encode($line['name'], false)."', - `comment` = '".plugin_formcreator_encode($line['comment'], false)."' - WHERE `id` = ".$line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - /** - * Migrate categories to tree structure - * - * @since 0.90-1.4 - */ - if (!$DB->fieldExists('glpi_plugin_formcreator_categories', "knowbaseitemcategories_id")) { - $migration->addField('glpi_plugin_formcreator_categories', 'completename', 'string', [ - 'after' => 'comment' - ]); - $migration->addField('glpi_plugin_formcreator_categories', 'plugin_formcreator_categories_id', 'integer', [ - 'after' => 'completename' - ]); - $migration->addField('glpi_plugin_formcreator_categories', 'level', 'integer', [ - 'value' => 1, - 'after' => 'plugin_formcreator_categories_id' - ]); - $migration->addField('glpi_plugin_formcreator_categories', 'sons_cache', 'longtext', [ - 'after' => 'level' - ]); - $migration->addField('glpi_plugin_formcreator_categories', 'ancestors_cache', 'longtext', [ - 'after' => 'sons_cache' - ]); - $migration->addField('glpi_plugin_formcreator_categories', 'knowbaseitemcategories_id', 'integer', [ - 'after' => 'ancestors_cache' - ]); - - $migration->addKey('glpi_plugin_formcreator_categories', 'knowbaseitemcategories_id'); - $migration->addKey('glpi_plugin_formcreator_categories', 'plugin_formcreator_categories_id'); - $migration->migrationOneTable('glpi_plugin_formcreator_categories'); - $query = "UPDATE `glpi_plugin_formcreator_categories` SET `completename` = `name` WHERE `completename` = ''"; - $DB->query($query); - } -} - -function plugin_formcreator_updateForm_Answer_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Form_Answers - $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_answers"); - - if ($DB->tableExists("glpi_plugin_formcreator_formanswers")) { - $migration->renameTable('glpi_plugin_formcreator_formanswers', 'glpi_plugin_formcreator_forms_answers'); - $itemTicket_table = Item_Ticket::getTable(); - $query = "UPDATE `$itemTicket_table` SET `itemtype` = 'PluginFormcreatorForm_Answer' WHERE `itemtype` = 'PluginFormcreatorFormanswer'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - $query = "SELECT `id`, `comment` FROM `glpi_plugin_formcreator_forms_answers`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_forms_answers` SET - `comment` = '" . plugin_formcreator_encode($line['comment']) . "' - WHERE `id` = " . $line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_forms_answers', 'name')) { - $query_update = 'ALTER TABLE `glpi_plugin_formcreator_forms_answers` ADD `name` VARCHAR(255) NOT NULL AFTER `id`'; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - // valdiator_id should not be set for waiting form answers - $query = "UPDATE `glpi_plugin_formcreator_forms_answers` - SET `validator_id` = '0' WHERE `status`='waiting'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - $migration->addKey('glpi_plugin_formcreator_forms_answers', 'plugin_formcreator_forms_id'); - $migration->addKey('glpi_plugin_formcreator_forms_answers', ['entities_id', 'is_recursive']); - $migration->addKey('glpi_plugin_formcreator_forms_answers', 'requester_id'); - $migration->addKey('glpi_plugin_formcreator_forms_answers', 'validator_id'); - $migration->addField('glpi_plugin_formcreator_forms_answers', 'is_deleted', 'bool'); - $migration->migrationOneTable('glpi_plugin_formcreator_forms_answers'); -} - -function plugin_formcreator_updateForm_Profile_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Form_Profiles - $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_profiles"); - - if ($DB->tableExists('glpi_plugin_formcreator_formprofiles')) { - $migration->renameTable('glpi_plugin_formcreator_formprofiles', 'glpi_plugin_formcreator_forms_profiles'); - } - - // change fk for profiles - if ($DB->fieldExists('glpi_plugin_formcreator_forms_profiles', 'plugin_formcreator_profiles_id', false)) { - $migration->changeField('glpi_plugin_formcreator_forms_profiles', 'plugin_formcreator_profiles_id', 'profiles_id', 'integer'); - } - - // redo an id key - if (!$DB->fieldExists('glpi_plugin_formcreator_forms_profiles', 'id', false)) { - $DB->query("ALTER TABLE 'glpi_plugin_formcreator_forms_profiles' DROP PRIMARY KEY"); - $migration->addField('glpi_plugin_formcreator_forms_profiles', 'id', 'autoincrement'); - $migration->addKey('glpi_plugin_formcreator_forms_profiles', 'id', 'id', 'PRIMARY KEY'); - $migration->addKey('glpi_plugin_formcreator_forms_profiles', - ['plugin_formcreator_forms_id', 'profiles_id'], - 'unicity', - 'UNIQUE KEY'); - } - - // add uuid to validator - if (!$DB->fieldExists('glpi_plugin_formcreator_forms_profiles', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_forms_profiles', 'uuid', 'string'); - } - $migration->migrationOneTable('glpi_plugin_formcreator_forms_profiles'); - - // fill missing uuid - $obj = new PluginFormcreatorForm_Profile(); - $all_form_profiles = $obj->find("uuid IS NULL"); - foreach ($all_form_profiles as $form_profiles_id => $form_profile) { - $obj->update([ - 'id' => $form_profiles_id, - 'uuid' => plugin_formcreator_getUuid() - ]); - } -} - -function plugin_formcreator_updateFormValidator_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Form_Validators - $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_validators"); - - // Convert the old relation in glpi_plugin_formcreator_formvalidators table - if ($DB->tableExists('glpi_plugin_formcreator_formvalidators')) { - $table_form = PluginFormcreatorForm::getTable(); - $old_table = 'glpi_plugin_formcreator_formvalidators'; - $query = "INSERT INTO `glpi_plugin_formcreator_forms_validators` (`plugin_formcreator_forms_id`, `itemtype`, `items_id`) - SELECT - `$old_table`.`forms_id`, - IF(`validation_required` = '".PluginFormcreatorForm_Validator::VALIDATION_USER."', 'User', 'Group'), - `$old_table`.`users_id` - FROM `$old_table` - LEFT JOIN `$table_form` ON (`$table_form`.`id` = `$old_table`.`forms_id`) - WHERE `validation_required` > 1"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - $migration->displayMessage('Backing up table glpi_plugin_formcreator_formvalidators'); - $migration->renameTable('glpi_plugin_formcreator_formvalidators', 'glpi_plugin_formcreator_formvalidators_backup'); - } - - // add uuid to validator - if (!$DB->fieldExists('glpi_plugin_formcreator_forms_validators', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_forms_validators', 'uuid', 'string'); - $migration->migrationOneTable('glpi_plugin_formcreator_forms_validators'); - } - - // fill missing uuid - $obj = new PluginFormcreatorForm_Validator(); - $all_validators = $obj->find("uuid IS NULL"); - foreach ($all_validators as $validators_id => $validator) { - $obj->update(['id' => $validators_id, - 'uuid' => plugin_formcreator_getUuid()]); - } -} - -function plugin_formcreator_updateForm_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Forms - $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms"); - - // Migration from previous version - if ($DB->fieldExists('glpi_plugin_formcreator_forms', 'cat', false) - || !$DB->fieldExists('glpi_plugin_formcreator_forms', 'plugin_formcreator_categories_id', false)) { - $migration->addField('glpi_plugin_formcreator_forms', 'plugin_formcreator_categories_id', - 'integer', ['value' => '0']); - } - - // Migration from previous version - if (!$DB->fieldExists('glpi_plugin_formcreator_forms', 'validation_required', false)) { - $migration->addField('glpi_plugin_formcreator_forms', 'validation_required', 'bool', ['value' => '0']); - } - - // Migration from previous version - if (!$DB->fieldExists('glpi_plugin_formcreator_forms', 'requesttype', false)) { - $migration->addField('glpi_plugin_formcreator_forms', 'access_rights', 'bool', ['value' => '1']); - $migration->addField('glpi_plugin_formcreator_forms', 'requesttype', 'integer', ['value' => '0']); - $migration->addField('glpi_plugin_formcreator_forms', 'description', 'string'); - $migration->addField('glpi_plugin_formcreator_forms', 'helpdesk_home', 'bool', ['value' => '0']); - $migration->addField('glpi_plugin_formcreator_forms', 'is_deleted', 'bool', ['value' => '0']); - } - $migration->migrationOneTable('glpi_plugin_formcreator_forms'); - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - $query = "SELECT `id`, `name`, `description`, `content` FROM `glpi_plugin_formcreator_forms`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_forms` SET - `name` = '" . plugin_formcreator_encode($line['name']) . "', - `description` = '" . plugin_formcreator_encode($line['description']) . "', - `content` = '" . plugin_formcreator_encode($line['content']) . "' - WHERE `id` = " . (int) $line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - /** - * Add natural language search - * Add form usage counter - * - * @since 0.90-1.4 - */ - // An error may occur if the Search index does not exists - // This is not critical as we need to (re) create it - If (isIndex('glpi_plugin_formcreator_forms', 'Search')) { - $query = "ALTER TABLE `glpi_plugin_formcreator_forms` DROP INDEX `Search`"; - $DB->query($query); - } - - // Re-add FULLTEXT index - $query = "ALTER TABLE `glpi_plugin_formcreator_forms` ADD FULLTEXT INDEX `Search` (`name`, `description`)"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - $migration->addField('glpi_plugin_formcreator_forms', 'usage_count', 'integer', [ - 'after' => 'validation_required', - 'value' => '0' - ]); - $migration->addField('glpi_plugin_formcreator_forms', 'is_default', 'bool', [ - 'after' => 'usage_count', - 'value' => '0' - ]); - - // add uuid to forms - if (!$DB->fieldExists('glpi_plugin_formcreator_forms', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_forms', 'uuid', 'string', ['after' => 'is_default']); - } - - $migration->addKey('glpi_plugin_formcreator_forms', 'entities_id'); - $migration->addKey('glpi_plugin_formcreator_forms', 'plugin_formcreator_categories_id'); - $migration->migrationOneTable('glpi_plugin_formcreator_forms'); - - // fill missing uuid (force update of forms, see PluginFormcreatorForm::prepareInputForUpdate) - $obj = new PluginFormcreatorForm(); - $all_forms = $obj->find("uuid IS NULL"); - foreach ($all_forms as $forms_id => $form) { - $obj->update(['id' => $forms_id]); - } - unset($obj); -} - -function plugin_formcreator_updateHeader_2_5(Migration $migration) { - // Drop Headers table - $migration->displayMessage("Drop glpi_plugin_formcreator_headers"); - $migration->dropTable('glpi_plugin_formcreator_headers'); -} - -function plugin_formcreator_updateIssue_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Issues - $migration->displayMessage("Upgrade glpi_plugin_formcreator_issues"); - - $DB->query("DROP VIEW IF EXISTS `glpi_plugin_formcreator_issues`"); - - $query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_issues` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `display_id` VARCHAR(255) NOT NULL, - `original_id` INT(11) NOT NULL DEFAULT '0', - `sub_itemtype` VARCHAR(100) NOT NULL DEFAULT '', - `name` VARCHAR(244) NOT NULL DEFAULT '', - `status` VARCHAR(244) NOT NULL DEFAULT '', - `date_creation` DATETIME NOT NULL, - `date_mod` DATETIME NOT NULL, - `entities_id` INT(11) NOT NULL DEFAULT '0', - `is_recursive` TINYINT(1) NOT NULL DEFAULT '0', - `requester_id` INT(11) NOT NULL DEFAULT '0', - `validator_id` INT(11) NOT NULL DEFAULT '0', - `comment` TEXT NULL COLLATE 'utf8_unicode_ci', - PRIMARY KEY (`id`), - INDEX `original_id_sub_itemtype` (`original_id`, `sub_itemtype`), - INDEX `entities_id` (`entities_id`), - INDEX `requester_id` (`requester_id`), - INDEX `validator_id` (`validator_id`) - ) - COLLATE='utf8_unicode_ci' - ENGINE=MyISAM"; - $DB->query($query) or die ($DB->error()); - CronTask::Register('PluginFormcreatorIssue', 'SyncIssues', HOUR_TIMESTAMP, - [ - 'comment' => __('Formcreator - Sync service catalog issues', 'formcreator'), - 'mode' => CronTask::MODE_EXTERNAL - ] - ); - - $query = "SELECT DISTINCT - NULL AS `id`, - CONCAT('f_',`fanswer`.`id`) AS `display_id`, - `fanswer`.`id` AS `original_id`, - 'PluginFormcreatorForm_Answer' AS `sub_itemtype`, - `f`.`name` AS `name`, - `fanswer`.`status` AS `status`, - `fanswer`.`request_date` AS `date_creation`, - `fanswer`.`request_date` AS `date_mod`, - `fanswer`.`entities_id` AS `entities_id`, - `fanswer`.`is_recursive` AS `is_recursive`, - `fanswer`.`requester_id` AS `requester_id`, - `fanswer`.`validator_id` AS `validator_id`, - `fanswer`.`comment` AS `comment` - FROM `glpi_plugin_formcreator_forms_answers` AS `fanswer` - LEFT JOIN `glpi_plugin_formcreator_forms` AS `f` - ON`f`.`id` = `fanswer`.`plugin_formcreator_forms_id` - LEFT JOIN `glpi_items_tickets` AS `itic` - ON `itic`.`items_id` = `fanswer`.`id` - AND `itic`.`itemtype` = 'PluginFormcreatorForm_Answer' - WHERE `fanswer`.`is_deleted` = '0' - GROUP BY `original_id` - HAVING COUNT(`itic`.`tickets_id`) != 1 - UNION - SELECT DISTINCT - NULL AS `id`, - CONCAT('t_',`tic`.`id`) AS `display_id`, - `tic`.`id` AS `original_id`, - 'Ticket' AS `sub_itemtype`, - `tic`.`name` AS `name`, - `tic`.`status` AS `status`, - `tic`.`date` AS `date_creation`, - `tic`.`date_mod` AS `date_mod`, - `tic`.`entities_id` AS `entities_id`, - 0 AS `is_recursive`, - `tic`.`users_id_recipient` AS `requester_id`, - 0 AS `validator_id`, - `tic`.`content` AS `comment` - FROM `glpi_tickets` AS `tic` - LEFT JOIN `glpi_items_tickets` AS `itic` - ON `itic`.`tickets_id` = `tic`.`id` - AND `itic`.`itemtype` = 'PluginFormcreatorForm_Answer' - WHERE `tic`.`is_deleted` = 0 - GROUP BY `original_id` - HAVING COUNT(`itic`.`items_id`) <= 1"; - - $countQuery = "SELECT COUNT(*) AS `cpt` FROM ($query) AS `issues`"; - $result = $DB->query($countQuery); - if ($result !== false) { - $count = $DB->fetch_assoc($result); - $table = PluginFormcreatorIssue::getTable(); - if (countElementsInTable($table) != $count['cpt']) { - if ($DB->query("TRUNCATE `$table`")) { - $DB->query("INSERT INTO `$table` SELECT * FROM ($query) as `dt`"); - $volume = 1; - } - } - } -} - -function plugin_formcreator_updateQuestionCondition_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Question conditions - $migration->displayMessage("Upgrade glpi_plugin_formcreator_questions_conditions"); - - // Migration 0.85-1.0 => 0.85-1.1 - if ($DB->fieldExists('glpi_plugin_formcreator_questions', 'show_type', false)) { - // Migrate date from "questions" table to "questions_conditions" table - $query = "SELECT `id`, `show_type`, `show_field`, `show_condition`, `show_value` - FROM `glpi_plugin_formcreator_questions`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $questionId = $line['id']; - switch ($line['show_type']) { - case 'hide' : - $show_rule = 'hidden'; - break; - default: - $show_rule = 'always'; - } - switch ($line['show_condition']) { - case 'notequal' : - $show_condition = '!='; - break; - case 'lower' : - $show_condition = '<'; - break; - case 'greater' : - $show_condition = '>'; - break; - default: - $show_condition = '=='; - } - - $show_field = empty($line['show_field']) ? 'NULL' : $line['show_field']; - - $query_udate = "UPDATE `glpi_plugin_formcreator_questions` SET - `show_rule` = '$show_rule' - WHERE `id` = '$questionId'"; - $DB->query($query_udate) or plugin_formcreator_upgrade_error($migration); - - $query_udate = "INSERT INTO `glpi_plugin_formcreator_questions_conditions` SET - `plugin_formcreator_questions_id` = '$questionId', - `show_field` = '$show_field', - `show_condition` = '$show_condition', - `show_value` = '" . Toolbox::addslashes_deep($line['show_value']) . "'"; - $DB->query($query_udate) or plugin_formcreator_upgrade_error($migration); - } - - // Delete old fields - $migration->dropField('glpi_plugin_formcreator_questions', 'show_type'); - $migration->dropField('glpi_plugin_formcreator_questions', 'show_field'); - $migration->dropField('glpi_plugin_formcreator_questions', 'show_condition'); - $migration->dropField('glpi_plugin_formcreator_questions', 'show_value'); - } - - // Migrate "question_conditions" table - $query = "SELECT `id`, `show_value` - FROM `glpi_plugin_formcreator_questions_conditions`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_questions_conditions` SET - `show_value` = '" . plugin_formcreator_encode($line['show_value'], false) . "' - WHERE `id` = " . $line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_questions_conditions', 'order', false)) { - $migration->addField('glpi_plugin_formcreator_questions_conditions', 'order', 'integer', ['after' => 'show_logic', 'value' => '1']); - $migration->migrationOneTable('glpi_plugin_formcreator_questions_conditions'); - } - - $enum_logic = "'".implode("', '", array_keys(PluginFormcreatorQuestion_Condition::getEnumShowLogic()))."'"; - $current_enum_show_logic = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_questions_conditions', 'show_logic'); - if (count($current_enum_show_logic) != count(PluginFormcreatorQuestion_Condition::getEnumShowLogic())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_questions_conditions` - CHANGE COLUMN `show_logic` `show_logic` - ENUM($enum_logic) - NULL DEFAULT NULL"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - // add uuid to questions conditions - if (!$DB->fieldExists('glpi_plugin_formcreator_questions_conditions', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_questions_conditions', 'uuid', 'string'); - $migration->migrationOneTable('glpi_plugin_formcreator_questions_conditions'); - } - - $migration->addKey('glpi_plugin_formcreator_questions_conditions', 'plugin_formcreator_questions_id'); - - // fill missing uuid (force update of questions, see PluginFormcreatorQuestoin_Condition::prepareInputForUpdate) - $condition_obj = new PluginFormcreatorQuestion_Condition(); - $all_conditions = $condition_obj->find("uuid IS NULL"); - foreach ($all_conditions as $conditions_id => $condition) { - $condition_obj->update(['id' => $conditions_id, - 'uuid' => plugin_formcreator_getUuid()]); - } -} - -function plugin_formcreator_updateQuestion_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Questions - $migration->displayMessage("Upgrade glpi_plugin_formcreator_questions"); - - // Migration 0.83-1.0 => 0.85-1.0 - if (!$DB->fieldExists('glpi_plugin_formcreator_questions', 'fieldtype', false)) { - // Migration from previous version - $query = "ALTER TABLE `glpi_plugin_formcreator_questions` - ADD `fieldtype` varchar(30) NOT NULL DEFAULT 'text', - ADD `show_type` enum ('show', 'hide') NOT NULL DEFAULT 'show', - ADD `show_field` int(11) DEFAULT NULL, - ADD `show_condition` enum('equal','notequal','lower','greater') COLLATE utf8_unicode_ci DEFAULT NULL, - ADD `show_value` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, - ADD `required` tinyint(1) NOT NULL DEFAULT '0', - ADD `show_empty` tinyint(1) NOT NULL DEFAULT '0', - ADD `default_values` text COLLATE utf8_unicode_ci, - ADD `values` text COLLATE utf8_unicode_ci, - ADD `range_min` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, - ADD `range_max` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, - ADD `regex` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, - CHANGE `content` `description` text COLLATE utf8_unicode_ci NOT NULL, - CHANGE `position` `order` int(11) NOT NULL DEFAULT '0';"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - // order start from 1 instead of 0 - $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `order` = `order` + 1;") or plugin_formcreator_upgrade_error($migration); - - // Match new type - $query = "SELECT `id`, `type`, `data`, `option` - FROM `glpi_plugin_formcreator_questions`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $data = json_decode($line['data']); - $options = json_decode($line['option']); - - $fieldtype = 'text'; - $values = ''; - $default = ''; - $regex = ''; - $required = 0; - - if (isset($data->value) && !empty($data->value)) { - if (is_object($data->value)) { - foreach ($data->value as $value) { - if (!empty($value)) { - $values .= urldecode($value) . "\r\n"; - } - } - } else { - $values .= urldecode($data->value); - } - } - - switch ($line['type']) { - case '1': - $fieldtype = 'text'; - - if (isset($options->type)) { - switch ($options->type) { - case '2': - $required = 1; - break; - case '3': - $regex = '[[:alpha:]]'; - break; - case '4': - $fieldtype = 'float'; - break; - case '5': - $regex = urldecode($options->value); - // Add leading and trailing regex marker (automaticaly added in V1) - if (substr($regex, 0, 1) != '/') { - $regex = '/' . $regex; - } - if (substr($regex, -1, 1) != '/') { - $regex = $regex . '/'; - } - break; - case '6': - $fieldtype = 'email'; - break; - case '7': - $fieldtype = 'date'; - break; - } - } - $default_values = $values; - $values = ''; - break; - - case '2': - $fieldtype = 'select'; - break; - - case '3': - $fieldtype = 'checkboxes'; - break; - - case '4': - $fieldtype = 'textarea'; - if (isset($options->type) && ($options->type == 2)) { - $required = 1; - } - $default_values = $values; - $values = ''; - break; - - case '5': - $fieldtype = 'file'; - break; - - case '8': - $fieldtype = 'select'; - break; - - case '9': - $fieldtype = 'select'; - break; - - case '10': - $fieldtype = 'dropdown'; - break; - - default : - break; - } - - $query_udate = "UPDATE `glpi_plugin_formcreator_questions` SET - `fieldtype` = '" . $fieldtype . "', - `values` = '" . addslashes(htmlspecialchars($values)) . "', - `default_values` = '" . addslashes(htmlspecialchars($default)) . "', - `regex` = '" . $regex . "', - `required` = " . (int) $required . " - WHERE `id` = " . $line['id']; - $DB->query($query_udate) or plugin_formcreator_upgrade_error($migration); - } - - $migration->dropField('glpi_plugin_formcreator_questions', 'type'); - $migration->dropField('glpi_plugin_formcreator_questions', 'data'); - $migration->dropField('glpi_plugin_formcreator_questions', 'option'); - $migration->dropField('glpi_plugin_formcreator_questions', 'plugin_formcreator_forms_id'); - } - - // Migration 0.85-1.0 => 0.85-1.1 - if ($DB->fieldExists('glpi_plugin_formcreator_questions', 'show_type', false)) { - - // Fix type of section ID - if (!$DB->fieldExists('glpi_plugin_formcreator_questions', 'show_rule')) { - $query = "ALTER TABLE `glpi_plugin_formcreator_questions` - CHANGE `plugin_formcreator_sections_id` `plugin_formcreator_sections_id` INT NOT NULL, - ADD `show_rule` enum('always','hidden','shown') NOT NULL DEFAULT 'always'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - // Migrate "questions" table - $query = "SELECT `id`, `name`, `values`, `default_values`, `description` - FROM `glpi_plugin_formcreator_questions`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_questions` SET - `name` = '" . addslashes(plugin_formcreator_encode($line['name'])) . "', - `values` = '" . addslashes(plugin_formcreator_encode($line['values'])) . "', - `default_values` = '" . addslashes(plugin_formcreator_encode($line['default_values'])) . "', - `description` = '" . addslashes(plugin_formcreator_encode($line['description'])) . "' - WHERE `id` = " . $line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - /** - * Add natural language search - * - * @since 0.90-1.4 - */ - // An error may occur if the Search index does not exists - // This is not critical as we need to (re) create it - If (isIndex('glpi_plugin_formcreator_questions', 'Search')) { - $query = "ALTER TABLE `glpi_plugin_formcreator_questions` DROP INDEX `Search`"; - $DB->query($query); - } - - // Re-add FULLTEXT index - $query = "ALTER TABLE `glpi_plugin_formcreator_questions` ADD FULLTEXT INDEX `Search` (`name`, `description`)"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - // add uuid to questions - if (!$DB->fieldExists('glpi_plugin_formcreator_questions', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_questions', 'uuid', 'string'); - $migration->migrationOneTable('glpi_plugin_formcreator_questions'); - } - - $migration->addKey('glpi_plugin_formcreator_questions', 'plugin_formcreator_sections_id'); - - // fill missing uuid (force update of questions, see PlugiinFormcreatorQuestion::prepareInputForUpdate) - $obj = new PluginFormcreatorQuestion(); - $all_questions = $obj->find("uuid IS NULL"); - foreach ($all_questions as $questions_id => $question) { - $obj->update(['id' => $questions_id]); - } -} - -function plugin_formcreator_updateSection_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Sections - $migration->displayMessage("Upgrade glpi_plugin_formcreator_sections"); - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - $query = "SELECT `id`, `name` - FROM `glpi_plugin_formcreator_sections`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `glpi_plugin_formcreator_sections` SET - `name` = '".plugin_formcreator_encode($line['name'])."' - WHERE `id` = ".$line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - - // Migration from previous version => Remove useless target field - if ($DB->fieldExists('glpi_plugin_formcreator_sections', 'plugin_formcreator_targets_id', false)) { - $migration->dropField('glpi_plugin_formcreator_sections', 'plugin_formcreator_targets_id'); - } - - // Migration from previous version => Rename "position" into "order" and start order from 1 instead of 0 - if ($DB->fieldExists('glpi_plugin_formcreator_sections', 'position', false)) { - $DB->query("ALTER TABLE `glpi_plugin_formcreator_sections` CHANGE `position` `order` INT(11) NOT NULL DEFAULT '0';"); - $DB->query("UPDATE `glpi_plugin_formcreator_sections` SET `order` = `order` + 1;"); - } - - // Migration from previous version => Update Question table, then create a "description" question from content - if ($DB->fieldExists('glpi_plugin_formcreator_sections', 'content', false)) { - // Increment the order of questions which are in a section with a description - $query = "UPDATE `PluginFormcreatorQuestion` - SET `order` = `order` + 1 - WHERE `plugin_formcreator_sections_id` IN ( - SELECT `id` - FROM `glpi_plugin_formcreator_sections` - WHERE `content` != '' - )"; - $DB->query($query); - - // Create description from content - $query = "INSERT INTO `PluginFormcreatorQuestion` ( - `plugin_formcreator_sections_id`, - `fieldtype`, - `name`, - `description`, - `order` - ) - SELECT - `id`, - 'description' AS fieldtype, - CONCAT('Description ', `id`) AS name, - `content`, - 1 AS `order` - FROM `glpi_plugin_formcreator_sections` - WHERE `content` != ''"; - $DB->query($query); - - // Delete content column - $DB->query("ALTER TABLE `glpi_plugin_formcreator_sections` DROP `content`;"); - } - - // add uuid to sections - if (!$DB->fieldExists('glpi_plugin_formcreator_sections', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_sections', 'uuid', 'string'); - $migration->migrationOneTable('glpi_plugin_formcreator_sections'); - } - - $migration->addKey('glpi_plugin_formcreator_sections', 'plugin_formcreator_forms_id'); - - // fill missing uuid (force update of sections, see PluginFormcreatorSection::prepareInputForUpdate) - $obj = new PluginFormcreatorSection(); - $all_sections = $obj->find("uuid IS NULL"); - foreach ($all_sections as $sections_id => $section) { - $obj->update(['id' => $sections_id]); - } -} - -function plugin_formcreator_updateTarget_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of targets - $migration->displayMessage("Upgrade glpi_plugin_formcreator_targets"); - - // Migration to 0.85-1.2.5 - if ($DB->fieldExists('glpi_plugin_formcreator_targets', 'plugin_formcreator_forms_id', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targets` - CHANGE `plugin_formcreator_forms_id` `plugin_formcreator_forms_id` INT NOT NULL;"; - $DB->query($query); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_targets', 'itemtype', false)) { - // Migration from version 1.5 to 1.6 - if (!$DB->fieldExists('glpi_plugin_formcreator_targets', 'type', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targets` - ADD `type` tinyint(1) NOT NULL default '2';"; - $DB->query($query); - } - - // Add new column for link with target items - $query = "ALTER TABLE `glpi_plugin_formcreator_targets` - ADD `itemtype` varchar(100) NOT NULL DEFAULT 'PluginFormcreatorTargetTicket', - ADD `items_id` int(11) NOT NULL DEFAULT 0;"; - $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` - FROM `glpi_plugin_formcreator_targets` t, `glpi_plugin_formcreator_forms` f - WHERE f.`id` = t.`plugin_formcreator_forms_id` - GROUP BY t.`urgency`, t.`priority`, t.`itilcategories_id`, t.`type`, f.`entities_id`"; - $result = $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - $i = 0; - while ($ligne = $DB->fetch_array($result)) { - $i++; - $id = $ligne['urgency'].$ligne['priority'].$ligne['itilcategories_id'].$ligne['type']; - - $template = new TicketTemplate(); - $template_id = $template->add([ - 'name' => 'Template Formcreator '.$i, - 'entities_id' => $ligne['entities_id'], - 'is_recursive' => 1, - ]); - - $predefinedField = new TicketTemplatePredefinedField(); - - // Urgency - if (!empty($ligne['urgency'])) { - $predefinedField->add([ - 'tickettemplates_id' => $template_id, - 'num' => 10, - 'value' => $ligne['urgency'], - ]); - } - - // Priority - if (!empty($ligne['priority'])) { - $predefinedField->add([ - 'tickettemplates_id' => $template_id, - 'num' => 3, - 'value' => $ligne['priority'], - ]); - } - - // Category - if (!empty($ligne['itilcategories_id'])) { - $predefinedField->add([ - 'tickettemplates_id' => $template_id, - 'num' => 7, - 'value' => $ligne['itilcategories_id'], - ]); - } - - // Type - if (!empty($ligne['type'])) { - $predefinedField->add([ - 'tickettemplates_id' => $template_id, - 'num' => 14, - 'value' => $ligne['type'], - ]); - } - - $_SESSION["formcreator_tmp"]["ticket_template"]["$id"] = $template_id; - } - - // Install or upgrade of TargetTicket is a prerequisite - $version = plugin_version_formcreator(); - plugin_formcreator_updateTarget_2_5($migration); - $table_targetticket = getTableForItemType('PluginFormcreatorTargetTicket'); - - // Convert targets to ticket templates only if at least one target extsis - if ($i > 0) { - // 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 - $query = "SELECT `id`, `name`, $mysql_case_template, `content` - FROM `glpi_plugin_formcreator_targets`;"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - // Insert target ticket - $query_insert = "INSERT INTO `$table_targetticket` SET - `name` = '".htmlspecialchars($line['name'])."', - `tickettemplates_id` = ".$line['tickettemplates_id'].", - `comment` = '".htmlspecialchars($line['content'])."'"; - $DB ->query($query_insert); - $targetticket_id = $DB->insert_id(); - - // Update target with target ticket id - $query_update = "UPDATE `glpi_plugin_formcreator_targets` - SET `items_id` = ".$targetticket_id." - WHERE `id` = ".$line['id']; - $DB->query($query_update); - } - } - - // Remove useless column content - $DB->query("ALTER TABLE `glpi_plugin_formcreator_targets` DROP `content`;"); - - /** - * Migration of special chars from previous versions - * - * @since 0.85-1.2.3 - */ - if ($DB->fieldExists($table_targetticket, 'comment')) { - $query = "SELECT `id`, `comment` - FROM `$table_targetticket`"; - $result = $DB->query($query); - while ($line = $DB->fetch_array($result)) { - $query_update = "UPDATE `$table_targetticket` SET - `comment` = '".plugin_formcreator_encode($line['comment'])."' - WHERE `id` = ".$line['id']; - $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); - } - } - } - - $migration->addKey('glpi_plugin_formcreator_targets', 'plugin_formcreator_forms_id'); - $migration->addKey('glpi_plugin_formcreator_targets', ['itemtype', 'items_id']); - - // add uuid to targets - if (!$DB->fieldExists('glpi_plugin_formcreator_targets', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_targets', 'uuid', 'string'); - $migration->migrationOneTable('glpi_plugin_formcreator_targets'); - } - - // fill missing uuid (force update of targets, see PluginFormcreatorTarget::prepareInputForUpdate) - $obj = new PluginFormcreatorTarget(); - $all_targets = $obj->find("uuid IS NULL"); - foreach ($all_targets as $targets_id => $target) { - $obj->update(['id' => $targets_id]); - } -} - -function plugin_formcreator_updateTargetChange_Actor_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Change actors - $migration->displayMessage("Upgrade glpi_plugin_formcreator_targetchanges_actors"); - - if (!$DB->tableExists('glpi_plugin_formcreator_targetchanges_actors')) { - $query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetchanges_actors` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `plugin_formcreator_targetchanges_id` int(11) NOT NULL, - `actor_role` enum('requester','observer','assigned') COLLATE utf8_unicode_ci NOT NULL, - `actor_type` enum('creator','validator','person','question_person','group','question_group','supplier','question_supplier','question_actors') COLLATE utf8_unicode_ci NOT NULL, - `actor_value` int(11) DEFAULT NULL, - `use_notification` tinyint(1) NOT NULL DEFAULT '1', - `uuid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, - PRIMARY KEY (`id`), - INDEX `plugin_formcreator_targetchanges_id` (`plugin_formcreator_targetchanges_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - $enum_actor_type = "'".implode("', '", array_keys(PluginFormcreatorTargetChange_Actor::getEnumActorType()))."'"; - $enum_actor_role = "'".implode("', '", array_keys(PluginFormcreatorTargetChange_Actor::getEnumRole()))."'"; - - $current_enum_actor_type = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges_actors', 'actor_type'); - if (count($current_enum_actor_type) != count(PluginFormcreatorTargetChange_Actor::getEnumActorType())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges_actors` - CHANGE COLUMN `actor_type` `actor_type` - ENUM($enum_actor_type) - NOT NULL"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - $current_enum_role = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges_actors', 'actor_role'); - if (count($current_enum_role) != count(PluginFormcreatorTargetChange_Actor::getEnumRole())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges_actors` - CHANGE COLUMN `actor_role` `actor_role` - ENUM($enum_actor_role) - NOT NULL"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - // fill missing uuid - $obj = new PluginFormcreatorTargetChange_Actor(); - $all_actor = $obj->find("uuid IS NULL"); - foreach ($all_actor as $actors_id => $actor) { - $obj->update(['id' => $actors_id, - 'uuid' => plugin_formcreator_getUuid()]); - } - -} - -function plugin_formcreator_updateTargetTicket_Actor_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of target ticket actors - $migration->displayMessage("Upgrade glpi_plugin_formcreator_targettickets_actors"); - - $enum_actor_type = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket_Actor::getEnumActorType()))."'"; - $enum_actor_role = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket_Actor::getEnumRole()))."'"; - - $current_enum_actor_type = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets_actors', 'actor_type'); - if (count($current_enum_actor_type) != count(PluginFormcreatorTargetTicket_Actor::getEnumActorType())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets_actors` - CHANGE COLUMN `actor_type` `actor_type` - ENUM($enum_actor_type) - NOT NULL"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - $current_enum_role = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets_actors', 'actor_role'); - if (count($current_enum_role) != count(PluginFormcreatorTargetTicket_Actor::getEnumRole())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets_actors` - CHANGE COLUMN `actor_role` `actor_role` - ENUM($enum_actor_role) - NOT NULL"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - // add uuid to actor - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets_actors', 'uuid', false)) { - $migration->addField('glpi_plugin_formcreator_targettickets_actors', 'uuid', 'string'); - $migration->migrationOneTable('glpi_plugin_formcreator_targettickets_actors'); - } - - // fill missing uuid - $obj = new PluginFormcreatorTargetTicket_Actor(); - $all_actor = $obj->find("uuid IS NULL"); - foreach ($all_actor as $actors_id => $actor) { - $obj->update(['id' => $actors_id, - 'uuid' => plugin_formcreator_getUuid()]); - } -} - -function plugin_formcreator_updateTargetTicket_2_5(Migration $migration) { - global $DB; - - // Legacy upgrade of Target tickets - $migration->displayMessage("Upgrade glpi_plugin_formcreator_targettickets"); - - $enum_destination_entity = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumDestinationEntity()))."'"; - $enum_tag_type = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumTagType()))."'"; - $enum_due_date_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumDueDateRule()))."'"; - $enum_urgency_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumUrgencyRule()))."'"; - $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumCategoryRule()))."'"; - - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'due_date_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - ADD `due_date_rule` ENUM($enum_due_date_rule) NULL DEFAULT NULL, - ADD `due_date_question` INT NULL DEFAULT NULL, - ADD `due_date_value` TINYINT NULL DEFAULT NULL, - ADD `due_date_period` ENUM('minute', 'hour', 'day', 'month') NULL DEFAULT NULL, - ADD `validation_followup` BOOLEAN NOT NULL DEFAULT TRUE;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - // Migration to Formcreator 0.90-1.4 - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'destination_entity', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - ADD `destination_entity` ENUM($enum_destination_entity) NOT NULL DEFAULT 'current', - ADD `destination_entity_value` int(11) NULL DEFAULT NULL;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_destination_entity = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'destination_entity'); - if (count($current_enum_destination_entity) != count(PluginFormcreatorTargetTicket::getEnumDestinationEntity())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - CHANGE COLUMN `destination_entity` `destination_entity` - ENUM($enum_destination_entity) - NOT NULL DEFAULT 'current'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` ALTER COLUMN `destination_entity` SET DEFAULT 'current'"; - $DB->query($query); - - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'tag_type', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - ADD `tag_type` ENUM($enum_tag_type) NOT NULL DEFAULT 'none', - ADD `tag_questions` VARCHAR(255) NOT NULL, - ADD `tag_specifics` VARCHAR(255) NOT NULL;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'urgency_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - ADD `urgency_rule` ENUM($enum_urgency_rule) NOT NULL DEFAULT 'none' AFTER `due_date_period`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_urgency_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'urgency_rule'); - if (count($current_enum_urgency_rule) != count(PluginFormcreatorTargetTicket::getEnumUrgencyRule())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - CHANGE COLUMN `urgency_rule` `urgency_rule` - ENUM($enum_urgency_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $migration->addField('glpi_plugin_formcreator_targettickets', 'urgency_question', 'integer', ['after' => 'urgency_rule']); - - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'category_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'category_rule'); - if (count($current_enum_category_rule) != count(PluginFormcreatorTargetTicket::getEnumCategoryRule())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` - CHANGE COLUMN `category_rule` `category_rule` - ENUM($enum_category_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $migration->addField('glpi_plugin_formcreator_targettickets', 'category_question', 'integer', ['after' => 'category_rule']); - - $migration->addKey('glpi_plugin_formcreator_targettickets', 'tickettemplates_id'); -} - -function plugin_formcreator_updateTitle_2_5(Migration $migration) { - // Drop Headers table - $migration->displayMessage("Drop glpi_plugin_formcreator_titles"); - $migration->dropTable('glpi_plugin_formcreator_titles'); -} - -function plugin_formcreator_updateTargetChange_2_5($migration) { - global $DB; - - // Legacy upgrade of Target changes - $migration->displayMessage("Upgrade glpi_plugin_formcreator_targetchanges"); - - $enum_destination_entity = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumDestinationEntity()))."'"; - $enum_tag_type = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumTagType()))."'"; - $enum_due_date_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumDueDateRule()))."'"; - $enum_urgency_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumUrgencyRule()))."'"; - $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumCategoryRule()))."'"; - - if (!$DB->tableExists('glpi_plugin_formcreator_targetchanges')) { - $query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetchanges` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(255) NOT NULL DEFAULT '', - `changetemplates_id` int(11) DEFAULT NULL, - `comment` text, - `impactcontent` text, - `controlistcontent` text, - `rolloutplancontent` text, - `backoutplancontent` text, - `checklistcontent` text, - `due_date_rule` enum($enum_due_date_rule) DEFAULT NULL, - `due_date_question` int(11) DEFAULT NULL, - `due_date_value` tinyint(4) DEFAULT NULL, - `due_date_period` enum('minute','hour','day','month') DEFAULT NULL, - `urgency_rule` enum($enum_urgency_rule) NOT NULL DEFAULT 'none', - `urgency_question` int(11) NOT NULL DEFAULT '0', - `validation_followup` tinyint(1) NOT NULL DEFAULT '1', - `destination_entity` enum($enum_destination_entity) NOT NULL DEFAULT 'requester', - `destination_entity_value` int(11) DEFAULT NULL, - `tag_type` enum($enum_tag_type) NOT NULL DEFAULT 'none', - `tag_questions` varchar(255) NOT NULL, - `tag_specifics` varchar(255) NOT NULL, - `category_rule` enum($enum_category_rule) NOT NULL DEFAULT 'none', - `category_question` int(11) NOT NULL DEFAULT '0', - PRIMARY KEY (`id`), - INDEX `changetemplates_id` (`changetemplates_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; - "; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'due_date_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `due_date_rule` ENUM($enum_due_date_rule) NULL DEFAULT NULL, - ADD `due_date_question` INT NULL DEFAULT NULL, - ADD `due_date_value` TINYINT NULL DEFAULT NULL, - ADD `due_date_period` ENUM('minute', 'hour', 'day', 'month') NULL DEFAULT NULL;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'destination_entity', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `destination_entity` ENUM($enum_destination_entity) NOT NULL DEFAULT 'current', - ADD `destination_entity_value` int(11) NULL DEFAULT NULL;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_destination_entity = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'destination_entity'); - if (count($current_enum_destination_entity) != count(PluginFormcreatorTargetChange::getEnumDestinationEntity())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - CHANGE COLUMN `destination_entity` `destination_entity` - ENUM($enum_destination_entity) - NOT NULL DEFAULT 'current'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` ALTER COLUMN `destination_entity` SET DEFAULT 'current'"; - $DB->query($query); - - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'tag_type', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `tag_type` ENUM($enum_tag_type) NOT NULL DEFAULT 'none', - ADD `tag_questions` VARCHAR(255) NOT NULL, - ADD `tag_specifics` VARCHAR(255) NOT NULL;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'urgency_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `urgency_rule` ENUM($enum_urgency_rule) NOT NULL DEFAULT 'none' AFTER `due_date_period`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_urgency_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'urgency_rule'); - if (count($current_enum_urgency_rule) != count(PluginFormcreatorTargetChange::getEnumUrgencyRule())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - CHANGE COLUMN `urgency_rule` `urgency_rule` - ENUM($enum_urgency_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $migration->addField('glpi_plugin_formcreator_targetchanges', 'urgency_question', 'integer', ['after' => 'urgency_rule']); - - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'category_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'category_rule'); - if (count($current_enum_category_rule) != count(PluginFormcreatorTargetChange::getEnumCategoryRule())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - CHANGE COLUMN `category_rule` `category_rule` - ENUM($enum_category_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $migration->addField('glpi_plugin_formcreator_targetchanges', 'category_question', 'integer', ['after' => 'category_rule']); -} diff --git a/install/update_2.5_2.6.php b/install/update_2.5_2.6.php deleted file mode 100644 index 65a603d27..000000000 --- a/install/update_2.5_2.6.php +++ /dev/null @@ -1,196 +0,0 @@ -. - * --------------------------------------------------------------------- - * @author Thierry Bugier - * @author Jérémy Moreau - * @copyright Copyright © 2011 - 2018 Teclib' - * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ - * @link https://github.com/pluginsGLPI/formcreator/ - * @link https://pluginsglpi.github.io/formcreator/ - * @link http://plugins.glpi-project.org/#/plugin/formcreator - * --------------------------------------------------------------------- - */ - -/** - * Upgrade any version of Formcreator >= 2.5.0 to 2.6.0 - * @param Migration $migration - */ -function plugin_formcreator_update_2_6(Migration $migration) { - global $DB; - - $migration->displayMessage("Upgrade to schema version 2.6"); - - $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_answers"); - - // update questions - $table = 'glpi_plugin_formcreator_questions'; - $migration->displayMessage("Upgrade $table"); - - $question = new PluginFormcreatorQuestion(); - $rows = $question->find("`fieldtype` = 'dropdown' AND `values` = 'ITILCategory'"); - foreach ($rows as $id => $row) { - $updatedValue = json_encode([ - 'itemtype' => $row['values'], - 'show_ticket_categories' => 'both', - 'show_ticket_categories_depth' => 0 - ]); - // Don't use update() method because the json will be HTML-entities-ified (see prepareInputForUpdate() ) - $query = "UPDATE `$table` SET `values`='$updatedValue' WHERE `id`='$id'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - // Update Form Answers - $table = 'glpi_plugin_formcreator_forms_answers'; - $migration->displayMessage("Upgrade $table"); - - $migration->addField($table, 'users_id_validator', 'integer', ['after' => 'requester_id']); - $migration->addField($table, 'groups_id_validator', 'integer', ['after' => 'users_id_validator']); - $migration->addKey($table, 'users_id_validator'); - $migration->addKey($table, 'groups_id_validator'); - $migration->migrationOneTable($table); - - $formTable = 'glpi_plugin_formcreator_forms'; - $query = "UPDATE `$table` - INNER JOIN `$formTable` ON (`$table`.`plugin_formcreator_forms_id` = `$formTable`.`id`) - SET `users_id_validator` = `validator_id` - WHERE `$formTable`.`validation_required` = '1'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - $query = "UPDATE `$table` - INNER JOIN `$formTable` ON (`$table`.`plugin_formcreator_forms_id` = `$formTable`.`id`) - SET `groups_id_validator` = `validator_id` - WHERE `$formTable`.`validation_required` = '2'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - - $migration->dropKey($table, 'validator_id'); - $migration->dropField($table, 'validator_id'); - - // add location rule - $enum_location_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumLocationRule()))."'"; - if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'location_rule', false)) { - $migration->addField( - 'glpi_plugin_formcreator_targettickets', - 'location_rule', - "ENUM($enum_location_rule) NOT NULL DEFAULT 'none'", - ['after' => 'category_question'] - ); - } else { - $current_enum_location_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'location_rule'); - if (count($current_enum_location_rule) != count(PluginFormcreatorTargetTicket::getEnumLocationRule())) { - $migration->changeField( - 'glpi_plugin_formcreator_targettickets', - 'location_rule', - 'location_rule', - "ENUM($enum_location_rule) NOT NULL DEFAULT 'none'", - ['after' => 'category_question'] - ); - } - } - $migration->addField('glpi_plugin_formcreator_targettickets', 'location_question', 'integer', ['after' => 'location_rule']); - - // Fix bad foreign key - $table = 'glpi_plugin_formcreator_answers'; - $migration->changeField($table, 'plugin_formcreator_question_id', 'plugin_formcreator_questions_id', 'integer'); - - $table = 'glpi_plugin_formcreator_items_targettickets'; - if (!$DB->tableExists($table)) { - $query = "CREATE TABLE `$table` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `plugin_formcreator_targettickets_id` int(11) NOT NULL DEFAULT '0', - `link` int(11) NOT NULL DEFAULT '0', - `itemtype` varchar(255) NOT NULL DEFAULT '', - `items_id` int(11) NOT NULL DEFAULT '0', - `uuid` varchar(255) DEFAULT NULL, - PRIMARY KEY (`id`), - INDEX `plugin_formcreator_targettickets_id` (`plugin_formcreator_targettickets_id`), - INDEX `item` (`itemtype`,`items_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - - // add uuid and generate for existing rows - $table = PluginFormcreatorTargetTicket::getTable(); - $migration->addField($table, 'uuid', 'string', ['after' => 'category_question']); - $migration->migrationOneTable($table); - $obj = new PluginFormcreatorTargetTicket(); - $all_targetTickets = $obj->find("`uuid` IS NULL"); - foreach ($all_targetTickets as $targetTicket) { - $targetTicket['_skip_checks'] = true; - $targetTicket['title'] = $targetTicket['name']; - $obj->update($targetTicket); - } - unset($obj); - - $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumCategoryRule()))."'"; - $table = 'glpi_plugin_formcreator_targettickets'; - if (!$DB->fieldExists($table, 'category_rule', false)) { - $query = "ALTER TABLE `$table` - ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues($table, 'category_rule'); - if (count($current_enum_category_rule) != count(PluginFormcreatorTargetTicket::getEnumCategoryRule())) { - $query = "ALTER TABLE `$table` - CHANGE COLUMN `category_rule` `category_rule` - ENUM($enum_category_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - - $table = 'glpi_plugin_formcreator_items_targetchanges'; - $enum_urgency_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumUrgencyRule()))."'"; - $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumCategoryRule()))."'"; - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'urgency_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `urgency_rule` ENUM($enum_urgency_rule) NOT NULL DEFAULT 'none' AFTER `due_date_period`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_urgency_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'urgency_rule'); - if (count($current_enum_urgency_rule) != count(PluginFormcreatorTargetChange::getEnumUrgencyRule())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - CHANGE COLUMN `urgency_rule` `urgency_rule` - ENUM($enum_urgency_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $migration->addField('glpi_plugin_formcreator_targetchanges', 'urgency_question', 'integer', ['after' => 'urgency_rule']); - - if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'category_rule', false)) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } else { - $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'category_rule'); - if (count($current_enum_category_rule) != count(PluginFormcreatorTargetChange::getEnumCategoryRule())) { - $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` - CHANGE COLUMN `category_rule` `category_rule` - ENUM($enum_category_rule) - NOT NULL DEFAULT 'none'"; - $DB->query($query) or plugin_formcreator_upgrade_error($migration); - } - } - $migration->addField('glpi_plugin_formcreator_targetchanges', 'category_question', 'integer', ['after' => 'category_rule']); - - $migration->executeMigration(); -} diff --git a/install/update_2.6.2_2.6.3.php b/install/update_2.6.2_2.6.3.php deleted file mode 100644 index c5dd34846..000000000 --- a/install/update_2.6.2_2.6.3.php +++ /dev/null @@ -1,88 +0,0 @@ -. - * --------------------------------------------------------------------- - * @author Thierry Bugier - * @author Jérémy Moreau - * @copyright Copyright © 2011 - 2018 Teclib' - * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ - * @link https://github.com/pluginsGLPI/formcreator/ - * @link https://pluginsglpi.github.io/formcreator/ - * @link http://plugins.glpi-project.org/#/plugin/formcreator - * --------------------------------------------------------------------- - */ - -/** - * @param Migration $migration - */ -function plugin_formcreator_update_2_6_3(Migration $migration) { - global $DB; - - // Change id of search option for status of form_answer - $table = 'glpi_displaypreferences'; - $query = "UPDATE `$table` SET `num`='8' WHERE `itemtype`='PluginFormcreatorForm_Answer' AND `num`='1'"; - $DB->query($query); - - // Remove abusive encding in sections - $table = 'glpi_plugin_formcreator_sections'; - $request = [ - 'FROM' => $table, - ]; - foreach ($DB->request($request) as $row) { - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); - $id = $row['id']; - $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); - } - - // Remove abusive encoding in targets - $table = 'glpi_plugin_formcreator_targets'; - $request = [ - 'FROM' => $table, - ]; - foreach ($DB->request($request) as $row) { - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); - $id = $row['id']; - $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); - } - - // Remove abusive encoding in target tickets - $table = 'glpi_plugin_formcreator_targettickets'; - $request = [ - 'FROM' => $table, - ]; - foreach ($DB->request($request) as $row) { - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); - $id = $row['id']; - $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); - } - - // Remove abusive encoding in target changes - $table = 'glpi_plugin_formcreator_targetchanges'; - $request = [ - 'FROM' => $table, - ]; - foreach ($DB->request($request) as $row) { - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); - $id = $row['id']; - $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); - } -} \ No newline at end of file diff --git a/install/update_2.6_2.7.php b/install/update_2.6_2.7.php deleted file mode 100644 index 1fd1060d6..000000000 --- a/install/update_2.6_2.7.php +++ /dev/null @@ -1,228 +0,0 @@ -. - * --------------------------------------------------------------------- - * - * @copyright Copyright © 2011 - 2018 Teclib' - * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ - * @link https://github.com/pluginsGLPI/formcreator/ - * @link https://pluginsglpi.github.io/formcreator/ - * @link http://plugins.glpi-project.org/#/plugin/formcreator - * --------------------------------------------------------------------- - */ -function plugin_formcreator_update_2_7(Migration $migration) { - global $DB; - - // Rename PluginFormcreatorForm_Answer into PluginFormcreatorFormAnswer - $displayPreference = new DisplayPreference(); - $DB->update( - 'glpi_displaypreferences', [ - 'itemtype' => 'PluginFormcreatorFormAnswer', - ], [ - 'itemtype' => 'PluginFormcreatorForm_Answer' - ] - ); - $DB->update( - 'glpi_items_tickets', [ - 'itemtype' => 'PluginFormcreatorFormAnswer', - ], [ - 'itemtype' => 'PluginFormcreatorForm_Answer' - ] - ); - $table = 'glpi_plugin_formcreator_answers'; - $migration->changeField( - $table, - 'plugin_formcreator_forms_answers', - 'plugin_formcreator_formanswers', - 'integer' - ); - $migration->migrationOneTable($table); - $migration->dropKey($table, 'plugin_formcreator_forms_answers_id'); - $migration->addKey($table, ['plugin_formcreator_formanswers_id'], 'plugin_formcreator_formanswers_id'); - - // Changes don't support templates, remove the relation - $table = 'glpi_plugin_formcreator_targetchanges'; - $migration->dropField($table, 'changetemplates_id'); - - // Migrate regex question parameters - $table = 'glpi_plugin_formcreator_questions'; - if ($DB->fieldExists($table, 'regex')) { - $request = [ - 'FROM' => $table, - 'WHERE' => ['fieldtype' => ['float', 'integer', 'text', 'textarea']] - ]; - foreach ($DB->request($request) as $row) { - $id = $row['id']; - $regex = $DB->escape($row['regex']); - $uuid = plugin_formcreator_getUuid(); - $DB->query("INSERT INTO `glpi_plugin_formcreator_questionregexes` - SET `plugin_formcreator_questions_id`='$id', `fieldname`='regex', `regex`='$regex', `uuid`='$uuid'" - ) or plugin_formcreator_upgrade_error($migration); - } - $migration->dropField($table, 'regex'); - } - - // Migrate range question parameters - $table = 'glpi_plugin_formcreator_questions'; - if ($DB->fieldExists($table, 'range_min')) { - $request = [ - 'FROM' => $table, - 'WHERE' => ['fieldtype' => ['float', 'integer', 'checkboxes', 'multiselect', 'text']] - ]; - foreach ($DB->request($request) as $row) { - $id = $row['id']; - $rangeMin = $DB->escape($row['range_min']); - $rangeMax = $DB->escape($row['range_max']); - $uuid = plugin_formcreator_getUuid(); - $DB->query("INSERT INTO `glpi_plugin_formcreator_questionranges` - SET `plugin_formcreator_questions_id`='$id', `fieldname`='range', `range_min`='$rangeMin', `range_max`='$rangeMax', `uuid`='$uuid'" - ) or plugin_formcreator_upgrade_error($migration); - } - $migration->dropField($table, 'range_min'); - $migration->dropField($table, 'range_max'); - - // decode html entities in answers - $request = [ - 'SELECT' => [ - 'glpi_plugin_formcreator_answers.*' - ], - 'FROM' => 'glpi_plugin_formcreator_answers', - 'INNER JOIN' => [ - 'glpi_plugin_formcreator_questions' => [ - 'FKEY' => [ - 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id', - 'glpi_plugin_formcreator_questions' => 'id' - ] - ] - ], - 'WHERE' => ['fieldtype' => 'textarea'] - ]; - foreach ($DB->request($request) as $row) { - $answer = Toolbox::addslashes_deep(html_entity_decode($row['answer'])); - $id = $row['id']; - $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer`='$answer' WHERE `id` = '$id'"); - } - } - - // decode html entities in question definitions - $request = [ - 'FROM' => 'glpi_plugin_formcreator_questions', - 'WHERE' => [ - 'fieldtype' => ['select', 'multiselect', 'checkboxes', 'radios'] - ] - ]; - foreach ($DB->request($request) as $row) { - $values = Toolbox::addslashes_deep(html_entity_decode($row['values'])); - $defaultValues = Toolbox::addslashes_deep(html_entity_decode($row['default_values'])); - $id = $row['id']; - $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `values` = '$values', `default_values` = '$defaultValues' WHERE `id` = '$id'"); - } - - // decode html entities in name of questions - foreach ($DB->request(['FROM' => 'glpi_plugin_formcreator_questions']) as $row) { - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'])); - $id = $row['id']; - $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `name`='$name' WHERE `id` = '$id'"); - } - - // Add properties for dropdown of ticket categories - $request = [ - 'FROM' => 'glpi_plugin_formcreator_questions', - 'WHERE' => [ - 'fieldtype' => 'dropdown' - ], - ]; - foreach ($DB->request($request) as $row) { - $values = json_decode($row['values'], true); - if ($values['itemtype'] === ITILCategory::class) { - if (!isset($values['show_ticket_categories'])) { - $values['show_ticket_categories'] = 'both'; - } - if (!isset($values['show_ticket_categories_depth'])) { - $values['show_ticket_categories_depth'] = '0'; - } - $id = $row['id']; - $values = json_encode($values); - $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `values`='$values' WHERE `id` = '$id'"); - } - } - - // multiple files upload per field - $request = [ - 'SELECT' => 'glpi_plugin_formcreator_answers.*', - 'FROM' => 'glpi_plugin_formcreator_answers', - 'LEFT JOIN' => [ - 'glpi_plugin_formcreator_questions' => [ - 'FKEY' => [ - 'glpi_plugin_formcreator_questions' => 'id', - 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id' - ] - ] - ], - 'WHERE' => [ - 'fieldtype' => 'file', - ] - ]; - foreach ($DB->request($request) as $row) { - if (!is_array(json_decode($row['answer'], true))) { - $id = $row['id']; - $answer = json_encode([$row['answer']]); - $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer` = '$answer' WHERE `id` = '$id'"); - } - } - - // Update target change columns - $table = 'glpi_plugin_formcreator_targetchanges'; - $migration->changeField($table, 'comment', 'content', 'longtext'); - $migration->changeField($table, 'impactcontent', 'impactcontent', 'longtext'); - $migration->changeField($table, 'controlistcontent', 'controlistcontent', 'longtext'); - $migration->changeField($table, 'rolloutplancontent', 'rolloutplancontent', 'longtext'); - $migration->changeField($table, 'backoutplancontent', 'backoutplancontent', 'longtext'); - $migration->changeField($table, 'checklistcontent', 'checklistcontent', 'longtext'); - - // Update target target columns - $table = 'glpi_plugin_formcreator_targettickets'; - $migration->changeField($table, 'comment', 'content', 'longtext'); - - // Reorder columns on some tables - $tables = [ - 'glpi_plugin_formcreator_forms', - 'glpi_plugin_formcreator_questions', - 'glpi_plugin_formcreator_sections', - 'glpi_plugin_formcreator_issues', - ]; - foreach ($tables as $table) { - $migration->changeField($table, 'name', 'name', 'string', ['after' => 'id']); - } - - //remove html entities in forms - $request = [ - 'FROM' => 'glpi_plugin_formcreator_forms', - ]; - foreach ($DB->request($request) as $row) { - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'])); - $description = Toolbox::addslashes_deep(html_entity_decode($row['description'])); - $content = Toolbox::addslashes_deep(html_entity_decode($row['content'])); - $id = $row['id']; - $DB->query("UPDATE `glpi_plugin_formcreator_forms` SET `name` = '$name', `description` = '$description', `content` = '$content' WHERE `id` = '$id'"); - } -} diff --git a/install/upgrade_to_2.5.php b/install/upgrade_to_2.5.php new file mode 100644 index 000000000..731e32d77 --- /dev/null +++ b/install/upgrade_to_2.5.php @@ -0,0 +1,1328 @@ +. + * --------------------------------------------------------------------- + * @author Thierry Bugier + * @author Jérémy Moreau + * @copyright Copyright © 2011 - 2018 Teclib' + * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ + * @link https://github.com/pluginsGLPI/formcreator/ + * @link https://pluginsglpi.github.io/formcreator/ + * @link http://plugins.glpi-project.org/#/plugin/formcreator + * --------------------------------------------------------------------- + */ + +/** + * Upgrade any version of Formcreator < 2.5.0 to 2.5.0 + * @param Migration $migration + */ +class PluginFormcreatorUpgradeTo2_5 { + /** + * @param Migration $migration + */ + public function upgrade(Migration $migration) { + global $DB; + + $migration->displayMessage("Upgrade to schema version 2.5"); + + $this->updateAnswer_2_5($migration); + $this->updateCategory_2_5($migration); + $this->updateForm_Answer_2_5($migration); + $this->updateForm_Profile_2_5($migration); + $this->updateFormValidator_2_5($migration); + $this->updateForm_2_5($migration); + $this->updateHeader_2_5($migration); + $this->updateIssue_2_5($migration); + $this->updateQuestionCondition_2_5($migration); + $this->updateQuestion_2_5($migration); + $this->updateSection_2_5($migration); + $this->updateTarget_2_5($migration); + $this->updateTargetChange_2_5($migration); + $this->updateTargetChange_Actor_2_5($migration); + $this->updateTargetTicket_Actor_2_5($migration); + $this->updateTargetTicket_2_5($migration); + $this->updateTitle_2_5($migration); + + $migration->executeMigration(); + } + + private function updateAnswer_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Answers + $migration->displayMessage("Upgrade glpi_plugin_formcreator_answers"); + // Update field type from previous version (Need answer to be text since text can be WYSIWING). + $query = "ALTER TABLE `glpi_plugin_formcreator_answers` CHANGE `answer` `answer` text;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + $query = "SELECT `id`, `answer` FROM `glpi_plugin_formcreator_answers`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_answers` SET + `answer` = '".addslashes($line['answer'])."' + WHERE `id` = ".$line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + //rename foreign key, to match table plugin_formcreator_forms_answers name + $migration->changeField('glpi_plugin_formcreator_answers', + 'plugin_formcreator_formanwers_id', + 'plugin_formcreator_forms_answers_id', + 'integer'); + + $migration->addKey('glpi_plugin_formcreator_answers', 'plugin_formcreator_forms_answers_id'); + $migration->addKey('glpi_plugin_formcreator_answers', 'plugin_formcreator_question_id'); + + $migration->migrationOneTable('glpi_plugin_formcreator_answers'); + } + + function updateCategory_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Categories + $migration->displayMessage("Upgrade glpi_plugin_formcreator_categories"); + + if ($DB->tableExists('glpi_plugin_formcreator_cats')) { + $query = "INSERT IGNORE INTO `glpi_plugin_formcreator_categories` (`id`, `name`) + SELECT `id`,`name` FROM glpi_plugin_formcreator_cats"; + $DB->query($query); + $DB->query("DROP TABLE glpi_plugin_formcreator_cats"); + } + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + $query = "SELECT `id`, `name`, `comment` FROM `glpi_plugin_formcreator_categories`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_categories` SET + `name` = '".plugin_formcreator_encode($line['name'], false)."', + `comment` = '".plugin_formcreator_encode($line['comment'], false)."' + WHERE `id` = ".$line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + /** + * Migrate categories to tree structure + * + * @since 0.90-1.4 + */ + if (!$DB->fieldExists('glpi_plugin_formcreator_categories', "knowbaseitemcategories_id")) { + $migration->addField('glpi_plugin_formcreator_categories', 'completename', 'string', [ + 'after' => 'comment' + ]); + $migration->addField('glpi_plugin_formcreator_categories', 'plugin_formcreator_categories_id', 'integer', [ + 'after' => 'completename' + ]); + $migration->addField('glpi_plugin_formcreator_categories', 'level', 'integer', [ + 'value' => 1, + 'after' => 'plugin_formcreator_categories_id' + ]); + $migration->addField('glpi_plugin_formcreator_categories', 'sons_cache', 'longtext', [ + 'after' => 'level' + ]); + $migration->addField('glpi_plugin_formcreator_categories', 'ancestors_cache', 'longtext', [ + 'after' => 'sons_cache' + ]); + $migration->addField('glpi_plugin_formcreator_categories', 'knowbaseitemcategories_id', 'integer', [ + 'after' => 'ancestors_cache' + ]); + + $migration->addKey('glpi_plugin_formcreator_categories', 'knowbaseitemcategories_id'); + $migration->addKey('glpi_plugin_formcreator_categories', 'plugin_formcreator_categories_id'); + $migration->migrationOneTable('glpi_plugin_formcreator_categories'); + $query = "UPDATE `glpi_plugin_formcreator_categories` SET `completename` = `name` WHERE `completename` = ''"; + $DB->query($query); + } + } + + function updateForm_Answer_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Form_Answers + $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_answers"); + + if ($DB->tableExists("glpi_plugin_formcreator_formanswers")) { + $migration->renameTable('glpi_plugin_formcreator_formanswers', 'glpi_plugin_formcreator_forms_answers'); + $itemTicket_table = Item_Ticket::getTable(); + $query = "UPDATE `$itemTicket_table` SET `itemtype` = 'PluginFormcreatorForm_Answer' WHERE `itemtype` = 'PluginFormcreatorFormanswer'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + $query = "SELECT `id`, `comment` FROM `glpi_plugin_formcreator_forms_answers`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_forms_answers` SET + `comment` = '" . plugin_formcreator_encode($line['comment']) . "' + WHERE `id` = " . $line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_forms_answers', 'name')) { + $query_update = 'ALTER TABLE `glpi_plugin_formcreator_forms_answers` ADD `name` VARCHAR(255) NOT NULL AFTER `id`'; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + // valdiator_id should not be set for waiting form answers + $query = "UPDATE `glpi_plugin_formcreator_forms_answers` + SET `validator_id` = '0' WHERE `status`='waiting'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + $migration->addKey('glpi_plugin_formcreator_forms_answers', 'plugin_formcreator_forms_id'); + $migration->addKey('glpi_plugin_formcreator_forms_answers', ['entities_id', 'is_recursive']); + $migration->addKey('glpi_plugin_formcreator_forms_answers', 'requester_id'); + $migration->addKey('glpi_plugin_formcreator_forms_answers', 'validator_id'); + $migration->addField('glpi_plugin_formcreator_forms_answers', 'is_deleted', 'bool'); + $migration->migrationOneTable('glpi_plugin_formcreator_forms_answers'); + } + + function updateForm_Profile_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Form_Profiles + $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_profiles"); + + if ($DB->tableExists('glpi_plugin_formcreator_formprofiles')) { + $migration->renameTable('glpi_plugin_formcreator_formprofiles', 'glpi_plugin_formcreator_forms_profiles'); + } + + // change fk for profiles + if ($DB->fieldExists('glpi_plugin_formcreator_forms_profiles', 'plugin_formcreator_profiles_id', false)) { + $migration->changeField('glpi_plugin_formcreator_forms_profiles', 'plugin_formcreator_profiles_id', 'profiles_id', 'integer'); + } + + // redo an id key + if (!$DB->fieldExists('glpi_plugin_formcreator_forms_profiles', 'id', false)) { + $DB->query("ALTER TABLE 'glpi_plugin_formcreator_forms_profiles' DROP PRIMARY KEY"); + $migration->addField('glpi_plugin_formcreator_forms_profiles', 'id', 'autoincrement'); + $migration->addKey('glpi_plugin_formcreator_forms_profiles', 'id', 'id', 'PRIMARY KEY'); + $migration->addKey('glpi_plugin_formcreator_forms_profiles', + ['plugin_formcreator_forms_id', 'profiles_id'], + 'unicity', + 'UNIQUE KEY'); + } + + // add uuid to validator + if (!$DB->fieldExists('glpi_plugin_formcreator_forms_profiles', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_forms_profiles', 'uuid', 'string'); + } + $migration->migrationOneTable('glpi_plugin_formcreator_forms_profiles'); + + // fill missing uuid + $obj = new PluginFormcreatorForm_Profile(); + $all_form_profiles = $obj->find("uuid IS NULL"); + foreach ($all_form_profiles as $form_profiles_id => $form_profile) { + $obj->update([ + 'id' => $form_profiles_id, + 'uuid' => plugin_formcreator_getUuid() + ]); + } + } + + function updateFormValidator_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Form_Validators + $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_validators"); + + // Convert the old relation in glpi_plugin_formcreator_formvalidators table + if ($DB->tableExists('glpi_plugin_formcreator_formvalidators')) { + $table_form = PluginFormcreatorForm::getTable(); + $old_table = 'glpi_plugin_formcreator_formvalidators'; + $query = "INSERT INTO `glpi_plugin_formcreator_forms_validators` (`plugin_formcreator_forms_id`, `itemtype`, `items_id`) + SELECT + `$old_table`.`forms_id`, + IF(`validation_required` = '".PluginFormcreatorForm_Validator::VALIDATION_USER."', 'User', 'Group'), + `$old_table`.`users_id` + FROM `$old_table` + LEFT JOIN `$table_form` ON (`$table_form`.`id` = `$old_table`.`forms_id`) + WHERE `validation_required` > 1"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + $migration->displayMessage('Backing up table glpi_plugin_formcreator_formvalidators'); + $migration->renameTable('glpi_plugin_formcreator_formvalidators', 'glpi_plugin_formcreator_formvalidators_backup'); + } + + // add uuid to validator + if (!$DB->fieldExists('glpi_plugin_formcreator_forms_validators', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_forms_validators', 'uuid', 'string'); + $migration->migrationOneTable('glpi_plugin_formcreator_forms_validators'); + } + + // fill missing uuid + $obj = new PluginFormcreatorForm_Validator(); + $all_validators = $obj->find("uuid IS NULL"); + foreach ($all_validators as $validators_id => $validator) { + $obj->update(['id' => $validators_id, + 'uuid' => plugin_formcreator_getUuid()]); + } + } + + function updateForm_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Forms + $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms"); + + // Migration from previous version + if ($DB->fieldExists('glpi_plugin_formcreator_forms', 'cat', false) + || !$DB->fieldExists('glpi_plugin_formcreator_forms', 'plugin_formcreator_categories_id', false)) { + $migration->addField('glpi_plugin_formcreator_forms', 'plugin_formcreator_categories_id', + 'integer', ['value' => '0']); + } + + // Migration from previous version + if (!$DB->fieldExists('glpi_plugin_formcreator_forms', 'validation_required', false)) { + $migration->addField('glpi_plugin_formcreator_forms', 'validation_required', 'bool', ['value' => '0']); + } + + // Migration from previous version + if (!$DB->fieldExists('glpi_plugin_formcreator_forms', 'requesttype', false)) { + $migration->addField('glpi_plugin_formcreator_forms', 'access_rights', 'bool', ['value' => '1']); + $migration->addField('glpi_plugin_formcreator_forms', 'requesttype', 'integer', ['value' => '0']); + $migration->addField('glpi_plugin_formcreator_forms', 'description', 'string'); + $migration->addField('glpi_plugin_formcreator_forms', 'helpdesk_home', 'bool', ['value' => '0']); + $migration->addField('glpi_plugin_formcreator_forms', 'is_deleted', 'bool', ['value' => '0']); + } + $migration->migrationOneTable('glpi_plugin_formcreator_forms'); + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + $query = "SELECT `id`, `name`, `description`, `content` FROM `glpi_plugin_formcreator_forms`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_forms` SET + `name` = '" . plugin_formcreator_encode($line['name']) . "', + `description` = '" . plugin_formcreator_encode($line['description']) . "', + `content` = '" . plugin_formcreator_encode($line['content']) . "' + WHERE `id` = " . (int) $line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + /** + * Add natural language search + * Add form usage counter + * + * @since 0.90-1.4 + */ + // An error may occur if the Search index does not exists + // This is not critical as we need to (re) create it + If (isIndex('glpi_plugin_formcreator_forms', 'Search')) { + $query = "ALTER TABLE `glpi_plugin_formcreator_forms` DROP INDEX `Search`"; + $DB->query($query); + } + + // Re-add FULLTEXT index + $query = "ALTER TABLE `glpi_plugin_formcreator_forms` ADD FULLTEXT INDEX `Search` (`name`, `description`)"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + $migration->addField('glpi_plugin_formcreator_forms', 'usage_count', 'integer', [ + 'after' => 'validation_required', + 'value' => '0' + ]); + $migration->addField('glpi_plugin_formcreator_forms', 'is_default', 'bool', [ + 'after' => 'usage_count', + 'value' => '0' + ]); + + // add uuid to forms + if (!$DB->fieldExists('glpi_plugin_formcreator_forms', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_forms', 'uuid', 'string', ['after' => 'is_default']); + } + + $migration->addKey('glpi_plugin_formcreator_forms', 'entities_id'); + $migration->addKey('glpi_plugin_formcreator_forms', 'plugin_formcreator_categories_id'); + $migration->migrationOneTable('glpi_plugin_formcreator_forms'); + + // fill missing uuid (force update of forms, see PluginFormcreatorForm::prepareInputForUpdate) + $obj = new PluginFormcreatorForm(); + $all_forms = $obj->find("uuid IS NULL"); + foreach ($all_forms as $forms_id => $form) { + $obj->update(['id' => $forms_id]); + } + unset($obj); + } + + function updateHeader_2_5(Migration $migration) { + // Drop Headers table + $migration->displayMessage("Drop glpi_plugin_formcreator_headers"); + $migration->dropTable('glpi_plugin_formcreator_headers'); + } + + function updateIssue_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Issues + $migration->displayMessage("Upgrade glpi_plugin_formcreator_issues"); + + $DB->query("DROP VIEW IF EXISTS `glpi_plugin_formcreator_issues`"); + + $query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_issues` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `display_id` VARCHAR(255) NOT NULL, + `original_id` INT(11) NOT NULL DEFAULT '0', + `sub_itemtype` VARCHAR(100) NOT NULL DEFAULT '', + `name` VARCHAR(244) NOT NULL DEFAULT '', + `status` VARCHAR(244) NOT NULL DEFAULT '', + `date_creation` DATETIME NOT NULL, + `date_mod` DATETIME NOT NULL, + `entities_id` INT(11) NOT NULL DEFAULT '0', + `is_recursive` TINYINT(1) NOT NULL DEFAULT '0', + `requester_id` INT(11) NOT NULL DEFAULT '0', + `validator_id` INT(11) NOT NULL DEFAULT '0', + `comment` TEXT NULL COLLATE 'utf8_unicode_ci', + PRIMARY KEY (`id`), + INDEX `original_id_sub_itemtype` (`original_id`, `sub_itemtype`), + INDEX `entities_id` (`entities_id`), + INDEX `requester_id` (`requester_id`), + INDEX `validator_id` (`validator_id`) + ) + COLLATE='utf8_unicode_ci' + ENGINE=MyISAM"; + $DB->query($query) or die ($DB->error()); + CronTask::Register('PluginFormcreatorIssue', 'SyncIssues', HOUR_TIMESTAMP, + [ + 'comment' => __('Formcreator - Sync service catalog issues', 'formcreator'), + 'mode' => CronTask::MODE_EXTERNAL + ] + ); + + $query = "SELECT DISTINCT + NULL AS `id`, + CONCAT('f_',`fanswer`.`id`) AS `display_id`, + `fanswer`.`id` AS `original_id`, + 'PluginFormcreatorForm_Answer' AS `sub_itemtype`, + `f`.`name` AS `name`, + `fanswer`.`status` AS `status`, + `fanswer`.`request_date` AS `date_creation`, + `fanswer`.`request_date` AS `date_mod`, + `fanswer`.`entities_id` AS `entities_id`, + `fanswer`.`is_recursive` AS `is_recursive`, + `fanswer`.`requester_id` AS `requester_id`, + `fanswer`.`validator_id` AS `validator_id`, + `fanswer`.`comment` AS `comment` + FROM `glpi_plugin_formcreator_forms_answers` AS `fanswer` + LEFT JOIN `glpi_plugin_formcreator_forms` AS `f` + ON`f`.`id` = `fanswer`.`plugin_formcreator_forms_id` + LEFT JOIN `glpi_items_tickets` AS `itic` + ON `itic`.`items_id` = `fanswer`.`id` + AND `itic`.`itemtype` = 'PluginFormcreatorForm_Answer' + WHERE `fanswer`.`is_deleted` = '0' + GROUP BY `original_id` + HAVING COUNT(`itic`.`tickets_id`) != 1 + UNION + SELECT DISTINCT + NULL AS `id`, + CONCAT('t_',`tic`.`id`) AS `display_id`, + `tic`.`id` AS `original_id`, + 'Ticket' AS `sub_itemtype`, + `tic`.`name` AS `name`, + `tic`.`status` AS `status`, + `tic`.`date` AS `date_creation`, + `tic`.`date_mod` AS `date_mod`, + `tic`.`entities_id` AS `entities_id`, + 0 AS `is_recursive`, + `tic`.`users_id_recipient` AS `requester_id`, + 0 AS `validator_id`, + `tic`.`content` AS `comment` + FROM `glpi_tickets` AS `tic` + LEFT JOIN `glpi_items_tickets` AS `itic` + ON `itic`.`tickets_id` = `tic`.`id` + AND `itic`.`itemtype` = 'PluginFormcreatorForm_Answer' + WHERE `tic`.`is_deleted` = 0 + GROUP BY `original_id` + HAVING COUNT(`itic`.`items_id`) <= 1"; + + $countQuery = "SELECT COUNT(*) AS `cpt` FROM ($query) AS `issues`"; + $result = $DB->query($countQuery); + if ($result !== false) { + $count = $DB->fetch_assoc($result); + $table = PluginFormcreatorIssue::getTable(); + if (countElementsInTable($table) != $count['cpt']) { + if ($DB->query("TRUNCATE `$table`")) { + $DB->query("INSERT INTO `$table` SELECT * FROM ($query) as `dt`"); + $volume = 1; + } + } + } + } + + function updateQuestionCondition_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Question conditions + $migration->displayMessage("Upgrade glpi_plugin_formcreator_questions_conditions"); + + // Migration 0.85-1.0 => 0.85-1.1 + if ($DB->fieldExists('glpi_plugin_formcreator_questions', 'show_type', false)) { + // Migrate date from "questions" table to "questions_conditions" table + $query = "SELECT `id`, `show_type`, `show_field`, `show_condition`, `show_value` + FROM `glpi_plugin_formcreator_questions`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $questionId = $line['id']; + switch ($line['show_type']) { + case 'hide' : + $show_rule = 'hidden'; + break; + default: + $show_rule = 'always'; + } + switch ($line['show_condition']) { + case 'notequal' : + $show_condition = '!='; + break; + case 'lower' : + $show_condition = '<'; + break; + case 'greater' : + $show_condition = '>'; + break; + default: + $show_condition = '=='; + } + + $show_field = empty($line['show_field']) ? 'NULL' : $line['show_field']; + + $query_udate = "UPDATE `glpi_plugin_formcreator_questions` SET + `show_rule` = '$show_rule' + WHERE `id` = '$questionId'"; + $DB->query($query_udate) or plugin_formcreator_upgrade_error($migration); + + $query_udate = "INSERT INTO `glpi_plugin_formcreator_questions_conditions` SET + `plugin_formcreator_questions_id` = '$questionId', + `show_field` = '$show_field', + `show_condition` = '$show_condition', + `show_value` = '" . Toolbox::addslashes_deep($line['show_value']) . "'"; + $DB->query($query_udate) or plugin_formcreator_upgrade_error($migration); + } + + // Delete old fields + $migration->dropField('glpi_plugin_formcreator_questions', 'show_type'); + $migration->dropField('glpi_plugin_formcreator_questions', 'show_field'); + $migration->dropField('glpi_plugin_formcreator_questions', 'show_condition'); + $migration->dropField('glpi_plugin_formcreator_questions', 'show_value'); + } + + // Migrate "question_conditions" table + $query = "SELECT `id`, `show_value` + FROM `glpi_plugin_formcreator_questions_conditions`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_questions_conditions` SET + `show_value` = '" . plugin_formcreator_encode($line['show_value'], false) . "' + WHERE `id` = " . $line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_questions_conditions', 'order', false)) { + $migration->addField('glpi_plugin_formcreator_questions_conditions', 'order', 'integer', ['after' => 'show_logic', 'value' => '1']); + $migration->migrationOneTable('glpi_plugin_formcreator_questions_conditions'); + } + + $enum_logic = "'".implode("', '", array_keys(PluginFormcreatorQuestion_Condition::getEnumShowLogic()))."'"; + $current_enum_show_logic = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_questions_conditions', 'show_logic'); + if (count($current_enum_show_logic) != count(PluginFormcreatorQuestion_Condition::getEnumShowLogic())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_questions_conditions` + CHANGE COLUMN `show_logic` `show_logic` + ENUM($enum_logic) + NULL DEFAULT NULL"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + // add uuid to questions conditions + if (!$DB->fieldExists('glpi_plugin_formcreator_questions_conditions', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_questions_conditions', 'uuid', 'string'); + $migration->migrationOneTable('glpi_plugin_formcreator_questions_conditions'); + } + + $migration->addKey('glpi_plugin_formcreator_questions_conditions', 'plugin_formcreator_questions_id'); + + // fill missing uuid (force update of questions, see PluginFormcreatorQuestoin_Condition::prepareInputForUpdate) + $condition_obj = new PluginFormcreatorQuestion_Condition(); + $all_conditions = $condition_obj->find("uuid IS NULL"); + foreach ($all_conditions as $conditions_id => $condition) { + $condition_obj->update(['id' => $conditions_id, + 'uuid' => plugin_formcreator_getUuid()]); + } + } + + function updateQuestion_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Questions + $migration->displayMessage("Upgrade glpi_plugin_formcreator_questions"); + + // Migration 0.83-1.0 => 0.85-1.0 + if (!$DB->fieldExists('glpi_plugin_formcreator_questions', 'fieldtype', false)) { + // Migration from previous version + $query = "ALTER TABLE `glpi_plugin_formcreator_questions` + ADD `fieldtype` varchar(30) NOT NULL DEFAULT 'text', + ADD `show_type` enum ('show', 'hide') NOT NULL DEFAULT 'show', + ADD `show_field` int(11) DEFAULT NULL, + ADD `show_condition` enum('equal','notequal','lower','greater') COLLATE utf8_unicode_ci DEFAULT NULL, + ADD `show_value` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + ADD `required` tinyint(1) NOT NULL DEFAULT '0', + ADD `show_empty` tinyint(1) NOT NULL DEFAULT '0', + ADD `default_values` text COLLATE utf8_unicode_ci, + ADD `values` text COLLATE utf8_unicode_ci, + ADD `range_min` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, + ADD `range_max` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, + ADD `regex` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + CHANGE `content` `description` text COLLATE utf8_unicode_ci NOT NULL, + CHANGE `position` `order` int(11) NOT NULL DEFAULT '0';"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + // order start from 1 instead of 0 + $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `order` = `order` + 1;") or plugin_formcreator_upgrade_error($migration); + + // Match new type + $query = "SELECT `id`, `type`, `data`, `option` + FROM `glpi_plugin_formcreator_questions`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $data = json_decode($line['data']); + $options = json_decode($line['option']); + + $fieldtype = 'text'; + $values = ''; + $default = ''; + $regex = ''; + $required = 0; + + if (isset($data->value) && !empty($data->value)) { + if (is_object($data->value)) { + foreach ($data->value as $value) { + if (!empty($value)) { + $values .= urldecode($value) . "\r\n"; + } + } + } else { + $values .= urldecode($data->value); + } + } + + switch ($line['type']) { + case '1': + $fieldtype = 'text'; + + if (isset($options->type)) { + switch ($options->type) { + case '2': + $required = 1; + break; + case '3': + $regex = '[[:alpha:]]'; + break; + case '4': + $fieldtype = 'float'; + break; + case '5': + $regex = urldecode($options->value); + // Add leading and trailing regex marker (automaticaly added in V1) + if (substr($regex, 0, 1) != '/') { + $regex = '/' . $regex; + } + if (substr($regex, -1, 1) != '/') { + $regex = $regex . '/'; + } + break; + case '6': + $fieldtype = 'email'; + break; + case '7': + $fieldtype = 'date'; + break; + } + } + $default_values = $values; + $values = ''; + break; + + case '2': + $fieldtype = 'select'; + break; + + case '3': + $fieldtype = 'checkboxes'; + break; + + case '4': + $fieldtype = 'textarea'; + if (isset($options->type) && ($options->type == 2)) { + $required = 1; + } + $default_values = $values; + $values = ''; + break; + + case '5': + $fieldtype = 'file'; + break; + + case '8': + $fieldtype = 'select'; + break; + + case '9': + $fieldtype = 'select'; + break; + + case '10': + $fieldtype = 'dropdown'; + break; + + default : + break; + } + + $query_udate = "UPDATE `glpi_plugin_formcreator_questions` SET + `fieldtype` = '" . $fieldtype . "', + `values` = '" . addslashes(htmlspecialchars($values)) . "', + `default_values` = '" . addslashes(htmlspecialchars($default)) . "', + `regex` = '" . $regex . "', + `required` = " . (int) $required . " + WHERE `id` = " . $line['id']; + $DB->query($query_udate) or plugin_formcreator_upgrade_error($migration); + } + + $migration->dropField('glpi_plugin_formcreator_questions', 'type'); + $migration->dropField('glpi_plugin_formcreator_questions', 'data'); + $migration->dropField('glpi_plugin_formcreator_questions', 'option'); + $migration->dropField('glpi_plugin_formcreator_questions', 'plugin_formcreator_forms_id'); + } + + // Migration 0.85-1.0 => 0.85-1.1 + if ($DB->fieldExists('glpi_plugin_formcreator_questions', 'show_type', false)) { + + // Fix type of section ID + if (!$DB->fieldExists('glpi_plugin_formcreator_questions', 'show_rule')) { + $query = "ALTER TABLE `glpi_plugin_formcreator_questions` + CHANGE `plugin_formcreator_sections_id` `plugin_formcreator_sections_id` INT NOT NULL, + ADD `show_rule` enum('always','hidden','shown') NOT NULL DEFAULT 'always'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + // Migrate "questions" table + $query = "SELECT `id`, `name`, `values`, `default_values`, `description` + FROM `glpi_plugin_formcreator_questions`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_questions` SET + `name` = '" . addslashes(plugin_formcreator_encode($line['name'])) . "', + `values` = '" . addslashes(plugin_formcreator_encode($line['values'])) . "', + `default_values` = '" . addslashes(plugin_formcreator_encode($line['default_values'])) . "', + `description` = '" . addslashes(plugin_formcreator_encode($line['description'])) . "' + WHERE `id` = " . $line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + /** + * Add natural language search + * + * @since 0.90-1.4 + */ + // An error may occur if the Search index does not exists + // This is not critical as we need to (re) create it + If (isIndex('glpi_plugin_formcreator_questions', 'Search')) { + $query = "ALTER TABLE `glpi_plugin_formcreator_questions` DROP INDEX `Search`"; + $DB->query($query); + } + + // Re-add FULLTEXT index + $query = "ALTER TABLE `glpi_plugin_formcreator_questions` ADD FULLTEXT INDEX `Search` (`name`, `description`)"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + // add uuid to questions + if (!$DB->fieldExists('glpi_plugin_formcreator_questions', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_questions', 'uuid', 'string'); + $migration->migrationOneTable('glpi_plugin_formcreator_questions'); + } + + $migration->addKey('glpi_plugin_formcreator_questions', 'plugin_formcreator_sections_id'); + + // fill missing uuid (force update of questions, see PlugiinFormcreatorQuestion::prepareInputForUpdate) + $obj = new PluginFormcreatorQuestion(); + $all_questions = $obj->find("uuid IS NULL"); + foreach ($all_questions as $questions_id => $question) { + $obj->update(['id' => $questions_id]); + } + } + + function updateSection_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Sections + $migration->displayMessage("Upgrade glpi_plugin_formcreator_sections"); + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + $query = "SELECT `id`, `name` + FROM `glpi_plugin_formcreator_sections`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `glpi_plugin_formcreator_sections` SET + `name` = '".plugin_formcreator_encode($line['name'])."' + WHERE `id` = ".$line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + + // Migration from previous version => Remove useless target field + if ($DB->fieldExists('glpi_plugin_formcreator_sections', 'plugin_formcreator_targets_id', false)) { + $migration->dropField('glpi_plugin_formcreator_sections', 'plugin_formcreator_targets_id'); + } + + // Migration from previous version => Rename "position" into "order" and start order from 1 instead of 0 + if ($DB->fieldExists('glpi_plugin_formcreator_sections', 'position', false)) { + $DB->query("ALTER TABLE `glpi_plugin_formcreator_sections` CHANGE `position` `order` INT(11) NOT NULL DEFAULT '0';"); + $DB->query("UPDATE `glpi_plugin_formcreator_sections` SET `order` = `order` + 1;"); + } + + // Migration from previous version => Update Question table, then create a "description" question from content + if ($DB->fieldExists('glpi_plugin_formcreator_sections', 'content', false)) { + // Increment the order of questions which are in a section with a description + $query = "UPDATE `PluginFormcreatorQuestion` + SET `order` = `order` + 1 + WHERE `plugin_formcreator_sections_id` IN ( + SELECT `id` + FROM `glpi_plugin_formcreator_sections` + WHERE `content` != '' + )"; + $DB->query($query); + + // Create description from content + $query = "INSERT INTO `PluginFormcreatorQuestion` ( + `plugin_formcreator_sections_id`, + `fieldtype`, + `name`, + `description`, + `order` + ) + SELECT + `id`, + 'description' AS fieldtype, + CONCAT('Description ', `id`) AS name, + `content`, + 1 AS `order` + FROM `glpi_plugin_formcreator_sections` + WHERE `content` != ''"; + $DB->query($query); + + // Delete content column + $DB->query("ALTER TABLE `glpi_plugin_formcreator_sections` DROP `content`;"); + } + + // add uuid to sections + if (!$DB->fieldExists('glpi_plugin_formcreator_sections', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_sections', 'uuid', 'string'); + $migration->migrationOneTable('glpi_plugin_formcreator_sections'); + } + + $migration->addKey('glpi_plugin_formcreator_sections', 'plugin_formcreator_forms_id'); + + // fill missing uuid (force update of sections, see PluginFormcreatorSection::prepareInputForUpdate) + $obj = new PluginFormcreatorSection(); + $all_sections = $obj->find("uuid IS NULL"); + foreach ($all_sections as $sections_id => $section) { + $obj->update(['id' => $sections_id]); + } + } + + function updateTarget_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of targets + $migration->displayMessage("Upgrade glpi_plugin_formcreator_targets"); + + // Migration to 0.85-1.2.5 + if ($DB->fieldExists('glpi_plugin_formcreator_targets', 'plugin_formcreator_forms_id', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targets` + CHANGE `plugin_formcreator_forms_id` `plugin_formcreator_forms_id` INT NOT NULL;"; + $DB->query($query); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_targets', 'itemtype', false)) { + // Migration from version 1.5 to 1.6 + if (!$DB->fieldExists('glpi_plugin_formcreator_targets', 'type', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targets` + ADD `type` tinyint(1) NOT NULL default '2';"; + $DB->query($query); + } + + // Add new column for link with target items + $query = "ALTER TABLE `glpi_plugin_formcreator_targets` + ADD `itemtype` varchar(100) NOT NULL DEFAULT 'PluginFormcreatorTargetTicket', + ADD `items_id` int(11) NOT NULL DEFAULT 0;"; + $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` + FROM `glpi_plugin_formcreator_targets` t, `glpi_plugin_formcreator_forms` f + WHERE f.`id` = t.`plugin_formcreator_forms_id` + GROUP BY t.`urgency`, t.`priority`, t.`itilcategories_id`, t.`type`, f.`entities_id`"; + $result = $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + $i = 0; + while ($ligne = $DB->fetch_array($result)) { + $i++; + $id = $ligne['urgency'].$ligne['priority'].$ligne['itilcategories_id'].$ligne['type']; + + $template = new TicketTemplate(); + $template_id = $template->add([ + 'name' => 'Template Formcreator '.$i, + 'entities_id' => $ligne['entities_id'], + 'is_recursive' => 1, + ]); + + $predefinedField = new TicketTemplatePredefinedField(); + + // Urgency + if (!empty($ligne['urgency'])) { + $predefinedField->add([ + 'tickettemplates_id' => $template_id, + 'num' => 10, + 'value' => $ligne['urgency'], + ]); + } + + // Priority + if (!empty($ligne['priority'])) { + $predefinedField->add([ + 'tickettemplates_id' => $template_id, + 'num' => 3, + 'value' => $ligne['priority'], + ]); + } + + // Category + if (!empty($ligne['itilcategories_id'])) { + $predefinedField->add([ + 'tickettemplates_id' => $template_id, + 'num' => 7, + 'value' => $ligne['itilcategories_id'], + ]); + } + + // Type + if (!empty($ligne['type'])) { + $predefinedField->add([ + 'tickettemplates_id' => $template_id, + 'num' => 14, + 'value' => $ligne['type'], + ]); + } + + $_SESSION["formcreator_tmp"]["ticket_template"]["$id"] = $template_id; + } + + // Install or upgrade of TargetTicket is a prerequisite + $version = plugin_version_formcreator(); + plugin_formcreator_updateTarget_2_5($migration); + $table_targetticket = getTableForItemType('PluginFormcreatorTargetTicket'); + + // Convert targets to ticket templates only if at least one target extsis + if ($i > 0) { + // 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 + $query = "SELECT `id`, `name`, $mysql_case_template, `content` + FROM `glpi_plugin_formcreator_targets`;"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + // Insert target ticket + $query_insert = "INSERT INTO `$table_targetticket` SET + `name` = '".htmlspecialchars($line['name'])."', + `tickettemplates_id` = ".$line['tickettemplates_id'].", + `comment` = '".htmlspecialchars($line['content'])."'"; + $DB ->query($query_insert); + $targetticket_id = $DB->insert_id(); + + // Update target with target ticket id + $query_update = "UPDATE `glpi_plugin_formcreator_targets` + SET `items_id` = ".$targetticket_id." + WHERE `id` = ".$line['id']; + $DB->query($query_update); + } + } + + // Remove useless column content + $DB->query("ALTER TABLE `glpi_plugin_formcreator_targets` DROP `content`;"); + + /** + * Migration of special chars from previous versions + * + * @since 0.85-1.2.3 + */ + if ($DB->fieldExists($table_targetticket, 'comment')) { + $query = "SELECT `id`, `comment` + FROM `$table_targetticket`"; + $result = $DB->query($query); + while ($line = $DB->fetch_array($result)) { + $query_update = "UPDATE `$table_targetticket` SET + `comment` = '".plugin_formcreator_encode($line['comment'])."' + WHERE `id` = ".$line['id']; + $DB->query($query_update) or plugin_formcreator_upgrade_error($migration); + } + } + } + + $migration->addKey('glpi_plugin_formcreator_targets', 'plugin_formcreator_forms_id'); + $migration->addKey('glpi_plugin_formcreator_targets', ['itemtype', 'items_id']); + + // add uuid to targets + if (!$DB->fieldExists('glpi_plugin_formcreator_targets', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_targets', 'uuid', 'string'); + $migration->migrationOneTable('glpi_plugin_formcreator_targets'); + } + + // fill missing uuid (force update of targets, see PluginFormcreatorTarget::prepareInputForUpdate) + $obj = new PluginFormcreatorTarget(); + $all_targets = $obj->find("uuid IS NULL"); + foreach ($all_targets as $targets_id => $target) { + $obj->update(['id' => $targets_id]); + } + } + + function updateTargetChange_Actor_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Change actors + $migration->displayMessage("Upgrade glpi_plugin_formcreator_targetchanges_actors"); + + if (!$DB->tableExists('glpi_plugin_formcreator_targetchanges_actors')) { + $query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetchanges_actors` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `plugin_formcreator_targetchanges_id` int(11) NOT NULL, + `actor_role` enum('requester','observer','assigned') COLLATE utf8_unicode_ci NOT NULL, + `actor_type` enum('creator','validator','person','question_person','group','question_group','supplier','question_supplier','question_actors') COLLATE utf8_unicode_ci NOT NULL, + `actor_value` int(11) DEFAULT NULL, + `use_notification` tinyint(1) NOT NULL DEFAULT '1', + `uuid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + PRIMARY KEY (`id`), + INDEX `plugin_formcreator_targetchanges_id` (`plugin_formcreator_targetchanges_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + $enum_actor_type = "'".implode("', '", array_keys(PluginFormcreatorTargetChange_Actor::getEnumActorType()))."'"; + $enum_actor_role = "'".implode("', '", array_keys(PluginFormcreatorTargetChange_Actor::getEnumRole()))."'"; + + $current_enum_actor_type = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges_actors', 'actor_type'); + if (count($current_enum_actor_type) != count(PluginFormcreatorTargetChange_Actor::getEnumActorType())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges_actors` + CHANGE COLUMN `actor_type` `actor_type` + ENUM($enum_actor_type) + NOT NULL"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + $current_enum_role = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges_actors', 'actor_role'); + if (count($current_enum_role) != count(PluginFormcreatorTargetChange_Actor::getEnumRole())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges_actors` + CHANGE COLUMN `actor_role` `actor_role` + ENUM($enum_actor_role) + NOT NULL"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + // fill missing uuid + $obj = new PluginFormcreatorTargetChange_Actor(); + $all_actor = $obj->find("uuid IS NULL"); + foreach ($all_actor as $actors_id => $actor) { + $obj->update(['id' => $actors_id, + 'uuid' => plugin_formcreator_getUuid()]); + } + + } + + function updateTargetTicket_Actor_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of target ticket actors + $migration->displayMessage("Upgrade glpi_plugin_formcreator_targettickets_actors"); + + $enum_actor_type = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket_Actor::getEnumActorType()))."'"; + $enum_actor_role = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket_Actor::getEnumRole()))."'"; + + $current_enum_actor_type = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets_actors', 'actor_type'); + if (count($current_enum_actor_type) != count(PluginFormcreatorTargetTicket_Actor::getEnumActorType())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets_actors` + CHANGE COLUMN `actor_type` `actor_type` + ENUM($enum_actor_type) + NOT NULL"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + $current_enum_role = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets_actors', 'actor_role'); + if (count($current_enum_role) != count(PluginFormcreatorTargetTicket_Actor::getEnumRole())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets_actors` + CHANGE COLUMN `actor_role` `actor_role` + ENUM($enum_actor_role) + NOT NULL"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + // add uuid to actor + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets_actors', 'uuid', false)) { + $migration->addField('glpi_plugin_formcreator_targettickets_actors', 'uuid', 'string'); + $migration->migrationOneTable('glpi_plugin_formcreator_targettickets_actors'); + } + + // fill missing uuid + $obj = new PluginFormcreatorTargetTicket_Actor(); + $all_actor = $obj->find("uuid IS NULL"); + foreach ($all_actor as $actors_id => $actor) { + $obj->update(['id' => $actors_id, + 'uuid' => plugin_formcreator_getUuid()]); + } + } + + function updateTargetTicket_2_5(Migration $migration) { + global $DB; + + // Legacy upgrade of Target tickets + $migration->displayMessage("Upgrade glpi_plugin_formcreator_targettickets"); + + $enum_destination_entity = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumDestinationEntity()))."'"; + $enum_tag_type = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumTagType()))."'"; + $enum_due_date_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumDueDateRule()))."'"; + $enum_urgency_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumUrgencyRule()))."'"; + $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumCategoryRule()))."'"; + + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'due_date_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + ADD `due_date_rule` ENUM($enum_due_date_rule) NULL DEFAULT NULL, + ADD `due_date_question` INT NULL DEFAULT NULL, + ADD `due_date_value` TINYINT NULL DEFAULT NULL, + ADD `due_date_period` ENUM('minute', 'hour', 'day', 'month') NULL DEFAULT NULL, + ADD `validation_followup` BOOLEAN NOT NULL DEFAULT TRUE;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + // Migration to Formcreator 0.90-1.4 + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'destination_entity', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + ADD `destination_entity` ENUM($enum_destination_entity) NOT NULL DEFAULT 'current', + ADD `destination_entity_value` int(11) NULL DEFAULT NULL;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_destination_entity = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'destination_entity'); + if (count($current_enum_destination_entity) != count(PluginFormcreatorTargetTicket::getEnumDestinationEntity())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + CHANGE COLUMN `destination_entity` `destination_entity` + ENUM($enum_destination_entity) + NOT NULL DEFAULT 'current'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` ALTER COLUMN `destination_entity` SET DEFAULT 'current'"; + $DB->query($query); + + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'tag_type', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + ADD `tag_type` ENUM($enum_tag_type) NOT NULL DEFAULT 'none', + ADD `tag_questions` VARCHAR(255) NOT NULL, + ADD `tag_specifics` VARCHAR(255) NOT NULL;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'urgency_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + ADD `urgency_rule` ENUM($enum_urgency_rule) NOT NULL DEFAULT 'none' AFTER `due_date_period`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_urgency_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'urgency_rule'); + if (count($current_enum_urgency_rule) != count(PluginFormcreatorTargetTicket::getEnumUrgencyRule())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + CHANGE COLUMN `urgency_rule` `urgency_rule` + ENUM($enum_urgency_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $migration->addField('glpi_plugin_formcreator_targettickets', 'urgency_question', 'integer', ['after' => 'urgency_rule']); + + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'category_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'category_rule'); + if (count($current_enum_category_rule) != count(PluginFormcreatorTargetTicket::getEnumCategoryRule())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targettickets` + CHANGE COLUMN `category_rule` `category_rule` + ENUM($enum_category_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $migration->addField('glpi_plugin_formcreator_targettickets', 'category_question', 'integer', ['after' => 'category_rule']); + + $migration->addKey('glpi_plugin_formcreator_targettickets', 'tickettemplates_id'); + } + + function updateTitle_2_5(Migration $migration) { + // Drop Headers table + $migration->displayMessage("Drop glpi_plugin_formcreator_titles"); + $migration->dropTable('glpi_plugin_formcreator_titles'); + } + + function updateTargetChange_2_5($migration) { + global $DB; + + // Legacy upgrade of Target changes + $migration->displayMessage("Upgrade glpi_plugin_formcreator_targetchanges"); + + $enum_destination_entity = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumDestinationEntity()))."'"; + $enum_tag_type = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumTagType()))."'"; + $enum_due_date_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumDueDateRule()))."'"; + $enum_urgency_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumUrgencyRule()))."'"; + $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumCategoryRule()))."'"; + + if (!$DB->tableExists('glpi_plugin_formcreator_targetchanges')) { + $query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetchanges` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL DEFAULT '', + `changetemplates_id` int(11) DEFAULT NULL, + `comment` text, + `impactcontent` text, + `controlistcontent` text, + `rolloutplancontent` text, + `backoutplancontent` text, + `checklistcontent` text, + `due_date_rule` enum($enum_due_date_rule) DEFAULT NULL, + `due_date_question` int(11) DEFAULT NULL, + `due_date_value` tinyint(4) DEFAULT NULL, + `due_date_period` enum('minute','hour','day','month') DEFAULT NULL, + `urgency_rule` enum($enum_urgency_rule) NOT NULL DEFAULT 'none', + `urgency_question` int(11) NOT NULL DEFAULT '0', + `validation_followup` tinyint(1) NOT NULL DEFAULT '1', + `destination_entity` enum($enum_destination_entity) NOT NULL DEFAULT 'requester', + `destination_entity_value` int(11) DEFAULT NULL, + `tag_type` enum($enum_tag_type) NOT NULL DEFAULT 'none', + `tag_questions` varchar(255) NOT NULL, + `tag_specifics` varchar(255) NOT NULL, + `category_rule` enum($enum_category_rule) NOT NULL DEFAULT 'none', + `category_question` int(11) NOT NULL DEFAULT '0', + PRIMARY KEY (`id`), + INDEX `changetemplates_id` (`changetemplates_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + "; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'due_date_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `due_date_rule` ENUM($enum_due_date_rule) NULL DEFAULT NULL, + ADD `due_date_question` INT NULL DEFAULT NULL, + ADD `due_date_value` TINYINT NULL DEFAULT NULL, + ADD `due_date_period` ENUM('minute', 'hour', 'day', 'month') NULL DEFAULT NULL;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'destination_entity', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `destination_entity` ENUM($enum_destination_entity) NOT NULL DEFAULT 'current', + ADD `destination_entity_value` int(11) NULL DEFAULT NULL;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_destination_entity = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'destination_entity'); + if (count($current_enum_destination_entity) != count(PluginFormcreatorTargetChange::getEnumDestinationEntity())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + CHANGE COLUMN `destination_entity` `destination_entity` + ENUM($enum_destination_entity) + NOT NULL DEFAULT 'current'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` ALTER COLUMN `destination_entity` SET DEFAULT 'current'"; + $DB->query($query); + + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'tag_type', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `tag_type` ENUM($enum_tag_type) NOT NULL DEFAULT 'none', + ADD `tag_questions` VARCHAR(255) NOT NULL, + ADD `tag_specifics` VARCHAR(255) NOT NULL;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'urgency_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `urgency_rule` ENUM($enum_urgency_rule) NOT NULL DEFAULT 'none' AFTER `due_date_period`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_urgency_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'urgency_rule'); + if (count($current_enum_urgency_rule) != count(PluginFormcreatorTargetChange::getEnumUrgencyRule())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + CHANGE COLUMN `urgency_rule` `urgency_rule` + ENUM($enum_urgency_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $migration->addField('glpi_plugin_formcreator_targetchanges', 'urgency_question', 'integer', ['after' => 'urgency_rule']); + + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'category_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'category_rule'); + if (count($current_enum_category_rule) != count(PluginFormcreatorTargetChange::getEnumCategoryRule())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + CHANGE COLUMN `category_rule` `category_rule` + ENUM($enum_category_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $migration->addField('glpi_plugin_formcreator_targetchanges', 'category_question', 'integer', ['after' => 'category_rule']); + } +} diff --git a/install/update_2.6_2.6.1.php b/install/upgrade_to_2.6.1.php similarity index 51% rename from install/update_2.6_2.6.1.php rename to install/upgrade_to_2.6.1.php index 4e3429dd5..d6aeacd08 100644 --- a/install/update_2.6_2.6.1.php +++ b/install/upgrade_to_2.6.1.php @@ -31,34 +31,39 @@ * --------------------------------------------------------------------- */ -function plugin_formcreator_update_2_6_1() { - global $DB; +class PluginFormcreatorUpgradeTo2_6_1 { + /** + * @param Migration $migration + */ + public function upgrade(Migration $migration) { + global $DB; - // decode html entities in name of questions - $request = [ - 'SELECT' => ['glpi_plugin_formcreator_answers.*'], - 'FROM' => 'glpi_plugin_formcreator_answers', - 'INNER JOIN' => ['glpi_plugin_formcreator_questions' => [ - 'FKEY' => [ - 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id', - 'glpi_plugin_formcreator_questions' => 'id' - ] - ]], - 'WHERE' => ['fieldtype' => 'textarea'] - ]; - foreach ($DB->request($request) as $row) { - $answer = Toolbox::addslashes_deep(html_entity_decode($row['answer'], ENT_QUOTES|ENT_HTML5)); - $id = $row['id']; - $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer`='$answer' WHERE `id` = '$id'"); - } + // decode html entities in name of questions + $request = [ + 'SELECT' => ['glpi_plugin_formcreator_answers.*'], + 'FROM' => 'glpi_plugin_formcreator_answers', + 'INNER JOIN' => ['glpi_plugin_formcreator_questions' => [ + 'FKEY' => [ + 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id', + 'glpi_plugin_formcreator_questions' => 'id' + ] + ]], + 'WHERE' => ['fieldtype' => 'textarea'] + ]; + foreach ($DB->request($request) as $row) { + $answer = Toolbox::addslashes_deep(html_entity_decode($row['answer'], ENT_QUOTES|ENT_HTML5)); + $id = $row['id']; + $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer`='$answer' WHERE `id` = '$id'"); + } - $request = [ - 'FROM' => 'glpi_plugin_formcreator_questions', - ]; - foreach ($DB->request($request) as $row) { - $id = $row['id']; - $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); - $id = $row['id']; - $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `name`='$name' WHERE `id` = '$id'"); + $request = [ + 'FROM' => 'glpi_plugin_formcreator_questions', + ]; + foreach ($DB->request($request) as $row) { + $id = $row['id']; + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); + $id = $row['id']; + $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `name`='$name' WHERE `id` = '$id'"); + } } -} \ No newline at end of file +} diff --git a/install/upgrade_to_2.6.3.php b/install/upgrade_to_2.6.3.php new file mode 100644 index 000000000..89f8e17ca --- /dev/null +++ b/install/upgrade_to_2.6.3.php @@ -0,0 +1,90 @@ +. + * --------------------------------------------------------------------- + * @author Thierry Bugier + * @author Jérémy Moreau + * @copyright Copyright © 2011 - 2018 Teclib' + * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ + * @link https://github.com/pluginsGLPI/formcreator/ + * @link https://pluginsglpi.github.io/formcreator/ + * @link http://plugins.glpi-project.org/#/plugin/formcreator + * --------------------------------------------------------------------- + */ + +class PluginFormcreatorUpgradeTo2_6_3 { + /** + * @param Migration $migration + */ + public function upgrade(Migration $migration) { + global $DB; + + // Change id of search option for status of form_answer + $table = 'glpi_displaypreferences'; + $query = "UPDATE `$table` SET `num`='8' WHERE `itemtype`='PluginFormcreatorForm_Answer' AND `num`='1'"; + $DB->query($query); + + // Remove abusive encding in sections + $table = 'glpi_plugin_formcreator_sections'; + $request = [ + 'FROM' => $table, + ]; + foreach ($DB->request($request) as $row) { + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); + $id = $row['id']; + $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); + } + + // Remove abusive encoding in targets + $table = 'glpi_plugin_formcreator_targets'; + $request = [ + 'FROM' => $table, + ]; + foreach ($DB->request($request) as $row) { + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); + $id = $row['id']; + $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); + } + + // Remove abusive encoding in target tickets + $table = 'glpi_plugin_formcreator_targettickets'; + $request = [ + 'FROM' => $table, + ]; + foreach ($DB->request($request) as $row) { + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); + $id = $row['id']; + $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); + } + + // Remove abusive encoding in target changes + $table = 'glpi_plugin_formcreator_targetchanges'; + $request = [ + 'FROM' => $table, + ]; + foreach ($DB->request($request) as $row) { + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'], ENT_QUOTES|ENT_HTML5)); + $id = $row['id']; + $DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'"); + } + } +} diff --git a/install/upgrade_to_2.6.php b/install/upgrade_to_2.6.php new file mode 100644 index 000000000..c1876d083 --- /dev/null +++ b/install/upgrade_to_2.6.php @@ -0,0 +1,201 @@ +. + * --------------------------------------------------------------------- + * @author Thierry Bugier + * @author Jérémy Moreau + * @copyright Copyright © 2011 - 2018 Teclib' + * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ + * @link https://github.com/pluginsGLPI/formcreator/ + * @link https://pluginsglpi.github.io/formcreator/ + * @link http://plugins.glpi-project.org/#/plugin/formcreator + * --------------------------------------------------------------------- + */ + +/** + * Upgrade any version of Formcreator >= 2.5.0 to 2.6.0 + * @param Migration $migration + */ +class PluginFormcreatorUpgradeTo2_6 { + /** + * @param Migration $migration + */ + public function upgrade(Migration $migration) { + global $DB; + + $migration->displayMessage("Upgrade to schema version 2.6"); + + $migration->displayMessage("Upgrade glpi_plugin_formcreator_forms_answers"); + + // update questions + $table = 'glpi_plugin_formcreator_questions'; + $migration->displayMessage("Upgrade $table"); + + $question = new PluginFormcreatorQuestion(); + $rows = $question->find("`fieldtype` = 'dropdown' AND `values` = 'ITILCategory'"); + foreach ($rows as $id => $row) { + $updatedValue = json_encode([ + 'itemtype' => $row['values'], + 'show_ticket_categories' => 'both', + 'show_ticket_categories_depth' => 0 + ]); + // Don't use update() method because the json will be HTML-entities-ified (see prepareInputForUpdate() ) + $query = "UPDATE `$table` SET `values`='$updatedValue' WHERE `id`='$id'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + // Update Form Answers + $table = 'glpi_plugin_formcreator_forms_answers'; + $migration->displayMessage("Upgrade $table"); + + $migration->addField($table, 'users_id_validator', 'integer', ['after' => 'requester_id']); + $migration->addField($table, 'groups_id_validator', 'integer', ['after' => 'users_id_validator']); + $migration->addKey($table, 'users_id_validator'); + $migration->addKey($table, 'groups_id_validator'); + $migration->migrationOneTable($table); + + $formTable = 'glpi_plugin_formcreator_forms'; + $query = "UPDATE `$table` + INNER JOIN `$formTable` ON (`$table`.`plugin_formcreator_forms_id` = `$formTable`.`id`) + SET `users_id_validator` = `validator_id` + WHERE `$formTable`.`validation_required` = '1'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + $query = "UPDATE `$table` + INNER JOIN `$formTable` ON (`$table`.`plugin_formcreator_forms_id` = `$formTable`.`id`) + SET `groups_id_validator` = `validator_id` + WHERE `$formTable`.`validation_required` = '2'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + + $migration->dropKey($table, 'validator_id'); + $migration->dropField($table, 'validator_id'); + + // add location rule + $enum_location_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumLocationRule()))."'"; + if (!$DB->fieldExists('glpi_plugin_formcreator_targettickets', 'location_rule', false)) { + $migration->addField( + 'glpi_plugin_formcreator_targettickets', + 'location_rule', + "ENUM($enum_location_rule) NOT NULL DEFAULT 'none'", + ['after' => 'category_question'] + ); + } else { + $current_enum_location_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targettickets', 'location_rule'); + if (count($current_enum_location_rule) != count(PluginFormcreatorTargetTicket::getEnumLocationRule())) { + $migration->changeField( + 'glpi_plugin_formcreator_targettickets', + 'location_rule', + 'location_rule', + "ENUM($enum_location_rule) NOT NULL DEFAULT 'none'", + ['after' => 'category_question'] + ); + } + } + $migration->addField('glpi_plugin_formcreator_targettickets', 'location_question', 'integer', ['after' => 'location_rule']); + + // Fix bad foreign key + $table = 'glpi_plugin_formcreator_answers'; + $migration->changeField($table, 'plugin_formcreator_question_id', 'plugin_formcreator_questions_id', 'integer'); + + $table = 'glpi_plugin_formcreator_items_targettickets'; + if (!$DB->tableExists($table)) { + $query = "CREATE TABLE `$table` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `plugin_formcreator_targettickets_id` int(11) NOT NULL DEFAULT '0', + `link` int(11) NOT NULL DEFAULT '0', + `itemtype` varchar(255) NOT NULL DEFAULT '', + `items_id` int(11) NOT NULL DEFAULT '0', + `uuid` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + INDEX `plugin_formcreator_targettickets_id` (`plugin_formcreator_targettickets_id`), + INDEX `item` (`itemtype`,`items_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + + // add uuid and generate for existing rows + $table = PluginFormcreatorTargetTicket::getTable(); + $migration->addField($table, 'uuid', 'string', ['after' => 'category_question']); + $migration->migrationOneTable($table); + $obj = new PluginFormcreatorTargetTicket(); + $all_targetTickets = $obj->find("`uuid` IS NULL"); + foreach ($all_targetTickets as $targetTicket) { + $targetTicket['_skip_checks'] = true; + $targetTicket['title'] = $targetTicket['name']; + $obj->update($targetTicket); + } + unset($obj); + + $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumCategoryRule()))."'"; + $table = 'glpi_plugin_formcreator_targettickets'; + if (!$DB->fieldExists($table, 'category_rule', false)) { + $query = "ALTER TABLE `$table` + ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues($table, 'category_rule'); + if (count($current_enum_category_rule) != count(PluginFormcreatorTargetTicket::getEnumCategoryRule())) { + $query = "ALTER TABLE `$table` + CHANGE COLUMN `category_rule` `category_rule` + ENUM($enum_category_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + + $table = 'glpi_plugin_formcreator_items_targetchanges'; + $enum_urgency_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumUrgencyRule()))."'"; + $enum_category_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetChange::getEnumCategoryRule()))."'"; + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'urgency_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `urgency_rule` ENUM($enum_urgency_rule) NOT NULL DEFAULT 'none' AFTER `due_date_period`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_urgency_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'urgency_rule'); + if (count($current_enum_urgency_rule) != count(PluginFormcreatorTargetChange::getEnumUrgencyRule())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + CHANGE COLUMN `urgency_rule` `urgency_rule` + ENUM($enum_urgency_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $migration->addField('glpi_plugin_formcreator_targetchanges', 'urgency_question', 'integer', ['after' => 'urgency_rule']); + + if (!$DB->fieldExists('glpi_plugin_formcreator_targetchanges', 'category_rule', false)) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + ADD `category_rule` ENUM($enum_category_rule) NOT NULL DEFAULT 'none' AFTER `tag_specifics`;"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } else { + $current_enum_category_rule = PluginFormcreatorCommon::getEnumValues('glpi_plugin_formcreator_targetchanges', 'category_rule'); + if (count($current_enum_category_rule) != count(PluginFormcreatorTargetChange::getEnumCategoryRule())) { + $query = "ALTER TABLE `glpi_plugin_formcreator_targetchanges` + CHANGE COLUMN `category_rule` `category_rule` + ENUM($enum_category_rule) + NOT NULL DEFAULT 'none'"; + $DB->query($query) or plugin_formcreator_upgrade_error($migration); + } + } + $migration->addField('glpi_plugin_formcreator_targetchanges', 'category_question', 'integer', ['after' => 'category_rule']); + + $migration->executeMigration(); + } +} diff --git a/install/upgrade_to_2.7.php b/install/upgrade_to_2.7.php new file mode 100644 index 000000000..6c8aee2a3 --- /dev/null +++ b/install/upgrade_to_2.7.php @@ -0,0 +1,235 @@ +. + * --------------------------------------------------------------------- + * + * @copyright Copyright © 2011 - 2018 Teclib' + * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ + * @link https://github.com/pluginsGLPI/formcreator/ + * @link https://pluginsglpi.github.io/formcreator/ + * @link http://plugins.glpi-project.org/#/plugin/formcreator + * --------------------------------------------------------------------- + */ +class PluginFormcreatorUpgradeTo2_7 { + /** + * @param Migration $migration + */ + public function upgrade(Migration $migration) { + global $DB; + + // Rename PluginFormcreatorForm_Answer into PluginFormcreatorFormAnswer + $displayPreference = new DisplayPreference(); + $DB->update( + 'glpi_displaypreferences', [ + 'itemtype' => 'PluginFormcreatorFormAnswer', + ], [ + 'itemtype' => 'PluginFormcreatorForm_Answer' + ] + ); + $DB->update( + 'glpi_items_tickets', [ + 'itemtype' => 'PluginFormcreatorFormAnswer', + ], [ + 'itemtype' => 'PluginFormcreatorForm_Answer' + ] + ); + $table = 'glpi_plugin_formcreator_formanswers'; + $migration->renameTable('glpi_plugin_formcreator_forms_answers', $table); + $table = 'glpi_plugin_formcreator_answers'; + $migration->changeField( + $table, + 'plugin_formcreator_forms_answers_id', + 'plugin_formcreator_formanswers_id', + 'integer' + ); + $migration->migrationOneTable($table); + $migration->dropKey($table, 'plugin_formcreator_forms_answers_id'); + $migration->addKey($table, ['plugin_formcreator_formanswers_id'], 'plugin_formcreator_formanswers_id'); + + // Changes don't support templates, remove the relation + $table = 'glpi_plugin_formcreator_targetchanges'; + $migration->dropField($table, 'changetemplates_id'); + + // Migrate regex question parameters + $table = 'glpi_plugin_formcreator_questions'; + if ($DB->fieldExists($table, 'regex')) { + $request = [ + 'FROM' => $table, + 'WHERE' => ['fieldtype' => ['float', 'integer', 'text', 'textarea']] + ]; + foreach ($DB->request($request) as $row) { + $id = $row['id']; + $regex = $DB->escape($row['regex']); + $uuid = plugin_formcreator_getUuid(); + $DB->query("INSERT INTO `glpi_plugin_formcreator_questionregexes` + SET `plugin_formcreator_questions_id`='$id', `fieldname`='regex', `regex`='$regex', `uuid`='$uuid'" + ) or plugin_formcreator_upgrade_error($migration); + } + $migration->dropField($table, 'regex'); + } + + // Migrate range question parameters + $table = 'glpi_plugin_formcreator_questions'; + if ($DB->fieldExists($table, 'range_min')) { + $request = [ + 'FROM' => $table, + 'WHERE' => ['fieldtype' => ['float', 'integer', 'checkboxes', 'multiselect', 'text']] + ]; + foreach ($DB->request($request) as $row) { + $id = $row['id']; + $rangeMin = $DB->escape($row['range_min']); + $rangeMax = $DB->escape($row['range_max']); + $uuid = plugin_formcreator_getUuid(); + $DB->query("INSERT INTO `glpi_plugin_formcreator_questionranges` + SET `plugin_formcreator_questions_id`='$id', `fieldname`='range', `range_min`='$rangeMin', `range_max`='$rangeMax', `uuid`='$uuid'" + ) or plugin_formcreator_upgrade_error($migration); + } + $migration->dropField($table, 'range_min'); + $migration->dropField($table, 'range_max'); + + // decode html entities in answers + $request = [ + 'SELECT' => [ + 'glpi_plugin_formcreator_answers.*' + ], + 'FROM' => 'glpi_plugin_formcreator_answers', + 'INNER JOIN' => [ + 'glpi_plugin_formcreator_questions' => [ + 'FKEY' => [ + 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id', + 'glpi_plugin_formcreator_questions' => 'id' + ] + ] + ], + 'WHERE' => ['fieldtype' => 'textarea'] + ]; + foreach ($DB->request($request) as $row) { + $answer = Toolbox::addslashes_deep(html_entity_decode($row['answer'])); + $id = $row['id']; + $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer`='$answer' WHERE `id` = '$id'"); + } + } + + // decode html entities in question definitions + $request = [ + 'FROM' => 'glpi_plugin_formcreator_questions', + 'WHERE' => [ + 'fieldtype' => ['select', 'multiselect', 'checkboxes', 'radios'] + ] + ]; + foreach ($DB->request($request) as $row) { + $values = Toolbox::addslashes_deep(html_entity_decode($row['values'])); + $defaultValues = Toolbox::addslashes_deep(html_entity_decode($row['default_values'])); + $id = $row['id']; + $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `values` = '$values', `default_values` = '$defaultValues' WHERE `id` = '$id'"); + } + + // decode html entities in name of questions + foreach ($DB->request(['FROM' => 'glpi_plugin_formcreator_questions']) as $row) { + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'])); + $id = $row['id']; + $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `name`='$name' WHERE `id` = '$id'"); + } + + // Add properties for dropdown of ticket categories + $request = [ + 'FROM' => 'glpi_plugin_formcreator_questions', + 'WHERE' => [ + 'fieldtype' => 'dropdown' + ], + ]; + foreach ($DB->request($request) as $row) { + $values = json_decode($row['values'], true); + if ($values['itemtype'] === ITILCategory::class) { + if (!isset($values['show_ticket_categories'])) { + $values['show_ticket_categories'] = 'both'; + } + if (!isset($values['show_ticket_categories_depth'])) { + $values['show_ticket_categories_depth'] = '0'; + } + $id = $row['id']; + $values = json_encode($values); + $DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `values`='$values' WHERE `id` = '$id'"); + } + } + + // multiple files upload per field + $request = [ + 'SELECT' => 'glpi_plugin_formcreator_answers.*', + 'FROM' => 'glpi_plugin_formcreator_answers', + 'LEFT JOIN' => [ + 'glpi_plugin_formcreator_questions' => [ + 'FKEY' => [ + 'glpi_plugin_formcreator_questions' => 'id', + 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id' + ] + ] + ], + 'WHERE' => [ + 'fieldtype' => 'file', + ] + ]; + foreach ($DB->request($request) as $row) { + if (!is_array(json_decode($row['answer'], true))) { + $id = $row['id']; + $answer = json_encode([$row['answer']]); + $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer` = '$answer' WHERE `id` = '$id'"); + } + } + + // Update target change columns + $table = 'glpi_plugin_formcreator_targetchanges'; + $migration->changeField($table, 'comment', 'content', 'longtext'); + $migration->changeField($table, 'impactcontent', 'impactcontent', 'longtext'); + $migration->changeField($table, 'controlistcontent', 'controlistcontent', 'longtext'); + $migration->changeField($table, 'rolloutplancontent', 'rolloutplancontent', 'longtext'); + $migration->changeField($table, 'backoutplancontent', 'backoutplancontent', 'longtext'); + $migration->changeField($table, 'checklistcontent', 'checklistcontent', 'longtext'); + + // Update target target columns + $table = 'glpi_plugin_formcreator_targettickets'; + $migration->changeField($table, 'comment', 'content', 'longtext'); + + // Reorder columns on some tables + $tables = [ + 'glpi_plugin_formcreator_forms', + 'glpi_plugin_formcreator_questions', + 'glpi_plugin_formcreator_sections', + 'glpi_plugin_formcreator_issues', + ]; + foreach ($tables as $table) { + $migration->changeField($table, 'name', 'name', 'string', ['after' => 'id']); + } + + //remove html entities in forms + $request = [ + 'FROM' => 'glpi_plugin_formcreator_forms', + ]; + foreach ($DB->request($request) as $row) { + $name = Toolbox::addslashes_deep(html_entity_decode($row['name'])); + $description = Toolbox::addslashes_deep(html_entity_decode($row['description'])); + $content = Toolbox::addslashes_deep(html_entity_decode($row['content'])); + $id = $row['id']; + $DB->query("UPDATE `glpi_plugin_formcreator_forms` SET `name` = '$name', `description` = '$description', `content` = '$content' WHERE `id` = '$id'"); + } + } +} diff --git a/scripts/cliinstall.php b/scripts/cliinstall.php index 2a85fd3c8..be62b7197 100644 --- a/scripts/cliinstall.php +++ b/scripts/cliinstall.php @@ -45,11 +45,13 @@ cliinstall.php Usage: - cliinstall.php [--as-user USER] [ --tests ] + cliinstall.php [--as-user USER] [ --tests ] [ --force-upgrade | --force-install ] Options: --as-user USER Do install/upgrade as specified USER. If not provided, 'glpi' user will be used --tests Use GLPI test database + --force-upgrade Force upgrade to the latest version + --force-install ignore previous instalation and install from scratch DOC; @@ -114,6 +116,12 @@ exit(1); } print("Installing Plugin Id: " . $plugin->fields['id'] . " version " . $plugin->fields['version'] . "...\n"); +if ($args['--force-install']) { + $_SESSION['plugin_formcreator']['cli'] = 'force-install'; +} +if ($args['--force-upgrade']) { + $_SESSION['plugin_formcreator']['cli'] = 'force-upgrade'; +} ob_start(function($in) { return ''; }); $plugin->install($plugin->fields['id']); ob_end_clean();