Skip to content

Commit

Permalink
GH-224 Move deleting ignore entries validation to an util
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 14, 2022
1 parent 8510c0d commit 462074f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
2 changes: 2 additions & 0 deletions modules/settings/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

include($includePath . './utils/content/prepareChangeProcessEmails.content.php');

include($includePath . './utils/errorMappers/tryDeleteUserIgnoreEntries.errorMapper.php');
include($includePath . './utils/errorMappers/tryEnableVacation.errorMapper.php');
include($includePath . './utils/errorMappers/tryIgnoreUser.errorMapper.php');
include($includePath . './utils/errorMappers/validatePasswordChange.errorMapper.php');
include($includePath . './utils/errorMappers/validateEmailChange.errorMapper.php');

include($includePath . './utils/helpers/tryDeleteUserIgnoreEntries.helper.php');
include($includePath . './utils/helpers/tryEnableVacation.helper.php');
include($includePath . './utils/helpers/tryIgnoreUser.helper.php');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Utils\ErrorMappers;

/**
* @param object $error As returned by Settings\Utils\Helpers\tryDeleteUserIgnoreEntries
*/
function mapTryDeleteUserIgnoreEntriesErrorToReadableMessage($error) {
global $_Lang;

$errorCode = $error['code'];

$knownErrorsByCode = [
'NO_VALID_ENTRY_ID_PROVIDED' => $_Lang['Ignore_NothingSelected'],
'NO_VALID_ENTRY_ID_SELECTED' => $_Lang['Ignore_NothingDeleted'],
];

if (!isset($knownErrorsByCode[$errorCode])) {
return $_Lang['fleet_generic_errors_unknown'];
}

return $knownErrorsByCode[$errorCode];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Utils\Helpers;

/**
* @param array $params
* @param array $params['entriesIds']
* @param arrayRef $params['currentUser']
*/
function tryDeleteUserIgnoreEntries($params) {
$executor = function ($input, $resultHelpers) {
$currentUser = &$input['currentUser'];
$entriesIds = $input['entriesIds'];

if (empty($entriesIds)) {
return $resultHelpers['createFailure']([
'code' => 'NO_VALID_ENTRY_ID_PROVIDED',
]);
}

$existingEntriesIds = array_filter($entriesIds, function ($entryId) use (&$currentUser) {
return !empty($currentUser['IgnoredUsers'][$entryId]);
});

if (empty($existingEntriesIds)) {
return $resultHelpers['createFailure']([
'code' => 'NO_VALID_ENTRY_ID_SELECTED',
]);
}

return $resultHelpers['createSuccess']([
'entriesToDelete' => $existingEntriesIds,
]);
};

return createFuncWithResultHelpers($executor)($params);
}

?>
42 changes: 21 additions & 21 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,31 +807,31 @@
if ($_POST['saveType'] == 'delignore') {
$entriesToDelete = Settings\Utils\Input\normalizeDeleteUserIgnoreEntries($_POST['del_ignore']);

if (!empty($entriesToDelete)) {
foreach ($entriesToDelete as $ThisID) {
if (!empty($_User['IgnoredUsers'][$ThisID])) {
$IgnoreSystem_Deleted[] = $ThisID;
}
}

if (!empty($IgnoreSystem_Deleted)) {
Settings\Utils\Queries\deleteUserIgnoreEntries([
'entryOwnerId' => $_User['id'],
'entriesIds' => $IgnoreSystem_Deleted,
]);
$tryDeleteUserIgnoreEntriesResult = Settings\Utils\Helpers\tryDeleteUserIgnoreEntries([
'currentUser' => &$_User,
'entriesIds' => $entriesToDelete,
]);

foreach ($IgnoreSystem_Deleted as $entryId) {
unset($_User['IgnoredUsers'][$entryId]);
}
if (!$tryDeleteUserIgnoreEntriesResult['isSuccess']) {
$WarningMsgs[] = Settings\Utils\ErrorMappers\mapTryDeleteUserIgnoreEntriesErrorToReadableMessage(
$tryDeleteUserIgnoreEntriesResult['error']
);
} else {
$existingEntriesToDelete = $tryDeleteUserIgnoreEntriesResult['payload']['entriesToDelete'];

$IgnoreSystem_Deleted_Count = count($IgnoreSystem_Deleted);
Settings\Utils\Queries\deleteUserIgnoreEntries([
'entryOwnerId' => $_User['id'],
'entriesIds' => $existingEntriesToDelete,
]);

$InfoMsgs[] = sprintf($_Lang['Ignore_DeletedXUsers'], $IgnoreSystem_Deleted_Count);
} else {
$WarningMsgs[] = $_Lang['Ignore_NothingDeleted'];
foreach ($existingEntriesToDelete as $entryId) {
unset($_User['IgnoredUsers'][$entryId]);
}
} else {
$WarningMsgs[] = $_Lang['Ignore_NothingSelected'];

$InfoMsgs[] = sprintf(
$_Lang['Ignore_DeletedXUsers'],
count($existingEntriesToDelete)
);
}
}
else if(!empty($_POST['ignore_username']) OR !empty($_GET['ignoreadd']))
Expand Down

0 comments on commit 462074f

Please sign in to comment.