Skip to content

Commit

Permalink
Add an option to enable/disable une paae
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Aug 20, 2019
1 parent ca8606e commit ed57c8e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/Admin/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function store(Request $request)
{
$this->validate($request, $this->rules());

request_checkbox($request, 'is_enabled');

Page::create($request->all());

return redirect()->route('admin.pages.index')->with('success', 'Page created');
Expand Down Expand Up @@ -70,6 +72,8 @@ public function update(Request $request, Page $page)
{
$this->validate($request, $this->rules($page));

request_checkbox($request, 'is_enabled');

$page->update($request->all());

return redirect()->route('admin.pages.index')->with('success', 'Page updated');
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class PageController extends Controller
{
public function show(Page $page)
{
if (! $page->is_published && (auth()->guest() || ! auth()->user()->isAdmin())) {
abort(404);
}

return view('pages.show')->with('page', $page);
}
}
3 changes: 1 addition & 2 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{
Expand All @@ -16,7 +15,7 @@ public function index()

public function show(Post $post)
{
if (! $post->isPublished() && (Auth::guest() || ! auth()->user()->isAdmin())) {
if (! $post->isPublished() && (auth()->guest() || ! auth()->user()->isAdmin())) {
abort(404);
}

Expand Down
11 changes: 10 additions & 1 deletion app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class Page extends Model
* @var array
*/
protected $fillable = [
'title', 'description', 'slug', 'content',
'title', 'description', 'slug', 'content', 'is_enabled'
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'is_enabled' => 'boolean',
];
}
8 changes: 8 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

if (! function_exists('add_active')) {
Expand All @@ -15,3 +16,10 @@ function image_url(string $name)
return url(Storage::url('img/'.$name));
}
}

if (! function_exists('request_checkbox')) {
function request_checkbox(Request $request, string $name)
{
$request->offsetSet($name, $request->has($name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function up()
$table->string('description');
$table->string('slug')->unique();
$table->text('content');
$table->tinyInteger('is_enabled');
$table->timestamps();
});
}
Expand Down
5 changes: 5 additions & 0 deletions resources/views/admin/pages/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
@enderror
</div>

<div class="form-group custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="enableSwitch" name="is_enabled" checked>
<label class="custom-control-label" for="enableSwitch">Enable the page</label>
</div>

<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
Expand Down
6 changes: 6 additions & 0 deletions resources/views/admin/pages/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
@enderror
</div>

<div class="form-group custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="enableSwitch" name="is_enabled" @if($page->is_enabled) checked @endif>
<label class="custom-control-label" for="enableSwitch">Enable the page</label>
</div>

<button type="submit" class="btn btn-primary">Update</button>
<a href="{{ route('admin.pages.destroy', $page) }}" class="btn btn-danger" data-confirm="delete">Delete</a>
</form>
Expand Down
2 changes: 2 additions & 0 deletions resources/views/admin/pages/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Slug</th>
<th scope="col">Enabled</th>
<th scope="col">Action</th>
</tr>
</thead>
Expand All @@ -22,6 +23,7 @@
<th scope="row">{{ $page->id }}</th>
<td>{{ $page->title }}</td>
<td><a href="{{ route('pages.show', $page->slug) }}" target="_blank">{{ $page->slug }}</a></td>
<td>{{ $page->is_enabled ? 'Yes' : 'No' }}</td>
<td>
<a href="{{ route('admin.pages.edit', $page) }}" class="mx-1"><i class="fas fa-edit"></i></a>
<a href="{{ route('admin.pages.destroy', $page) }}" class="mx-1" data-confirm="delete"><i class="fas fa-trash"></i></a>
Expand Down

0 comments on commit ed57c8e

Please sign in to comment.