/** * Database table installation for the item type * * @param Migration $migration * @return boolean True on success */ public static function install(Migration $migration) { $obj = new self(); $table = $obj->getTable(); // Create new table if (!TableExists($table)) { $migration->displayMessage("Installing {$table}"); // Create questions table $query = "CREATE TABLE IF NOT EXISTS `{$table}` (\n `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n `plugin_formcreator_forms_id` int(11) NOT NULL,\n `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,\n `order` int(11) NOT NULL DEFAULT '0'\n )\n ENGINE = MyISAM\n DEFAULT CHARACTER SET = utf8\n COLLATE = utf8_unicode_ci;"; $GLOBALS['DB']->query($query) or die($GLOBALS['DB']->error()); } else { /** * Migration of special chars from previous versions * * @since 0.85-1.2.3 */ $query = "SELECT `id`, `name`\n FROM `{$table}`"; $result = $GLOBALS['DB']->query($query); while ($line = $GLOBALS['DB']->fetch_array($result)) { $query_update = 'UPDATE `' . $table . '` SET `name` = "' . plugin_formcreator_encode($line['name']) . '" WHERE `id` = ' . $line['id']; $GLOBALS['DB']->query($query_update) or die($GLOBALS['DB']->error()); } } // Migration from previous version => Remove useless target field if (FieldExists($table, 'plugin_formcreator_targets_id', false)) { $GLOBALS['DB']->query("ALTER TABLE `{$table}` DROP `plugin_formcreator_targets_id`;"); } // Migration from previous version => Rename "position" into "order" and start order from 1 instead of 0 if (FieldExists($table, 'position', false)) { $GLOBALS['DB']->query("ALTER TABLE `{$table}` CHANGE `position` `order` INT(11) NOT NULL DEFAULT '0';"); $GLOBALS['DB']->query("UPDATE `{$table}` SET `order` = `order` + 1;"); } // Migration from previous version => Update Question table, then create a "description" question from content if (FieldExists($table, 'content', false)) { $version = plugin_version_formcreator(); $migration = new Migration($version['version']); PluginFormcreatorQuestion::install($migration); $table_questions = getTableForItemType('PluginFormcreatorQuestion'); // Increment the order of questions which are in a section with a description $query = "UPDATE `{$table_questions}`\n SET `order` = `order` + 1\n WHERE `plugin_formcreator_sections_id` IN (\n SELECT `id`\n FROM {$table}\n WHERE `content` != ''\n );"; $GLOBALS['DB']->query($query); // Create description from content $query = "INSERT INTO `{$table_questions}` (`plugin_formcreator_sections_id`, `fieldtype`, `name`, `description`, `order`)\n SELECT `id`, 'description' AS fieldtype, CONCAT('Description ', `id`) AS name, `content`, 1 AS `order`\n FROM {$table}\n WHERE `content` != ''"; $GLOBALS['DB']->query($query); // Delete content column $GLOBALS['DB']->query("ALTER TABLE `{$table}` DROP `content`;"); } return true; }