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
17 changes: 1 addition & 16 deletions app/Http/Controllers/Campaign/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
use App\Services\Entity\RecoverySetupService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

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


public function __construct(RecoveryService $postService, EntityRecoveryService $entityService, RecoverySetupService $recoverySetupService)
{
$this->middleware('auth');
Expand All @@ -34,22 +32,9 @@ public function index(Campaign $campaign)
{
$this->authorize('recover', $campaign);

$elements = DB::select(
'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'));
return view('campaigns.recovery.index', compact('campaign'));
}


public function setup(Campaign $campaign)
{
$this->authorize('recover', $campaign);
Expand Down
5 changes: 2 additions & 3 deletions app/Services/Entity/RecoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function recover(array $ids): array
return $entities;
}


/**
*/
public function count(): int
Expand All @@ -41,13 +40,13 @@ protected function entity(int $id): string
{
$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 '';
return null;
}

$entity->restore();
Expand Down
5 changes: 3 additions & 2 deletions app/Services/Entity/RecoverySetupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ protected function elements(): array
$element->date = \Carbon\Carbon::createFromTimeStamp(strtotime($element->deleted_at))->diffForHumans();
$element->position = $key;
}
//This will cast each object in the array to an array, and the toArray gets you from the collection back to an array.
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
return collect($elements)->map(function ($x) { return (array) $x; })->toArray();
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
}

protected function i18n(): array
{
$translations = [
'0' => __('entities.post'),
'model_0' => __('entities.post'),
'order_by_newest' => __('campaigns/recovery.order.newest'),
'order_by_oldest' => __('campaigns/recovery.order.oldest'),
'order_by_type' => __('campaigns/recovery.order.type'),
Expand All @@ -111,7 +112,7 @@ protected function i18n(): array
if ($this->campaign->superboosted() && $this->campaign->hasModuleName($id)) {
$moduleName = $this->campaign->moduleName($id);
}
$translations[$id] = $moduleName;
$translations['model_' . $id] = $moduleName;
}
return $translations;
}
Expand Down
4 changes: 2 additions & 2 deletions app/Services/Posts/RecoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ protected function post(int $id): string
/** @var ?Post $post */
$post = Post::onlyTrashed()->find($id);
if (!$post) {
return '';
return null;
}
if ($post->entity->deleted_at) {
return '';
return null;
}
$post->restore();
$this->count++;
Expand Down
37 changes: 18 additions & 19 deletions resources/js/recovery/Element.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<template>
<div v-if="!file.is_hidden && !file.url" :class="fileClass()" @click="click">
<div v-if="!model.is_hidden && !model.url" :class="modelClass()" @click="click">
<div class="flex gap-4 items-center">
<div class="flex-none">
<input v-if="!file.url" type="checkbox" v-model="file.is_selected" @click="startBulking"/>
<input v-if="!model.url" type="checkbox" v-model="model.is_selected" @click="startBulking"/>
</div>
<div class="grow font-extrabold" v-html="file.name"></div>
<div class="rounded px-2 py-1 text-sm bg-base-200" v-html="trans(file.type_id)"></div>
<div class="grow font-extrabold" v-html="model.name"></div>
<div class="rounded px-2 py-1 text-sm bg-base-200" v-html="trans('model_' + model.type_id)"></div>
</div>
<hr />
<div class="flex items-center">
<div class="grow text-neutral-content">
<span v-html="trans('deleted_at', [file.date, file.deleted_name])"></span>
<span v-html="trans('deleted_at', [model.date, model.deleted_name])"></span>
</div>
<div v-if="!file.url" class="flex-none">
<div v-if="!model.url" class="flex-none">
<button :class="buttonClass()" @click="restoreElement()" v-html="trans('recover')" ></button>
</div>
</div>
</div>

<div v-if="!file.is_hidden && file.url" class="rounded shadow p-4 flex gap-2 items-center bg-base-200 hover:shadow-lg">
<div v-if="!model.is_hidden && model.url" class="rounded shadow p-4 flex gap-2 items-center bg-base-200 hover:shadow-lg">
<div class="flex-none">
<i class="fa-solid text-green-500 fa-circle-check" aria-hidden="true" />
</div>
<div class="grow" v-html="trans('recovery_success', [file.url, file.name])"></div>
<div class="rounded px-2 py-1 text-sm bg-base-200" v-html="trans(file.type_id)"></div> <!-- px and py might need tweaking -->
<div class="grow" v-html="trans('recovery_success', [model.url, model.name])"></div>
<div class="rounded px-2 py-1 text-sm bg-base-200" v-html="trans('model_' + model.type_id)"></div>
</div>

</template>
Expand All @@ -35,25 +35,24 @@ import {matches} from "lodash";
const emit = defineEmits(['recover'])

const props = defineProps<{
file: Object,
isBulking: Boolean,
model: Object,
i18n: Object
}>()

const click = () => {
if (!props.file.url) {
props.file.is_selected = !props.file.is_selected
if (!props.model.url) {
props.model.is_selected = !props.model.is_selected
}
}

const fileClass = () => {
const modelClass = () => {
let css = 'rounded shadow bg-base-100 flex flex-col gap-2 p-2 hover:shadow-lg'

if (!props.file.url) {
if (!props.model.url) {
css = css + ' cursor-pointer'
}

if (!props.file.is_selected) {
if (!props.model.is_selected) {
return css + ' '
}

Expand All @@ -76,14 +75,14 @@ const trans = (key, replace = []) => {

const buttonClass = () => {
let css = 'btn2 btn-default btn-sm'
if (props.file.is_recovering) {
css += ' loading btn-disabled btn-sm'
if (props.model.is_recovering) {
css += ' loading btn-disabled'
}
return css
}

const restoreElement = () => {
emit('recover', props.file)
emit('recover', props.model)
}

</script>
Loading
Loading