Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 63 additions & 10 deletions import/import_items.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,12 +2,16 @@
<?php
/**
***********************************************************************************************
* Import items from a csv file
* Import items from a file
*
* @see https://github.com/MightyMCoder/InventoryManager/ The InventoryManager GitHub project
* @author MightyMCoder
* @copyright 2024 - today MightyMCoder
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 only
*
* methods:
*
* compareArrays(array $array1, array $array2) : Compares two arrays to determine if they are different based on specific criteria.
***********************************************************************************************
*/

Expand All@@ -24,6 +28,39 @@
$gMessage->show($gL10n->get('SYS_NO_RIGHTS'));
}

/**
* Compares two arrays to determine if they are different based on specific criteria.
*
* This function filters out certain keys ('KEEPER', 'LAST_RECEIVER', 'CATEGORY') from the first array,
* converts date fields ('RECEIVED_ON', 'RECEIVED_BACK_ON') from 'd.m.Y' format to 'Y-m-d' format,
* and then checks if any value in the filtered and transformed first array is not present in the second array.
*
* @param array $array1 The first array to compare.
* @param array $array2 The second array to compare.
* @return bool Returns true if the arrays are different based on the criteria, otherwise false.
*/
function compareArrays(array $array1, array $array2): bool {
$array1 = array_filter($array1, function($key) {
return $key !== 'KEEPER' && $key !== 'LAST_RECEIVER' && $key !== 'CATEGORY';
}, ARRAY_FILTER_USE_KEY);

foreach (['RECEIVED_ON', 'RECEIVED_BACK_ON'] as $dateField) {
if (isset($array1[$dateField])) {
$date = DateTime::createFromFormat('d.m.Y', $array1[$dateField]);
if ($date) {
$array1[$dateField] = $date->format('Y-m-d');
}
}
}

foreach ($array1 as $value) {
if (!in_array($value, $array2, true)) {
return true;
}
}
return false;
}

$_SESSION['import_csv_request'] = $_POST;

// check the CSRF token of the form against the session token
Expand All@@ -38,6 +75,8 @@
$pPreferences = new CConfigTablePIM();
$pPreferences->read();

$user = new User($gDb, $gProfileFields);

