-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #891 from owlchester/posts-soft-deletion
Posts can now be recovered after soft deletion
- Loading branch information
Showing
17 changed files
with
412 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\v1; | ||
|
||
use App\Models\Campaign; | ||
use App\Http\Resources\PostResource as Resource; | ||
use App\Http\Requests\RecoverPost as Request; | ||
use App\Services\RecoveryService; | ||
|
||
class PostRecoveryApiController extends ApiController | ||
{ | ||
protected RecoveryService $service; | ||
|
||
public function __construct(RecoveryService $service) | ||
{ | ||
$this->middleware('auth'); | ||
|
||
$this->service = $service; | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function index(Campaign $campaign) | ||
{ | ||
$this->authorize('recover', $campaign); | ||
return Resource::collection($campaign | ||
->posts()->onlyTrashed() | ||
->paginate()); | ||
} | ||
|
||
/** | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function recover(Request $request, Campaign $campaign) | ||
{ | ||
$this->authorize('recover', $campaign); | ||
|
||
if (!$campaign->boosted()) { | ||
return response()->json(null, 204); | ||
} | ||
$this->service->recoverPosts($request->posts); | ||
|
||
return response()->json(['success' => 'Succesfully recovered deleted posts']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Campaign; | ||
|
||
use App\Facades\Datagrid; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Campaign; | ||
use App\Services\RecoveryService; | ||
use Carbon\Carbon; | ||
use Exception; | ||
use Illuminate\Http\Request; | ||
|
||
class PostRecoveryController extends Controller | ||
{ | ||
protected RecoveryService $service; | ||
|
||
public function __construct(RecoveryService $service) | ||
{ | ||
$this->middleware('auth'); | ||
|
||
$this->service = $service; | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function index(Campaign $campaign) | ||
{ | ||
$this->authorize('recover', $campaign); | ||
|
||
Datagrid::layout(\App\Renderers\Layouts\Campaign\PostRecovery::class) | ||
->permissions(false); | ||
|
||
$rows = $campaign->posts()->onlyTrashed() | ||
->sort(request()->only(['o', 'k']), ['deleted_at' => 'DESC']) | ||
->whereDate('posts.deleted_at', '>=', Carbon::today()->subDays(config('entities.hard_delete_posts'))) | ||
->paginate(); | ||
|
||
// Ajax Datagrid | ||
if (request()->ajax()) { | ||
$html = view('layouts.datagrid._table') | ||
->with('rows', $rows) | ||
->render(); | ||
return response()->json([ | ||
'success' => true, | ||
'html' => $html, | ||
]); | ||
} | ||
$isPost = true; | ||
return view('campaigns.recovery.index', compact('rows', 'campaign', 'isPost')); | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Http\RedirectResponse | ||
*/ | ||
public function recover(Request $request, Campaign $campaign) | ||
{ | ||
if (!$campaign->boosted()) { | ||
return redirect() | ||
->route('recovery', $campaign) | ||
->with('boosted-pitch', true) | ||
; | ||
} | ||
try { | ||
$count = $this->service->recoverPosts($request->get('model', [])); | ||
return redirect() | ||
->route('recovery.posts', $campaign) | ||
->with('success', trans_choice('campaigns/recovery.posts.success', $count, ['count' => $count])); | ||
} catch (Exception $e) { | ||
return redirect() | ||
->route('recovery.posts', $campaign) | ||
->with('error', __('campaigns/recovery.posts.error')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class RecoverPost extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$rules = [ | ||
'posts' => 'array', | ||
'posts.*' => 'distinct|exists:posts,id' | ||
]; | ||
|
||
return $rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Renderers\Layouts\Campaign; | ||
|
||
use App\Renderers\Layouts\Layout; | ||
|
||
class PostRecovery extends Layout | ||
{ | ||
/** | ||
* Available columns | ||
* @return array[] | ||
*/ | ||
public function columns(): array | ||
{ | ||
$columns = [ | ||
'name' => [ | ||
'key' => 'name', | ||
'label' => 'crud.fields.name', | ||
], | ||
'deleted' => [ | ||
'key' => 'deleted_at', | ||
'label' => 'campaigns/recovery.fields.deleted', | ||
'class' => self::ONLY_DESKTOP, | ||
'render' => function ($post) { | ||
return $post->deleted_at->diffForHumans(); | ||
} | ||
], | ||
]; | ||
|
||
return $columns; | ||
} | ||
|
||
/** | ||
* Bulk actions | ||
*/ | ||
public function bulks(): array | ||
{ | ||
return [ | ||
[ | ||
'action' => 'recover', | ||
'label' => 'campaigns/recovery.actions.recover', | ||
'icon' => 'fa-solid fa-history', | ||
'can' => 'campaign:recover', | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.