このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
kikikan/backend/app/Http/Controllers/Api/MeetingsController.php

632 行
25 KiB
PHP
Raw 通常表示 履歴

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
2021-10-01 13:17:11 +09:00
use Illuminate\Support\Facades\Validator;
2021-10-15 10:37:47 +09:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
2021-11-01 00:22:21 +09:00
use Illuminate\Support\Facades\Storage;
use App\Models\Meeting;
use App\Models\MeetingImage;
use App\Models\MeetingApprovals;
use App\Models\Child;
use App\Models\Father;
2021-11-03 02:30:46 +09:00
use App\Models\FatherRelation;
class MeetingsController extends Controller {
2021-10-01 14:26:48 +09:00
public function register (Request $r) {
if (!isset($r->father_id)) {
2021-10-06 14:50:13 +09:00
return ['status_code' => 400, 'error_messages' => ['ミーティングの登録に失敗しました。']];
2021-10-01 14:26:48 +09:00
}
2021-11-10 22:13:09 +09:00
// ミームタイプ
Validator::extend('pdf_meme', function ($attribute, $value, $params, $validator) {
try {
return mime_content_type($value) == 'application/pdf';
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return false;
}
});
2021-10-01 14:26:48 +09:00
$validate = Validator::make($r->all(), [
2021-10-06 14:50:13 +09:00
'title' => 'required|max:100',
'text' => 'required|max:2000',
'memo' => 'max:2000',
2021-11-10 22:13:09 +09:00
'pdf' => 'pdf_meme'
2021-10-01 14:26:48 +09:00
]);
2021-10-01 13:17:11 +09:00
2021-10-01 14:26:48 +09:00
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
2021-10-01 13:17:11 +09:00
2021-10-06 14:50:13 +09:00
$insert = [
'father_id' => $r->father_id,
'title' => $r->title,
'text' => $r->text,
2021-11-10 22:13:09 +09:00
'memo' => $r->memo
2021-10-01 14:26:48 +09:00
];
2021-10-01 13:17:11 +09:00
2021-10-06 14:50:13 +09:00
try {
2021-11-10 22:13:09 +09:00
if (isset($r->pdf)) {
$filename = uniqid() . '.pdf';
$pdf = base64_decode(substr($r->pdf, strpos($r->pdf, ',') + 1));
$insert['pdf'] = '/storage/'.$filename;
Storage::disk('public')->put($filename, $pdf);
}
Meeting::create($insert);
2021-10-06 14:50:13 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => ['ミーティングの登録に失敗しました。']];
2021-10-01 14:26:48 +09:00
}
2021-10-01 13:17:11 +09:00
2021-10-06 14:50:13 +09:00
return ['status_code' => 200, 'success_messages' => ['ミーティングの登録に成功しました。']];
2021-10-01 13:17:11 +09:00
}
2021-10-01 14:26:48 +09:00
public function registerFavorite (Request $r) {
if (!isset($r->meeting_id) || !isset($r->is_favorite) || $r->is_favorite > 1) {
return ['status_code' => 400];
}
2021-10-01 13:17:11 +09:00
2021-10-01 14:26:48 +09:00
$update = ['is_favorite' => $r->is_favorite];
2021-10-07 13:51:53 +09:00
try {
2021-10-31 00:50:35 +09:00
Meeting::where('id', (int)$r->meeting_id)->update($update);
2021-10-07 13:51:53 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400];
2021-10-01 14:26:48 +09:00
}
2021-10-01 13:17:11 +09:00
2021-10-07 13:51:53 +09:00
return ['status_code' => 200];
}
2021-10-15 10:37:47 +09:00
public function search (Request $r) {
if (!isset($r->keyword)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
2021-10-15 10:37:47 +09:00
$meeting_select = ['id', 'title', 'text', 'updated_at'];
$child_select = ['image'];
$meeting_approvals_select = ['child_id', 'approval_at'];
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-15 10:37:47 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->orderBy('created_at', 'desc')->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
$result[] = $l;
2021-10-31 00:50:35 +09:00
if (null === ($result[$i]['approval'] = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->orderBy('approval_at', 'desc')->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
2021-10-15 10:37:47 +09:00
foreach ($result[$i]['approval'] as $j => $k) {
if (null === ($result[$i]['approval'][$j]['child'] = Child::select($child_select)->where('id', $k->child_id)->first())) {
2021-11-06 23:09:48 +09:00
$result[$i]['approval'][$j]['child'] = new \stdClass();
2021-10-15 10:37:47 +09:00
}
2021-10-07 13:51:53 +09:00
}
2021-10-15 10:37:47 +09:00
}
return ['status_code' => 200, 'params' => $result];
}
2021-10-01 14:26:48 +09:00
public function list () {
$result = [];
2021-10-15 10:37:47 +09:00
$meeting_select = ['id', 'title', 'text', 'updated_at'];
$child_select = ['image'];
$meeting_approvals_select = ['child_id', 'approval_at'];
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-15 10:37:47 +09:00
if (null === ($list = Meeting::select($meeting_select)->orderBy('created_at', 'desc')->get())) {
return ['status_code' => 400];
}
foreach ($list as $i => $l) {
$result[] = $l;
2021-10-31 00:50:35 +09:00
if (null === ($result[$i]['approval'] = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->orderBy('approval_at', 'desc')->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
2021-10-01 14:26:48 +09:00
}
2021-10-15 10:37:47 +09:00
foreach ($result[$i]['approval'] as $j => $k) {
2021-10-31 00:50:35 +09:00
if (null === ($result[$i]['approval'][$j]['child'] = Child::select($child_select)->where('id', (int)$k->child_id)->first())) {
2021-10-15 10:37:47 +09:00
return ['status_code' => 400];
}
}
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-10-01 14:26:48 +09:00
public function listOfApprovalOfChild (Request $r) {
if (!isset($r->child_id)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at'];
$meeting_images_select = ['image'];
$father_select = ['image', 'company'];
$meeting_approvals_select = ['approval_at'];
// 取得に成功
2021-11-05 18:01:37 +09:00
if (null === ($approval = MeetingApprovals::select('meeting_id')->where('child_id', (int)$r->child_id)->whereNotNull('approval_at')->orderBy('updated_at', 'desc')->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
foreach ($approval as $a) {
2021-11-06 19:11:25 +09:00
if (null !== ($list = Meeting::select($meeting_select)->where('id', (int)$a->meeting_id)->get())) {
foreach ($list as $i => $l) {
if (null === ($fr = FatherRelation::select('id')->where('father_id', (int)$l->father_id)->where('child_id', (int)$r->child_id)->first())) {
continue;
}
if (null === ($l->father = Father::select($father_select)->where('id', (int)$l->father_id)->first())) {
$l->father = new \stdClass();
}
if (null === ($l->meeting_images = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$l->id)->get())) {
$l->meeting_images = [];
}
2021-11-06 23:09:48 +09:00
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->first())) {
$l->approval = new \stdClass();
2021-11-06 19:11:25 +09:00
}
$result[] = $l;
2021-10-01 14:26:48 +09:00
}
}
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-10-01 14:26:48 +09:00
public function listOfNonApprovalOfChild (Request $r) {
if (!isset($r->child_id)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at'];
$meeting_images_select = ['image'];
$father_select = ['image', 'company'];
$meeting_approvals_select = ['approval_at'];
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($approval = MeetingApprovals::select('meeting_id')->where('child_id', (int)$r->child_id)->whereNull('approval_at')->orderBy('approval_at', 'asc')->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
foreach ($approval as $a) {
2021-11-06 19:11:25 +09:00
if (null !== ($list = Meeting::select($meeting_select)->where('id', (int)$a->meeting_id)->get())) {
foreach ($list as $i => $l) {
if (null === ($fr = FatherRelation::select('id')->where('father_id', (int)$l->father_id)->where('child_id', (int)$r->child_id)->first())) {
continue;
}
if (null === ($l->father = Father::select($father_select)->where('id', (int)$l->father_id)->first())) {
$l->father = new \stdClass();
}
if (null === ($l->meeting_images = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$l->id)->get())) {
$l->meeting_images = [];
}
2021-11-06 23:09:48 +09:00
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->whereNull('approval_at')->where('meeting_id', (int)$l->id)->first())) {
$l->approval = new \stdClass();
2021-11-06 19:11:25 +09:00
}
$result[] = $l;
2021-10-01 14:26:48 +09:00
}
}
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-10-01 14:26:48 +09:00
public function listOfCompleteOfFather (Request $r) {
if (!isset($r->father_id)) {
return ['status_code' => 400];
}
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at', 'is_favorite'];
2021-10-01 14:26:48 +09:00
$meeting_approvals_select = ['child_id', 'approval_at'];
$child_select = ['image'];
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)$r->father_id)->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
foreach ($list as $i => $l) {
2021-11-13 13:16:03 +09:00
if (null !== ($l->approval = MeetingApprovals::select($meeting_approvals_select)->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->get())) {
foreach ($l->approval as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = [];
}
2021-10-01 14:26:48 +09:00
}
}
2021-11-13 13:16:03 +09:00
2021-11-10 22:13:09 +09:00
$result[] = $l;
2021-10-01 14:26:48 +09:00
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-10-01 14:26:48 +09:00
public function listOfIncompleteOfFather (Request $r) {
if (!isset($r->father_id)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at', 'is_favorite'];
2021-10-01 14:26:48 +09:00
$meeting_approvals_select = ['child_id', 'approval_at'];
$child_select = ['image'];
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)$r->father_id)->get())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-13 13:16:03 +09:00
if (null !== ($l->approval = MeetingApprovals::select($meeting_approvals_select)->whereNull('approval_at')->where('meeting_id', (int)$l->id)->get())) {
foreach ($l->approval as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = [];
}
2021-11-10 22:13:09 +09:00
}
2021-10-07 13:51:53 +09:00
}
2021-10-01 14:26:48 +09:00
2021-11-10 22:13:09 +09:00
$result[] = $l;
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-11-04 13:28:58 +09:00
public function listOfFavoriteOfFather (Request $r) {
2021-10-01 14:26:48 +09:00
if (!isset($r->father_id)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at', 'is_favorite'];
$meeting_approvals_select = ['child_id', 'approval_at'];
$child_select = ['image'];
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)$r->father_id)->where('is_favorite', 1)->get())) {
2021-11-05 17:07:47 +09:00
$list = [];
2021-10-07 13:51:53 +09:00
}
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-06 23:09:48 +09:00
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->orderBy('updated_at', 'desc')->get())) {
$l->approval = [];
2021-10-01 14:26:48 +09:00
}
2021-11-06 23:09:48 +09:00
foreach ($l->approval as $ii => $ra) {
if (null === ($l->approval[$ii]['child'] = Child::select($child_select)->where('id', (int)$l->approval[$ii]['child_id'])->first())) {
$l->approval[$ii]['child'] = [];
2021-10-07 13:51:53 +09:00
}
}
2021-11-05 17:07:47 +09:00
$result[] = $l;
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-11-04 13:28:58 +09:00
public function listOfNonFavoriteOfFather (Request $r) {
2021-10-01 14:26:48 +09:00
if (!isset($r->father_id)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at', 'is_favorite'];
$meeting_approvals_select = ['child_id', 'approval_at'];
$child_select = ['image'];
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)$r->father_id)->where('is_favorite', 0)->get())) {
2021-11-05 17:07:47 +09:00
$list = [];
2021-10-07 13:51:53 +09:00
}
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-06 23:09:48 +09:00
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->orderBy('updated_at', 'desc')->get())) {
$l->approval = [];
2021-10-01 14:26:48 +09:00
}
2021-11-06 23:09:48 +09:00
foreach ($l->approval as $ii => $ra) {
if (null === ($l->approval[$ii]['child'] = Child::select($child_select)->where('id', (int)$l->approval[$ii]['child_id'])->first())) {
$l->approval[$ii]['child'] = [];
2021-10-07 13:51:53 +09:00
}
}
2021-11-05 17:07:47 +09:00
$result[] = $l;
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
}
2021-10-01 14:26:48 +09:00
public function searchOfApprovalOfChild (Request $r) {
if (!isset($r->child_id) || !isset($r->keyword)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at'];
$father_select = ['image', 'company'];
2021-11-08 21:53:56 +09:00
$meeting_approvals_select = ['approval_at'];
2021-09-30 14:32:15 +09:00
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-07 13:51:53 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->get())) {
2021-11-03 20:46:48 +09:00
$list = [];
2021-10-07 13:51:53 +09:00
}
2021-09-30 14:32:15 +09:00
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-08 19:41:06 +09:00
if (null === (FatherRelation::select('id')->where('father_id', (int)$l->father_id)->where('child_id', (int)$r->child_id)->first())) {
continue;
}
2021-11-08 21:02:25 +09:00
if (null === ($ma = MeetingApprovals::select('id')->where('child_id', (int)$r->child_id)->where('meeting_id', (int)$l->id)->whereNotNull('approval_at')->first())) {
continue;
}
2021-11-06 18:24:18 +09:00
if (null === ($l->father = Father::select($father_select)->where('id', (int)$l->father_id)->first())) {
$l->father = new \stdClass();
2021-10-07 13:51:53 +09:00
}
2021-11-06 23:17:15 +09:00
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('child_id', (int)$r->child_id)->whereNotNull('approval_at')->first())) {
2021-11-08 19:41:06 +09:00
$l->approval = new \stdClass();
2021-10-07 13:51:53 +09:00
}
2021-11-05 17:07:47 +09:00
$result[] = $l;
2021-10-01 14:26:48 +09:00
}
2021-09-30 14:32:15 +09:00
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
2021-09-30 14:32:15 +09:00
}
2021-10-01 14:26:48 +09:00
public function searchOfNonApprovalOfChild (Request $r) {
if (!isset($r->child_id) || !isset($r->keyword)) {
return ['status_code' => 400];
}
2021-09-30 14:32:15 +09:00
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at'];
$father_select = ['image', 'company'];
2021-11-08 21:53:56 +09:00
$meeting_approvals_select = ['approval_at'];
2021-09-30 14:32:15 +09:00
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-07 13:51:53 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->get())) {
2021-11-03 20:46:48 +09:00
$list = [];
2021-10-07 13:51:53 +09:00
}
2021-09-30 14:32:15 +09:00
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-08 19:41:06 +09:00
if (null === (FatherRelation::select('id')->where('father_id', (int)$l->father_id)->where('child_id', (int)$r->child_id)->first())) {
continue;
}
2021-11-08 21:02:25 +09:00
if (null === ($ma = MeetingApprovals::select('id')->where('child_id', (int)$r->child_id)->where('meeting_id', (int)$l->id)->whereNull('approval_at')->first())) {
continue;
}
2021-11-06 18:24:18 +09:00
if (null === ($l->father = Father::select($father_select)->where('id', (int)$l->father_id)->first())) {
$l->father = new \stdClass();
2021-10-07 13:51:53 +09:00
}
2021-11-06 23:17:15 +09:00
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('child_id', (int)$r->child_id)->whereNull('approval_at')->first())) {
$l->approval = new \stdClass();
2021-10-07 13:51:53 +09:00
}
2021-11-05 17:07:47 +09:00
$result[] = $l;
2021-10-01 14:26:48 +09:00
}
2021-09-30 14:32:15 +09:00
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
2021-09-30 14:32:15 +09:00
}
2021-10-01 14:26:48 +09:00
public function searchOfCompleteofFather (Request $r) {
if (!isset($r->father_id) || !isset($r->keyword)) {
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at'];
$meeting_images_select = ['image'];
$meeting_approvals_select = ['approval_at', 'child_id'];
$child_select = ['image'];
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)$r->father_id)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->get())) {
2021-11-05 17:07:47 +09:00
$list = [];
2021-10-07 13:51:53 +09:00
}
2021-10-01 14:26:48 +09:00
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-05 17:07:47 +09:00
if (null === ($l->meeting_image = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$l->id)->get())) {
$l->meeting_image = [];
2021-10-07 13:51:53 +09:00
}
2021-11-10 22:13:09 +09:00
if (null !== ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->whereNull('approval_at')->first())) {
if (null === ($l->approval->child = Child::select($child_select)->where('id', (int)$l->approval->child_id)->first())) {
$l->approval->child = new \stdClass();
2021-10-07 13:51:53 +09:00
}
}
2021-11-10 22:13:09 +09:00
else {
$l->approval = new \stdClass();
$l->approval->child = new \stdClass();
}
2021-11-05 17:07:47 +09:00
$result[] = $l;
2021-09-30 14:32:15 +09:00
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
2021-09-30 14:32:15 +09:00
}
2021-10-01 14:26:48 +09:00
public function searchOfIncompleteofFather (Request $r) {
if (!isset($r->father_id) || !isset($r->keyword)) {
return ['status_code' => 400];
}
2021-09-30 14:32:15 +09:00
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at', 'is_favorite'];
2021-10-01 14:26:48 +09:00
$meeting_images_select = ['image'];
$meeting_approvals_select = ['approval_at', 'child_id'];
$child_select = ['image'];
// 取得に成功
2021-10-31 00:50:35 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)$r->father_id)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->get())) {
2021-11-05 17:07:47 +09:00
$list = [];
2021-10-07 13:51:53 +09:00
}
2021-10-01 14:26:48 +09:00
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-05 17:07:47 +09:00
if (null === ($l->meeting_image = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$l->id)->get())) {
$l->meeting_image = [];
2021-10-07 13:51:53 +09:00
}
2021-11-10 22:13:09 +09:00
if (null !== ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->whereNotNull('approval_at')->first())) {
if (null === ($l->approval->child = Child::select($child_select)->where('id', (int)$l->approval->child_id)->first())) {
$l->approval->child = new \stdClass();
2021-10-07 13:51:53 +09:00
}
}
2021-11-10 22:13:09 +09:00
else {
$l->approval = new \stdClass();
$l->approval->child = new \stdClass();
}
2021-11-05 17:07:47 +09:00
$result[] = $l;
2021-09-30 14:32:15 +09:00
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
2021-09-30 14:32:15 +09:00
}
2021-10-01 14:26:48 +09:00
public function detail (Request $r, $meeting_id) {
2021-10-15 10:37:47 +09:00
if (!isset($meeting_id)) {
2021-10-01 14:26:48 +09:00
return ['status_code' => 400];
}
2021-09-30 14:32:15 +09:00
2021-10-01 14:26:48 +09:00
$result = [];
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'pdf', 'updated_at', 'is_favorite'];
2021-10-31 22:56:36 +09:00
$meeting_images_select = ['id', 'image'];
2021-10-01 14:26:48 +09:00
$meeting_approvals_select = ['approval_at', 'child_id'];
$father_select = ['image', 'company', 'tel'];
$child_select = ['id', 'image', 'last_name', 'first_name', 'tel'];
2021-11-03 02:30:46 +09:00
$all_child_select = ['id as child_id', 'last_name', 'first_name'];
2021-10-01 14:26:48 +09:00
// 取得に成功
2021-10-31 13:38:12 +09:00
if (null === ($result = Meeting::select($meeting_select)->where('id', (int)$meeting_id)->first())) {
2021-10-07 13:51:53 +09:00
return ['status_code' => 400];
}
2021-10-01 14:26:48 +09:00
2021-10-31 01:42:13 +09:00
if (null === ($result->meeting_image = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$result->id)->get())) {
2021-11-03 02:30:46 +09:00
$result->meeting_image = [];
2021-10-31 01:42:13 +09:00
}
2021-11-10 15:59:55 +09:00
if (request()->route()->action['as'] != 'mdc') {
if (null === ($result->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$result->id)->get())) {
$result->approval = [];
}
}
else {
if (null === ($result->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$result->id)->first())) {
$result->approval = new \stdClass();
}
2021-11-03 02:30:46 +09:00
}
2021-11-05 19:34:36 +09:00
if (request()->route()->action['as'] == 'mdc') {
if (null === ($result->father = Father::select($father_select)->where('id', (int)$result->father_id)->first())) {
$result->father = new \stdClass();
}
}
2021-11-05 19:34:36 +09:00
if (request()->route()->action['as'] != 'mdc') {
$result->children = [];
if (null !== ($rel = FatherRelation::select('child_id')->where('father_id', (int)$result->father_id)->first())) {
if (null === ($result->children = Child::select($all_child_select)->where('id', $rel->child_id)->get()->toArray())) {
$result->children = [];
}
2021-11-03 02:30:46 +09:00
}
2021-10-01 14:26:48 +09:00
foreach ($result->approval as $i => $a) {
if (null === ($result->approval[$i]['child'] = Child::select($child_select)->where('id', (int)$a->child_id)->first())) {
2021-11-06 23:09:48 +09:00
$result->approval[$i]['child'] = new \stdClass();
}
2021-10-07 13:51:53 +09:00
}
2021-09-30 14:32:15 +09:00
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200, 'params' => $result];
2021-09-30 14:32:15 +09:00
}
2021-10-15 10:37:47 +09:00
public function update (Request $r, $meeting_id) {
if (!isset($meeting_id)) {
return ['status_code' => 400, 'error_messages' => ['ミーティングの登録に失敗しました。']];
}
$validate = Validator::make($r->all(), [
'title' => 'required|max:100',
'text' => 'required|max:2000',
2021-10-28 17:45:37 +09:00
'memo' => 'nullable|max:2000',
2021-10-15 10:37:47 +09:00
]);
2021-11-01 01:08:02 +09:00
// ミームタイプ
Validator::extend('pdf_meme', function ($attribute, $value, $params, $validator) {
try {
return mime_content_type($value) == 'application/pdf';
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return false;
}
});
2021-10-15 10:37:47 +09:00
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
2021-10-01 14:26:48 +09:00
}
2021-10-15 10:37:47 +09:00
$update = [
'title' => $r->title,
'text' => $r->text,
];
if (isset($r->memo)) $update['memo'] = $r->memo;
try {
2021-11-01 21:26:37 +09:00
// リクエストでPDFがある場合
2021-10-31 23:50:54 +09:00
if (isset($r->pdf)) {
$filename = uniqid() . '.pdf';
2021-11-01 01:08:02 +09:00
2021-11-01 21:26:37 +09:00
// DBにミーティングがある場合
2021-11-01 15:18:40 +09:00
if ($chk = Meeting::select('pdf')->where('id', (int)$meeting_id)->first()) {
2021-11-01 21:26:37 +09:00
// base64の場合(ファイルパスだったら、スキップ)
2021-11-01 22:43:32 +09:00
if (!preg_match('/\/storage\/(.*).pdf/', $r->pdf)) {
2021-11-01 21:26:37 +09:00
// もう存在しているPDFのファイル名からパスを外します。
$opdf = str_replace('/storage/', '', $chk->pdf);
// PDFのbase64をGETします。
$pdf = base64_decode(substr($r->pdf, strpos($r->pdf, ',') + 1));
// 既にPDFが存在する場合(なければ、スキップ)
2021-11-01 16:23:03 +09:00
if (Storage::disk('public')->exists($opdf)) {
2021-11-01 23:17:07 +09:00
// 既に存在しているPDFとアップロードしているPDFを比べてみます。異なる場合、存在しているPDFを削除します。
2021-11-01 21:26:37 +09:00
if (strcmp(Storage::disk('public')->get($opdf), $pdf) !== 0) {
Storage::disk('public')->delete($opdf);
}
2021-11-01 01:31:14 +09:00
}
2021-11-01 23:15:48 +09:00
$update['pdf'] = '/storage/'.$filename;
Storage::disk('public')->put($filename, $pdf);
2021-11-01 01:08:02 +09:00
}
}
2021-11-01 21:26:37 +09:00
// なければ、そのままストレージに保存します。
2021-11-01 16:06:39 +09:00
else {
2021-11-01 16:23:03 +09:00
$update['pdf'] = '/storage/'.$filename;
2021-11-01 16:06:39 +09:00
Storage::disk('public')->put($filename, $pdf);
}
2021-10-31 23:50:54 +09:00
}
2021-11-01 21:26:37 +09:00
// データベースに保存します。
2021-10-31 00:50:35 +09:00
Meeting::where('id', (int)$meeting_id)->update($update);
2021-10-15 10:37:47 +09:00
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return ['status_code' => 400];
}
return ['status_code' => 200];
2021-10-01 14:26:48 +09:00
}
2021-10-01 14:26:48 +09:00
public function delete ($meeting_id) {
2021-10-07 13:51:53 +09:00
try {
2021-10-28 17:45:37 +09:00
Meeting::where('id', (int)$meeting_id)->delete();
2021-10-07 13:51:53 +09:00
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return ['status_code' => 400];
2021-10-01 14:26:48 +09:00
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200];
2021-10-01 14:26:48 +09:00
}
}