diff --git a/CHANGELOG.md b/CHANGELOG.md index 714326e..f3dc38f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [UNRELEASED] +### Added +- Added support to choose importing/merging mobile devices as Computers or Phones + ### Fixes - SQL error when merging the Jamf device linked to a GLPI asset diff --git a/ajax/import.php b/ajax/import.php index 8173f0e..a6b2bd3 100644 --- a/ajax/import.php +++ b/ajax/import.php @@ -56,21 +56,22 @@ if (isset($_REQUEST['import_ids']) && is_array($_REQUEST['import_ids'])) { // Get data for each item to import $toimport = $DB->request([ - 'SELECT' => ['type', 'jamf_type', 'jamf_items_id'], - 'FROM' => PluginJamfImport::getTable(), - 'WHERE' => [ - 'id' => $_REQUEST['import_ids'], - ], + 'SELECT' => ['id', 'type', 'jamf_type', 'jamf_items_id'], + 'FROM' => PluginJamfImport::getTable(), + 'WHERE' => [ + 'id' => $_REQUEST['import_ids'] + ] ]); // Trigger extension attribute definition sync PluginJamfMobileSync::syncExtensionAttributeDefinitions(); PluginJamfComputerSync::syncExtensionAttributeDefinitions(); // Import the requested device(s) foreach ($toimport as $data) { + $glpi_itemtype = $_REQUEST['itemtype_overrides'][$data['id']] ?? $data['type']; if ($data['jamf_type'] === 'MobileDevice') { - PluginJamfMobileSync::import($data['type'], $data['jamf_items_id']); + PluginJamfMobileSync::import($glpi_itemtype, $data['jamf_items_id']); } else { - PluginJamfComputerSync::import($data['type'], $data['jamf_items_id']); + PluginJamfComputerSync::import($glpi_itemtype, $data['jamf_items_id']); } } } else { diff --git a/ajax/merge.php b/ajax/merge.php index a465874..ca6d09a 100644 --- a/ajax/merge.php +++ b/ajax/merge.php @@ -54,6 +54,10 @@ // Trigger extension attribute definition sync PluginJamfMobileSync::syncExtensionAttributeDefinitions(); PluginJamfComputerSync::syncExtensionAttributeDefinitions(); + $supported_glpi_types = [ + 'Computer' => PluginJamfComputerSync::getSupportedGlpiItemtypes(), + 'MobileDevice' => PluginJamfMobileSync::getSupportedGlpiItemtypes() + ]; // An array of item IDs is required if (isset($_REQUEST['item_ids']) && is_array($_REQUEST['item_ids'])) { $failures = 0; @@ -65,7 +69,7 @@ $jamf_id = $data['jamf_id']; $itemtype = $data['itemtype']; - if (($itemtype !== 'Computer') && ($itemtype !== 'Phone')) { + if (!in_array($itemtype, $supported_glpi_types[$data['jamf_type']])) { // Invalid itemtype for a mobile device throw new RuntimeException('Invalid itemtype!'); } diff --git a/front/merge.php b/front/merge.php index 1b3ed93..3b0e580 100644 --- a/front/merge.php +++ b/front/merge.php @@ -64,35 +64,46 @@ $linked[$data['itemtype']][] = $data; } +$supported_glpi_types = [ + 'Computer' => PluginJamfComputerSync::getSupportedGlpiItemtypes(), + 'MobileDevice' => PluginJamfMobileSync::getSupportedGlpiItemtypes() +]; + foreach ($pending as &$data) { - $itemtype = $data['type']; - /** @var CommonDBTM $item */ - $item = new $itemtype(); - $jamftype = ('PluginJamf' . $data['jamf_type']); - $guesses = $DB->request([ - 'SELECT' => ['id'], - 'FROM' => $itemtype::getTable(), - 'WHERE' => [ - 'OR' => [ - 'uuid' => $data['udid'], - 'name' => Sanitizer::sanitize($data['name']), + $queries = []; + foreach ($supported_glpi_types[$data['jamf_type']] as $type) { + $queries[] = [ + 'SELECT' => [ + new QueryExpression($DB::quoteValue($type) . ' AS ' . $DB::quoteName('itemtype')), + 'id' + ], + 'FROM' => $type::getTable(), + 'WHERE' => [ + 'OR' => [ + 'uuid' => $data['udid'], + 'name' => Sanitizer::sanitize($data['name']) + ], + 'is_deleted' => 0, + 'is_template' => 0 ], - 'is_deleted' => 0, - 'is_template' => 0, - ], - 'ORDER' => new QueryExpression("CASE WHEN uuid='" . $data['udid'] . "' THEN 0 ELSE 1 END"), - 'LIMIT' => 1, - ]); - if (count($guesses)) { - $data['guessed_item'] = $guesses->current()['id']; - } else { - $data['guessed_item'] = 0; + 'ORDER' => new QueryExpression("CASE WHEN uuid='" . $data['udid'] . "' THEN 0 ELSE 1 END"), + 'LIMIT' => 1 + ]; + } + $guesses = $DB->request(new QueryUnion($queries)); + $data['guessed_item'] = null; + foreach ($guesses as $guess) { + $data['guessed_item'][$guess['itemtype']] = $guess['id']; } } TemplateRenderer::getInstance()->display('@jamf/merge.html.twig', [ 'pending' => $pending, 'total_count' => $importcount, - 'linked' => $linked, + 'linked' => $linked, + 'supported_glpi_types' => array_map( + static fn ($ts) => array_combine($ts, array_map(static fn ($t) => $t::getTypeName(1), $ts)), + $supported_glpi_types + ) ]); Html::footer(); diff --git a/templates/import.html.twig b/templates/import.html.twig index 385435c..3a07cc0 100644 --- a/templates/import.html.twig +++ b/templates/import.html.twig @@ -28,6 +28,7 @@ * ------------------------------------------------------------------------- */ #} +{% import 'components/form/fields_macros.html.twig' as fields %}