Skip to content

Commit

Permalink
handle custom field history
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Sep 15, 2024
1 parent 6400876 commit a32441d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
29 changes: 29 additions & 0 deletions src/Glpi/Asset/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use CommonDBTM;
use Entity;
use Glpi\Application\View\TemplateRenderer;
use Glpi\Asset\Capacity\HasHistoryCapacity;
use Glpi\Asset\CustomFieldType\DropdownType;
use Group;
use Group_Item;
Expand Down Expand Up @@ -501,6 +502,34 @@ public function post_getFromDB()
}
}

public function post_updateItem($history = true)
{
if ($this->dohistory) {
$old_custom_fields = json_decode($this->oldvalues['custom_fields'] ?? '[]', true);
$current_custom_fields = $this->getDecodedCustomFields();

foreach (static::getDefinition()->getCustomFieldDefinitions() as $custom_field) {
$field_type = $custom_field->getFieldType();
$old_value = $field_type->formatValueFromDB($old_custom_fields[$custom_field->getID()] ?? $field_type->getDefaultValue());
$current_value = $field_type->formatValueFromDB($current_custom_fields[$custom_field->getID()] ?? null);
$opt = $custom_field->getFieldType()->getSearchOption();

if ($old_value !== $current_value) {
$dropdown = $opt['table'] !== static::getTable();
if ($dropdown) {
$old_value = $old_value !== null ? \Dropdown::getDropdownName($opt['table'], $old_value) : $old_value;
$current_value = $current_value !== null ? \Dropdown::getDropdownName($opt['table'], $current_value) : $current_value;
}
\Log::history($this->getID(), static::class, [
$custom_field->getSearchOptionID(),
$old_value,
$current_value,
]);
}
}
}
}

/**
* Ensure definition input corresponds to the current concrete class.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Glpi/Asset/CustomFieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public function computeFriendlyName(): string
return $this->fields['label'];
}

public function getSearchOptionID(): int
{
return 45000 + $this->getID();
}

public function getFieldType(): TypeInterface
{
$field_types = AssetDefinitionManager::getInstance()->getCustomFieldTypes();
Expand Down
8 changes: 2 additions & 6 deletions src/Glpi/Asset/CustomFieldType/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,13 @@ public function getDefaultValueFormInput(): string
return $this->getFormInput($this->custom_field->fields['default_value'], __('Default value'), 'default_value');
}

protected function getSearchOptionId(): int
{
return 45000 + $this->custom_field->getID();
}

protected function getCommonSearchOptionData(): array
{
/** @var \DBmysql $DB */
global $DB;

return [
'id' => $this->getSearchOptionId(),
'id' => $this->custom_field->getSearchOptionID(),
'name' => $this->custom_field->fields['label'],
'table' => 'glpi_assets_assets',
'field' => 'value',
Expand All @@ -121,6 +116,7 @@ protected function getCommonSearchOptionData(): array
]),
'nometa' => true,
'field_definition' => $this->custom_field,
'no_history' => true, // Do not use standard handling for field value history since all cusotm field values are stored in the same column
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Glpi/Asset/CustomFieldType/DropdownType.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getSearchOption(): ?array
$multiple = $this->custom_field->fields['field_options']['multiple'] ?? false;

$opt = [
'id' => $this->getSearchOptionId(),
'id' => $this->custom_field->getSearchOptionID(),
'name' => $this->custom_field->fields['label'],
'itemtype' => $itemtype,
'table' => getTableForItemType($itemtype),
Expand All @@ -111,7 +111,8 @@ public function getSearchOption(): ?array
'itemlink_type' => $itemtype,
'joinparams' => [
'jointype' => 'custom_condition_only',
]
],
'no_history' => true,
];

if (!$multiple) {
Expand Down
4 changes: 2 additions & 2 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public static function constructHistory(CommonDBTM $item, $oldvalues, $values)

// Parsing $SEARCHOPTION to find changed field
foreach ($searchopt as $key2 => $val2) {
if (!isset($val2['table'])) {
// skip sub-title
if (!isset($val2['table']) || ($val2['no_history'] ?? false) === true) {
// skip sub-title and no_history fields
continue;
}
// specific for profile
Expand Down
54 changes: 54 additions & 0 deletions tests/functional/Glpi/Asset/CustomFieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,58 @@ public function testDateTimezones()

date_default_timezone_set($original_tz);
}

public function testCustomFieldHistory()
{
$asset_definition = $this->initAssetDefinition(
capacities: [
\Glpi\Asset\Capacity\HasHistoryCapacity::class,
]
);
$field_1 = new \Glpi\Asset\CustomFieldDefinition();
$field_2 = new \Glpi\Asset\CustomFieldDefinition();

$field_1->add([
'assets_assetdefinitions_id' => $asset_definition->getID(),
'name' => 'test',
'label' => 'Test',
'type' => StringType::class,
'default_value' => 'default',
]);
$field_2->add([
'assets_assetdefinitions_id' => $asset_definition->getID(),
'name' => 'test_two',
'label' => 'Test2',
'type' => DropdownType::class,
'itemtype' => \Computer::class,
]);

$asset = new ($asset_definition->getAssetClassName());
$asset->add([
'entities_id' => $this->getTestRootEntity(true),
'name' => 'Test asset'
]);

$asset->update([
'id' => $asset->getID(),
'custom_test' => 'value',
'custom_test_two' => getItemByTypeName(\Computer::class, '_test_pc01', true),
]);

$this->integer(countElementsInTable(\Log::getTable(), [
'itemtype' => $asset_definition->getAssetClassName(),
'items_id' => $asset->getID(),
'id_search_option' => $field_1->getSearchOptionID(),
'old_value' => 'default',
'new_value' => 'value',
]))->isIdenticalTo(1);

$this->integer(countElementsInTable(\Log::getTable(), [
'itemtype' => $asset_definition->getAssetClassName(),
'items_id' => $asset->getID(),
'id_search_option' => $field_2->getSearchOptionID(),
'old_value' => '',
'new_value' => '_test_pc01',
]))->isIdenticalTo(1);
}
}

0 comments on commit a32441d

Please sign in to comment.