Skip to content

Commit

Permalink
Merge pull request #115 from LIBCAS/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JarkaP authored May 19, 2022
2 parents 6f86dd9 + 276733a commit 66ab1d1
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 26 deletions.
63 changes: 40 additions & 23 deletions app/Http/Controllers/IdentityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ public function index()

public function create()
{
$identity = new Identity;

return view('pages.identities.form', [
'title' => __('hiko.new_identity'),
'identity' => $identity,
'action' => route('identities.store'),
'label' => __('hiko.create'),
'types' => $this->getTypes(),
'selectedType' => $this->getSelectedType($identity),
'selectedProfessions' => $this->getSelectedProfessions($identity),
'selectedCategories' => $this->getSelectedCategories($identity),
]);
return view(
'pages.identities.form',
array_merge(
[
'title' => __('hiko.new_identity'),
'action' => route('identities.store'),
'label' => __('hiko.create'),
'canRemove' => false,
'canMerge' => false,
],
$this->viewData(new Identity)
)
);
}

public function store(IdentityRequest $request)
Expand All @@ -50,17 +51,22 @@ public function store(IdentityRequest $request)

public function edit(Identity $identity)
{
return view('pages.identities.form', [
'title' => __('hiko.identity') . ': ' . $identity->id,
'identity' => $identity,
'method' => 'PUT',
'action' => route('identities.update', $identity),
'label' => __('hiko.edit'),
'types' => $this->getTypes(),
'selectedType' => $this->getSelectedType($identity),
'selectedProfessions' => $this->getSelectedProfessions($identity),
'selectedCategories' => $this->getSelectedCategories($identity),
]);
$hasLetters = $identity->letters->isNotEmpty();

return view(
'pages.identities.form',
array_merge(
[
'title' => __('hiko.identity') . ': ' . $identity->id,
'method' => 'PUT',
'action' => route('identities.update', $identity),
'label' => __('hiko.edit'),
'canRemove' => !$hasLetters,
'canMerge' => $hasLetters,
],
$this->viewData($identity)
)
);
}

public function update(IdentityRequest $request, Identity $identity)
Expand Down Expand Up @@ -90,6 +96,17 @@ public function export()
return Excel::download(new IdentitiesExport, 'identities.xlsx');
}

protected function viewData(Identity $identity)
{
return [
'identity' => $identity,
'types' => $this->getTypes(),
'selectedType' => $this->getSelectedType($identity),
'selectedProfessions' => $this->getSelectedProfessions($identity),
'selectedCategories' => $this->getSelectedCategories($identity),
];
}

protected function attachProfessionsAndCategories(Identity $identity, $validated)
{
$identity->professions()->detach();
Expand Down
19 changes: 19 additions & 0 deletions app/Http/Controllers/MergeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\MergeRelationships;

class MergeController extends Controller
{
public function __invoke(Request $request)
{
(new MergeRelationships($request->input('oldId'), $request->input('newId'), $request->input('model')))
->merge();

return redirect()
->route('identities.edit', $request->input('oldId'))
->with('success', __('hiko.merged'));
}
}
69 changes: 69 additions & 0 deletions app/Services/MergeRelationships.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Services;

use Exception;
use App\Models\Letter;
use App\Jobs\RegenerateNames;
use Illuminate\Support\Facades\DB;

class MergeRelationships
{
public $oldId;
public $newId;
public $model;

protected $affectedLetters;
protected $models = [
'identity' => [
'handler' => 'handleIdentity',
'relationship' => 'identities',
],
];

public function __construct($oldId, $newId, $model)
{
if (!array_key_exists($model, $this->models)) {
throw new Exception(__('Not found'), 404);
}

$this->oldId = $oldId;
$this->newId = $newId;
$this->model = $model;
$this->affectedLetters = $this->getAffectedLetters();

$this->merge();
}

public function merge()
{
call_user_func([$this, $this->models[$this->model]['handler']]);
}

protected function handleIdentity()
{
try {
DB::table('identity_letter')
->where('identity_id', '=', $this->oldId)
->update(['identity_id' => $this->newId]);
} catch (\Illuminate\Database\QueryException $ex) {
throw new Exception($ex->getMessage(), 500);
}

$this->affectedLetters->each(function ($letter) {
RegenerateNames::dispatch($letter->authors()->get());
RegenerateNames::dispatch($letter->recipients()->get());
});
}

protected function getAffectedLetters()
{
$relationship = $this->models[$this->model]['relationship'];

return Letter::select('id')
->whereHas($relationship, function ($query) use ($relationship) {
$query->where("{$relationship}.id", '=', $this->oldId);
})
->get();
}
}
5 changes: 5 additions & 0 deletions resources/lang/cs/hiko.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@
'new_password_settings' => 'Nastavení nového hesla',
'insert' => 'Vložit',
'visibility' => 'Viditelnost',
'merge_field' => 'Záznam, do kterého má být sloučena současná položka',
'merge_field_required' => 'Nejprve vyberte záznam, do kterého má být sloučeno',
'merge' => 'Sloučit',
'confirm_merge' => 'Opravdu chcete sloučit vybrané záznamy?',
'merged' => 'Záznamy byly sloučeny, můžete pokračovat smazáním této položky',
];
5 changes: 5 additions & 0 deletions resources/lang/en/hiko.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@
'new_password_settings' => 'New password settings',
'insert' => 'Insert',
'visibility' => 'Visibility',
'merge_field' => 'Record to merge',
'merge_field_required' => 'Select record to merge',
'merge' => 'Merge',
'confirm_merge' => 'Are you sure you want to merge this items?',
'merged' => 'Records were merged, you can now remove this item',
];
4 changes: 4 additions & 0 deletions resources/views/components/icons/inbox-in.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" {{ $attributes }} fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round"
d="M8 4H6a2 2 0 00-2 2v12a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-2m-4-1v8m0 0l3-3m-3 3L9 8m-5 5h2.586a1 1 0 01.707.293l2.414 2.414a1 1 0 00.707.293h3.172a1 1 0 00.707-.293l2.414-2.414a1 1 0 01.707-.293H20" />
</svg>
37 changes: 37 additions & 0 deletions resources/views/components/merge-form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div x-data="{ open: false }" class="max-w-sm mt-8 bg-gray-200 border rounded-md shadow">
<button type="button" @click="open = !open"
class="inline-flex items-center p-3 text-sm font-semibold text-primary hover:underline">
<x-icons.inbox-in class="h-4 mr-2" /><span>{{ __('hiko.merge') }}</span>
</button>
<span x-show="open" x-transition.duration.500ms>
<form x-data="{ form: $el }" action="{{ route('merge') }}" method="post" class="px-3 pb-3 space-y-6">
@csrf
@method('POST')
<div class="required">
<x-label for="merge" value="{{ __('hiko.merge_field') }}" />
<x-select x-data="ajaxChoices({ url: '{{ $route }}', element: $el })" x-init="initSelect();" name="newId" id="merge"
class="block w-full mt-1" required>
</x-select>
</div>
<input type="hidden" name="oldId" value="{{ $oldId }}">
<input type="hidden" name="model" value="{{ $model }}">
<x-button-danger type="button" class="w-full" x-on:click="handleMergeForm(form)">
{{ __('hiko.merge') }}
</x-button-danger>
</form>
</span>
</div>

