Skip to content

Commit

Permalink
2ヶ月分のカレンダー配列作成と前後の空白埋めを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
yKazihara committed Mar 12, 2021
1 parent e9c0405 commit aa6b8a1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
90 changes: 56 additions & 34 deletions src/Eccube/Controller/Block/CalendarController.php
Expand Up @@ -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
{
Expand All @@ -29,8 +29,6 @@ class CalendarController extends AbstractController

/**
* CalendarController constructor.
*
* @param CalendarRepository $calendarRepository
*/
public function __construct(CalendarRepository $calendarRepository)
{
Expand All @@ -43,43 +41,67 @@ 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,
'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;
}
}
18 changes: 12 additions & 6 deletions src/Eccube/Repository/CalendarRepository.php
Expand Up @@ -13,6 +13,7 @@

namespace Eccube\Repository;

use Carbon\Carbon;
use Doctrine\ORM\NoResultException;
use Eccube\Entity\Calendar;
use Symfony\Bridge\Doctrine\RegistryInterface;
Expand All @@ -27,8 +28,6 @@ class CalendarRepository extends AbstractRepository
{
/**
* CalendarRepository constructor.
*
* @param RegistryInterface $registry
*/
public function __construct(RegistryInterface $registry)
{
Expand Down Expand Up @@ -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()
Expand All @@ -85,7 +91,7 @@ public function getHoridayListOfLastTwoMonths()
/**
* delete.
*
* @param int|\Eccube\Entity\Calend $Calendar
* @param int|\Eccube\Entity\Calend $Calendar
*
* @throws NoResultException
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Eccube/Resource/template/default/Block/calendar.twig
Expand Up @@ -18,7 +18,7 @@ file that was distributed with this source code.
<span class="ec-secHeading__ja">{{ 'front.block.calendar.title__ja'|trans }}</span>
</div>
{% for Holiday in Holidays %}
{{ Holiday.title }}
{{ Holiday.title }} : {{ Holiday.holiday|date_day }}
{% endfor %}
<br>
<table>
Expand All @@ -38,7 +38,7 @@ file that was distributed with this source code.
</tr>
</table>
{% for day in ThisMonthCalendar %}
{{ day.day }} {{ day.dayOfWeek }} :
{{ day.day }}
{% endfor %}
<br><br>
<table>
Expand All @@ -58,7 +58,7 @@ file that was distributed with this source code.
</tr>
</table>
{% for day in NextMonthCalendar %}
{{ day.day }} {{ day.dayOfWeek }} :
{{ day.day }}
{% endfor %}
</div>
</div>

0 comments on commit aa6b8a1

Please sign in to comment.