/** * Create a table to store custom data for a device type if it doesn't * already exist * * @param $itemtype Type of item * @return bool Success */ function plugin_customfields_create_data_table($itemtype) { global $DB; $table = plugin_customfields_table($itemtype); if (!TableExists($table)) { $sql = "CREATE TABLE `{$table}` (\r\n `id` int(11) NOT NULL default '0',\r\n PRIMARY KEY (`id`)\r\n )ENGINE=MyISAM\r\n DEFAULT\r\n CHARSET=utf8\r\n COLLATE=utf8_unicode_ci\r\n AUTO_INCREMENT=3"; $result = $DB->query($sql); return $result ? true : false; } return true; }
/** * Upgrade => 1.2 */ function plugin_customfields_upgradeto12() { global $DB; $glpi_tables = array('glpi_plugin_customfields_software' => 'glpi_plugin_customfields_softwares', 'glpi_plugin_customfields_networking' => 'glpi_plugin_customfields_networkequipments', 'glpi_plugin_customfields_enterprises' => 'glpi_plugin_customfields_suppliers', 'glpi_plugin_customfields_docs' => 'glpi_plugin_customfields_documents', 'glpi_plugin_customfields_tracking' => 'glpi_plugin_customfields_tickets', 'glpi_plugin_customfields_user' => 'glpi_plugin_customfields_users', 'glpi_plugin_customfields_networking_ports' => 'glpi_plugin_customfields_networkports'); foreach ($glpi_tables as $oldtable => $newtable) { if (!TableExists("{$newtable}") && TableExists("{$oldtable}")) { $query = "RENAME TABLE `{$oldtable}` TO `{$newtable}`"; $DB->query($query) or die($DB->error()); } } if (TableExists("glpi_plugin_customfields")) { $query = "RENAME TABLE `glpi_plugin_customfields`\n TO `glpi_plugin_customfields_itemtypes`"; $DB->query($query) or die($DB->error()); $query = "ALTER TABLE `glpi_plugin_customfields_itemtypes`\n CHANGE `ID` `id` int(11) NOT NULL auto_increment,\n CHANGE `device_type` `itemtype` VARCHAR(100) NOT NULL\n default ''"; $DB->query($query) or die($DB->error()); $tables = array('glpi_plugin_customfields_itemtypes'); Plugin::migrateItemType(array(-1 => 'Version'), array(), $tables); $query = "SELECT `itemtype`\n FROM `glpi_plugin_customfields_itemtypes`\n WHERE `itemtype` <> 'Version' "; $result = $DB->query($query); $enabled = array(); while ($data = $DB->fetch_array($result)) { $enabled[] = $data['itemtype']; $table = plugin_customfields_table($data['itemtype']); if (TableExists($table)) { $query = "ALTER TABLE `{$table}`\n CHANGE `ID` `id` int(11) NOT NULL auto_increment"; $DB->query($query) or die($DB->error()); } } foreach ($enabled as $itemtype) { $sql = "UPDATE `glpi_plugin_customfields_itemtypes`\n SET `enabled` = 1\n WHERE `itemtype` = '{$itemtype}';"; $DB->query($sql) or die($DB->error()); } } if (TableExists("glpi_plugin_customfields_dropdowns")) { $query = "ALTER TABLE `glpi_plugin_customfields_dropdowns`\n CHANGE `ID` `id` int(11) NOT NULL auto_increment"; $DB->query($query) or die($DB->error()); } if (TableExists("glpi_plugin_customfields_fields")) { $query = "ALTER TABLE `glpi_plugin_customfields_fields`\n CHANGE `ID` `id` int(11) NOT NULL auto_increment,\n CHANGE `device_type` `itemtype` VARCHAR(100) NOT NULL\n default ''"; $DB->query($query) or die($DB->error()); $tables = array('glpi_plugin_customfields_fields'); Plugin::migrateItemType(array(-1 => 'Version'), array(), $tables); } if (TableExists("glpi_plugin_customfields_profiledata")) { $query = "RENAME TABLE `glpi_plugin_customfields_profiledata` \n TO `glpi_plugin_customfields_profiles`"; $DB->query($query) or die($DB->error()); $query = "ALTER TABLE `glpi_plugin_customfields_profiles`\n CHANGE `ID` `id` int(11) NOT NULL auto_increment"; $DB->query($query) or die($DB->error()); } }
} if ($data_ok) { // Get next search option position $sql = "SELECT MAX(`sopt_pos`)+1 AS next_sopt_pos\n FROM `glpi_plugin_customfields_fields`\n WHERE `itemtype` = '{$itemtype}'"; $result = $DB->query($sql); $data = $DB->fetch_assoc($result); $sopt_pos = $data['next_sopt_pos']; if (!$sopt_pos) { $sopt_pos = 1; } // Insert field $sql = "INSERT INTO `glpi_plugin_customfields_fields`\n (`itemtype`, `system_name`, `label`, `data_type`,\n `sort_order`, `dropdown_table`, `deleted`,\n `sopt_pos`, `restricted`)\n VALUES ('{$itemtype}', '{$system_name}', '{$label}',\n '{$data_type}', '{$sort}', '{$dd_table}', 0,\n '{$sopt_pos}', 0)"; $result = $DB->query($sql); if ($data_type != 'sectionhead') { // add the field to the data table if it isn't a section header $table = plugin_customfields_table($itemtype); if (CUSTOMFIELDS_AUTOACTIVATE) { // creates table and activates IF necessary plugin_customfields_activate_all($itemtype); } else { // creates table if it doesn't alreay exist plugin_customfields_create_data_table($itemtype); } switch ($data_type) { case 'general': $db_data_type = 'VARCHAR(255) collate utf8_unicode_ci default NULL'; break; case 'yesno': $db_data_type = 'SMALLINT(6) NOT NULL default \'0\''; break; case 'text':
/** * Hook done on purge item case * * @param $parm Object to be purged * @return bool Success */ function plugin_item_purge_customfields($parm) { global $ALL_CUSTOMFIELDS_TYPES, $DB; // Must delete custom fields when main item is purged, // even if custom fields for this device are currently disabled if (in_array($parm->getType(), $ALL_CUSTOMFIELDS_TYPES) && ($table = plugin_customfields_table($parm->getType()))) { $sql = "DELETE FROM {$table} where id=" . $parm->getID(); $DB->query($sql); return true; } return false; }
/** * Unregister an item type brought by an other plugin * * @param $itemtype * */ static function unregisterItemType($itemType) { global $DB; // Check if the itemtype has already been registered $query = "SELECT `enabled`\n FROM `glpi_plugin_customfields_itemtypes`\n WHERE `itemtype` = '{$itemType}'"; $result = $DB->query($query); if ($DB->numrows($result) != 0) { // The itemtype must be disabled before unregistration // Has no effect if the itemtype is not enabled plugin_customfields_disable_device($itemType); // The new item type needs to be unregistered // Remove the fields definition for the itemtype being deleted $query = "DELETE FROM `glpi_plugin_customfields_fields`\n WHERE `itemtype` = '{$itemType}'"; $result = $DB->query($query) or die($DB->error()); // Remove the itemtype from the itemtypes supported by the plugin $query = "DELETE FROM `glpi_plugin_customfields_itemtypes`\n WHERE `itemtype` = '{$itemType}'"; $result = $DB->query($query) or die($DB->error()); // Remove all data for the itemtype $table = plugin_customfields_table($itemType); if ($table) { $query = "DROP TABLE IF EXISTS `{$table}`"; $DB->query($query) or die($DB->error()); } // Unregister the itemtype $query = "DELETE FROM `glpi_plugin_customfields_itemtypes`\n WHERE `itemtype` = '{$itemType}'"; $DB->query($query) or die($DB->error()); } }
/** * Constructor. Adds itemtype and various initialisations. * * @param string $itemtype */ function __construct($itemtype = "") { $this->type = $itemtype; $this->dohistory = true; $this->forceTable(plugin_customfields_table($itemtype)); }