Skip to content

Commit

Permalink
Improve the Content module
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Jan 4, 2020
1 parent 124b4ef commit 599f5a4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
82 changes: 44 additions & 38 deletions modules/Content/Controllers/Admin/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,18 @@ public function update(Request $request, $id)
return Response::json(array('redirectTo' => 'refresh'), 400);
}

$postType = PostType::make($post->type);
$postType = PostType::make($type = $post->type);

$creating = (bool) Arr::get($input, 'creating', 0);

// Fire the starting event.
Event::dispatch('content.post.updating', array($post, $creating));

//
$type = $post->type;
// Create a new Revision from the current Post instance.

if (! $creating) {
$this->createRevision($post, $authUser);
}

$slug = Arr::get($input, 'slug') ?: Post::uniqueName($input['title'], $post->id);

Expand Down Expand Up @@ -314,20 +317,20 @@ public function update(Request $request, $id)

// Handle the MetaData.
$post->saveMeta(array(
'thumbnail_id' => (int) $request->input('thumbnail'),
'thumbnail_id' => (int) Arr::get($input, 'thumbnail'),

'edit_last' => $authUser->id,
));

if ($type == 'block') {
$post->saveMeta(array(
'block_show_title' => (int) $request->input('block-show-title'),
'block_show_title' => (int) Arr::get($input, 'block-show-title'),

'block_visibility_mode' => $request->input('block-show-mode'),
'block_visibility_path' => $request->input('block-show-path'),
'block_visibility_mode' => Arr::get($input, 'block-show-mode'),
'block_visibility_path' => Arr::get($input, 'block-show-path'),

'block_visibility_filter' => $request->input('block-show-filter', 'any'),
'block_widget_position' => $request->input('block-position'),
'block_visibility_filter' => Arr::get($input, 'block-show-filter', 'any'),
'block_widget_position' => Arr::get($input, 'block-position'),
));
}

Expand Down Expand Up @@ -370,20 +373,45 @@ public function update(Request $request, $id)
});
}

// Create a new revision from the current Post instance.
$count = 0;
// Fire the finishing event.
Event::dispatch('content.post.updated', array($post, $creating));

// Update the edit lock.
$post->saveMeta('edit_lock', null);

// Invalidate the content caches.
Cache::section('content')->flush();

//
Session::flash(
'success', __d('content', 'The {0} <b>#{1}</b> was successfully saved.', $postType->label('name'), $post->id)
);

return Response::json(array(
'redirectTo' => site_url('admin/content/' .Str::plural($type))

), 200);
}

protected function createRevision(Post $post, User $user)
{
$version = 0;

$names = $post->revisions()->lists('name');

foreach ($names as $name) {
if (preg_match('#^(?:\d+)-revision-v(\d+)$#', $name, $matches) === 1) {
$count = max($count, (int) $matches[1]);
$version = max($version, (int) $matches[1]);
}
}

$count++;
$version++;

// Save the current version also on the Post metadata.
$post->saveMeta('version', $version);

$slug = sprintf('%d-revision-v%d', $post->id, $count);
// Compute the slug of current Revision.
$slug = sprintf('%d-revision-v%d', $post->id, $version);

$revision = Post::create(array(
'content' => $post->content,
Expand All @@ -397,33 +425,11 @@ public function update(Request $request, $id)
'menu_order' => $post->menu_order,
'type' => 'revision',
'mime_type' => $post->mime_type,
'author_id' => $authUser->id,
'author_id' => $user->id,
'comment_status' => 'closed',
));

$revision->saveMeta('version', $count);

//
$post->saveMeta('version', $count);

// Fire the finishing event.
Event::dispatch('content.post.updated', array($post, $creating));

// Update the edit lock.
$post->saveMeta('edit_lock', null);

// Invalidate the content caches.
Cache::section('content')->flush();

//
Session::flash(
'success', __d('content', 'The {0} <b>#{1}</b> was successfully saved.', $postType->label('name'), $post->id)
);

return Response::json(array(
'redirectTo' => site_url('admin/content/' .Str::plural($type))

), 200);
$revision->saveMeta('version', $version);
}

public function destroy($id)
Expand Down
4 changes: 2 additions & 2 deletions modules/Content/Platform/ContentLabelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ protected function resolveLabels($type)

protected function getCurrentLocale()
{
$languageManager = $this->container['language'];
$language = $this->container['language'];

return $languageManager->getLocale();
return $language->getLocale();
}
}
2 changes: 1 addition & 1 deletion modules/Content/Platform/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function label($name, $default = null)
$locale = $this->manager->getCurrentLocale();

if (! isset($this->labels[$locale])) {
$this->labels[$locale] = static::labels();
$this->labels[$locale] = $this->labels();
}

$key = sprintf('%s.%s', $locale, Str::camel($name));
Expand Down
16 changes: 9 additions & 7 deletions modules/Content/Support/ActionHookDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ protected function sortListeners($hook)
{
$this->sorted[$hook] = array();

if (isset($this->listeners[$hook])) {
uksort($this->listeners[$hook], function ($param1, $param2)
{
return strnatcmp($param1, $param2);
});

$this->sorted[$hook] = $this->listeners[$hook];
if (! isset($this->listeners[$hook])) {
return;
}

uksort($this->listeners[$hook], function ($param1, $param2)
{
return strnatcmp($param1, $param2);
});

$this->sorted[$hook] = $this->listeners[$hook];
}

/**
Expand Down

0 comments on commit 599f5a4

Please sign in to comment.