Skip to content

Commit

Permalink
Overall improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Jan 2, 2020
1 parent bd6ad29 commit 98b9090
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 92 deletions.
2 changes: 1 addition & 1 deletion app/Platform/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// Define The Application Version
//--------------------------------------------------------------------------

define('VERSION', '1.1.5');
define('VERSION', '1.1.6');

//--------------------------------------------------------------------------
// Set PHP Error Reporting Options
Expand Down
4 changes: 1 addition & 3 deletions app/Platform/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ protected function renderHttpException(HttpException $e, Request $request)
*/
protected function createErrorResponse(HttpException $e, Request $request)
{
$status = $e->getStatusCode();

$e = FlattenException::create($e, $status);
$e = FlattenException::create($e, $status = $e->getStatusCode());

if ($this->isAjaxRequest($request)) {
return Response::json($e->toArray(), $status, $e->getHeaders());
Expand Down
133 changes: 133 additions & 0 deletions modules/Content/Controllers/Admin/Editor/Tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Modules\Content\Controllers\Admin\Editor;

use Nova\Database\ORM\ModelNotFoundException;
use Nova\Http\Request;
use Nova\Routing\Controller as BaseController;
use Nova\Support\Facades\Response;

use Modules\Content\Models\Post;
use Modules\Content\Models\Taxonomy;
use Modules\Content\Models\Term;


class Tags extends BaseController
{

public function lists(Request $request, $postId)
{
try {
$post = Post::with('taxonomies')->findOrFail($postId);
}
catch (ModelNotFoundException $e) {
return Response::json(array('error' => __d('content', 'Record not found: #{0}', $id)), 400);
}

$taxonomies = $post->taxonomies->where('taxonomy', 'post_tag');

$results = $taxonomies->map(function ($taxonomy)
{
return array(
'id' => $taxonomy->id,
'name' => $taxonomy->name
);

})->toArray();

return Response::json($results, 200);
}

public function attach(Request $request, $postId)
{
try {
$post = Post::with('taxonomies')->findOrFail($postId);
}
catch (ModelNotFoundException $e) {
return Response::json(array('error' => __d('content', 'Record not found: #{0}', $postId)), 400);
}

$taxonomies = $post->taxonomies->where('taxonomy', 'post_tag');

if (empty($value = $request->input('tags'))) {
return Response::json(array('error' => __d('content', 'The Tags value is required')), 400);
}

$names = array_filter(array_map(function ($name)
{
return trim(preg_replace('/[\s]+/mu', ' ', $name));

}, explode(',', $value)));

$results = array_filter(array_map(function ($name) use ($post, $taxonomies)
{
$taxonomy = $taxonomies->where('name', $name)->first();

if (is_null($taxonomy)) {
$taxonomy = $this->resolveTaxonomyByName($name);

$post->taxonomies()->attach($taxonomy);

$taxonomy->updateCount();

return array(
'id' => $taxonomy->id,
'name' => $taxonomy->name
);
}

}, $names));

return Response::json($results, 200);
}

protected function resolveTaxonomyByName($name)
{
return Taxonomy::with('term')->where('taxonomy', 'post_tag')->whereHas('term', function ($query) use ($name)
{
$query->where('name', $name);

})->firstOr(function () use ($name)
{
$slug = Term::uniqueSlug($name, 'post_tag');

$term = Term::create(array(
'name' => $name,
'slug' => $slug,
));

$taxonomy = Taxonomy::create(array(
'term_id' => $term->id,
'taxonomy' => 'post_tag',
'description' => '',
));

$taxonomy->load('term');

return $taxonomy;
});
}

public function detach(Request $request, $postId, $tagId)
{
try {
$post = Post::with('taxonomies')->findOrFail($postId);
}
catch (ModelNotFoundException $e) {
return Response::json(array('error' => __d('content', 'Record not found: #{0}', $id)), 400);
}

$taxonomy = $post->taxonomies->where('taxonomy', 'post_tag')->find($tagId);

if (is_null($taxonomy)) {
return Response::json(array('error' => __d('content', 'Taxonomy not found: #{0}', $tagId)), 400);
}

$post->taxonomies()->detach($tagId);

// Update the count field in the taxonomy.
$taxonomy->updateCount();

return Response::json(array('success' => true), 200);
}
}
85 changes: 0 additions & 85 deletions modules/Content/Controllers/Admin/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,91 +538,6 @@ public function revisions($id)
->shares('title', __d('content', 'Revisions of the {0} : {1}', $name, $post->title));
}

public function addTags(Request $request, $id)
{
try {
$post = Post::with('taxonomies')->findOrFail($id);
}
catch (ModelNotFoundException $e) {
return Response::json(array('error' => __d('content', 'Record not found: #{0}', $id)), 400);
}

$taxonomies = $post->taxonomies->where('taxonomy', 'post_tag');

if (empty($value = $request->input('tags'))) {
return Response::json(array('error' => __d('content', 'The Tags value is required')), 400);
}

$names = array_map(function ($tag)
{
return trim(preg_replace('/[\s]+/mu', ' ', $tag));

}, explode(',', $value));

$results = array();

foreach ($names as $names) {
if (! is_null($taxonomy = $taxonomies->where('name', $name)->first())) {
continue;
}

$taxonomy = Taxonomy::with('term')->where('taxonomy', 'post_tag')->whereHas('term', function ($query) use ($name)
{
$query->where('name', $name);

})->firstOr(function () use ($name)
{
$slug = Term::uniqueSlug($name, 'post_tag');

$term = Term::create(array(
'name' => $name,
'slug' => $slug,
));

$taxonomy = Taxonomy::create(array(
'term_id' => $term->id,
'taxonomy' => 'post_tag',
'description' => '',
));

$taxonomy->load('term');

return $taxonomy;
});

$post->taxonomies()->attach($taxonomy);

$taxonomy->updateCount();

array_push($results, array(
'id' => $taxonomy->id,
'name' => $taxonomy->name
));
}

return Response::json($results, 200);
}

public function detachTag(Request $request, $id, $tagId)
{
try {
$post = Post::with('taxonomies')->findOrFail($id);
}
catch (ModelNotFoundException $e) {
return Response::json(array('error' => 'Not Found'), 400);
}

$post->taxonomies()->detach($tagId);

// Update the count field in the associated taxonomies.
$post->taxonomies->each(function ($taxonomy)
{
$taxonomy->updateCount();
});

return Response::json(array('success' => true), 200);
}

protected function generateCategories(array $categories = array(), $taxonomies = null, $level = 0)
{
$result = '';
Expand Down
8 changes: 6 additions & 2 deletions modules/Content/Routes/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@

Route::get( 'content/{id}/revisions', 'Posts@revisions');

Route::post('content/{id}/tags', 'Posts@addTags');
// The Post editor's Tags management
Route::group(array('namespace' => 'Editor'), function ()
{
Route::post('content/{id}/tags', 'Tags@attach');

Route::post('content/{id}/tags/{tagId}/detach', 'Posts@detachTag')->where('tagId', '(\d+)');
Route::post('content/{id}/tags/{tagId}/detach', 'Tags@detach')->where('tagId', '(\d+)');
});

// The Posts listing.
Route::get('content/{type}', 'Posts@index')->where('type', Posts::routePattern(true));
Expand Down
2 changes: 1 addition & 1 deletion modules/Content/Views/Admin/Posts/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function contentEditorInsertMedia(url, type, upload) {

<div class="box box-widget">
<div class="box-header">
<h3 class="box-title"><?= __d('content', 'Revisions : {0}', $post->revision->count()); ?></h3>
<h3 class="box-title"><?= __d('content', 'Revisions : {0}', $revisions->count()); ?></h3>
<div class="box-tools">
<a href="<?= site_url('admin/content/' .$post->id .'/revisions'); ?>" class="btn btn-primary btn-sm pull-right" role="button"><i class="fa fa-list"></i> <?= __d('content', 'View all'); ?></a>
</div>
Expand Down

0 comments on commit 98b9090

Please sign in to comment.