Skip to content

Commit 0bfd136

Browse files
Creating Content changes
1 parent 7d033f6 commit 0bfd136

File tree

9 files changed

+135
-6
lines changed

9 files changed

+135
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\Content;
7+
use App\Models\Post;
8+
use App\Models\PostContent;
9+
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Http\Request;
11+
12+
class ContentController extends Controller
13+
{
14+
public function deleteCategoryContent(Content $content)
15+
{
16+
$content->delete();
17+
return redirect()->back()->withSuccess(__('This language data has been deleted.'));
18+
}
19+
20+
public function deletePostContent(PostContent $postContent)
21+
{
22+
$result = DB::select('select post_id from contents_of_posts where content_id = ?', [$postContent->id]);
23+
if (!empty($result)){
24+
$result = json_decode(json_encode($result), true);
25+
$post = Post::find($result[0]['post_id']);
26+
27+
if (!empty($post)){
28+
if ($post->author_id != auth()->user()->id && !auth()->user()->hasOneOfRoles(['admin', 'mod'])){
29+
abort(403);
30+
}
31+
}
32+
}
33+
34+
$postContent->delete();
35+
return redirect()->back()->withSuccess('This language data has been deleted.');
36+
}
37+
}

public/js/admin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/admin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ bsCustomFileInput.init();
44
require('./scripts/tinymce');
55
require('./scripts/sidebar');
66
require('./scripts/proper-url');
7+
require('./scripts/hidden-form');
78
require('./scripts/post-date');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const hiddenForm = document.querySelector('.hidden-form');
2+
const hiddenFormInfo = document.querySelector('.hidden-form-info');
3+
const hiddenFormInfoButton = document.querySelector('.hidden-form-info button');
4+
5+
if (hiddenForm && hiddenFormInfo && hiddenFormInfoButton){
6+
hiddenFormInfoButton.addEventListener('click', () => {
7+
hiddenForm.classList.remove('d-none');
8+
hiddenFormInfo.classList.add('d-none');
9+
});
10+
}

resources/lang/pl.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,9 @@
127127
"Other posts that may interest you": "Inne posty ktore mogą cię zainteresować",
128128
"Toggle navigation": "Przełącz nawigację",
129129
"This url already exists.": "Taki link już istnieje.",
130-
"This value is not language.": "Ta wartość nie jest językiem."
130+
"This value is not language.": "Ta wartość nie jest językiem.",
131+
"Delete only data for this langauge": "Usuń dane tylko dla tego języka",
132+
"There is no data defined for this language.": "Nie ma zdefiniowanych danych dla tego języka.",
133+
"Add data for this language": "Dodaj dane dla tego języka",
134+
"This language data has been deleted.": "Dane tego języka zostały usuniętę."
131135
}

resources/views/admin/categories/save.blade.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929
@empty($category)
3030
<form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data">
3131
@else
32-
<form action="{{ route('admin.categories.update', $category->id) }}" method="POST">
32+
@empty($content)
33+
<div class="text-center hidden-form-info">
34+
<p>{{ __('There is no data defined for this language.') }}</p>
35+
<button type="button" class="btn btn-lg btn-primary fas fa-2x fa-plus" data-toggle="tooltip" title="{{ __('Add data for this language') }}"></button>
36+
</div>
37+
@endempty
38+
39+
<form action="{{ route('admin.categories.update', $category->id) }}" method="POST" class="{{ empty($content) ? 'd-none hidden-form' : '' }}">
3340
@method('PUT')
3441
@endempty
3542
@csrf
@@ -133,11 +140,19 @@
133140
</div>
134141
<div class="card-body">
135142
<p class="text-muted">{{ __('Be careful when using this operation.') }}</p>
136-
<form action="{{ route('admin.categories.destroy', $category->id) }}" method="POST">
143+
<form action="{{ route('admin.categories.destroy', $category->id) }}" method="POST" class="d-inline pr-2">
137144
@csrf
138145
@method('DELETE')
139146
<button type="submit" class="btn btn-danger">{{ __('Delete') }}</button>
140147
</form>
148+
149+
@if(!empty($content))
150+
<form action="{{ route('admin.content.destroy', $content->id) }}" method="POST" class="d-inline">
151+
@csrf
152+
@method('DELETE')
153+
<button type="submit" class="btn btn-danger">{{ __('Delete only data for this langauge') }}</button>
154+
</form>
155+
@endif
141156
</div>
142157
</div>
143158
@endif

