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

Tag children add multiple entities #777

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions app/Http/Controllers/Tags/ChildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function store(StoreTagEntity $request, Campaign $campaign, Tag $tag)
$redirectUrlOptions['tag_id'] = $tag->id;
}

$tag->attachEntity($request->only('entity_id'));
$count = $tag->attachEntities($request->only('entities'));
return redirect()->route('entities.show', $redirectUrlOptions)
->with('success', trans('tags.children.create.success', ['name' => $tag->name]));
->with('success', trans_choice('tags.children.create.attach_success', $count, ['name' => $tag->name, 'count' => $count]));
}
}
5 changes: 4 additions & 1 deletion app/Http/Requests/StoreTagEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function rules()
{
return [
'tag_id' => 'required|exists:entities,id',
'entity_id' => 'required|exists:entities,id|different:tag_id',
'entities' => 'required',
'entities' => [
'*' => 'different:tag_id|exists:entities,id',
],
];
}
}
34 changes: 18 additions & 16 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,26 +292,28 @@ public function hasColour(): bool
}

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

// Make sure the tag isn't already attached to the entity
foreach ($entity->entityTags as $tag) {
if ($tag->tag_id == $this->id) {
return true;
$entityIds = Arr::get($request, 'entities');
$entities = Entity::with('tags')->findOrFail($entityIds);
$count = 0;
foreach ($entities as $entity) {
// Make sure the tag isn't already attached to the entity
foreach ($entity->tags as $tag) {
if ($tag->tag_id == $this->id) {
continue;
}
}
}

$entityTag = EntityTag::create([
'tag_id' => $this->id,
'entity_id' => $entityId
]);

return $entityTag !== false;
EntityTag::create([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do $entity->tags->syncWithoutDetatching([$this->id]) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syncWithoutDetatching() doesnt work with hasManyThrough relations.

'tag_id' => $this->id,
'entity_id' => $entity->id,
]);
$count++;
}
return $count;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/View/Components/Forms/Foreign.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Foreign extends Component
public bool $allowNew;
public bool $allowClear;
public bool $required;
public bool $multiple;
public ?string $entityType;
public ?string $label;
public ?string $placeholder;
Expand All @@ -46,6 +47,7 @@ public function __construct(
bool $allowClear = false,
bool $required = false,
bool $parent = false,
bool $multiple = false,
string $entityType = null,
string $key = null,
string $label = null,
Expand Down Expand Up @@ -74,6 +76,7 @@ public function __construct(
$this->className = $class;
$this->entityTypeID = $entityTypeID;
$this->campaign = $campaign;
$this->multiple = $multiple;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lang/en/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
'add' => 'Add to tag',
],
'create' => [
'success' => 'Added the tag :name to the entity.',
'title' => 'Add an entity to :name',
'attach_success' => '{1} Added :count entity to tag :name.|[2,*] Added :count entities to tag :name.',
'modal_title' => 'Add entities to :name',
],
],
'create' => [
Expand Down
6 changes: 5 additions & 1 deletion 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
@if ($multiple) multiple="multiple" @endif
name="{{ $name }}"
id="{{ $id }}"
class="w-full select2 join-item"
style="width: 100%"
data-url="{{ $route }}"
Expand All @@ -19,6 +22,7 @@ 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>
Expand Down
17 changes: 11 additions & 6 deletions resources/views/tags/entities/_form.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php /** @var \App\Models\Tag $model */?>
{{ csrf_field() }}
<x-grid type="1/1">
@include('cruds.fields.entity', [
'placeholder' => __('entities/relations.placeholders.target'),
'preset' => false,
'route' => 'search.tag-children',
'dropdownParent' => request()->ajax() ? '#primary-dialog' : null
])
<x-forms.foreign
field="entities"
:required="true"
label="abilities.show.tabs.entities"
:multiple="true"
name="entities[]"
id="entities[]"
:campaign="$campaign"
:route="route('search.tag-children', [$campaign, 'exclude-entity' => true])"
>
</x-forms.foreign>
</x-grid>


12 changes: 9 additions & 3 deletions resources/views/tags/entities/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php /** @var \App\Models\Tag $model */ ?>
@extends('layouts.' . (request()->ajax() ? 'ajax' : 'app'), [
'title' => __('tags.children.create.title', ['name' => $model->name]),
'title' => __('tags.children.create.modal_title', ['name' => $model->name]),
'description' => '',
'breadcrumbs' => [
Breadcrumb::entity($model->entity)->list(),
Expand All @@ -10,9 +10,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' => __('tags.children.create.title', ['name' => $model->name]),
'title' => __('tags.children.create.modal_title', ['name' => $model->name]),
'content' => 'tags.entities._form',
'submit' => __('tags.children.actions.add'),
'dialog' => true,
Expand Down