/
CalendarController.php
107 lines (89 loc) · 3.34 KB
/
CalendarController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Controller\Block;
use Carbon\Carbon;
use Eccube\Controller\AbstractController;
use Eccube\Repository\CalendarRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class CalendarController extends AbstractController
{
/**
* @var CalendarRepository
*/
protected $calendarRepository;
/**
* CalendarController constructor.
*/
public function __construct(CalendarRepository $calendarRepository)
{
$this->calendarRepository = $calendarRepository;
}
/**
* @Route("/block/calendar", name="block_calendar")
* @Template("Block/calendar.twig")
*/
public function index(Request $request)
{
// TODO あとやりたいことは休日データ取ってフラグ入れる?
$firstDateOfThisMonth = Carbon::now()->startOfMonth();
$firstDateOfNextMonth = Carbon::parse('+ 1 month')->startOfMonth();
$endDateOfNextMonth = Carbon::parse('+ 1 month')->endOfMonth();
// 2ヶ月間の定休日を取得
$Holidays = $this->calendarRepository->getHolidayList($firstDateOfThisMonth, $endDateOfNextMonth);
// 今月のカレンダー配列を取得
$thisMonthCalendar = $this->createCalendar($firstDateOfThisMonth);
// 来月のカレンダー配列を取得
$nextMonthCalendar = $this->createCalendar($firstDateOfNextMonth);
return [
'ThisMonthCalendar' => $thisMonthCalendar,
'NextMonthCalendar' => $nextMonthCalendar,
'Holidays' => $Holidays,
];
}
/**
* カレンダーの配列を生成します
*
* @param Carbon $firstDateOfTargetMonth 月初日
*
* @return array カレンダーの配列
*/
private function createCalendar(Carbon $firstDateOfTargetMonth)
{
// 週のうちの何日目か 0 (日曜)から 6 (土曜)を取得
$firstDayOfWeek = $firstDateOfTargetMonth->dayOfWeek;
$targetMonthCalendar = [];
// 1日目の曜日の位置手前まで空文字を追加
for ($i = 0; $i <= $firstDayOfWeek; $i++) {
$targetMonthCalendar[$i]['day'] = '@'; // TODO あとで空文字に変えよう
}
// 1日目の曜日の位置+月の日数
$loopCount = $firstDayOfWeek + $firstDateOfTargetMonth->daysInMonth;
// 月の日数に合わせて日を追加
$dayNumber = 1;
for ($i = $firstDayOfWeek; $i < $loopCount; $i++) {
$targetMonthCalendar[$i]['day'] = $dayNumber;
$dayNumber++;
}
// 1日目の曜日の位置+月の日数に合わせて後に空文字を追加
// 7日*6週=42日、7日*5週=35日
$paddingLoopCount = 35;
if ($loopCount > 35) {
$paddingLoopCount = 42;
}
for ($i = $loopCount; $i < $paddingLoopCount; $i++) {
$targetMonthCalendar[$i]['day'] = '@'; // TODO あとで空文字に変えよう
}
return $targetMonthCalendar;
}
}