Skip to content

Commit

Permalink
Inventories Phase 1 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed May 3, 2024
1 parent c984da2 commit 2979823
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 4 deletions.
45 changes: 44 additions & 1 deletion app/Http/Controllers/Entity/InventoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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
]));
}

}
33 changes: 33 additions & 0 deletions app/Http/Requests/CopyInventory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests;

use App\Traits\ApiRequest;
use Illuminate\Foundation\Http\FormRequest;

class CopyInventory extends FormRequest
{
use ApiRequest;

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return $this->clean([
'copy_from' => 'required:exists:entities,id'
]);
}
}
5 changes: 4 additions & 1 deletion app/Http/Requests/StoreInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
}
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @property Image[] $folders
* @property Image[] $images
* @property Entity[] $entities
* @property Inventory[] $inventories
* @property Entity[] $headers
*
*
Expand Down Expand Up @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -40,6 +42,7 @@ class Inventory extends Model
'created_by',
'is_equipped',
'copy_item_entry',
'image_uuid',
];

/**
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('inventories', function (Blueprint $table) {
$table->uuid('image_uuid')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('inventories', function (Blueprint $table) {
$table->dropColumn('image_uuid');
});
}
};
4 changes: 3 additions & 1 deletion lang/en/entities/inventories.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
return [
'actions' => [
'add' => 'Add Item',
'copy_from' => 'Copy from',
'copy_inventory' => 'Copy inventory',
],
'create' => [
'success' => 'Item :item added to :entity.',
Expand All @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions resources/views/entities/pages/inventory/_buttons.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
<x-icon class="plus"></x-icon>
{{ __('entities/inventories.actions.add') }}
</a>

<a href="{{ route('entities.inventory.copy', [$campaign, 'entity' => $entity]) }}" class="btn2 btn-sm"
data-toggle="dialog" data-target="inventory-dialog"
data-url="{{ route('entities.inventory.copy', [$campaign, 'entity' => $entity]) }}"
>
<x-icon class="copy"></x-icon>
{{ __('entities/inventories.actions.copy_from') }}
</a>
12 changes: 12 additions & 0 deletions resources/views/entities/pages/inventory/_copy.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php /** @var \App\Models\Inventory $inventory */?>
{{ csrf_field() }}
<x-grid>
@include('cruds.fields.entity', [
'name' => 'copy_from',
'required' => true,
'label' => __('bookmarks.fields.entity'),
])
</x-grid>



27 changes: 26 additions & 1 deletion resources/views/entities/pages/inventory/_form.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php /** @var \App\Models\Inventory $inventory */?>
{{ csrf_field() }}
@php
$preset = null;
if (isset($inventory) && $inventory->image_uuid) {
$preset = $inventory->image;
}
@endphp
<x-grid>
<input type="hidden" name="item_id" value="" />
@include('cruds.fields.item', [
Expand Down Expand Up @@ -67,7 +73,26 @@
{!! Form::checkbox('is_equipped', 1, isset($inventory) ? $inventory->is_equipped : null) !!}
</x-checkbox>
</x-forms.field>

<div class="col-span-3">
<x-forms.foreign
field="image-uuid"
:campaign="$campaign"
name="image_uuid"
:allowClear="true"
:route="route('images.find', $campaign)"
:label="__('crud.fields.image')"
:placeholder="__('fields.gallery.placeholder')"
:selected="$preset">
</x-forms.foreign>
</div>
@if (!isset($bulk) && !empty($inventory) && !empty($inventory->image_uuid) && !empty($inventory->image))
<div class="preview w-32">
@include('cruds.fields._image_preview', [
'image' => $inventory->image->getUrl(192, 144),
'title' => $inventory->name,
])
</div>
@endif
@include('cruds.fields.visibility_id', ['model' => $inventory ?? null])
</x-grid>

Expand Down
22 changes: 22 additions & 0 deletions resources/views/entities/pages/inventory/copy.blade.php
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions routes/campaigns/entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 2979823

Please sign in to comment.