Skip to content

Commit

Permalink
Propagate date change to descendants
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurperton committed Sep 3, 2022
1 parent 5bf6a08 commit cd776de
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ public function update(Request $request, $collection, $entry)
}

if ($entry->collection()->dated()) {
$entry->date($this->toCarbonInstanceForSaving($request->date));
$date = $this->formatDateForSaving($request->date);

$entry->date($date);

$entry->descendants()->each(fn ($entry) => $this->saveUpdatedEntry($entry->date($date)));
}

$entry->slug($this->resolveSlug($request));
Expand All @@ -227,18 +231,7 @@ public function update(Request $request, $collection, $entry)

$this->validateUniqueUri($entry, $tree ?? null, $parent ?? null);

if ($entry->revisionsEnabled() && $entry->published()) {
$entry
->makeWorkingCopy()
->user(User::current())
->save();
} else {
if (! $entry->revisionsEnabled() && User::current()->can('publish', $entry)) {
$entry->published($request->published);
}

$entry->updateLastModified(User::current())->save();
}
$this->saveUpdatedEntry($entry, $request->published);

return new EntryResource($entry->fresh());
}
Expand Down Expand Up @@ -460,6 +453,22 @@ protected function extractAssetsFromValues($values)
->values();
}

protected function saveUpdatedEntry($entry, $published = null)
{
if ($entry->revisionsEnabled() && $entry->published()) {
$entry
->makeWorkingCopy()
->user(User::current())
->save();
} else {
if (!is_null($published) && ! $entry->revisionsEnabled() && User::current()->can('publish', $entry)) {
$entry->published($published);
}

$entry->updateLastModified(User::current())->save();
}
}

protected function toCarbonInstanceForSaving($date): Carbon
{
// Since assume `Y-m-d ...` format, we can use `parse` here.
Expand Down

0 comments on commit cd776de

Please sign in to comment.