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

767 行
30 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-26 16:15:22 +09:00
if (count(Storage::disk('private')->files('/')) >= (int)env('MAX_FILES')) {
Log::critical('ストレージの限界を超えています。'.env('MAX_FILES').'個ファイルまで保存可能ですので、不要なファイルを削除して下さい。');
return ['status_code' => 400, 'error_messages' => ['親の更新に失敗しました。']];
}
2021-11-17 13:09:21 +09:00
if (isset($r->image)) {
$r->image = json_decode($r->image);
}
2021-11-16 21:31:26 +09:00
// ファイルサイズは10MiB以内
Validator::extend('image_size', function ($attribute, $value, $params, $validator) {
return $this->imagesizemulti($value);
2021-11-16 21:31:26 +09:00
});
// ミームタイプ
2021-11-17 21:23:29 +09:00
//// 画像
2021-11-17 23:51:07 +09:00
Validator::extend('image_meme', function ($attribute, $value, $params, $validator) {
return $this->imagememeorfile($value);
2021-11-17 23:51:07 +09:00
});
2021-11-16 21:31:26 +09:00
2021-11-17 21:23:29 +09:00
//// PDF
2021-11-17 23:51:07 +09:00
Validator::extend('pdf_meme', function ($attribute, $value, $params, $validator) {
return $this->pdfmeme($value);
2021-11-17 23:51:07 +09:00
});
2021-11-10 22:13:09 +09:00
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-16 21:31:26 +09:00
'pdf' => 'pdf_meme',
2021-11-17 13:09:21 +09:00
'image' => 'image_size|image_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
$filename = '';
$fnames = [];
$meeting = 0;
2021-10-06 14:50:13 +09:00
try {
2021-11-10 22:13:09 +09:00
if (isset($r->pdf)) {
$filename = $this->uuidv4() . '.pdf';
$insert['pdf'] = '/files/'.$filename;
2021-11-18 00:31:39 +09:00
if (substr($r->pdf, -4) != '.pdf') {
$pdf = base64_decode(substr($r->pdf, strpos($r->pdf, ',') + 1));
Storage::disk('private')->put($filename, $pdf);
2021-11-18 00:31:39 +09:00
}
else {
$insert['pdf'] = $r->pdf;
}
2021-11-10 22:13:09 +09:00
}
2021-11-25 11:11:21 +09:00
$meeting = Meeting::create($insert);
$meeting = $meeting->id;
2021-11-18 00:31:39 +09:00
if (isset($r->image)) {
foreach ($r->image as $img) {
if (substr($img, -5) != '.jpeg' && substr($img, -4) != '.jpg' && substr($img, -4) != '.png' && substr($img, -4) != '.gif') {
$ext = explode('/', mime_content_type($img))[1];
$fname = $this->uuidv4() . '.'.$ext;
$fnames[] = $fname;
2021-11-18 00:31:39 +09:00
$image = base64_decode(substr($img, strpos($img, ',') + 1));
Storage::disk('private')->put($fname, $image);
2021-11-18 00:31:39 +09:00
$imgname = '/files/'.$fname;
2021-11-18 00:31:39 +09:00
}
else {
$imgname = $img;
2021-11-18 00:31:39 +09:00
}
2021-11-17 22:53:07 +09:00
$insert_image = [
'meeting_id' => (int)$meeting,
'image' => $imgname,
2021-11-17 22:53:07 +09:00
];
2021-11-18 00:31:39 +09:00
2021-11-17 22:53:07 +09:00
MeetingImage::create($insert_image);
}
2021-11-16 21:31:26 +09:00
}
2021-11-17 13:09:21 +09:00
foreach (json_decode($r->children) as $child) {
2021-11-16 22:07:18 +09:00
$insert_approval = [
'child_id' => $child,
'meeting_id' => (int)$meeting,
2021-11-17 20:44:35 +09:00
'approval_at' => null,
2021-11-16 22:07:18 +09:00
];
MeetingApprovals::create($insert_approval);
}
$params = ['meeting_id' => $meeting];
2021-10-06 14:50:13 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
if (!is_null($meeting) && $meeting != 0) {
if (isset($r->pdf)) {
Storage::disk('private')->delete($filename);
}
if (isset($r->image)) {
foreach ($fnames as $f) {
Storage::disk('private')->delete($f);
}
}
}
2021-10-06 14:50:13 +09:00
return ['status_code' => 400, 'error_messages' => ['ミーティングの登録に失敗しました。']];
2021-10-01 14:26:48 +09:00
}
2021-10-01 13:17:11 +09:00
2021-11-16 20:24:19 +09:00
return ['status_code' => 200, 'success_messages' => ['ミーティングの登録に成功しました。'], 'params' => $params];
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-11-18 01:25:31 +09:00
return ['status_code' => 200, 'params' => ['meeting_id' => (int)$r->meeting_id]];
}
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];
}
2021-11-16 02:32:48 +09:00
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-16 18:02:03 +09:00
if (null === ($l->approvals = MeetingApprovals::select($meeting_approvals_select)->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->get())) {
continue;
}
2021-11-13 13:16:03 +09:00
2021-11-16 18:02:03 +09:00
if (count($l->approvals->toArray()) != MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->count()) {
continue;
}
foreach ($l->approvals as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = new \stdClass();
}
if (!in_array($l, $result)) {
$result[] = $l;
2021-11-15 12:42:00 +09:00
}
2021-11-13 14:38:02 +09:00
}
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-16 18:02:03 +09:00
if (null === ($l->approvals = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->get())) {
continue;
}
2021-10-01 14:26:48 +09:00
2021-11-17 18:36:56 +09:00
$cnt = MeetingApprovals::select($meeting_approvals_select)->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->count();
$acnt = count($l->approvals);
if ($acnt != 0 && $acnt == $cnt) {
2021-11-16 18:02:03 +09:00
continue;
}
2021-11-17 18:36:56 +09:00
if ($acnt == 0 && $cnt == 0) {
if (!in_array($l, $result)) {
$result[] = $l;
}
}
2021-11-16 18:02:03 +09:00
foreach ($l->approvals as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = new \stdClass();
}
if (!in_array($l, $result)) {
$result[] = $l;
2021-11-15 12:42:00 +09:00
}
2021-11-13 14:38:02 +09:00
}
}
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-16 18:02:03 +09:00
return ['status_code' => 400];
2021-10-07 13:51:53 +09:00
}
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-18 01:11:49 +09:00
if (null === ($l->approvals = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->get())) {
2021-11-16 18:02:03 +09:00
continue;
2021-10-01 14:26:48 +09:00
}
2021-11-16 18:02:03 +09:00
foreach ($l->approvals as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = new \stdClass();
}
2021-11-16 19:28:11 +09:00
}
2021-11-16 18:02:03 +09:00
2021-11-16 19:28:11 +09:00
if (!in_array($l, $result)) {
$result[] = $l;
2021-11-16 18:02:03 +09:00
}
}
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-16 18:02:03 +09:00
return ['status_code' => 400];
2021-10-07 13:51:53 +09:00
}
2021-10-07 13:51:53 +09:00
foreach ($list as $i => $l) {
2021-11-18 01:11:49 +09:00
if (null === ($l->approvals = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->get())) {
2021-11-16 18:02:03 +09:00
continue;
2021-10-01 14:26:48 +09:00
}
2021-11-16 18:02:03 +09:00
foreach ($l->approvals as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = new \stdClass();
}
2021-11-16 19:28:11 +09:00
}
2021-11-16 18:02:03 +09:00
2021-11-16 19:28:11 +09:00
if (!in_array($l, $result)) {
$result[] = $l;
2021-11-16 18:02:03 +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 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 = [];
2021-11-16 16:05:00 +09:00
$meeting_select = ['id', 'father_id', 'title', 'text', 'memo', 'updated_at', 'is_favorite'];
2021-10-01 14:26:48 +09:00
$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-16 18:02:03 +09:00
if (null === ($l->approvals = MeetingApprovals::select($meeting_approvals_select)->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->get())) {
continue;
}
2021-11-16 16:05:00 +09:00
2021-11-16 18:02:03 +09:00
if (count($l->approvals->toArray()) != MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->count()) {
continue;
}
foreach ($l->approvals as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = new \stdClass();
}
if (!in_array($l, $result)) {
$result[] = $l;
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-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_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-16 18:02:03 +09:00
if (null === ($l->approvals = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$l->id)->get())) {
continue;
}
2021-11-16 16:05:00 +09:00
2021-11-17 19:42:21 +09:00
$cnt = MeetingApprovals::select($meeting_approvals_select)->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->count();
$acnt = count($l->approvals);
if ($acnt != 0 && $acnt == $cnt) {
2021-11-16 18:02:03 +09:00
continue;
}
2021-11-17 19:42:21 +09:00
if ($acnt == 0 && $cnt == 0) {
if (!in_array($l, $result)) {
$result[] = $l;
}
}
2021-11-16 18:02:03 +09:00
foreach ($l->approvals as $ii => $ll) {
if (null === ($ll->child = Child::select($child_select)->where('id', (int)$ll->child_id)->first())) {
$ll->child = new \stdClass();
}
if (!in_array($l, $result)) {
$result[] = $l;
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-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-17 22:31:18 +09:00
$all_child_select = ['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 = [];
2021-11-10 15:59:55 +09:00
}
}
else {
2021-11-17 19:19:21 +09:00
if (null === ($result->approval = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$result->id)->where('child_id', (int)session()->get('children')['id'])->first())) {
2021-11-10 15:59:55 +09:00
$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 = [];
2021-11-16 01:38:16 +09:00
$rec = [];
2021-11-05 19:34:36 +09:00
2021-11-16 01:38:16 +09:00
if (null !== ($rel = FatherRelation::select('child_id')->where('father_id', (int)$result->father_id)->get())) {
foreach ($rel as $i => $re) {
if (null !== ($rech = Child::select($all_child_select)->where('id', $re->child_id)->first())) {
if (!in_array($rech, $rec)) {
$rec[$i] = $rech;
}
}
2021-11-05 19:34:36 +09:00
}
2021-11-16 01:38:16 +09:00
$result->children = $rec;
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())) {
$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' => ['ミーティングの登録に失敗しました。']];
}
2021-11-26 16:15:22 +09:00
if (count(Storage::disk('private')->files('/')) >= (int)env('MAX_FILES')) {
Log::critical('ストレージの限界を超えています。'.env('MAX_FILES').'個ファイルまで保存可能ですので、不要なファイルを削除して下さい。');
return ['status_code' => 400, 'error_messages' => ['親の更新に失敗しました。']];
}
2021-11-01 01:08:02 +09:00
// ミームタイプ
Validator::extend('pdf_meme', function ($attribute, $value, $params, $validator) {
return $this->pdfmeme($value);
});
2021-11-01 01:08:02 +09:00
2021-11-16 21:31:26 +09:00
$validate = Validator::make($r->all(), [
'title' => 'required|max:100',
'text' => 'required|max:2000',
'memo' => 'nullable|max:2000',
'pdf' => 'pdf_meme',
]);
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
}
if ($r->pdf == '/assets/default/default.pdf') {
$r->pdf = null;
}
2021-10-15 10:37:47 +09:00
$update = [
'title' => $r->title,
'text' => $r->text,
];
if (isset($r->memo)) $update['memo'] = $r->memo;
2021-11-16 02:15:10 +09:00
else if (is_null($r->memo)) $update['memo'] = '';
2021-10-15 10:37:47 +09:00
$filename = '';
2021-10-15 10:37:47 +09:00
try {
2021-11-01 21:26:37 +09:00
// リクエストでPDFがある場合
if (isset($r->pdf) && !is_null($r->pdf)) {
$filename = $this->uuidv4() . '.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の場合(ファイルパスだったら、スキップ)
if (!preg_match('/\/files\/(.*).pdf/', $r->pdf)) {
2021-11-01 21:26:37 +09:00
// もう存在しているPDFのファイル名からパスを外します。
$opdf = str_replace('/files/', '', $chk->pdf);
2021-11-01 21:26:37 +09:00
// PDFのbase64をGETします。
$pdf = base64_decode(substr($r->pdf, strpos($r->pdf, ',') + 1));
// 既にPDFが存在する場合(なければ、スキップ)
if (Storage::disk('private')->exists($opdf)) {
2021-11-01 23:17:07 +09:00
// 既に存在しているPDFとアップロードしているPDFを比べてみます。異なる場合、存在しているPDFを削除します。
if (strcmp(Storage::disk('private')->get($opdf), $pdf) !== 0) {
Storage::disk('private')->delete($opdf);
2021-11-01 21:26:37 +09:00
}
2021-11-01 01:31:14 +09:00
}
2021-11-01 23:15:48 +09:00
$update['pdf'] = '/files/'.$filename;
Storage::disk('private')->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 {
$update['pdf'] = '/files/'.$filename;
Storage::disk('private')->put($filename, $pdf);
2021-11-01 16:06:39 +09:00
}
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());
if (isset($r->pdf) && !is_null($r->pdf)) {
Storage::disk('private')->delete($filename);
}
2021-10-15 10:37:47 +09:00
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
}
}