diff --git a/app/Http/Controllers/Entity/InventoryController.php b/app/Http/Controllers/Entity/InventoryController.php index 5d6b184a34..8ad6a88368 100644 --- a/app/Http/Controllers/Entity/InventoryController.php +++ b/app/Http/Controllers/Entity/InventoryController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Controller; use App\Http\Requests\StoreInventory; +use App\Http\Requests\CopyInventory; use App\Models\Campaign; use App\Models\Entity; use App\Models\Inventory; @@ -23,7 +24,8 @@ class InventoryController extends Controller 'description', 'visibility_id', 'is_equipped', - 'copy_item_entry' + 'copy_item_entry', + 'image_uuid' ]; public function index(Campaign $campaign, Entity $entity) @@ -147,4 +149,45 @@ public function destroy(Campaign $campaign, Entity $entity, Inventory $inventory 'entity' => $entity->name ])); } + + /** + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function copy(Campaign $campaign, Entity $entity) + { + $this->authorize('update', $entity->child); + + return view('entities.pages.inventory.copy', compact( + 'campaign', + 'entity', + )); + } + + /** + */ + public function copyFrom(CopyInventory $request, Campaign $campaign, Entity $entity) + { + $this->authorize('update', $entity->child); + + if ($request->ajax()) { + return response()->json(['success' => true]); + } + + $copyFrom = Entity::where('id', $request->only('copy_from'))->first(); + foreach ($copyFrom->inventories->toArray() as $old) { + unset($old['id']); + $old['entity_id'] = $entity->id; + $inventory = new Inventory(); + $inventory = $inventory->create($old); + } + + return redirect() + ->route('entities.inventory', [$campaign, $entity]) + ->with('success_raw', __('entities/inventories.copy.success', [ + 'item' => $inventory->itemName(), + 'entity' => $entity->name + ])); + } + } diff --git a/app/Http/Requests/CopyInventory.php b/app/Http/Requests/CopyInventory.php new file mode 100644 index 0000000000..b992556ea9 --- /dev/null +++ b/app/Http/Requests/CopyInventory.php @@ -0,0 +1,33 @@ +clean([ + 'copy_from' => 'required:exists:entities,id' + ]); + } +} diff --git a/app/Http/Requests/StoreInventory.php b/app/Http/Requests/StoreInventory.php index ea08d7b7d7..058e1ab4ea 100644 --- a/app/Http/Requests/StoreInventory.php +++ b/app/Http/Requests/StoreInventory.php @@ -28,12 +28,15 @@ public function rules() { return $this->clean([ 'entity_id' => 'required|exists:entities,id', - 'item_id' => 'nullable|required_without:name|exists:items,id', + 'item_ids' => 'nullable|array|required_without:name', + 'item_ids.*' => 'exists:items,id', + //'item_id' => 'nullable|required_without:name|exists:items,id', 'name' => 'nullable|string|required_without:item_id', 'amount' => 'required|numeric', 'position' => 'nullable|string|max:191', 'description' => 'nullable|string|max:191', 'visibility_id' => 'nullable|exists:visibilities,id', + 'image_uuid' => 'nullable|exists:images,id', 'is_equipped' => 'boolean', ]); } diff --git a/app/Models/Image.php b/app/Models/Image.php index b11f26a9c0..4237165af9 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -40,6 +40,7 @@ * @property Image[] $folders * @property Image[] $images * @property Entity[] $entities + * @property Inventory[] $inventories * @property Entity[] $headers * * @@ -106,6 +107,11 @@ public function entities() return $this->hasMany(Entity::class, 'image_uuid', 'id'); } + public function inventories() + { + return $this->hasMany(Inventory::class, 'image_uuid', 'id'); + } + public function headers() { return $this->hasMany(Entity::class, 'header_uuid', 'id'); diff --git a/app/Models/Inventory.php b/app/Models/Inventory.php index c523b6052f..29b3ab456f 100644 --- a/app/Models/Inventory.php +++ b/app/Models/Inventory.php @@ -17,10 +17,12 @@ * @property int $amount * @property string $position * @property string $description + * @property string|null $image_uuid * @property bool $is_equipped * @property bool $copy_item_entry * @property Item|null $item * @property Entity|null $entity + * @property Image|null $image */ class Inventory extends Model { @@ -40,6 +42,7 @@ class Inventory extends Model 'created_by', 'is_equipped', 'copy_item_entry', + 'image_uuid', ]; /** @@ -65,6 +68,14 @@ public function creator() return $this->belongsTo('App\User', 'created_by'); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function image() + { + return $this->hasOne('App\Models\Image', 'id', 'image_uuid'); + } + /** * List of recently used positions for the form suggestions */ diff --git a/database/migrations/2024_05_02_162659_update_inventories_add_image_uuid.php b/database/migrations/2024_05_02_162659_update_inventories_add_image_uuid.php new file mode 100644 index 0000000000..3b35049a4f --- /dev/null +++ b/database/migrations/2024_05_02_162659_update_inventories_add_image_uuid.php @@ -0,0 +1,28 @@ +uuid('image_uuid')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('inventories', function (Blueprint $table) { + $table->dropColumn('image_uuid'); + }); + } +}; diff --git a/lang/en/entities/inventories.php b/lang/en/entities/inventories.php index 35f55fc99f..fc06332b3f 100644 --- a/lang/en/entities/inventories.php +++ b/lang/en/entities/inventories.php @@ -3,6 +3,8 @@ return [ 'actions' => [ 'add' => 'Add Item', + 'copy_from' => 'Copy from', + 'copy_inventory' => 'Copy inventory', ], 'create' => [ 'success' => 'Item :item added to :entity.', @@ -22,7 +24,7 @@ ], 'helpers' => [ 'copy_entity_entry' => 'Display the item\'s entry instead of the custom description.', - 'is_equipped' => 'Mark this items as being equipped.', + 'is_equipped' => 'Mark this item as being equipped.', ], 'placeholders' => [ 'amount' => 'Any amount', diff --git a/resources/views/entities/pages/inventory/_buttons.blade.php b/resources/views/entities/pages/inventory/_buttons.blade.php index 28fb7ff088..e111340ac5 100644 --- a/resources/views/entities/pages/inventory/_buttons.blade.php +++ b/resources/views/entities/pages/inventory/_buttons.blade.php @@ -5,3 +5,11 @@ {{ __('entities/inventories.actions.add') }} + + + + {{ __('entities/inventories.actions.copy_from') }} + \ No newline at end of file diff --git a/resources/views/entities/pages/inventory/_copy.blade.php b/resources/views/entities/pages/inventory/_copy.blade.php new file mode 100644 index 0000000000..470cf0e4bf --- /dev/null +++ b/resources/views/entities/pages/inventory/_copy.blade.php @@ -0,0 +1,12 @@ + +{{ csrf_field() }} + +@include('cruds.fields.entity', [ + 'name' => 'copy_from', + 'required' => true, + 'label' => __('bookmarks.fields.entity'), + ]) + + + + diff --git a/resources/views/entities/pages/inventory/_form.blade.php b/resources/views/entities/pages/inventory/_form.blade.php index f48eb18a65..23a6cee62f 100644 --- a/resources/views/entities/pages/inventory/_form.blade.php +++ b/resources/views/entities/pages/inventory/_form.blade.php @@ -1,6 +1,12 @@ {{ csrf_field() }} +@php +$preset = null; +if (isset($inventory) && $inventory->image_uuid) { + $preset = $inventory->image; +} +@endphp @include('cruds.fields.item', [ @@ -67,7 +73,26 @@ {!! Form::checkbox('is_equipped', 1, isset($inventory) ? $inventory->is_equipped : null) !!} - +
+ + +
+ @if (!isset($bulk) && !empty($inventory) && !empty($inventory->image_uuid) && !empty($inventory->image)) +
+ @include('cruds.fields._image_preview', [ + 'image' => $inventory->image->getUrl(192, 144), + 'title' => $inventory->name, + ]) +
+ @endif @include('cruds.fields.visibility_id', ['model' => $inventory ?? null])
diff --git a/resources/views/entities/pages/inventory/copy.blade.php b/resources/views/entities/pages/inventory/copy.blade.php new file mode 100644 index 0000000000..f02b20b1a3 --- /dev/null +++ b/resources/views/entities/pages/inventory/copy.blade.php @@ -0,0 +1,22 @@ +@extends('layouts.' . (request()->ajax() ? 'ajax' : 'app'), [ + 'title' => __('entities/inventories.create.title', ['name' => $entity->name]), + 'description' => '', + 'breadcrumbs' => [ + Breadcrumb::entity($entity)->list(), + Breadcrumb::show(), + ['url' => route('entities.inventory', [$campaign, $entity->id]), 'label' => __('crud.tabs.inventory')], + ] +]) + +@section('content') + {!! Form::open(['route' => ['entities.inventory.copy-from', $campaign, $entity->id], 'method'=>'POST', 'data-shortcut' => 1, 'data-maintenance' => 1, 'class' => 'ajax-subform']) !!} + @include('partials.forms.form', [ + 'title' => __('entities/inventories.create.title', ['name' => $entity->name]), + 'content' => 'entities.pages.inventory._copy', + 'submit' => __('entities/inventories.actions.copy_inventory'), + 'dialog' => true, + ]) + + {!! Form::hidden('entity_id', $entity->id) !!} + {!! Form::close() !!} +@endsection diff --git a/routes/campaigns/entities.php b/routes/campaigns/entities.php index 0ca26052a4..7e8dc27895 100644 --- a/routes/campaigns/entities.php +++ b/routes/campaigns/entities.php @@ -269,6 +269,8 @@ // Inventory Route::get('/w/{campaign}/entities/{entity}/inventory', 'Entity\InventoryController@index')->name('entities.inventory'); +Route::post('/w/{campaign}/entities/{entity}/inventory/copy_from', 'Entity\InventoryController@copyFrom')->name('entities.inventory.copy-from'); +Route::get('/w/{campaign}/entities/{entity}/inventory/copy', 'Entity\InventoryController@copy')->name('entities.inventory.copy'); // Export Route::get('/w/{campaign}/entities/{entity}/html-export', 'Entity\ExportController@html')->name('entities.html-export');