Skip to content

Commit

Permalink
Fix: #5068 - timestamps for new/edit news articles
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Dec 23, 2024
1 parent fceae36 commit 8359930
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
34 changes: 14 additions & 20 deletions app/Module/FamilyTreeNewsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ public function description(): string
/**
* Generate the HTML content of this block.
*
* @param Tree $tree
* @param int $block_id
* @param string $context
* @param array<string,string> $config
*
* @return string
*/
public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
{
Expand Down Expand Up @@ -173,38 +168,37 @@ public function postEditNewsAction(ServerRequestInterface $request): ResponseInt
$news_id = Validator::queryParams($request)->integer('news_id', 0);
$subject = Validator::parsedBody($request)->string('subject');
$body = Validator::parsedBody($request)->string('body');
$now = Registry::timestampFactory()->now();

$subject = $this->html_service->sanitize($subject);
$body = $this->html_service->sanitize($body);

if ($news_id !== 0) {
$use_current_timestamp = Validator::parsedBody($request)->boolean('use-current-timestamp', false);

if ($use_current_timestamp) {
$updated = $now;
} else {
$timestamp = Validator::parsedBody($request)->string('timestamp');
$timezone = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'));
$utc = new DateTimeZone('UTC');
$updated = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s', $timestamp, $timezone)
->setTimezone($utc);
}
$use_current_timestamp = Validator::parsedBody($request)->boolean('use-current-timestamp', false);

if ($use_current_timestamp) {
$updated = Registry::timestampFactory()->now();
} else {
$timestamp = Validator::parsedBody($request)->string('timestamp');
$timezone = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'));
$utc = new DateTimeZone('UTC');
$updated = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s', $timestamp, $timezone)
->setTimezone($utc);
}

if ($news_id !== 0) {
DB::table('news')
->where('news_id', '=', $news_id)
->where('gedcom_id', '=', $tree->id()) // Check this is our own tree - validates news_id
->update([
'body' => $body,
'subject' => $subject,
'updated' => $updated,
'updated' => $updated->format('Y-m-d H:i:s'),
]);
} else {
DB::table('news')->insert([
'body' => $body,
'subject' => $subject,
'gedcom_id' => $tree->id(),
'updated' => $now,
'updated' => $updated->format('Y-m-d H:i:s'),
]);
}

Expand Down
32 changes: 13 additions & 19 deletions app/Module/UserJournalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ public function description(): string
/**
* Generate the HTML content of this block.
*
* @param Tree $tree
* @param int $block_id
* @param string $context
* @param array<string,string> $config
*
* @return string
*/
public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
{
Expand Down Expand Up @@ -173,24 +168,23 @@ public function postEditJournalAction(ServerRequestInterface $request): Response
$news_id = Validator::queryParams($request)->integer('news_id', 0);
$subject = Validator::parsedBody($request)->string('subject');
$body = Validator::parsedBody($request)->string('body');
$now = Registry::timestampFactory()->now();

$subject = $this->html_service->sanitize($subject);
$body = $this->html_service->sanitize($body);

if ($news_id !== 0) {
$use_current_timestamp = Validator::parsedBody($request)->boolean('use-current-timestamp', false);

if ($use_current_timestamp) {
$updated = $now;
} else {
$timestamp = Validator::parsedBody($request)->string('timestamp');
$timezone = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'));
$utc = new DateTimeZone('UTC');
$updated = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s', $timestamp, $timezone)
->setTimezone($utc);
}
$use_current_timestamp = Validator::parsedBody($request)->boolean('use-current-timestamp', false);

if ($use_current_timestamp) {
$updated = Registry::timestampFactory()->now();
} else {
$timestamp = Validator::parsedBody($request)->string('timestamp');
$timezone = new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'));
$utc = new DateTimeZone('UTC');
$updated = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s', $timestamp, $timezone)
->setTimezone($utc);
}

if ($news_id !== 0) {
DB::table('news')
->where('news_id', '=', $news_id)
->where('user_id', '=', Auth::id()) // Check this is our own page - validates news_id
Expand All @@ -204,7 +198,7 @@ public function postEditJournalAction(ServerRequestInterface $request): Response
'body' => $body,
'subject' => $subject,
'user_id' => Auth::id(),
'updated' => $now,
'updated' => $updated->format('Y-m-d H:i:s'),
]);
}

Expand Down

0 comments on commit 8359930

Please sign in to comment.