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

928 行
36 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\DB;
2021-10-15 10:37:47 +09:00
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
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;
use App\Mail\FathersApprovalMail;
use App\Mail\MeetingEditNotification;
class MeetingsController extends Controller {
2021-10-01 14:26:48 +09:00
public function register (Request $r) {
if (!isset(session()->get('fathers')['id'])) {
2021-10-06 14:50:13 +09:00
return ['status_code' => 400, 'error_messages' => ['ミーティングの登録に失敗しました。']];
2021-10-01 14:26:48 +09:00
}
if (count(Storage::disk('private')->files('/')) >= 9999) {
Log::critical('ストレージの限界を超えています。9999個ファイルまで保存可能ですので、不要なファイルを削除して下さい。');
2021-11-26 16:15:22 +09:00
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-30 12:45:32 +09:00
// 画像サイズは各5MiB以内、PDFサイズは50MiB以内
//// 画像
2021-11-16 21:31:26 +09:00
Validator::extend('image_size', function ($attribute, $value, $params, $validator) {
return $this->imagesizemulti($value);
2021-11-16 21:31:26 +09:00
});
2021-11-30 12:45:32 +09:00
//// PDF
Validator::extend('pdf_size', function ($attribute, $value, $params, $validator) {
return $this->pdfsize($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-30 12:45:32 +09:00
'pdf' => 'pdf_size|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
if ($r->pdf == 'null') {
$r->pdf = null;
}
if ($r->pdf == '/assets/default/default.pdf') {
$r->pdf = null;
}
2021-10-06 14:50:13 +09:00
$insert = [
'father_id' => (int)session()->get('fathers')['id'],
2021-10-06 14:50:13 +09:00
'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 {
2022-01-31 20:10:30 +09:00
$pdf = Storage::disk('private')->get(substr($r->pdf, 7));
Storage::disk('private')->put($filename, $pdf);
2021-11-18 00:31:39 +09:00
}
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) {
2022-01-31 20:10:30 +09:00
$fname = $this->uuidv4() . '.jpg';
$fnames[] = $fname;
2021-11-18 00:31:39 +09:00
if (substr($img, -5) != '.jpeg' && substr($img, -4) != '.jpg' && substr($img, -4) != '.png' && substr($img, -4) != '.gif') {
$image = base64_decode(substr($img, strpos($img, ',') + 1));
}
else {
2022-01-31 20:10:30 +09:00
$image = Storage::disk('private')->get(substr($img, 7));
2021-11-18 00:31:39 +09:00
}
2022-01-31 20:10:30 +09:00
Storage::disk('private')->put($fname, $image);
$this->fiximg($fname);
2021-11-17 22:53:07 +09:00
$insert_image = [
'meeting_id' => (int)$meeting,
2022-01-31 20:10:30 +09:00
'image' => '/files/'.$fname,
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);
$c = Child::select('email')->where('id', $child)->first();
Mail::to($c->email)->send(new FathersApprovalMail(session()->get('fathers')['company'], (int)$meeting));
}
$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
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->orderBy('updated_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
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->orderBy('updated_at', 'desc')->get())) {
2021-10-15 10:37:47 +09:00
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(session()->get('children')['id'])) {
2021-10-01 14:26:48 +09:00
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'];
// 取得に成功
if (null === ($approval = MeetingApprovals::select('meeting_id')->where('child_id', (int)session()->get('children')['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)session()->get('children')['id'])->first())) {
2021-11-06 19:11:25 +09:00
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(session()->get('children')['id'])) {
2021-10-01 14:26:48 +09:00
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'];
// 取得に成功
2022-01-08 20:45:08 +09:00
if (null === ($approval = MeetingApprovals::select('meeting_id')->where('child_id', (int)session()->get('children')['id'])->whereNull('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)session()->get('children')['id'])->first())) {
2021-11-06 19:11:25 +09:00
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(session()->get('fathers')['id'])) {
2021-10-01 14:26:48 +09:00
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'];
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)session()->get('fathers')['id'])->orderBy('updated_at', 'desc')->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
$cnt = MeetingApprovals::select('id')->where('meeting_id', (int)$l->id)->count();
$acnt = count($l->approvals->toArray());
if ($acnt != $cnt) {
2021-11-16 18:02:03 +09:00
continue;
}
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-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(session()->get('fathers')['id'])) {
2021-10-01 14:26:48 +09:00
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'];
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)session()->get('fathers')['id'])->orderBy('updated_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-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
$cnt = MeetingApprovals::select('id')->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->count();
2021-11-17 18:36:56 +09:00
$acnt = count($l->approvals);
if ($acnt != 0 && $acnt == $cnt) {
2021-11-16 18:02:03 +09:00
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-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) {
if (!isset(session()->get('fathers')['id'])) {
2021-10-01 14:26:48 +09:00
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
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)session()->get('fathers')['id'])->where('is_favorite', 1)->orderBy('updated_at', 'desc')->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) {
if (!isset(session()->get('fathers')['id'])) {
2021-10-01 14:26:48 +09:00
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
// 取得に成功
2022-01-08 16:36:34 +09:00
if (null === ($list = Meeting::select($meeting_select)->where('father_id', (int)session()->get('fathers')['id'])->where('is_favorite', 0)->orderBy('updated_at', 'desc')->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(session()->get('children')['id']) || !isset($r->keyword)) {
2021-10-01 14:26:48 +09:00
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
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->orderBy('created_at', 'desc')->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) {
if (null === ($rel = FatherRelation::select('id')->where('father_id', (int)$l->father_id)->where('child_id', (int)session()->get('children')['id'])->first())) {
continue;
}
if ($rel->child_id != (int)$rel->child_id) {
2021-11-08 19:41:06 +09:00
continue;
}
if (null === ($ma = MeetingApprovals::select('id')->where('child_id', (int)session()->get('children')['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
}
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('child_id', (int)session()->get('children')['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(session()->get('children')['id']) || !isset($r->keyword)) {
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', '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
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->orderBy('created_at', 'desc')->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) {
if (null === ($rel = FatherRelation::select('id')->where('father_id', (int)$l->father_id)->where('child_id', (int)session()->get('children')['id'])->first())) {
continue;
}
if ($rel->child_id != (int)$rel->child_id) {
2021-11-08 19:41:06 +09:00
continue;
}
if (null === ($ma = MeetingApprovals::select('id')->where('child_id', (int)session()->get('children')['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
}
if (null === ($l->approval = MeetingApprovals::select($meeting_approvals_select)->where('child_id', (int)session()->get('children')['id'])->whereNull('approval_at')->first())) {
2021-11-06 23:17:15 +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 searchOfCompleteofFather (Request $r) {
if (!isset(session()->get('fathers')['id']) || !isset($r->keyword)) {
2021-10-01 14:26:48 +09:00
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'];
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->orderBy('updated_at', 'desc')->get())) {
return ['status_code' => 200, 'params' => $result];
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) {
if ($l->father_id != (int)session()->get('fathers')['id']) {
continue;
}
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
$cnt = MeetingApprovals::select('id')->where('meeting_id', (int)$l->id)->count();
$acnt = count($l->approvals->toArray());
if ($acnt != $cnt) {
2021-11-16 18:02:03 +09:00
continue;
}
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 searchOfIncompleteofFather (Request $r) {
if (!isset(session()->get('fathers')['id']) || !isset($r->keyword)) {
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', 'updated_at', 'is_favorite'];
2021-10-01 14:26:48 +09:00
$meeting_approvals_select = ['approval_at', 'child_id'];
$child_select = ['image'];
// 取得に成功
if (null === ($list = Meeting::select($meeting_select)->where('title', 'LIKE', '%'.$r->keyword.'%')->orWhere('text', 'LIKE', '%'.$r->keyword.'%')->orderBy('updated_at', 'desc')->get())) {
return ['status_code' => 200, 'params' => $result];
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) {
if ($l->father_id != (int)session()->get('fathers')['id']) {
continue;
}
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
$cnt = MeetingApprovals::select('id')->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->count();
2021-11-17 19:42:21 +09:00
$acnt = count($l->approvals);
if ($acnt != 0 && $acnt == $cnt) {
2021-11-16 18:02:03 +09:00
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 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
// 親画面から他の親としてアクセスすれば、404となります。
$err = 'アクセスできません。';
if (request()->route()->action['as'] == 'mdp') {
abort_if(null === session()->get('fathers') || (int)session()->get('fathers')['id'] != $result->father_id, 404, $err);
}
// 同じく子画面から他の親の詳細ページをアクセスすれば、404となります。
if (request()->route()->action['as'] == 'mdc') {
abort_if(null === session()->get('children') || null === ($rel = FatherRelation::where('father_id', $result->father_id)->where('child_id', (int)session()->get('children')['id'])->first()), 404, $err);
}
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();
}
}
2022-01-06 14:01:49 +09:00
else $result->father = Father::select('company')->where('id', (int)$result->father_id)->first();
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 = [];
2022-01-26 12:06:12 +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' => ['ミーティングの登録に失敗しました。']];
}
if (count(Storage::disk('private')->files('/')) >= 9999) {
Log::critical('ストレージの限界を超えています。9999個ファイルまで保存可能ですので、不要なファイルを削除して下さい。');
2021-12-14 18:35:09 +09:00
return ['status_code' => 400, 'error_messages' => ['ミーティングの更新に失敗しました。']];
2021-11-26 16:15:22 +09:00
}
2021-11-30 12:45:32 +09:00
// ファイルサイズは50MiB以内
Validator::extend('pdf_size', function ($attribute, $value, $params, $validator) {
return $this->pdfsize($value);
});
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_size|pdf_meme',
2021-11-16 21:31:26 +09:00
]);
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 == 'null') {
$r->pdf = null;
}
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
}
2022-03-07 18:19:32 +09:00
else {
// DBにミーティングがある場合
if ($chk = Meeting::select('pdf')->where('id', (int)$meeting_id)->first()) {
$opdf = str_replace('/files/', '', $chk->pdf);
if (Storage::disk('private')->get($opdf)) {
Storage::disk('private')->delete($opdf);
}
}
}
2021-10-31 23:50:54 +09:00
2021-11-01 21:26:37 +09:00
// データベースに保存します。
DB::beginTransaction();
$meeting = Meeting::find((int)$meeting_id);
$meeting->title = $update['title'];
$meeting->text = $update['text'];
if (isset($update['memo'])) $meeting->memo = $update['memo'];
if (isset($update['pdf'])) $meeting->pdf = $update['pdf'];
2022-03-07 18:19:32 +09:00
else $meeting->pdf = null;
$meeting->save();
// 子供達を知らせます。
if (null !== ($fr = FatherRelation::where('father_id', (int)$meeting->id)->get())) {
foreach ($fr as $f) {
if (null !== ($meeting = MeetingApprovals::where('meeting_id', (int)$meeting_id)->where('child_id', $f->child_id)->whereNotNull('approval_at')->first())) {
if (null !== ($child = Child::where('id', $f->child_id)->first())) {
Mail::to($child->email)->send(new MeetingEditNotification(session()->get('fathers')['company'], (int)$meeting_id));
}
}
}
}
DB::commit();
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);
}
DB::rollback();
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) {
$delimg = false;
if (!isset($meeting_id)) {
return ['status_code' => 400];
}
if (null === (Meeting::select('id')->where('id', (int)$meeting_id)->get())) {
return ['status_code' => 400];
}
if (null !== ($img = MeetingImage::select('image')->where('meeting_id', (int)$meeting_id)->get())) {
$delimg = true;
}
2021-10-07 13:51:53 +09:00
try {
// DBに入ります。
DB::beginTransaction();
$meeting = Meeting::find((int)$meeting_id);
$pdf = $meeting->pdf;
$meeting->delete();
if (!is_null($img)) {
$pdf = str_replace('/files/', '', $pdf);
if (!Storage::disk('private')->exists($pdf)) {
Log::warning($pdf.'というパスは不正です。');
}
else {
Storage::disk('private')->delete($pdf);
}
}
Storage::disk('private')->delete($pdf);
if ($delimg) {
foreach ($img as $m) {
$image = str_replace('/files/', '', $m->image);
if (!Storage::disk('private')->exists($image)) {
Log::warning($image.'というパスは不正です。');
}
else {
Storage::disk('private')->delete($image);
}
}
}
DB::commit();
2021-10-07 13:51:53 +09:00
} catch (\Throwable $e) {
Log::critical($e->getMessage());
DB::rollback();
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
return ['status_code' => 200];
2021-10-01 14:26:48 +09:00
}
2021-12-13 22:07:15 +09:00
public function updateMemo (Request $r) {
if (!isset($r->meeting_id)) {
2021-12-18 12:40:12 +09:00
return ['status_code' => 400, 'error_messages' => ['メモの更新に失敗しました。']];
2021-12-13 22:07:15 +09:00
}
2021-12-18 12:40:12 +09:00
$validate = Validator::make($r->all(), ['memo' => 'required|max:2000']);
2021-12-13 22:07:15 +09:00
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
$update = ['memo' => $r->memo];
try {
// データベースに保存します。
Meeting::where('id', (int)$r->meeting_id)->update($update);
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => 'メモの更新に失敗しました。'];
}
return ['status_code' => 200, 'success_messages' => 'メモを更新しました。'];
}
}