Skip to content

Commit

Permalink
Add multiple Abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Dec 1, 2023
1 parent 61a8f5a commit 38768b4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 35 deletions.
9 changes: 6 additions & 3 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 @@ public function rules()
{
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',
],
];
}
}
36 changes: 19 additions & 17 deletions app/Models/Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,29 @@ 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');
$entities = Entity::with('abilities')->findOrFail($entityIds);
$count = 0;
foreach ($entities as $entity) {
// Make sure the tag isn't already attached to the entity
foreach ($entity->abilities as $ability) {
if ($ability->ability_id == $this->id) {
continue;
}
}
}

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

return $entityAbility !== false;
EntityAbility::create([
'ability_id' => $this->id,
'entity_id' => $entity->id,
'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All),
]);
$count++;
}
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
10 changes: 3 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,9 @@
{{ 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.field field="entities" :required="true" >
@include('components.form.entities', ['options' => ['exclude-entity' => $model->entity->id]])
</x-forms.field>

@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
49 changes: 49 additions & 0 deletions resources/views/components/form/entities.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
use Illuminate\Support\Arr;
/**
* We want to pre-load the data from the model, or what has been sent with the form.
* @var \App\Models\MiscModel $model
*/
$selectedOption = [];
$model = Arr::get($options, 'model', null);
$enableNew = Arr::get($options, 'enableNew', true);
$label = Arr::get($options, 'label', true);
// From source to exclude duplicates
$searchParams = [$campaign];
if (Arr::has($options, 'exclude', false)) {
$searchParams['exclude'] = Arr::get($options, 'exclude');
} elseif (Arr::has($options, 'exclude-entity', false)) {
$searchParams['exclude-entity'] = Arr::get($options, 'exclude-entity');
}
// Try to load what was sent with the form first, in case there was a form validation error
$previous = old('entities[]');
if (!empty($previous)) {
//dd($previous);
}
?>
@if ($label)
<label>
{{ __('abilities.show.tabs.entities') }}
</label>
@endif

<select
multiple="multiple"
name="entities[]"
id="{{ Arr::get($options, 'id', 'entities[]') }}"
class="w-full form-tags form-entities"
style="width: 100%"
data-url="{{ route('search.ability-entities', $searchParams) }}"
data-allow-new="{{ $enableNew ? 'true' : 'false' }}"
data-allow-clear="{{ Arr::get($options, 'allowClear', 'true') }}"
data-new-tag="{{ __('entities.new_entity') }}"
data-placeholder="{{ __('entities/relations.placeholders.target') }}"
:dropdownParent="$dropdownParent ?? (request()->ajax() ? '#primary-dialog' : null)"
>
@foreach ($selectedOption as $key => $entity)
<option value="{{ $key }}" class="select2-entity" selected="selected">{{ $entity->name }}</option>
@endforeach
</select>

0 comments on commit 38768b4

Please sign in to comment.