Skip to content

Commit

Permalink
Creating Content changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislawfortonski committed Jul 11, 2021
1 parent 7d033f6 commit 0bfd136
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 6 deletions.
37 changes: 37 additions & 0 deletions app/Http/Controllers/Admin/ContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Content;
use App\Models\Post;
use App\Models\PostContent;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ContentController extends Controller
{
public function deleteCategoryContent(Content $content)
{
$content->delete();
return redirect()->back()->withSuccess(__('This language data has been deleted.'));
}

public function deletePostContent(PostContent $postContent)
{
$result = DB::select('select post_id from contents_of_posts where content_id = ?', [$postContent->id]);
if (!empty($result)){
$result = json_decode(json_encode($result), true);
$post = Post::find($result[0]['post_id']);

if (!empty($post)){
if ($post->author_id != auth()->user()->id && !auth()->user()->hasOneOfRoles(['admin', 'mod'])){
abort(403);
}
}
}

$postContent->delete();
return redirect()->back()->withSuccess('This language data has been deleted.');
}
}
2 changes: 1 addition & 1 deletion public/js/admin.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions resources/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ bsCustomFileInput.init();
require('./scripts/tinymce');
require('./scripts/sidebar');
require('./scripts/proper-url');
require('./scripts/hidden-form');
require('./scripts/post-date');
10 changes: 10 additions & 0 deletions resources/js/scripts/hidden-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const hiddenForm = document.querySelector('.hidden-form');
const hiddenFormInfo = document.querySelector('.hidden-form-info');
const hiddenFormInfoButton = document.querySelector('.hidden-form-info button');

if (hiddenForm && hiddenFormInfo && hiddenFormInfoButton){
hiddenFormInfoButton.addEventListener('click', () => {
hiddenForm.classList.remove('d-none');
hiddenFormInfo.classList.add('d-none');
});
}
6 changes: 5 additions & 1 deletion resources/lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,9 @@
"Other posts that may interest you": "Inne posty ktore mogą cię zainteresować",
"Toggle navigation": "Przełącz nawigację",
"This url already exists.": "Taki link już istnieje.",
"This value is not language.": "Ta wartość nie jest językiem."
"This value is not language.": "Ta wartość nie jest językiem.",
"Delete only data for this langauge": "Usuń dane tylko dla tego języka",
"There is no data defined for this language.": "Nie ma zdefiniowanych danych dla tego języka.",
"Add data for this language": "Dodaj dane dla tego języka",
"This language data has been deleted.": "Dane tego języka zostały usuniętę."
}
19 changes: 17 additions & 2 deletions resources/views/admin/categories/save.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
@empty($category)
<form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data">
@else
<form action="{{ route('admin.categories.update', $category->id) }}" method="POST">
@empty($content)
<div class="text-center hidden-form-info">
<p>{{ __('There is no data defined for this language.') }}</p>
<button type="button" class="btn btn-lg btn-primary fas fa-2x fa-plus" data-toggle="tooltip" title="{{ __('Add data for this language') }}"></button>
</div>
@endempty

<form action="{{ route('admin.categories.update', $category->id) }}" method="POST" class="{{ empty($content) ? 'd-none hidden-form' : '' }}">
@method('PUT')
@endempty
@csrf
Expand Down Expand Up @@ -133,11 +140,19 @@
</div>
<div class="card-body">
<p class="text-muted">{{ __('Be careful when using this operation.') }}</p>
<form action="{{ route('admin.categories.destroy', $category->id) }}" method="POST">
<form action="{{ route('admin.categories.destroy', $category->id) }}" method="POST" class="d-inline pr-2">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">{{ __('Delete') }}</button>
</form>

@if(!empty($content))
<form action="{{ route('admin.content.destroy', $content->id) }}" method="POST" class="d-inline">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">{{ __('Delete only data for this langauge') }}</button>
</form>
@endif
</div>
</div>
@endif
Expand Down
19 changes: 17 additions & 2 deletions resources/views/admin/posts/save.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
@empty($post)
<form action="{{ route('admin.posts.store') }}" method="POST" enctype="multipart/form-data">
@else
<form action="{{ route('admin.posts.update', $post->id) }}" method="POST">
@empty($content)
<div class="text-center hidden-form-info">
<p>{{ __('There is no data defined for this language.') }}</p>
<button type="button" class="btn btn-lg btn-primary fas fa-2x fa-plus" data-toggle="tooltip" title="{{ __('Add data for this language') }}"></button>
</div>
@endempty

<form action="{{ route('admin.posts.update', $post->id) }}" method="POST" class="{{ empty($content) ? 'd-none hidden-form' : '' }}">
@method('PUT')
@endempty
@csrf
Expand Down Expand Up @@ -183,11 +190,19 @@
</div>
<div class="card-body">
<p class="text-muted">{{ __('Be careful when using this operation.') }}</p>
<form action="{{ route('admin.posts.destroy', $post->id) }}" method="POST">
<form action="{{ route('admin.posts.destroy', $post->id) }}" method="POST" class="d-inline pr-2">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">{{ __('Delete') }}</button>
</form>

@if(!empty($content))
<form action="{{ route('admin.post-content.destroy', $content->id) }}" method="POST" class="d-inline">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">{{ __('Delete only data for this langauge') }}</button>
</form>
@endif
</div>
</div>
@endif
Expand Down
4 changes: 4 additions & 0 deletions routes/web-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
Route::delete('/posts/{post}/image', 'PostsController@destroyImage')->name('posts.image.destroy');
Route::resource('posts', 'PostsController')->except('show');

Route::delete('/post-content/{postContent}', 'ContentController@deletePostContent')->name('post-content.destroy');

Route::middleware('role:admin')->group(function(){
Route::delete('/content/{content}', 'ContentController@deleteCategoryContent')->name('content.destroy');

Route::put('/users/{user}/image', 'UsersController@updateImage')->name('users.image.update');
Route::delete('/users/{user}/image', 'UsersController@destroyImage')->name('users.image.destroy');
Route::put('/users/{user}/password', 'UsersPasswordController')->name('users.password');
Expand Down
43 changes: 43 additions & 0 deletions tests/Feature/Admin/ContentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests\Feature\Admin;

use App\Models\PostContent;
use App\Models\Content;
use Tests\TestCase;

class ContentTest extends TestCase
{
private $admin;

public function setUp(): void
{
parent::setUp();

$this->admin = $this->getAdmin();
}

public function testDestroyCategoryContent()
{
$content = Content::all()->random();
$response = $this->actingAs($this->admin)->delete(route('admin.content.destroy', $content->id));

$response->assertSessionHas('success');
$response->assertStatus(302);

$emptyContent = Content::find($content->id);
$this->assertEmpty($emptyContent);
}

public function testDestroyPostContent()
{
$content = PostContent::all()->random();
$response = $this->actingAs($this->admin)->delete(route('admin.post-content.destroy', $content->id));

$response->assertSessionHas('success');
$response->assertStatus(302);

$emptyContent = PostContent::find($content->id);
$this->assertEmpty($emptyContent);
}
}

0 comments on commit 0bfd136

Please sign in to comment.