From aa6b8a1dc9d7649557024e0174fc3a2e588ff8e7 Mon Sep 17 00:00:00 2001 From: Yuko Kajihara Date: Fri, 12 Mar 2021 12:15:34 +0900 Subject: [PATCH] =?UTF-8?q?2=E3=83=B6=E6=9C=88=E5=88=86=E3=81=AE=E3=82=AB?= =?UTF-8?q?=E3=83=AC=E3=83=B3=E3=83=80=E3=83=BC=E9=85=8D=E5=88=97=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E3=81=A8=E5=89=8D=E5=BE=8C=E3=81=AE=E7=A9=BA=E7=99=BD?= =?UTF-8?q?=E5=9F=8B=E3=82=81=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/Block/CalendarController.php | 90 ++++++++++++------- src/Eccube/Repository/CalendarRepository.php | 18 ++-- .../template/default/Block/calendar.twig | 6 +- 3 files changed, 71 insertions(+), 43 deletions(-) diff --git a/src/Eccube/Controller/Block/CalendarController.php b/src/Eccube/Controller/Block/CalendarController.php index 8018a56f77b..0f1b11e8443 100644 --- a/src/Eccube/Controller/Block/CalendarController.php +++ b/src/Eccube/Controller/Block/CalendarController.php @@ -13,12 +13,12 @@ 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; -use Carbon\Carbon; class CalendarController extends AbstractController { @@ -29,8 +29,6 @@ class CalendarController extends AbstractController /** * CalendarController constructor. - * - * @param CalendarRepository $calendarRepository */ public function __construct(CalendarRepository $calendarRepository) { @@ -43,38 +41,20 @@ public function __construct(CalendarRepository $calendarRepository) */ public function index(Request $request) { - // TODO あとやりたいことは月初の前にどんだけ空白埋めるか?と休日データ取ってフラグ入れる? - - // 当月と翌月で指定して定休日データ取る? - $Holidays = $this->calendarRepository->getHoridayListOfLastTwoMonths(); - - // 今月のカレンダーを作る - $today = Carbon::now(); - $thisMonthFirstDayOfWeek = $today->startOfMonth()->dayOfWeek; // 月初の曜日 - $thisMonthCalendar = []; - for ($i = 1; $i <= $today->daysInMonth; $i++) { - $thisMonthCalendar[$i]['day'] = $i; - $thisMonthCalendar[$i]['dayOfWeek'] = $thisMonthFirstDayOfWeek; // ホントは曜日詰めなくていい確認だけ - if ($thisMonthFirstDayOfWeek == 6) { - $thisMonthFirstDayOfWeek = 0; // 曜日を日曜に戻す - } else { - $thisMonthFirstDayOfWeek++; - } - } + // TODO あとやりたいことは休日データ取ってフラグ入れる? - // 来月のカレンダーを作る - $nextMonth = Carbon::parse('+ 1 month'); - $nextMonthFirstDayOfWeek = $nextMonth->startOfMonth()->dayOfWeek; // 月初の曜日 - $nextMonthCalendar = []; - for ($i = 1; $i <= $nextMonth->daysInMonth; $i++) { - $nextMonthCalendar[$i]['day'] = $i; - $nextMonthCalendar[$i]['dayOfWeek'] = $nextMonthFirstDayOfWeek; // ホントは曜日詰めなくていい確認だけ - if ($nextMonthFirstDayOfWeek == 6) { - $nextMonthFirstDayOfWeek = 0; // 曜日を日曜に戻す - } else { - $nextMonthFirstDayOfWeek++; - } - } + $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, @@ -82,4 +62,46 @@ public function index(Request $request) '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; + } } diff --git a/src/Eccube/Repository/CalendarRepository.php b/src/Eccube/Repository/CalendarRepository.php index e242fdbd5ea..67dc0bcc024 100644 --- a/src/Eccube/Repository/CalendarRepository.php +++ b/src/Eccube/Repository/CalendarRepository.php @@ -13,6 +13,7 @@ namespace Eccube\Repository; +use Carbon\Carbon; use Doctrine\ORM\NoResultException; use Eccube\Entity\Calendar; use Symfony\Bridge\Doctrine\RegistryInterface; @@ -27,8 +28,6 @@ class CalendarRepository extends AbstractRepository { /** * CalendarRepository constructor. - * - * @param RegistryInterface $registry */ public function __construct(RegistryInterface $registry) { @@ -67,15 +66,22 @@ public function getListOrderByIdDesc() } /** - * getListOfLastTwoMonths + * getHolidayList + * + * @param Carbon $startDate 取得開始日 + * @param Carbon $endDate 取得終了日 * * @return array|null */ - public function getHoridayListOfLastTwoMonths() + public function getHolidayList(Carbon $startDate, Carbon $endDate) { $qb = $this->createQueryBuilder('c') ->orderBy('c.id', 'DESC') - ->where('c.holiday >= \'2020-02-27 00:00:00\' and c.holiday <= \'2021-02-20 00:00:00\''); + ->andWhere('c.holiday >= :startDate') + ->andWhere('c.holiday <= :endDate') + ->setParameter(':startDate', $startDate->copy()) + ->setParameter(':endDate', $endDate->copy()) + ->orderBy('c.holiday'); return $qb ->getQuery() @@ -85,7 +91,7 @@ public function getHoridayListOfLastTwoMonths() /** * delete. * - * @param int|\Eccube\Entity\Calend $Calendar + * @param int|\Eccube\Entity\Calend $Calendar * * @throws NoResultException */ diff --git a/src/Eccube/Resource/template/default/Block/calendar.twig b/src/Eccube/Resource/template/default/Block/calendar.twig index 5951850d0d3..ffd436681e5 100644 --- a/src/Eccube/Resource/template/default/Block/calendar.twig +++ b/src/Eccube/Resource/template/default/Block/calendar.twig @@ -18,7 +18,7 @@ file that was distributed with this source code. {{ 'front.block.calendar.title__ja'|trans }} {% for Holiday in Holidays %} - {{ Holiday.title }} + {{ Holiday.title }} : {{ Holiday.holiday|date_day }} {% endfor %}
@@ -38,7 +38,7 @@ file that was distributed with this source code.
{% for day in ThisMonthCalendar %} - {{ day.day }} {{ day.dayOfWeek }} : + {{ day.day }} {% endfor %}

@@ -58,7 +58,7 @@ file that was distributed with this source code.
{% for day in NextMonthCalendar %} - {{ day.day }} {{ day.dayOfWeek }} : + {{ day.day }} {% endfor %}