2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
use DateTimeInterface;
|
|
|
|
use RuntimeException;
|
|
|
|
use Misuzu\Changelog\Changelog;
|
2024-08-04 21:37:12 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use Index\{XArray,XDateTime};
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2024-12-02 02:28:08 +00:00
|
|
|
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
|
|
|
|
die('Script must be called through the Misuzu route dispatcher.');
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if(!$msz->authInfo->getPerms('global')->check(Perm::G_CL_CHANGES_MANAGE))
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(403);
|
2022-09-13 13:14:49 +00:00
|
|
|
|
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
|
|
|
$changeId = (string)filter_input(INPUT_GET, 'c', FILTER_SANITIZE_NUMBER_INT);
|
2023-08-05 13:50:15 +00:00
|
|
|
$changeInfo = null;
|
|
|
|
$changeTagIds = [];
|
2024-11-30 04:09:29 +00:00
|
|
|
$tagInfos = $msz->changelog->getTags();
|
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;
|
2024-11-30 04:09:29 +00:00
|
|
|
$changeInfo = $msz->changelog->getChange($changeId);
|
|
|
|
$changeTagIds = XArray::select($msz->changelog->getTags(changeInfo: $changeInfo), fn($tagInfo) => $tagInfo->id);
|
2023-07-15 02:05:49 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(404);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['delete'])) {
|
2023-08-31 15:59:53 +00:00
|
|
|
if(!CSRF::validateRequest())
|
|
|
|
Template::throwError(403);
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->changelog->deleteChange($changeInfo);
|
|
|
|
$msz->createAuditLog('CHANGELOG_ENTRY_DELETE', [$changeInfo->id]);
|
|
|
|
Tools::redirect($msz->urls->format('manage-changelog-changes'));
|
2023-07-15 02:05:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make errors not echos lol
|
2024-12-02 21:33:15 +00:00
|
|
|
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
|
2023-07-15 02:05:49 +00:00
|
|
|
$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 {
|
2024-08-04 21:37:12 +00:00
|
|
|
$createdAt = CarbonImmutable::createFromFormat(DateTimeInterface::ATOM, $createdAt . ':00Z');
|
|
|
|
if((int)$createdAt->format('U') < 0)
|
2023-07-15 02:05:49 +00:00
|
|
|
$createdAt = null;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
if($isNew) {
|
2024-11-30 04:09:29 +00:00
|
|
|
$changeInfo = $msz->changelog->createChange($action, $summary, $body, $userId, $createdAt);
|
2023-07-15 02:05:49 +00:00
|
|
|
} else {
|
2024-11-30 04:09:29 +00:00
|
|
|
if($action === $changeInfo->action)
|
2023-07-15 02:05:49 +00:00
|
|
|
$action = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($summary === $changeInfo->summary)
|
2023-07-15 02:05:49 +00:00
|
|
|
$summary = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($body === $changeInfo->body)
|
2023-07-15 02:05:49 +00:00
|
|
|
$body = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
if($createdAt !== null && XDateTime::compare($createdAt, $changeInfo->createdAt) === 0)
|
2023-07-15 02:05:49 +00:00
|
|
|
$createdAt = null;
|
2024-11-30 04:09:29 +00:00
|
|
|
$updateUserInfo = $userId !== $changeInfo->userId;
|
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)
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->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)) {
|
2023-08-05 13:50:15 +00:00
|
|
|
$tCurrent = $changeTagIds;
|
2023-07-15 02:14:39 +00:00
|
|
|
$tApply = $tags;
|
|
|
|
$tRemove = [];
|
|
|
|
|
|
|
|
foreach($tCurrent as $tag)
|
|
|
|
if(!in_array($tag, $tApply)) {
|
|
|
|
$tRemove[] = $tag;
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->changelog->removeTagFromChange($changeInfo, $tag);
|
2023-07-15 02:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$tCurrent = array_diff($tCurrent, $tRemove);
|
|
|
|
|
|
|
|
foreach($tApply as $tag)
|
|
|
|
if(!in_array($tag, $tCurrent)) {
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->changelog->addTagToChange($changeInfo, $tag);
|
2023-07-15 02:14:39 +00:00
|
|
|
$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',
|
2024-11-30 04:09:29 +00:00
|
|
|
[$changeInfo->id]
|
2023-07-15 02:05:49 +00:00
|
|
|
);
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
Tools::redirect($msz->urls->format('manage-changelog-change', ['change' => $changeInfo->id]));
|
2023-08-05 13:50:15 +00:00
|
|
|
return;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Template::render('manage.changelog.change', [
|
2023-07-15 02:05:49 +00:00
|
|
|
'change_new' => $isNew,
|
2023-08-05 13:50:15 +00:00
|
|
|
'change_info' => $changeInfo,
|
|
|
|
'change_info_tags' => $changeTagIds,
|
|
|
|
'change_tags' => $tagInfos,
|
2023-07-15 02:05:49 +00:00
|
|
|
'change_actions' => $changeActions,
|
2024-11-30 04:09:29 +00:00
|
|
|
'change_author_id' => $msz->authInfo->userId,
|
2022-09-13 13:14:49 +00:00
|
|
|
]);
|