diff --git a/app/Platform/Bootstrap.php b/app/Platform/Bootstrap.php index b2fb589f..ac144d60 100644 --- a/app/Platform/Bootstrap.php +++ b/app/Platform/Bootstrap.php @@ -14,7 +14,7 @@ // Define The Application Version //-------------------------------------------------------------------------- -define('VERSION', '1.1.5'); +define('VERSION', '1.1.6'); //-------------------------------------------------------------------------- // Set PHP Error Reporting Options diff --git a/app/Platform/Exceptions/Handler.php b/app/Platform/Exceptions/Handler.php index 27fa0cde..fa33ed16 100644 --- a/app/Platform/Exceptions/Handler.php +++ b/app/Platform/Exceptions/Handler.php @@ -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()); diff --git a/modules/Content/Controllers/Admin/Editor/Tags.php b/modules/Content/Controllers/Admin/Editor/Tags.php new file mode 100644 index 00000000..eebcba3d --- /dev/null +++ b/modules/Content/Controllers/Admin/Editor/Tags.php @@ -0,0 +1,133 @@ +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); + } +} diff --git a/modules/Content/Controllers/Admin/Posts.php b/modules/Content/Controllers/Admin/Posts.php index da139f4f..0b77077d 100644 --- a/modules/Content/Controllers/Admin/Posts.php +++ b/modules/Content/Controllers/Admin/Posts.php @@ -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 = ''; diff --git a/modules/Content/Routes/Web.php b/modules/Content/Routes/Web.php index 8d34f2d7..6f05c1ad 100644 --- a/modules/Content/Routes/Web.php +++ b/modules/Content/Routes/Web.php @@ -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)); diff --git a/modules/Content/Views/Admin/Posts/Edit.php b/modules/Content/Views/Admin/Posts/Edit.php index 0edd00ac..a8f2f989 100644 --- a/modules/Content/Views/Admin/Posts/Edit.php +++ b/modules/Content/Views/Admin/Posts/Edit.php @@ -136,7 +136,7 @@ function contentEditorInsertMedia(url, type, upload) {
-

revision->count()); ?>

+

count()); ?>