mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-02-23 18:53:13 +01:00
Add views, update controller for tag editing
This commit is contained in:
parent
9156d95f9c
commit
42e1fa0681
@ -5,61 +5,91 @@ namespace App\Http\Controllers;
|
||||
use App\Http\Requests\TagDeleteRequest;
|
||||
use App\Http\Requests\TagStoreRequest;
|
||||
use App\Http\Requests\TagUpdateRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Tag;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return void
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
return view('models.tags.index')
|
||||
->with('tags', Tag::byUser(auth()->user()->id)
|
||||
->orderBy('name', 'ASC')
|
||||
->paginate(config('linkace.default.pagination'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return void
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
return view('models.tags.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param TagStoreRequest $request
|
||||
* @return void
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(TagStoreRequest $request)
|
||||
{
|
||||
//
|
||||
$data = $request->except(['tags', 'reload_view']);
|
||||
|
||||
// Set the user ID
|
||||
$data['user_id'] = auth()->user()->id;
|
||||
|
||||
$tag = Tag::create($data);
|
||||
|
||||
alert(trans('tag.added_successfully'), 'success');
|
||||
|
||||
if ($request->get('reload_view')) {
|
||||
session()->flash('reload_view', true);
|
||||
return redirect()->route('tags.create');
|
||||
}
|
||||
|
||||
return redirect()->route('tags.show', [$tag->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
$tag = Tag::find($id);
|
||||
|
||||
if (empty($tag)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return view('models.tags.show')->with('tag', $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
$tag = Tag::find($id);
|
||||
|
||||
if (empty($tag)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return view('models.tags.edit')->with('tag', $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,11 +97,23 @@ class TagController extends Controller
|
||||
*
|
||||
* @param TagUpdateRequest $request
|
||||
* @param int $id
|
||||
* @return void
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function update(TagUpdateRequest $request, $id)
|
||||
{
|
||||
//
|
||||
$tag = Tag::find($id);
|
||||
|
||||
if (empty($tag)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$data = $request->all();
|
||||
|
||||
$tag->update($data);
|
||||
|
||||
alert(trans('tag.updated_successfully'), 'success');
|
||||
|
||||
return redirect()->route('tags.show', [$tag->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,10 +121,22 @@ class TagController extends Controller
|
||||
*
|
||||
* @param TagDeleteRequest $request
|
||||
* @param int $id
|
||||
* @return void
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function destroy(TagDeleteRequest $request, $id)
|
||||
{
|
||||
//
|
||||
$tag = Tag::find($id);
|
||||
|
||||
if (empty($tag)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
// Delete all attached links
|
||||
$tag->links()->detach();
|
||||
|
||||
$tag->delete();
|
||||
|
||||
return redirect()->route('tags.index');
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TagStoreRequest extends FormRequest
|
||||
{
|
||||
@ -24,7 +25,12 @@ class TagStoreRequest extends FormRequest
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'name' => [
|
||||
'required',
|
||||
Rule::unique('tags')->where(function ($query) {
|
||||
return $query->where('user_id', auth()->user()->id);
|
||||
}),
|
||||
],
|
||||
'is_private' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
@ -5,9 +5,16 @@ namespace App\Http\Requests;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TagUpdateRequest extends FormRequest
|
||||
{
|
||||
/** @var Tag */
|
||||
private $tag;
|
||||
|
||||
/** @var bool */
|
||||
private $unique_validation = false;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
@ -21,13 +28,18 @@ class TagUpdateRequest extends FormRequest
|
||||
return false;
|
||||
}
|
||||
|
||||
$tag = Tag::find($request->get('tag_id'));
|
||||
$this->tag = Tag::find($request->get('tag_id'));
|
||||
|
||||
// Check if the tag belongs to the user
|
||||
if ($tag->user_id !== auth()->user()->id) {
|
||||
if ($this->tag->user_id !== auth()->user()->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Enable unique validation if the url was changed
|
||||
if ($this->tag->name !== $request->get('name')) {
|
||||
$this->unique_validation = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -38,9 +50,22 @@ class TagUpdateRequest extends FormRequest
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
if (!$this->unique_validation) {
|
||||
return [
|
||||
'tag_id' => 'required',
|
||||
'name' => 'required',
|
||||
'is_private' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'tag_id' => 'required',
|
||||
'name' => 'required',
|
||||
'name' => [
|
||||
'required',
|
||||
Rule::unique('tags')->where(function ($query) {
|
||||
return $query->where('user_id', auth()->user()->id);
|
||||
}),
|
||||
],
|
||||
'is_private' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
return [
|
||||
'tag' => 'Tag',
|
||||
'tags' => 'Tags',
|
||||
'all_tags' => 'All Tags',
|
||||
|
||||
'add' => 'Add Tag',
|
||||
'show' => 'Show Tag',
|
||||
@ -11,5 +12,9 @@ return [
|
||||
|
||||
'private' => 'Private Tag',
|
||||
|
||||
'tag_name' => 'Tag Name',
|
||||
'name' => 'Tag Name',
|
||||
|
||||
'added_successfully' => 'Tag added successfully.',
|
||||
'updated_successfully' => 'Tag updated successfully.',
|
||||
'deleted_successfully' => 'Tag deleted successfully.',
|
||||
];
|
||||
|
44
resources/views/models/tags/_table.blade.php
Normal file
44
resources/views/models/tags/_table.blade.php
Normal file
@ -0,0 +1,44 @@
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>@lang('tag.name')</th>
|
||||
<th>@lang('link.links')</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($tags as $tag)
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{ route('tags.show', [$tag->id]) }}">
|
||||
{{ $tag->name }}
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
{{ $tag->links->count() }}
|
||||
</td>
|
||||
<td>
|
||||
<div class="field has-addons">
|
||||
<div class="control">
|
||||
<a href="{{ route('tags.edit', [$tag->id]) }}" class="button is-small">
|
||||
<i class="fa fa-pencil"></i>
|
||||
</a>
|
||||
</div>
|
||||
<div class="control">
|
||||
<a onclick="event.preventDefault();document.getElementById('tag-delete-{{ $tag->id }}').submit();"
|
||||
title=" @lang('tag.delete')" class="button is-small is-danger">
|
||||
<i class="fa fa-trash"></i>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<form id="tag-delete-{{ $tag->id }}" method="POST" style="display: none;"
|
||||
action="{{ route('tags.destroy', [$tag->id]) }}">
|
||||
@method('DELETE')
|
||||
@csrf
|
||||
<input type="hidden" name="tag_id" value="{{ $tag->id }}">
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
73
resources/views/models/tags/create.blade.php
Normal file
73
resources/views/models/tags/create.blade.php
Normal file
@ -0,0 +1,73 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
@lang('tag.add')
|
||||
</p>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
|
||||
<form action="{{ route('tags.store') }}" method="POST">
|
||||
@csrf
|
||||
|
||||
<div class="field">
|
||||
<label class="label" for="name">@lang('tag.name')</label>
|
||||
<div class="control">
|
||||
<input name="name" id="name"
|
||||
class="input is-large{{ $errors->has('name') ? ' is-danger' : '' }}"
|
||||
type="text" placeholder="@lang('tag.name')" value="{{ old('name') }}"
|
||||
required autofocus>
|
||||
</div>
|
||||
@if ($errors->has('name'))
|
||||
<p class="help has-text-danger" role="alert">
|
||||
{{ $errors->first('name') }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
<div class="field">
|
||||
<label class="label" for="is_private">@lang('linkace.is_private')</label>
|
||||
<div class="control">
|
||||
<div class="select{{ $errors->has('is_private') ? ' is-danger' : '' }}">
|
||||
<select id="is_private" name="is_private">
|
||||
<option value="0">@lang('linkace.no')</option>
|
||||
<option value="1">@lang('linkace.yes')</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@if ($errors->has('is_private'))
|
||||
<p class="help has-text-danger" role="alert">
|
||||
{{ $errors->first('is_private') }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
<div class="field">
|
||||
<div class="control is-flex align-items-center has-text-right">
|
||||
|
||||
<label class="checkbox mr ml-auto has-text-grey-light">
|
||||
<input type="checkbox" name="reload_view"
|
||||
@if(session('reload_view')) checked @endif>
|
||||
@lang('linkace.continue_adding')
|
||||
</label>
|
||||
|
||||
<button type="submit" class="button is-primary is-medium">
|
||||
<i class="fa fa-save fa-mr"></i> @lang('tag.add')
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@endsection
|
72
resources/views/models/tags/edit.blade.php
Normal file
72
resources/views/models/tags/edit.blade.php
Normal file
@ -0,0 +1,72 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
@lang('tag.add')
|
||||
</p>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
|
||||
<form action="{{ route('tags.update', [$tag->id]) }}" method="POST">
|
||||
@method('PATCH')
|
||||
@csrf
|
||||
|
||||
<input type="hidden" name="tag_id" value="{{ $tag->id }}">
|
||||
|
||||
<div class="field">
|
||||
<label class="label" for="name">@lang('tag.name')</label>
|
||||
<div class="control">
|
||||
<input name="name" id="name"
|
||||
class="input is-large{{ $errors->has('name') ? ' is-danger' : '' }}"
|
||||
type="text" placeholder="@lang('tag.name')" value="{{ old('name') ?: $tag->name }}"
|
||||
required autofocus>
|
||||
</div>
|
||||
@if ($errors->has('name'))
|
||||
<p class="help has-text-danger" role="alert">
|
||||
{{ $errors->first('name') }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
<div class="field">
|
||||
<label class="label" for="is_private">@lang('linkace.is_private')</label>
|
||||
<div class="control">
|
||||
<div class="select{{ $errors->has('is_private') ? ' is-danger' : '' }}">
|
||||
<select id="is_private" name="is_private">
|
||||
<option value="0" @if($tag->is_private === 0) selected @endif>
|
||||
@lang('linkace.no')
|
||||
</option>
|
||||
<option value="1" @if($tag->is_private === 1) selected @endif>
|
||||
@lang('linkace.yes')
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@if ($errors->has('is_private'))
|
||||
<p class="help has-text-danger" role="alert">
|
||||
{{ $errors->first('is_private') }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
<div class="field has-text-right">
|
||||
|
||||
<button type="submit" class="button is-primary is-medium">
|
||||
<i class="fa fa-save fa-mr"></i> @lang('tag.update')
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@endsection
|
39
resources/views/models/tags/index.blade.php
Normal file
39
resources/views/models/tags/index.blade.php
Normal file
@ -0,0 +1,39 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
@lang('tag.tags')
|
||||
</p>
|
||||
<a href="{{ route('tags.create') }}" class="card-header-icon" aria-label="@lang('tag.add')">
|
||||
<div class="icon">
|
||||
<i class="fa fa-plus fa-mr" aria-hidden="true"></i>
|
||||
</div>
|
||||
@lang('linkace.add')
|
||||
</a>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
|
||||
@if(!$tags->isEmpty())
|
||||
|
||||
@include('models.tags._table')
|
||||
|
||||
@else
|
||||
|
||||
<div class="message is-warning">
|
||||
<div class="message-body">
|
||||
@lang('linkace.no_results_found', ['model' => trans('tag.tags')])
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@endif
|
||||
|
||||
</div>
|
||||
@if(!$tags->isEmpty())
|
||||
{!! $tags->links('partials.card-pagination', ['paginator' => $tags]) !!}
|
||||
@endif
|
||||
</div>
|
||||
|
||||
@endsection
|
37
resources/views/models/tags/show.blade.php
Normal file
37
resources/views/models/tags/show.blade.php
Normal file
@ -0,0 +1,37 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<p class="card-header-title">
|
||||
@lang('tag.tag')
|
||||
</p>
|
||||
<a href="{{ route('tags.edit', [$tag->id]) }}" class="card-header-icon" aria-label="@lang('tag.edit')">
|
||||
<div class="icon">
|
||||
<i class="fa fa-pencil fa-mr" aria-hidden="true"></i>
|
||||
</div>
|
||||
@lang('linkace.edit')
|
||||
</a>
|
||||
<a onclick="event.preventDefault();document.getElementById('tag-delete-{{ $tag->id }}').submit();"
|
||||
class="card-header-icon has-text-danger" aria-label="@lang('tag.delete')">
|
||||
<div class="icon">
|
||||
<i class="fa fa-trash fa-mr" aria-hidden="true"></i>
|
||||
</div>
|
||||
@lang('linkace.delete')
|
||||
</a>
|
||||
<form id="tag-delete-{{ $tag->id }}" method="POST" style="display: none;"
|
||||
action="{{ route('tags.destroy', [$tag->id]) }}">
|
||||
@method('DELETE')
|
||||
@csrf
|
||||
<input type="hidden" name="tag_id" value="{{ $tag->id }}">
|
||||
</form>
|
||||
</header>
|
||||
<div class="card-content">
|
||||
|
||||
<h2 class="is-size-3">{{ $tag->name }}</h2>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@endsection
|
Loading…
x
Reference in New Issue
Block a user