public static function stripMedia($request) { $result = []; $regex = '/^media_id_/'; $request = $request instanceof Request ? $request->all() : (is_array($request) ? $request : []); foreach (array_keys($request) as $key) { switch ($key) { case preg_match($regex, $key) === 1: $media_id = preg_replace($regex, '', $key); if (intval($request[$key]) !== 1) { Media::destroy($media_id); } elseif (($media = Media::find($media_id)) instanceof Media) { $result[] = $media; } break; case $key === 'media_new' && ($file = $request[$key]) instanceof UploadedFile && $file->isValid(): $fileName = $file->getClientOriginalName(); if (pathinfo($fileName, PATHINFO_FILENAME) === '') { $fileName = 'file' . $fileName; } if (pathinfo($fileName, PATHINFO_EXTENSION) === '') { $fileName .= '.' . (trim($file->guessClientExtension()) !== '' ? $file->guessClientExtension() : $file->guessExtension()); } $result[] = Media::store(file_get_contents($file->getPathName()), $file->getMimeType(), $fileName); } } return $result; }
public static function stripPropertyDetailOptions(Request $request) { $result = []; $regex = ['existing' => '/^issue_detail_/', 'new' => '/^issue_detail_new_/']; $request = $request instanceof Request ? $request->all() : (is_array($request) ? $request : []); foreach (array_keys($request) as $key) { $striped_key = preg_replace($regex['existing'], '', $key); preg_match('/\\d+/', $striped_key, $detail_id); if (sizeof($detail_id) < 1) { continue; } $detail_id = intval($detail_id[0]) === 0 ? 'new' : intval($detail_id[0]); if (!isset($result[$detail_id])) { $result[$detail_id] = ['media' => []]; } $field = preg_replace('/\\d_/', '', $striped_key); if (preg_match('/^(media_id_|media_new)/', $field) === 1) { $media = Media::find($media_id = intval(preg_replace('/^media_id_/', '', $field))); if (($file = $request[$key]) instanceof UploadedFile && $file->isValid()) { $fileName = $file->getClientOriginalName(); if (pathinfo($fileName, PATHINFO_FILENAME) === '') { $fileName = 'file' . $fileName; } if (pathinfo($fileName, PATHINFO_EXTENSION) === '') { $fileName .= '.' . (trim($file->guessClientExtension()) !== '' ? $file->guessClientExtension() : $file->guessExtension()); } $media = Media::store(file_get_contents($file->getPathName()), $file->getMimeType(), $fileName); } if ($media instanceof Media && boolval($request[$key]) === true) { $result[$detail_id]['media'][$media->id] = $media; } } else { $result[$detail_id][$field] = $request[$key]; } } // endforeach return $result; }
public function media() { return Media::whereIn('id', json_decode($this->media_ids)); }
$factory->define(Rental::class, function (Faker\Generator $faker) { $array = ['property_id' => $faker->randomElement(Property::all()->all())->id, 'dailyAmount' => $faker->randomFloat(4, 500 / 30, 2000 / 30), 'from' => random_int(0, 1) === 0 ? null : $faker->dateTimeBetween('-10 years'), 'to' => random_int(0, 1) === 0 ? null : $faker->dateTimeBetween('now', '+10 years'), 'media_ids' => []]; for ($i = 0; $i < random_int(1, 10); $i++) { $array['media_ids'][] = $faker->randomElement(Media::all()->all())->id; } $array['media_ids'] = json_encode($array['media_ids']); return $array; }); $factory->define(AdminAccess::class, function (Faker\Generator $faker) { do { $array = ['rental_id' => $faker->randomElement(Rental::all()->all())->id, 'role_id' => $faker->randomElement(Role::all()->all())->id, 'canManage' => $faker->boolean(), 'canManage' => $faker->boolean(), 'canIssue' => $faker->boolean(), 'canDocument' => $faker->boolean(), 'canStatement' => $faker->boolean(), 'canMessage' => $faker->boolean()]; } while (AdminAccess::where(['rental_id' => $array['rental_id'], 'role_id' => $array['role_id']])->get()->count() > 0); return $array; }); $factory->define(Issue::class, function (Faker\Generator $faker) { return ['requester_user_id' => $faker->randomElement(User::all()->all())->id, 'rental_id' => $faker->randomElement(Rental::all()->all())->id, 'status' => $faker->words(random_int(1, 2), true)]; }); $factory->define(IssueDetail::class, function (Faker\Generator $faker) { $array = ['issue_id' => $faker->randomElement(Issue::all()->all())->id, 'content' => $faker->sentences(random_int(1, 200), true), 'type' => $faker->words(random_int(1, 2), true), '3rdParty' => $faker->url, 'priority' => $faker->numberBetween(0, 5), 'media_ids' => []]; for ($i = 0; $i < random_int(1, 10); $i++) { $array['media_ids'][] = $faker->randomElement(Media::all()->all())->id; } $array['media_ids'] = json_encode($array['media_ids']); return $array; }); $factory->define(IssueProgress::class, function (Faker\Generator $faker) { return ['issue_id' => $faker->randomElement(Issue::all()->all())->id, 'content' => $faker->sentences(random_int(1, 200), true)]; }); $factory->define(RentalUser::class, function (Faker\Generator $faker) { return ['user_id' => $faker->randomElement(User::all()->all())->id, 'role_id' => $faker->randomElement(Role::all()->all())->id, 'rental_id' => $faker->randomElement(Rental::all()->all())->id]; });