-
Notifications
You must be signed in to change notification settings - Fork 1
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 #115 from LIBCAS/dev
Dev
- Loading branch information
Showing
9 changed files
with
191 additions
and
26 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
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')); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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> |
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,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 |
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