Skip to content

Commit

Permalink
Added support for unidirectional links
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed May 31, 2024
1 parent 08b7abe commit 8d7e7a9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/Requests/CreateLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public function urlOfModel(Model $model): ?string;
public function getTargetModel(): ?Model;

public function getLinkType(): string;

public function wantsUnidirectionalLink(): bool;
}
25 changes: 19 additions & 6 deletions src/Http/Controllers/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,38 @@
use Vanilo\Links\Contracts\LinkGroupItem;
use Vanilo\Links\Models\LinkTypeProxy;
use Vanilo\Links\Query\Establish;
use Vanilo\Links\Query\Get;

class LinkController extends BaseController
{
public function create(CreateLinkForm $request)
{
$source = $request->getSourceModel();
$existingGroups = [];
foreach ($types = LinkTypeProxy::choices(false, true) as $slug => $name) {
if ($group = Get::the($slug)->groups()->of($source)->first()) {
$existingGroups[$slug] = [
'omnidirectional' => null === $group->root_item_id,
];
}
}

return view('vanilo::link.create', [
'sourceModel' => $request->getSourceModel(),
'linkTypes' => LinkTypeProxy::choices(false, true),
'sourceModel' => $source,
'linkTypes' => $types,
'existingGroups' => $existingGroups,
]);
}

public function store(CreateLink $request)
{
$source = $request->getSourceModel();
try {
Establish::a($request->getLinkType())
->link()
->between($source)
->and($request->getTargetModel());
$establishALinkBetweenSource = Establish::a($request->getLinkType())->link()->between($source);
if ($request->wantsUnidirectionalLink()) {
$establishALinkBetweenSource->unidirectional();
}
$establishALinkBetweenSource->and($request->getTargetModel());
} catch (UniqueConstraintViolationException $e) {
flash()->error(
__(
Expand Down
6 changes: 6 additions & 0 deletions src/Http/Requests/CreateLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function rules(): array
return [
'link_type' => 'required|string|exclude_with:link_type_to_create|exists:link_types,slug',
'link_type_to_create' => 'sometimes|string|min:1|max:255',
'omnidirectional' => 'sometimes|bool',
'source_type' => ['sometimes', 'nullable', 'string', Rule::in(CreateLinkForm::$acceptedTypes)],
'source_id' => 'required|numeric',
'target_type' => ['sometimes', 'nullable', 'string', Rule::in(CreateLinkForm::$acceptedTypes)],
Expand Down Expand Up @@ -70,6 +71,11 @@ public function getLinkType(): string
return $this->input('link_type');
}

public function wantsUnidirectionalLink(): bool
{
return !$this->has('omnidirectional') || !$this->input('omnidirectional');
}

public function authorize()
{
return true;
Expand Down
48 changes: 46 additions & 2 deletions src/resources/views/link/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,70 @@
<div class="row">
<div class="my-2 col col-md-4 col-xl-2">
{!! Form::select('link_type', array_merge($linkTypes, ['___create' => __('--New Link Type--')]), null,
['class' => 'form-select', 'x-model' => 'selectedLinkType'])
[
'class' => 'form-select' . ($errors->has('link_type') ? ' is-invalid': ''),
'x-model' => 'selectedLinkType',
'placeholder' => '--'])
!!}
@if ($errors->has('link_type'))
<div class="invalid-feedback">{{ $errors->first('link_type') }}</div>
@endif
</div>
<div class="my-2 col col-md-4 col-xl-2">
<template x-if="wantsToCreateNewType">
{!! Form::text('link_type_to_create', null, ['class' => 'form-control']) !!}
</template>
</div>
</div>
<div class="mt-2 d-flex">
<div class="form-check form-switch">
{{ Form::checkbox('omnidirectional', 1, null, ['class' => 'form-check-input', 'id' => 'is-omnidirectional', 'role' => 'switch', 'x-model' => 'isOmnidirectional', 'x-bind:disabled' => 'canNotChooseDirection']) }}
<label class="form-check-label" for="is-omnidirectional">{{ __('Omnidirectional') }}</label>
</div>
<div class="form-check">
{!! icon(
'help',
'info',
[
'data-bs-toggle' => 'tooltip',
'data-bs-placement' => 'right',
'data-bs-title' => __('Omnidirectional means that all products are linking back to each other. Useful for cases like \'similar\' products')
]
) !!}
</div>
</div>
</section>

@push('scripts')
<script>
document.addEventListener('alpine:init', function() {
Alpine.data('vaniloLinkTypeWidget', () => ({
selectedLinkType: null,
selectedLinkType: @json(empty($linkTypes) ? '___create' : null),
existingGroups: @json($existingGroups),
wantsOmnidirectional: false,
get isOmnidirectional() {
const existingGroup = this.existingGroups[this.selectedLinkType]
if (undefined !== existingGroup) {
return existingGroup.omnidirectional
}
return this.wantsOmnidirectional
},
set isOmnidirectional(value) {
this.wantsOmnidirectional = !!value
},
get canNotChooseDirection() {
return undefined !== this.existingGroups[this.selectedLinkType];
},
wantsToCreateNewType() {
return this.selectedLinkType === '___create'
}
}))
})
</script>
@endpush

@push('onload-scripts')
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
@endpush()

0 comments on commit 8d7e7a9

Please sign in to comment.