2019-01-11 00:44:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Link;
|
2019-10-30 10:39:57 +01:00
|
|
|
use App\Models\LinkList;
|
2019-01-17 21:28:17 +01:00
|
|
|
use App\Models\Note;
|
2019-01-11 00:44:21 +01:00
|
|
|
use App\Models\Tag;
|
2019-11-28 11:15:22 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2020-01-24 13:15:49 +01:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2019-01-11 00:44:21 +01:00
|
|
|
use Illuminate\Http\Request;
|
2019-11-28 11:15:22 +01:00
|
|
|
use Illuminate\View\View;
|
2019-01-11 00:44:21 +01:00
|
|
|
|
2020-01-24 13:15:49 +01:00
|
|
|
/**
|
|
|
|
* Class TrashController
|
|
|
|
*
|
|
|
|
* @package App\Http\Controllers\App
|
|
|
|
*/
|
2019-01-11 00:44:21 +01:00
|
|
|
class TrashController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2020-01-24 13:15:49 +01:00
|
|
|
* @return Factory|View
|
2019-01-11 00:44:21 +01:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$links = Link::onlyTrashed()
|
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
|
2019-10-30 10:39:57 +01:00
|
|
|
$lists = LinkList::onlyTrashed()
|
2019-01-11 00:44:21 +01:00
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$tags = Tag::onlyTrashed()
|
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
|
2019-01-17 21:28:17 +01:00
|
|
|
$notes = Note::onlyTrashed()
|
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
|
2019-01-11 00:44:21 +01:00
|
|
|
return view('actions.trash.index', [
|
|
|
|
'links' => $links,
|
2019-10-30 10:39:57 +01:00
|
|
|
'lists' => $lists,
|
2019-01-11 00:44:21 +01:00
|
|
|
'tags' => $tags,
|
2019-01-17 21:28:17 +01:00
|
|
|
'notes' => $notes,
|
2019-01-11 00:44:21 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permanently delete entries for a model from the trash
|
|
|
|
*
|
|
|
|
* @param Request $reques
|
2019-11-28 11:15:22 +01:00
|
|
|
* @param string $model
|
2020-01-24 13:15:49 +01:00
|
|
|
* @return RedirectResponse
|
2019-01-11 00:44:21 +01:00
|
|
|
*/
|
|
|
|
public function clearTrash(Request $reques, $model)
|
|
|
|
{
|
|
|
|
$entries = [];
|
|
|
|
|
|
|
|
switch ($model) {
|
|
|
|
case 'links':
|
|
|
|
$entries = Link::onlyTrashed()
|
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
break;
|
2019-10-30 10:39:57 +01:00
|
|
|
case 'lists':
|
|
|
|
$entries = LinkList::onlyTrashed()
|
2019-01-11 00:44:21 +01:00
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
break;
|
|
|
|
case 'tags':
|
|
|
|
$entries = Tag::onlyTrashed()
|
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
break;
|
2019-01-17 21:28:17 +01:00
|
|
|
case 'notes':
|
|
|
|
$entries = Note::onlyTrashed()
|
|
|
|
->byUser(auth()->id())
|
|
|
|
->get();
|
|
|
|
break;
|
2019-01-11 00:44:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 12:25:03 +01:00
|
|
|
if ($entries->isEmpty()) {
|
2020-04-28 10:15:32 +02:00
|
|
|
flash(trans('trash.delete_no_entries'), 'warning');
|
2019-01-11 00:44:21 +01:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$entry->forceDelete();
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:15:32 +02:00
|
|
|
flash(trans('trash.delete_success.' . $model), 'success');
|
2019-01-11 00:44:21 +01:00
|
|
|
|
|
|
|
return redirect()->route('get-trash');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore an entry from the trash
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2019-11-28 11:15:22 +01:00
|
|
|
* @param string $model
|
|
|
|
* @param string $id
|
|
|
|
* @return Factory|View
|
2019-01-11 00:44:21 +01:00
|
|
|
*/
|
|
|
|
public function restoreEntry(Request $request, $model, $id)
|
|
|
|
{
|
|
|
|
$entry = null;
|
|
|
|
|
|
|
|
switch ($model) {
|
|
|
|
case 'link':
|
|
|
|
$entry = Link::withTrashed()->find($id);
|
|
|
|
break;
|
2019-10-30 10:39:57 +01:00
|
|
|
case 'list':
|
|
|
|
$entry = LinkList::withTrashed()->find($id);
|
2019-01-11 00:44:21 +01:00
|
|
|
break;
|
|
|
|
case 'tag':
|
|
|
|
$entry = Tag::withTrashed()->find($id);
|
|
|
|
break;
|
2019-01-17 21:28:17 +01:00
|
|
|
case 'note':
|
|
|
|
$entry = Note::withTrashed()->find($id);
|
|
|
|
break;
|
2019-01-11 00:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($entry)) {
|
2020-01-31 12:25:03 +01:00
|
|
|
abort(404, trans('trash.restore.not_found'));
|
2019-01-11 00:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($entry->user_id !== auth()->id()) {
|
2020-01-31 12:25:03 +01:00
|
|
|
abort(403, trans('trash.restore.not_allowed'));
|
2019-01-11 00:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$entry->restore();
|
|
|
|
|
2020-04-28 10:15:32 +02:00
|
|
|
flash(trans('trash.restore.' . $model), 'success');
|
2019-01-11 00:44:21 +01:00
|
|
|
|
|
|
|
return redirect()->route('get-trash');
|
|
|
|
}
|
|
|
|
}
|