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

Add multiple Abilities #776

Merged
merged 7 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/Http/Controllers/Abilities/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public function store(StoreAbilityEntity $request, Campaign $campaign, Ability $
$redirectUrlOptions['ability_id'] = $ability->id;
}

$ability->attachEntity($request->only('entity_id', 'visibility_id'));
$count = $ability->attachEntity($request->only('entities', 'visibility_id'));

return redirect()->route('abilities.entities', [$campaign, 'ability' => $ability->id])
->with('success', __('abilities.children.create.success', ['name' => $ability->name]));
//->with('success', __('abilities.children.create.success', ['name' => $ability->name]));
->with('success', trans_choice('abilities.children.create.attach_success', $count, ['count' => $count, 'name' => $ability->name]));

}
}
5 changes: 4 additions & 1 deletion app/Http/Requests/StoreAbilityEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
{
return [
'ability_id' => 'required|exists:entities,id',
'entity_id' => 'required|exists:entities,id|different:ability_id',
'visibility_id' => 'required|exists:visibilities,id',
'entities' => 'required',

Check failure on line 29 in app/Http/Requests/StoreAbilityEntity.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Array has 2 duplicate keys with value 'entities' ('entities', 'entities').
'entities' => [
'*' => 'different:ability_id|exists:entities,id',
],
];
}
}
29 changes: 12 additions & 17 deletions app/Models/Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,22 @@ public function entryWithAttributes()
}

/**
* Attach an entity to the tag
* Attach an entity to the ability
*/
public function attachEntity(array $request): bool
public function attachEntity(array $request): int
{
$entityId = Arr::get($request, 'entity_id');
$entity = Entity::with('abilities')->findOrFail($entityId);

// Make sure the tag isn't already attached to the entity
foreach ($entity->abilities as $ability) {
if ($ability->ability_id == $this->id) {
return true;
}
$entityIds = Arr::get($request, 'entities');
$count = 0;
$visibility = Arr::get($request, 'visibility_id', \App\Enums\Visibility::All);
$sync = [];

foreach ($entityIds as $entity) {
$sync[$entity] = ['visibility_id' => $visibility];
$count++;
}
$this->entities()->syncWithoutDetaching($sync);

$entityAbility = EntityAbility::create([
'ability_id' => $this->id,
'entity_id' => $entityId,
'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All),
]);

return $entityAbility !== false;
return $count;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lang/en/abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
return [
'children' => [
'actions' => [
'add' => 'Attach entity',
'attach' => 'Attach to entities',
],
'create' => [
'success' => 'Attached the ability :name to the entity.',
'title' => 'Attach :name to an entity',
'attach_success' => '{1} Attached the ability :name to :count entity.|[2,*] Attached the ability :name to :count entities.',
'modal' => 'Attach :name to entities',
],
'description' => 'Entities having the ability',
'title' => 'Ability :name Entities',
Expand Down
2 changes: 1 addition & 1 deletion resources/views/abilities/entities.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="header-buttons inline-block ml-auto">
<a href="{{ route('abilities.entity-add', [$campaign, $model]) }}" class="btn2 btn-sm"
data-toggle="dialog" data-target="primary-dialog" data-url="{{ route('abilities.entity-add', [$campaign, $model]) }}">
<x-icon class="plus"></x-icon> <span class="hidden md:inline">{{ __('abilities.children.actions.add') }}</span>
<x-icon class="plus"></x-icon> <span class="hidden md:inline">{{ __('abilities.children.actions.attach') }}</span>
</a>
</div>
@endcan
Expand Down
18 changes: 11 additions & 7 deletions resources/views/abilities/entities/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
{{ csrf_field() }}

<x-grid type="1/1">
@include('cruds.fields.entity', [
'required' => true,
'route' => 'search.ability-entities',
'placeholder' => __('entities/relations.placeholders.target'),
'preset' => false,
'dropdownParent' => request()->ajax() ? '#primary-dialog' : null,
])
<x-forms.foreign
field="entities"
:required="true"
label="abilities.show.tabs.entities"
multiple="multiple"
name="entities[]"
id="entities[]"
:campaign="$campaign"
:route="route('search.ability-entities', [$campaign, 'exclude-entity' => $model->entity->id])"
>
</x-forms.foreign>

@include('cruds.fields.visibility_id', ['model' => null])
</x-grid>
12 changes: 9 additions & 3 deletions resources/views/abilities/entities/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php /** @var \App\Models\Ability $model */ ?>
@extends('layouts.' . (request()->ajax() ? 'ajax' : 'app'), [
'title' => __('abilities.children.create.title', ['name' => $model->name]),
'title' => __('abilities.children.create.modal', ['name' => $model->name]),
'breadcrumbs' => [
Breadcrumb::entity($model->entity)->list(),
Breadcrumb::show($model),
Expand All @@ -9,9 +9,15 @@
])

@section('content')
{!! Form::open(['route' => $formOptions, 'method' => 'POST']) !!}
{!! Form::open([
'route' => $formOptions,
'method' => 'POST',
'data-shortcut' => 1,
'class' => 'ajax-subform',
]) !!}

@include('partials.forms.form', [
'title' => __('abilities.children.create.title', ['name' => $model->name]),
'title' => __('abilities.children.create.modal', ['name' => $model->name]),
'content' => 'abilities.entities._form',
'dialog' => true,
])
Expand Down
13 changes: 11 additions & 2 deletions resources/views/components/forms/foreign.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<div class="join w-full">
@endif

<select name="{{ $name }}" id="{{ $id }}"
<select
multiple="$multiple"
name="{{ $name }}"
id="{{ $id }}"
class="w-full select2 join-item"
style="width: 100%"
data-url="{{ $route }}"
Expand All @@ -19,9 +22,15 @@ class="w-full select2 join-item"
data-language="{{ LaravelLocalization::getCurrentLocale() }}"
data-allow-clear="{{ $allowClear ? 'true' : 'false' }}"
@if (!empty($dropdownParent)) data-dropdown-parent="{{ $dropdownParent }}" @endif
:dropdownParent="request()->ajax() ? '#primary-dialog' : null"

>
@foreach ($options as $key => $value)
<option value="{{ $key }}">{!! $value !!}</option>
@if ($multiple)
<option value="{{ $key }}">{!! $value !!}</option>
@else
<option value="{{ $key }}" class="select2-entity" selected="selected">{{ $entity->name }}</option>
@endif
@endforeach
</select>

Expand Down
Loading