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.

61 changes: 27 additions & 34 deletions app/Http/Controllers/Campaign/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

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;

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 +32,23 @@ public function index(Campaign $campaign)
{
$this->authorize('recover', $campaign);

Datagrid::layout(\App\Renderers\Layouts\Campaign\Recovery::class)
->permissions(false);

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

// 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 +57,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
3 changes: 2 additions & 1 deletion app/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public function url(string $action = 'show', array $options = [])
if ($action == 'index') {
return route($this->pluralType() . '.index', $campaign);
} elseif ($action === 'show') {
return route('entities.show', [$campaign, $this]);
$routeOptions = array_merge([$campaign, $this], $options);
return route('entities.show', $routeOptions);
}
$routeOptions = array_merge([$campaign, $this->entity_id], $options);
return route($this->pluralType() . '.' . $action, $routeOptions);
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
32 changes: 11 additions & 21 deletions app/Services/Entity/RecoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,43 @@

class RecoveryService
{
/** Number of total recovered entities */
protected int $count = 0;

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

return $this->count;
}


/**
*/
public function count(): int
{
return $this->count;
return $entities;
}

/**
* Restore an entity and it's child
* @return bool if the restore worked
*/
protected function entity(int $id): bool
protected function entity(int $id): mixed
{
$entity = Entity::onlyTrashed()->find($id);
if (!$entity) {
return false;
return null;
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
}

// @phpstan-ignore-next-line
$child = $entity->child()->onlyTrashed()->first();
if (!$child) {
return false;
return null;
}

$entity->restore();

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

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