Skip to content

Commit

Permalink
Implemented requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Dec 1, 2023
1 parent a84a628 commit d17e22d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
21 changes: 7 additions & 14 deletions app/Models/Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,16 @@ public function entryWithAttributes()
public function attachEntity(array $request): int
{
$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;
}
}
$visibility = Arr::get($request, 'visibility_id', \App\Enums\Visibility::All);
$sync = [];

EntityAbility::create([
'ability_id' => $this->id,
'entity_id' => $entity->id,
'visibility_id' => Arr::get($request, 'visibility_id', \App\Enums\Visibility::All),
]);
$count++;
foreach ($entityIds as $entity) {
$sync[$entity] = ['visibility_id' => $visibility];
$count++;
}
$this->entities()->syncWithoutDetaching($sync);

return $count;
}

Expand Down
7 changes: 6 additions & 1 deletion resources/views/abilities/entities/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

<x-grid type="1/1">
<x-forms.field field="entities" :required="true" >
@include('components.form.entities', ['options' => ['exclude-entity' => $model->entity->id]])
<x-form.entities
:options="['exclude-entity' => $model->entity->id, 'route' => 'search.ability-entities']"
:campaign="$campaign"
:ajax="request()->ajax()"
>
</x-form.entities>
</x-forms.field>

@include('cruds.fields.visibility_id', ['model' => null])
Expand Down
7 changes: 2 additions & 5 deletions resources/views/components/form/entities.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
* @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)) {
Expand All @@ -36,12 +33,12 @@
id="{{ Arr::get($options, 'id', 'entities[]') }}"
class="w-full form-tags form-entities"
style="width: 100%"
data-url="{{ route('search.ability-entities', $searchParams) }}"
data-url="{{ route(Arr::get($options, '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)"
:dropdownParent="$dropdownParent ?? ($ajax ? '#primary-dialog' : null)"
>
@foreach ($selectedOption as $key => $entity)
<option value="{{ $key }}" class="select2-entity" selected="selected">{{ $entity->name }}</option>
Expand Down

0 comments on commit d17e22d

Please sign in to comment.