diff --git a/app/Http/Controllers/Admin/Characters/CharacterController.php b/app/Http/Controllers/Admin/Characters/CharacterController.php index cd3fe974bb..6d1efea157 100644 --- a/app/Http/Controllers/Admin/Characters/CharacterController.php +++ b/app/Http/Controllers/Admin/Characters/CharacterController.php @@ -14,7 +14,6 @@ use App\Models\Species\Subtype; use App\Models\Trade; use App\Models\User\User; -use App\Models\User\UserItem; use App\Services\CharacterManager; use App\Services\TradeManager; use Illuminate\Http\Request; @@ -671,30 +670,12 @@ public function getTradeQueue($type) { $openTransfersQueue = Settings::get('open_transfers_queue'); - $stacks = []; - foreach ($trades->get() as $trade) { - foreach ($trade->data as $side=> $assets) { - if (isset($assets['user_items'])) { - $user_items = UserItem::with('item')->find(array_keys($assets['user_items'])); - $items = []; - foreach ($assets['user_items'] as $id=> $quantity) { - $user_item = $user_items->find($id); - $user_item['quantity'] = $quantity; - array_push($items, $user_item); - } - $items = collect($items)->groupBy('item_id'); - $stacks[$trade->id][$side] = $items; - } - } - } - return view('admin.masterlist.character_trades', [ 'trades' => $trades->orderBy('id', 'DESC')->paginate(20), 'tradesQueue' => Settings::get('open_transfers_queue'), 'openTransfersQueue' => $openTransfersQueue, 'transferCount' => $openTransfersQueue ? CharacterTransfer::active()->where('is_approved', 0)->count() : 0, 'tradeCount' => $openTransfersQueue ? Trade::where('status', 'Pending')->count() : 0, - 'stacks' => $stacks, ]); } diff --git a/app/Http/Controllers/Users/TradeController.php b/app/Http/Controllers/Users/TradeController.php index f93fe8b66d..d7ce4f73e2 100644 --- a/app/Http/Controllers/Users/TradeController.php +++ b/app/Http/Controllers/Users/TradeController.php @@ -36,26 +36,8 @@ public function getIndex($status = 'open') { $query->where('recipient_id', Auth::user()->id)->orWhere('sender_id', Auth::user()->id); })->where('status', ucfirst($status))->orderBy('id', 'DESC'); - $stacks = []; - foreach ($trades->get() as $trade) { - foreach ($trade->data as $side=> $assets) { - if (isset($assets['user_items'])) { - $user_items = UserItem::with('item')->find(array_keys($assets['user_items'])); - $items = []; - foreach ($assets['user_items'] as $id=> $quantity) { - $user_item = $user_items->find($id); - $user_item['quantity'] = $quantity; - array_push($items, $user_item); - } - $items = collect($items)->groupBy('item_id'); - $stacks[$trade->id][$side] = $items; - } - } - } - return view('home.trades.index', [ 'trades' => $trades->paginate(20), - 'stacks' => $stacks, ]); } diff --git a/app/Models/Trade.php b/app/Models/Trade.php index 369070ed6b..b83d01ff3a 100644 --- a/app/Models/Trade.php +++ b/app/Models/Trade.php @@ -5,6 +5,7 @@ use App\Facades\Settings; use App\Models\Character\Character; use App\Models\User\User; +use App\Models\User\UserItem; class Trade extends Model { /** @@ -24,6 +25,16 @@ class Trade extends Model { * @var string */ protected $table = 'trades'; + + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'data' => 'array', + ]; + /** * Whether the model contains timestamps to be saved and updated. * @@ -107,21 +118,34 @@ public function getIsConfirmableAttribute() { } /** - * Get the data attribute as an associative array. + * Gets the URL of the trade. * - * @return array + * @return string */ - public function getDataAttribute() { - return json_decode($this->attributes['data'], true); + public function getUrlAttribute() { + return url('trades/'.$this->id); } /** - * Gets the URL of the trade. + * Gets the stacks of the trade keyed by sender and recipient. * - * @return string + * @return array */ - public function getUrlAttribute() { - return url('trades/'.$this->id); + public function getStacksAttribute() { + $stacks = []; + foreach ($this->data as $side => $assets) { + if (isset($assets['user_items'])) { + $user_items = UserItem::with('item')->find(array_keys($assets['user_items'])); + $items = $user_items->map(function ($user_item) use ($assets) { + $user_item['quantity'] = $assets['user_items'][$user_item->id]; + + return $user_item; + }); + $stacks[$side] = $items->groupBy('item_id'); + } + } + + return $stacks; } /********************************************************************************************** diff --git a/app/Services/TradeManager.php b/app/Services/TradeManager.php index fd57e0e058..c53a350385 100644 --- a/app/Services/TradeManager.php +++ b/app/Services/TradeManager.php @@ -56,7 +56,9 @@ public function createTrade($data, $user) { ]); if ($assetData = $this->handleTradeAssets($trade, $data, $user)) { - $trade->data = json_encode(['sender' => getDataReadyAssets($assetData['sender'])]); + $trade->data = [ + 'sender' => getDataReadyAssets($assetData['sender']), + ]; $trade->save(); // send a notification @@ -103,7 +105,7 @@ public function editTrade($data, $user) { $tradeData = $trade->data; $isSender = ($trade->sender_id == $user->id); $tradeData[$isSender ? 'sender' : 'recipient'] = getDataReadyAssets($assetData[$isSender ? 'sender' : 'recipient']); - $trade->data = json_encode($tradeData); + $trade->data = $tradeData; $trade->{'is_'.($isSender ? 'sender' : 'recipient').'_confirmed'} = 0; $trade->{'is_'.($isSender ? 'recipient' : 'sender').'_trade_confirmed'} = 0; $trade->save(); diff --git a/resources/views/home/trades/_trade.blade.php b/resources/views/home/trades/_trade.blade.php index 0fe57621c8..aa8ca666b2 100644 --- a/resources/views/home/trades/_trade.blade.php +++ b/resources/views/home/trades/_trade.blade.php @@ -1,8 +1,12 @@
-

{{ $trade->status }}Trade (#{{ $trade->id }})

+

+ {{ $trade->status }} + Trade (#{{ $trade->id }}) +

+ @if ($trade->staff) + Processed by {!! $trade->staff->displayName !!} + @endif
@if ($trade->comments) @@ -29,7 +33,7 @@ class="float-right badge badge-{{ $trade->status == 'Pending' || $trade->status 'user' => $trade->sender, 'data' => isset($trade->data['sender']) ? parseAssetData($trade->data['sender']) : null, 'trade' => $trade, - 'stacks' => isset($stacks[$trade->id]['sender']) ? $stacks[$trade->id]['sender'] : null, + 'stacks' => isset($trade->stacks['sender']) ? $trade->stacks['sender'] : [], ])
@@ -51,7 +55,7 @@ class="float-right badge badge-{{ $trade->status == 'Pending' || $trade->status 'user' => $trade->recipient, 'data' => isset($trade->data['recipient']) ? parseAssetData($trade->data['recipient']) : null, 'trade' => $trade, - 'stacks' => isset($stacks[$trade->id]['recipient']) ? $stacks[$trade->id]['recipient'] : null, + 'stacks' => isset($trade->stacks['recipient']) ? $trade->stacks['recipient'] : [], ])