2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
use DateTimeInterface;
|
|
|
|
use RuntimeException;
|
|
|
|
use Index\DateTime;
|
|
|
|
use Misuzu\Changelog\Changelog;
|
2022-09-13 13:14:49 +00:00
|
|
|
use Misuzu\Users\User;
|
|
|
|
use Misuzu\Users\UserNotFoundException;
|
|
|
|
|
|
|
|
if(!User::hasCurrent() || !perms_check_user(MSZ_PERMS_CHANGELOG, User::getCurrent()->getId(), MSZ_PERM_CHANGELOG_MANAGE_CHANGES)) {
|
|
|
|
echo render_error(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
$changeActions = [];
|
|
|
|
foreach(Changelog::ACTIONS as $action)
|
|
|
|
$changeActions[$action] = Changelog::actionText($action);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
$changelog = $msz->getChangelog();
|
|
|
|
$changeId = (string)filter_input(INPUT_GET, 'c', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$loadChangeInfo = fn() => $changelog->getChangeById($changeId, withTags: true);
|
|
|
|
$changeTags = $changelog->getAllTags();
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if(empty($changeId))
|
|
|
|
$isNew = true;
|
|
|
|
else
|
2022-09-13 13:14:49 +00:00
|
|
|
try {
|
2023-07-15 02:05:49 +00:00
|
|
|
$isNew = false;
|
|
|
|
$changeInfo = $loadChangeInfo();
|
|
|
|
} catch(RuntimeException $ex) {
|
|
|
|
echo render_error(404);
|
2022-09-13 13:14:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
|
|
|
|
if(CSRF::validateRequest()) {
|
|
|
|
$changelog->deleteChange($changeInfo);
|
2023-07-17 17:43:17 +00:00
|
|
|
$msz->createAuditLog('CHANGELOG_ENTRY_DELETE', [$changeInfo->getId()]);
|
2023-07-15 02:05:49 +00:00
|
|
|
url_redirect('manage-changelog-changes');
|
|
|
|
} else render_error(403);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make errors not echos lol
|
|
|
|
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
|
|
|
|
$action = trim((string)filter_input(INPUT_POST, 'cl_action'));
|
|
|
|
$summary = trim((string)filter_input(INPUT_POST, 'cl_summary'));
|
|
|
|
$body = trim((string)filter_input(INPUT_POST, 'cl_body'));
|
|
|
|
$userId = (int)filter_input(INPUT_POST, 'cl_user', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$createdAt = trim((string)filter_input(INPUT_POST, 'cl_created'));
|
|
|
|
$tags = filter_input(INPUT_POST, 'cl_tags', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if($userId < 1) $userId = null;
|
|
|
|
else $userId = (string)$userId;
|
|
|
|
|
|
|
|
if(empty($createdAt))
|
|
|
|
$createdAt = null;
|
|
|
|
else {
|
|
|
|
$createdAt = DateTime::createFromFormat(DateTimeInterface::ATOM, $createdAt . ':00Z');
|
|
|
|
if($createdAt->getUnixTimeSeconds() < 0)
|
|
|
|
$createdAt = null;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if($isNew) {
|
|
|
|
$changeInfo = $changelog->createChange($action, $summary, $body, $userId, $createdAt);
|
|
|
|
} else {
|
|
|
|
if($action === $changeInfo->getAction())
|
|
|
|
$action = null;
|
|
|
|
if($summary === $changeInfo->getSummary())
|
|
|
|
$summary = null;
|
|
|
|
if($body === $changeInfo->getBody())
|
|
|
|
$body = null;
|
|
|
|
if($createdAt !== null && $createdAt->equals($changeInfo->getCreatedAt()))
|
|
|
|
$createdAt = null;
|
|
|
|
$updateUserInfo = $userId !== $changeInfo->getUserId();
|
2023-07-05 01:33:12 +00:00
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if($action !== null || $summary !== null || $body !== null || $createdAt !== null || $updateUserInfo)
|
|
|
|
$changelog->updateChange($changeInfo, $action, $summary, $body, $updateUserInfo, $userId, $createdAt);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 02:14:39 +00:00
|
|
|
if(!empty($tags)) {
|
|
|
|
$tCurrent = $changeInfo->getTagIds();
|
|
|
|
$tApply = $tags;
|
|
|
|
$tRemove = [];
|
|
|
|
|
|
|
|
foreach($tCurrent as $tag)
|
|
|
|
if(!in_array($tag, $tApply)) {
|
|
|
|
$tRemove[] = $tag;
|
|
|
|
$changelog->removeTagFromChange($changeInfo, $tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tCurrent = array_diff($tCurrent, $tRemove);
|
|
|
|
|
|
|
|
foreach($tApply as $tag)
|
|
|
|
if(!in_array($tag, $tCurrent)) {
|
|
|
|
$changelog->addTagToChange($changeInfo, $tag);
|
|
|
|
$tCurrent[] = $tag;
|
|
|
|
}
|
|
|
|
}
|
2023-07-15 02:05:49 +00:00
|
|
|
|
2023-07-17 17:43:17 +00:00
|
|
|
$msz->createAuditLog(
|
|
|
|
$isNew ? 'CHANGELOG_ENTRY_CREATE' : 'CHANGELOG_ENTRY_EDIT',
|
2023-07-15 02:05:49 +00:00
|
|
|
[$changeInfo->getId()]
|
|
|
|
);
|
|
|
|
|
|
|
|
if($isNew) {
|
|
|
|
url_redirect('manage-changelog-change', ['change' => $changeInfo->getId()]);
|
2022-09-13 13:14:49 +00:00
|
|
|
return;
|
2023-07-15 02:05:49 +00:00
|
|
|
} else $changeInfo = $loadChangeInfo();
|
|
|
|
break;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Template::render('manage.changelog.change', [
|
2023-07-15 02:05:49 +00:00
|
|
|
'change_new' => $isNew,
|
|
|
|
'change_info' => $changeInfo ?? null,
|
|
|
|
'change_tags' => $changeTags,
|
|
|
|
'change_actions' => $changeActions,
|
2022-09-13 13:14:49 +00:00
|
|
|
]);
|