Skip to content

Commit 68fc4d4

Browse files
Fix: Replace multiline_text fields when prefixed with #replace (#458)
* Fix: Replace multiline_text fields when prefixed with #replace - If a new value for a multiline_text field starts with "#replace", the old value is completely replaced instead of being concatenated. - The "#replace" tag is removed before storing the new value. - Default behavior remains unchanged for other cases. * Fix: Add multiline_value_replace field and update function setValueForItemtype() * fix: use dedicated function for adding fields to the database refactor: renamed multiline_value_replace to replace_multiline_value * refactor: renamed multiline_value_replace to replace_multiline_value * fix: remove whitespace * update changelog for 2.15.0 * update: changelog
1 parent 811bf56 commit 68fc4d4

5 files changed

+53
-5
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- Use `global` configuration for injection links
1313
- Escape data when check if already exist
1414

15+
## [Unreleased]
16+
17+
### Added
18+
19+
- Add option to `replace` (instead of `append`) the value of multiline text fields (e.g. `comment`)
20+
1521
## [2.14.1] - 2024-12-27
1622

1723
### Added

hook.php

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function plugin_datainjection_install()
6767
plugin_datainjection_migration_251_252($migration);
6868
plugin_datainjection_migration_264_270($migration);
6969
plugin_datainjection_migration_2121_2122($migration);
70+
plugin_datainjection_migration_2141_2150($migration);
7071
break;
7172

7273
case 0:
@@ -91,6 +92,7 @@ function plugin_datainjection_install()
9192
`float_format` tinyint NOT NULL DEFAULT '0',
9293
`port_unicity` tinyint NOT NULL DEFAULT '0',
9394
`step` int NOT NULL DEFAULT '0',
95+
`replace_multiline_value` tinyint NOT NULL DEFAULT '0',
9496
PRIMARY KEY (`id`)
9597
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
9698
$DB->doQueryOrDie($query, $DB->error());
@@ -196,6 +198,7 @@ function plugin_datainjection_install()
196198
plugin_datainjection_migration_264_270($migration);
197199
plugin_datainjection_migration_290_2100($migration);
198200
plugin_datainjection_migration_2121_2122($migration);
201+
plugin_datainjection_migration_2141_2150($migration);
199202
break;
200203

201204
default:
@@ -233,6 +236,19 @@ function plugin_datainjection_uninstall()
233236
return true;
234237
}
235238

239+
function plugin_datainjection_migration_2141_2150(Migration $migration)
240+
{
241+
$migration->setVersion('2.15.0');
242+
243+
$migration->addField(
244+
'glpi_plugin_datainjection_models',
245+
'replace_multiline_value',
246+
'bool'
247+
);
248+
249+
$migration->executeMigration();
250+
}
251+
236252
function plugin_datainjection_migration_2121_2122(Migration $migration)
237253
{
238254

inc/commoninjectionlib.class.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public function setDefaultValues(): void
139139
'overwrite_notempty_fields' => false,
140140
'can_add' => false,
141141
'can_update' => false,
142-
'can_delete' => false
142+
'can_delete' => false,
143+
'replace_multiline_value' => false,
143144
];
144145

145146
//Field format options
@@ -943,10 +944,14 @@ private function setValueForItemtype($itemtype, $field, $value, $fromdb = false)
943944
$option = self::findSearchOption($injectionClass->getOptions($itemtype), $field);
944945

945946
if (isset($option['displaytype']) && $option['displaytype'] == 'multiline_text') {
946-
if ($fromdb) {
947-
$this->values[$itemtype][$field] = $value . "\n" . $this->values[$itemtype][$field];
948-
} else {
949-
$this->values[$itemtype][$field] = $this->values[$itemtype][$field] . "\n" . $value;
947+
// If replace_multiline_value is true, the old value is replaced with the new value
948+
// else, the new value is added to the old value
949+
if (!$this->rights['replace_multiline_value']) {
950+
if ($fromdb) {
951+
$this->values[$itemtype][$field] = $value . "\n" . $this->values[$itemtype][$field];
952+
} else {
953+
$this->values[$itemtype][$field] = $this->values[$itemtype][$field] . "\n" . $value;
954+
}
950955
}
951956
} else if (
952957
($fromdb && $value && !$this->rights['overwrite_notempty_fields'])

inc/engine.class.php

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public function injectLine($line, $index)
131131
//Rights options
132132
$rights = ['add_dropdown' => $this->getModel()->getCanAddDropdown(),
133133
'overwrite_notempty_fields' => $this->getModel()->getCanOverwriteIfNotEmpty(),
134+
'replace_multiline_value' => $this->getModel()->getReplaceMultilineValue(),
134135
'can_add' => $this->model->getBehaviorAdd(),
135136
'can_update' => $this->model->getBehaviorUpdate(),
136137
'can_delete' => false

inc/model.class.php

+20
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ public function getPortUnicity()
252252
}
253253

254254

255+
public function getReplaceMultilineValue()
256+
{
257+
258+
return $this->fields["replace_multiline_value"];
259+
}
260+
261+
255262
public function getNumberOfMappings()
256263
{
257264

@@ -520,6 +527,14 @@ public function rawSearchOptions()
520527
'datatype' => 'bool',
521528
];
522529

530+
$tab[] = [
531+
'id' => 87,
532+
'table' => $this->getTable(),
533+
'field' => 'replace_multiline_value',
534+
'name' => __('Replacing the value of multiline text fields', 'datainjection'),
535+
'datatype' => 'bool',
536+
];
537+
523538
return $tab;
524539
}
525540

@@ -753,6 +768,11 @@ public function showAdvancedForm($ID, $options = [])
753768
PluginDatainjectionDropdown::portUnicityValues(),
754769
['value' => $this->fields['port_unicity']]
755770
);
771+
echo "</td>";
772+
echo "<td>" . __('Replacing the value of multiline text fields', 'datainjection') . "</td>";
773+
echo "<td>";
774+
Dropdown::showYesNo("replace_multiline_value", $this->fields['replace_multiline_value']);
775+
echo "</td>";
756776
echo "</td></tr>";
757777
if ($ID > 0) {
758778
$tmp = self::getInstance('csv');

0 commit comments

Comments
 (0)