Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New recovery UI #962

Merged
merged 13 commits into from
Sep 5, 2024
2 changes: 1 addition & 1 deletion app/Console/Commands/Migrations/MigrateTutorials.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle()
$tutorial->code = mb_substr($key, 9);
$tutorial->save();
unset($settings[$key]);
} elseif(str_starts_with($key, 'releases_')) {
} elseif (str_starts_with($key, 'releases_')) {
$tutorial = new Tutorial();
$tutorial->user_id = $user->id;
$tutorial->code = $key . '_' . $setting;
Expand Down
77 changes: 0 additions & 77 deletions app/Http/Controllers/Campaign/PostRecoveryController.php

This file was deleted.

74 changes: 41 additions & 33 deletions app/Http/Controllers/Campaign/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

namespace App\Http\Controllers\Campaign;

use App\Facades\Datagrid;
use App\Http\Controllers\Controller;
use App\Models\Campaign;
use App\Models\Entity;
use App\Services\Entity\RecoveryService;
use Carbon\Carbon;
use App\Services\Entity\RecoveryService as EntityRecoveryService;
use App\Services\Posts\RecoveryService;
use App\Services\Entity\RecoverySetupService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RecoveryController extends Controller
{
protected RecoveryService $service;
protected RecoveryService $postService;
protected EntityRecoveryService $entityService;
protected RecoverySetupService $service;

public function __construct(RecoveryService $service)

public function __construct(RecoveryService $postService, EntityRecoveryService $entityService, RecoverySetupService $recoverySetupService)
{
$this->middleware('auth');

$this->service = $service;
$this->postService = $postService;
$this->entityService = $entityService;
$this->service = $recoverySetupService;
}

/**
Expand All @@ -30,34 +34,36 @@ public function index(Campaign $campaign)
{
$this->authorize('recover', $campaign);

Datagrid::layout(\App\Renderers\Layouts\Campaign\Recovery::class)
->permissions(false);
$elements = DB::select(
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
'select id, name, deleted_at, deleted_by, "entity" as type
from entities
where deleted_at is not null and campaign_id = ' . $campaign->id . '
union all
select p.id, p.name, p.deleted_at, p.deleted_by, "post" as type
from posts as p
left join entities as e on e.id = p.entity_id
where p.deleted_at is not null and e.deleted_at is null and e.campaign_id = ' . $campaign->id .
' order by deleted_at DESC'
);

return view('campaigns.recovery.index', compact('elements', 'campaign'));
}

$rows = Entity::onlyTrashed()
->with('image')
->sort(request()->only(['o', 'k']), ['deleted_at' => 'DESC'])
->whereDate('deleted_at', '>=', Carbon::today()->subDays(config('entities.hard_delete')))
->paginate();

// Ajax Datagrid
if (request()->ajax()) {
$html = view('layouts.datagrid._table')
->with('rows', $rows)
->render();
return response()->json([
'success' => true,
'html' => $html,
]);
}
public function setup(Campaign $campaign)
{
$this->authorize('recover', $campaign);

return view('campaigns.recovery.index', compact('rows', 'campaign'));
return response()->json(
$this->service
->user(auth()->user())
->campaign($campaign)
->setup()
);
}

public function recover(Request $request, Campaign $campaign)
{
if (request()->ajax()) {
return response()->json(['success' => true]);
}
if (!$campaign->boosted()) {
return redirect()
->route('recovery', $campaign)
Expand All @@ -66,10 +72,12 @@ public function recover(Request $request, Campaign $campaign)
}

try {
$count = $this->service->recover($request->get('model', []));
return redirect()
->route('recovery', $campaign)
->with('success', trans_choice('campaigns/recovery.success', $count, ['count' => $count]));
$entities = $this->entityService->recover($request->get('entities', []));
$posts = $this->postService->recover($request->get('posts', []));

$count = count($entities) + count($posts);

return response()->json(['entities' => $entities, 'posts' => $posts, 'toast' => trans_choice('campaigns/recovery.success_v2', $count, ['count' => $count])]);
} catch (Exception $e) {
return redirect()
->route('recovery', $campaign)
Expand Down
1 change: 0 additions & 1 deletion app/Http/Middleware/ApiLogMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Middleware;

use App\Facades\ApiLog;

use Closure;

class ApiLogMiddleware
Expand Down
1 change: 0 additions & 1 deletion app/Http/Middleware/OTPMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Middleware;

use App\Models\Google2FAAuthentication;

use Closure;
use App\Facades\Identity;

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Resources/GalleryFileFull.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function toArray(Request $request): array
$file = $this->resource;

$mentions = [];
foreach($file->inEntities() as $entity) {
foreach ($file->inEntities() as $entity) {
$mentions[] = [
'url' => $entity->url(),
'name' => $entity->name,
'type' => 'image',
];
}
foreach ($file->mentions as $mention) {
if($mention->isPost()) {
if ($mention->isPost()) {
$mentions[] = [
'url' => $mention->entity->url() . '?#post-' . $mention->post_id,
'name' => $mention->post->name,
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ public function url(string $action = 'show', array $options = [])
{
$campaign = CampaignLocalization::getCampaign();
try {
$routeOptions = array_merge([$campaign, $this->entity_id], $options);
if ($action == 'index') {
return route($this->pluralType() . '.index', $campaign);
} elseif ($action === 'show') {
return route('entities.show', [$campaign, $this]);
return route('entities.show', $routeOptions);
}
$routeOptions = array_merge([$campaign, $this->entity_id], $options);
return route($this->pluralType() . '.' . $action, $routeOptions);
} catch (Exception $e) {
return route('dashboard', $campaign);
Expand Down
2 changes: 1 addition & 1 deletion app/Renderers/DatagridRenderer2.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function isHighlighted(mixed $row): bool
public function rowAttributes(mixed $row): string
{
$attributes = [];
foreach($row->rowAttributes() as $attr => $val) {
foreach ($row->rowAttributes() as $attr => $val) {
if ($val instanceof UnitEnum) {
// @phpstan-ignore-next-line
$val = $val->value;
Expand Down
2 changes: 1 addition & 1 deletion app/Services/BulkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function editing(array $fields, Bulk $bulk): int
}
$entityFields = $filledFields;

if(isset($entityFields[$parent]) && intval($entityFields[$parent]) == $entity->id) {
if (isset($entityFields[$parent]) && intval($entityFields[$parent]) == $entity->id) {
unset($entityFields[$parent]);
}

Expand Down
20 changes: 12 additions & 8 deletions app/Services/Entity/RecoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ class RecoveryService

/**
*/
public function recover(array $ids): int
public function recover(array $ids): array
{
$this->count = 0;
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
$entities = [];
foreach ($ids as $id) {
if ($this->entity($id)) {
$this->count++;
$url = $this->entity($id);
if ($url) {
$entities[$id] = $url;
}
}

return $this->count;
return $entities;
}


Expand All @@ -33,9 +35,9 @@ public function count(): int

/**
* Restore an entity and it's child
* @return bool if the restore worked
* @return string if the restore worked
*/
protected function entity(int $id): bool
protected function entity(int $id): string
{
$entity = Entity::onlyTrashed()->find($id);
if (!$entity) {
Expand All @@ -45,14 +47,16 @@ protected function entity(int $id): bool
// @phpstan-ignore-next-line
$child = $entity->child()->onlyTrashed()->first();
if (!$child) {
return false;
return '';
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
}

$entity->restore();

// Refresh the child first to not re-trigger the entity creation on save
$child->refresh();
$child->restoreQuietly();
return true;
$this->count++;

return $entity->url();
}
}
Loading