![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/rentpix.corals.io/Corals/modules/RentPix/Transformers/API/ |
<?php namespace Corals\Modules\RentPix\Transformers\API; use Corals\Foundation\Facades\Hashids; use Corals\Foundation\Transformers\APIBaseTransformer; use Corals\Modules\RentPix\Models\Inspection; use Illuminate\Support\Facades\URL; class InspectionTransformer extends APIBaseTransformer { /** * @param Inspection $inspection * @return array * * @throws \Throwable */ public function transform(Inspection $inspection) { $reservationPresenter = new ReservationPresenter(); $images = []; $media = $inspection->media->whereNull('driver_id'); foreach ($media as $img) { $images[] = [ 'id' => $img['id'], 'uuid' => $img['uuid'], 'driver_id' => $img['driver_id'], 'file_name' => $img['file_name'], 'mime_type' => $img['mime_type'], 'collection' => $img['collection_name'], 'description' => $img['custom_properties']['description'], 'url' => getMediaPublicURL($img), 'thumb' => getMediaPublicURL($img, 'thumb'), 'damage_tags' => $img['custom_properties']['tags'] ?? [], ]; } $driverPresenter = new DriverPresenter(); $drivers = []; foreach ($inspection->drivers as $driver) { $media = $inspection->media->where('driver_id', '=', $driver->id); $is_primary['is_primary'] = $driver->pivot->is_primary; $driverImages['images'] = []; foreach ($media as $img) { $driverImages['images'][] = [ 'id' => $img['id'], 'driver_id' => $img['driver_id'], 'file_name' => $img['file_name'], 'mime_type' => $img['mime_type'], 'collection' => $img['collection_name'], 'description' => $img['custom_properties']['description'], 'url' => getMediaPublicURL($img), 'thumb' => getMediaPublicURL($img, 'thumb'), 'damage_tags' => $img['custom_properties']['tags'] ?? [], ]; } $drivers[] = $driverPresenter->present($driver)['data'] + $is_primary + $driverImages; } $reservation = $reservationPresenter->present($inspection->reservation)['data']; $statusToggleAction = null; if (user()) { if (user()->can('completeInspection', $inspection)) { $statusToggleAction = 'complete'; } elseif (user()->can('openInspection', $inspection)) { $statusToggleAction = 'open'; } } $transformedArray = [ 'public_link' => URL::signedRoute('inspection-details', ['inspection' => Hashids::encode($inspection->id)]), 'id' => $inspection->id, 'code' => $inspection->code, 'type' => $inspection->type->value, 'status' => $inspection->status->value, 'inspected_at' => format_date_time($inspection->inspected_at), 'reservation' => $reservation, 'drivers' => $drivers, 'images' => $images, 'notes' => $this->getNotesList($inspection), 'status_toggle_action' => $statusToggleAction, 'is_driver_required' => $inspection->is_driver_required, 'inspection_title' => sprintf('%s-%s(%s)', data_get($reservation, 'unit.code'), data_get($reservation, 'unit.rate_code'), data_get($reservation, 'code') ), 'timestamp' => $inspection->created_at->getTimestampMs(), ]; return parent::transformResponse($transformedArray); } protected function getNotesList(Inspection $inspection) { $notes = []; foreach ($inspection->reservation->comments as $comment) { $notes[] = [ 'body' => $comment->body, 'agent' => !empty($comment->properties['agent']) ? $comment->properties['agent'] : (optional($comment->comment_author)->full_name ?? '-'), 'time_for_humans' => $comment->created_at->diffForHumans(), 'created_at' => $comment->created_at, ]; } return $notes; } }