resources/views/admin/posts/save.blade.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929
@empty($post)
3030
<form action="{{ route('admin.posts.store') }}" method="POST" enctype="multipart/form-data">
3131
@else
32-
<form action="{{ route('admin.posts.update', $post->id) }}" method="POST">
32+
@empty($content)
33+
<div class="text-center hidden-form-info">
34+
<p>{{ __('There is no data defined for this language.') }}</p>
35+
<button type="button" class="btn btn-lg btn-primary fas fa-2x fa-plus" data-toggle="tooltip" title="{{ __('Add data for this language') }}"></button>
36+
</div>
37+
@endempty
38+
39+
<form action="{{ route('admin.posts.update', $post->id) }}" method="POST" class="{{ empty($content) ? 'd-none hidden-form' : '' }}">
3340
@method('PUT')
3441
@endempty
3542
@csrf
@@ -183,11 +190,19 @@
183190
</div>
184191
<div class="card-body">
185192
<p class="text-muted">{{ __('Be careful when using this operation.') }}</p>
186-
<form action="{{ route('admin.posts.destroy', $post->id) }}" method="POST">
193+
<form action="{{ route('admin.posts.destroy', $post->id) }}" method="POST" class="d-inline pr-2">
187194
@csrf
188195
@method('DELETE')
189196
<button type="submit" class="btn btn-danger">{{ __('Delete') }}</button>
190197
</form>
198+
199+
@if(!empty($content))
200+
<form action="{{ route('admin.post-content.destroy', $content->id) }}" method="POST" class="d-inline">
201+
@csrf
202+
@method('DELETE')
203+
<button type="submit" class="btn btn-danger">{{ __('Delete only data for this langauge') }}</button>
204+
</form>
205+
@endif
191206
</div>
192207
</div>
193208
@endif

routes/web-admin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
Route::delete('/posts/{post}/image', 'PostsController@destroyImage')->name('posts.image.destroy');
3030
Route::resource('posts', 'PostsController')->except('show');
3131

32+
Route::delete('/post-content/{postContent}', 'ContentController@deletePostContent')->name('post-content.destroy');
33+
3234
Route::middleware('role:admin')->group(function(){
35+
Route::delete('/content/{content}', 'ContentController@deleteCategoryContent')->name('content.destroy');
36+
3337
Route::put('/users/{user}/image', 'UsersController@updateImage')->name('users.image.update');
3438
Route::delete('/users/{user}/image', 'UsersController@destroyImage')->name('users.image.destroy');
3539
Route::put('/users/{user}/password', 'UsersPasswordController')->name('users.password');
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Tests\Feature\Admin;
4+
5+
use App\Models\PostContent;
6+
use App\Models\Content;
7+
use Tests\TestCase;
8+
9+
class ContentTest extends TestCase
10+
{
11+
private $admin;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->admin = $this->getAdmin();
18+
}
19+
20+
public function testDestroyCategoryContent()
21+
{
22+
$content = Content::all()->random();
23+
$response = $this->actingAs($this->admin)->delete(route('admin.content.destroy', $content->id));
24+
25+
$response->assertSessionHas('success');
26+
$response->assertStatus(302);
27+
28+
$emptyContent = Content::find($content->id);
29+
$this->assertEmpty($emptyContent);
30+
}
31+
32+
public function testDestroyPostContent()
33+
{
34+
$content = PostContent::all()->random();
35+
$response = $this->actingAs($this->admin)->delete(route('admin.post-content.destroy', $content->id));
36+
37+
$response->assertSessionHas('success');
38+
$response->assertStatus(302);
39+
40+
$emptyContent = PostContent::find($content->id);
41+
$this->assertEmpty($emptyContent);
42+
}
43+
}

0 commit comments

Comments
 (0)