@push('scripts')
<script>
function handleMergeForm(form) {
if (!form.checkValidity()) {
return alert('{{ __('hiko.merge_field_required') }}');
};
if (confirm('{{ __('hiko.confirm_merge') }}')) {
form.submit();
}
}
</script>
@endpush
10 changes: 7 additions & 3 deletions resources/views/pages/identities/form.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<x-app-layout :title="$title">
<x-success-alert />
<form x-data="identityForm({ type: '{{ $identity->type ? $identity->type : 'person' }}', similarNamesUrl: '{{ route('ajax.identities.similar') }}', id: '{{ $identity->id }}', surname: '{{ $identity->surname }}', name: '{{ $identity->name }}', forename: '{{ $identity->forename }}' })" x-init="$watch('fullName', () => findSimilarNames($data))" action="{{ $action }}" method="post" class="max-w-sm space-y-6">
<form x-data="identityForm({ type: '{{ $identity->type ? $identity->type : 'person' }}', similarNamesUrl: '{{ route('ajax.identities.similar') }}', id: '{{ $identity->id }}', surname: '{{ $identity->surname }}', name: '{{ $identity->name }}', forename: '{{ $identity->forename }}' })" x-init="$watch('fullName', () => findSimilarNames($data))" action="{{ $action }}" method="post"
class="max-w-sm space-y-6">
@csrf
@isset($method)
@method($method)
Expand Down Expand Up @@ -35,12 +36,15 @@ class="inline-flex items-center text-sm font-semibold text-primary hover:underli
{{ $label }}
</x-button-simple>
</form>
@if ($identity->id)
@if ($canMerge)
<x-merge-form :oldId="$identity->id" model="identity" route="{{ route('ajax.identities') }}" />
@endif
@if ($canRemove)
<form x-data="{ form: $el }" action="{{ route('identities.destroy', $identity->id) }}" method="post"
class="max-w-sm mt-8">
@csrf
@method('DELETE')
<x-button-danger class="w-full"
<x-button-danger type="button" class="w-full"
x-on:click.prevent="if (confirm('{{ __('hiko.confirm_remove') }}')) form.submit()">
{{ __('hiko.remove') }}
</x-button-danger>
Expand Down
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use App\Http\Controllers\Ajax\SimilarLocationsController;
use App\Http\Controllers\Ajax\AjaxKeywordCategoryController;
use App\Http\Controllers\Ajax\AjaxProfessionCategoryController;
use App\Http\Controllers\MergeController;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -393,6 +394,10 @@
->name('account')
->middleware(['auth']);

Route::post('merge', MergeController::class)
->name('merge')
->middleware(['auth', 'can:manage-metadata']);

Route::get('lang/{lang}', LanguageController::class)
->name('lang');

Expand Down

0 comments on commit 66ab1d1

Please sign in to comment.