// create array with all profile fields that where assigned to columns of the import file
foreach ($_POST as $formFieldId => $importFileColumn) {
if ($importFileColumn !== '' && $formFieldId !== 'admidio-csrf-token' && $formFieldId !== 'first_row') {
Expand DownExpand Up@@ -74,16 +113,30 @@
$items->readItems($gCurrentOrgId);
$importSuccess = false;

// check if the item already exists
foreach ($items->items as $fieldId => $value) {
$items->readItemData($value['imi_id'], $gCurrentOrgId);
$i = 0;
$itemValues = array();
foreach ($items->mItemData as $key => $itemData) {
$itemValue = $itemData->getValue('imd_value');
if ($itemData->getValue('imf_name_intern') === 'KEEPER' || $itemData->getValue('imf_name_intern') === 'LAST_RECEIVER' || $itemData->getValue('imf_name_intern') === 'CATEGORY')
{
continue;
}

$itemValues[] = array($itemData->getValue('imf_name_intern') => $itemValue);
}
$itemValues = array_merge_recursive(...$itemValues);

foreach ($items->mItemData as $itemData) {
foreach ($assignedFieldColumn as $row => $values) {
if ($itemData->getValue('imd_value') == $values['ITEMNAME']) {
unset($assignedFieldColumn[$row]);
continue;
}
if (count($assignedFieldColumn) === 0) {
break;
}

foreach($assignedFieldColumn as $key => $value) {
$ret = compareArrays($value, $itemValues);
if (!$ret) {
unset($assignedFieldColumn[$key]);
continue;
}
}
}
Expand DownExpand Up@@ -282,6 +335,6 @@
$gMessage->show($gL10n->get('SYS_SAVE_DATA'));
}
else {
$gMessage->setForwardUrl($gNavigation->getUrl(), 1000);
$gMessage->show($gL10n->get('SYS_REDIRECT'));
$gMessage->setForwardUrl($gNavigation->getUrl());
$gMessage->show($gL10n->get('PLG_INVENTORY_MANAGER_NO_NEW_IMPORT_DATA'));
}
1 change: 1 addition & 0 deletions languages/de-DE.xml
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,6 +72,7 @@
<string name="PLG_INVENTORY_MANAGER_MENU_URL_ERROR">Die Authorisierungsprüfung des Plugins ist fehlgeschlagen. Es ist mehr als ein Menüpunkt mit derselben URL definiert.\n\n=> #VAR1_BOLD#</string>
<string name="PLG_INVENTORY_MANAGER_NAME_OF_PLUGIN">InventoryManager</string>
<string name="PLG_INVENTORY_MANAGER_NEW">Neu</string>
<string name="PLG_INVENTORY_MANAGER_NO_NEW_IMPORT_DATA">In der Importdatei waren keine neuen Daten vorhanden!</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CHANGED">Der Gegenstand #VAR1_BOLD# wurde von #VAR2_BOLD# geändert:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CREATED">Der Gegenstand #VAR1_BOLD# wurde von #VAR2_BOLD# angelegt:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_DELETED">Ein Gegenstand im Inventar wurde gelöscht:</string>
Expand Down
1 change: 1 addition & 0 deletions languages/de.xml
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,6 +72,7 @@
<string name="PLG_INVENTORY_MANAGER_MENU_URL_ERROR">Die Authorisierungsprüfung des Plugins ist fehlgeschlagen. Es ist mehr als ein Menüpunkt mit derselben URL definiert.\n\n=> #VAR1_BOLD#</string>
<string name="PLG_INVENTORY_MANAGER_NAME_OF_PLUGIN">InventoryManager</string>
<string name="PLG_INVENTORY_MANAGER_NEW">Neu</string>
<string name="PLG_INVENTORY_MANAGER_NO_NEW_IMPORT_DATA">In der Importdatei waren keine neuen Daten vorhanden!</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CHANGED">Der Gegenstand #VAR1_BOLD# wurde von #VAR2_BOLD# geändert:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CREATED">Der Gegenstand #VAR1_BOLD# wurde von #VAR2_BOLD# angelegt:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_DELETED">Ein Gegenstand im Inventar wurde gelöscht:</string>
Expand Down
1 change: 1 addition & 0 deletions languages/en.xml
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,6 +72,7 @@
<string name="PLG_INVENTORY_MANAGER_MENU_URL_ERROR">The authorization check of the plugin failed. There is more than one menu item with the same URL defined.\n\n=> #VAR1_BOLD#</string>
<string name="PLG_INVENTORY_MANAGER_NAME_OF_PLUGIN">InventoryManager</string>
<string name="PLG_INVENTORY_MANAGER_NEW">New</string>
<string name="PLG_INVENTORY_MANAGER_NO_NEW_IMPORT_DATA">There was no new data in the import file!</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CHANGED">The item #VAR1_BOLD# was changed by #VAR2_BOLD#:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CREATED">The item #VAR1_BOLD# was created by #VAR2_BOLD#:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_DELETED">An item was deleted:</string>
Expand Down
1 change: 1 addition & 0 deletions languages/fr.xml
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,6 +72,7 @@
<string name="PLG_INVENTORY_MANAGER_MENU_URL_ERROR">Le contrôle d\'autorisation du plug-in a échoué. Plus d\'un élément de menu est défini avec la même URL.\n\n=> #VAR1_BOLD#</string>
<string name="PLG_INVENTORY_MANAGER_NAME_OF_PLUGIN">InventoryManager</string>
<string name="PLG_INVENTORY_MANAGER_NEW">Nouveau</string>
<string name="PLG_INVENTORY_MANAGER_NO_NEW_IMPORT_DATA">Aucune nouvelle donnée n'était présente dans le fichier d'importation!</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CHANGED">L'objet #VAR1_BOLD# a été modifié par #VAR2_BOLD#:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_CREATED">L'objet #VAR1_BOLD# a été créé par #VAR2_BOLD#:</string>
<string name="PLG_INVENTORY_MANAGER_NOTIFICATION_MESSAGE_ITEM_DELETED">Un objet dans l'inventaire a été supprimé:</string>
Expand Down