Skip to content

[カレンダー]開始日時・終了日時の初期値を利用しやすい時刻にしました #2011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions app/Plugins/User/Calendars/CalendarsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use App\Rules\CustomValiWysiwygMax;

use App\Utilities\File\FileUtils;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\Exceptions\Exception;

/**
* カレンダー・プラグイン
Expand Down Expand Up @@ -350,8 +353,7 @@ public function edit($request, $page_id, $frame_id, $post_id = null)

// id が空なら、新規オブジェクトとみなして、デフォルトの日付を設定して画面を表示する。
if (empty($post->id) && $request->filled("date")) {
$post->start_date = $request->date;
$post->end_date = $request->date;
$post = $this->setInitDatetime($post, $request->date);
}

// 変更画面を呼び出す。
Expand Down Expand Up @@ -672,4 +674,40 @@ public function changeBuckets($request, $page_id, $frame_id)

return;
}

/**
* 初期日時を設定する
*
* @param CalendarPost $post
* @param string $date
* @return CalendarPost
*/
private function setInitDatetime(CalendarPost $post, string $date): CalendarPost
{
// 想定しない形式の日付だったら初期日時を設定しない
try {
$init_date = Carbon::parse($date);
} catch (Exception $e) {
return $post;
}

if ($init_date->isToday()) {
// 当日は現在時刻から近い時刻を設定する
$init_date = CarbonImmutable::now();
$start_date = $init_date->addHour(1);
$end_date = $init_date->addHour(2);
$post->start_date = $start_date->format('Y-m-d');
$post->start_time = $start_date->format('H:00');
$post->end_date = $end_date->format('Y-m-d');
$post->end_time = $end_date->format('H:00');
} else {
// 当日以外は9:00-10:00を設定する
$post->start_date = $init_date->format('Y-m-d');
$post->start_time = $init_date->format('09:00');
$post->end_date = $init_date->format('Y-m-d');
$post->end_time = $init_date->format('10:00');
}

return $post;
}
}