Skip to content

Commit

Permalink
Inventory: show selected image and order alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed May 17, 2024
1 parent 73c59a7 commit 6a3ca61
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 260 deletions.
8 changes: 1 addition & 7 deletions app/Http/Controllers/Entity/InventoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ public function index(Campaign $campaign, Entity $entity)
);
}

$inventory = $entity
->inventories()
->with(['entity', 'item', 'item.entity'])
->get()
->sortBy(function ($model, $key) {
return !empty($model->position) ? $model->position : 'zzzz' . $model->itemName();
});
$inventory = $entity->orderedInventory();

return view('entities.pages.inventory.index', compact(
'campaign',
Expand Down
23 changes: 20 additions & 3 deletions app/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Collator;

/**
* Class Entity
Expand Down Expand Up @@ -482,17 +484,32 @@ public function export(): array
/**
* List of inventory items, group by alphabetical position
*/
public function orderedInventory(): array
public function orderedInventory(): Collection
{
$inventory = [];
foreach ($this->inventories as $item) {
$items = $this->inventories()->with(['image', 'item', 'item.entity', 'item.entity.image'])->get();
foreach ($items as $item) {
if ($item->item_id && empty($item->item)) {
continue;
}
$position = $item->position ?: __('entities/inventories.default_position');
$inventory[$position][] = $item;
}

return $inventory;
// We want the inventory ordered by position, then by item name
$collator = new Collator(app()->getLocale());
$positions = array_keys($inventory);
$collator->asort($positions);

$ordered = [];
foreach ($positions as $position) {
$items = new Collection($inventory[$position]);
$ordered[$position] = $items->sortBy(function ($model) {
/** @var Inventory $model */
return $model->itemName();
});
}

return new Collection($ordered);
}
}
Loading

0 comments on commit 6a3ca61

Please sign in to comment.