app/Customize/Controller/Block/CustomCalendarController.php line 206

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  * 
  12.  * カレンダーはPHP7.4出ないと動かないので注意
  13.  * 2024/03/27 k.saito
  14.  *  index()のreturnに、爆速商品フラグを追加
  15.  */
  16. namespace Customize\Controller\Block;
  17. use Carbon\Carbon;
  18. use Eccube\Controller\Block\CalendarController;
  19. use Eccube\Repository\CalendarRepository;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. //kintone連携用
  24. use Symfony\Component\HttpKernel\KernelInterface//ルートディレクトリの取得に使用
  25. use Customize\Service\kintoneCalendarService;
  26. use Customize\Service\getJapanholidayService;
  27. use Customize\Service\getProductCategoryService;
  28. use Customize\Service\judgmentBakusokuKoukanService;
  29. use Customize\Service\getAreaForCalendarService//対応エリア情報
  30. date_default_timezone_set('Asia/Tokyo');
  31. class CustomCalendarController extends CalendarController
  32. {
  33.     /**
  34.      * @var CalendarRepository
  35.      */
  36.     protected $calendarRepository;
  37.     /**
  38.      * @var KernelInterface
  39.      */
  40.     private $kernel;
  41.     /**
  42.      * @var kintoneCalendarService
  43.      */
  44.     private $calendar;
  45.     /**
  46.      * @var getJapanholidayService
  47.      */
  48.     private $getJapanholiday;
  49.     /**
  50.      * @var getProductCategoryService
  51.      */
  52.     private $getProductCategory;
  53.     /**
  54.      * @var judgmentBakusokuKoukanService
  55.      */
  56.     private $judgmentBakusokuKoukan;
  57.     /**
  58.      * @var getAreaForCalendarService
  59.      */
  60.     private $areaInfomation;
  61.     /**
  62.      * CalendarController constructor.
  63.      */
  64.     public function __construct(CalendarRepository $calendarRepositoryKernelInterface $kernelkintoneCalendarService $calendargetJapanholidayService $getJapanholidaygetProductCategoryService $getProductCategoryjudgmentBakusokuKoukanService $judgmentBakusokuKoukangetAreaForCalendarService $areaInfomation)
  65.     {
  66.         $this->calendarRepository $calendarRepository;
  67.         $this->kernel $kernel;
  68.         $this->calendar $calendar;
  69.         $this->getJapanholiday $getJapanholiday;
  70.         $this->getProductCategory $getProductCategory;
  71.         $this->judgmentBakusokuKoukan =  $judgmentBakusokuKoukan;
  72.         $this->areaInfomation $areaInfomation;
  73.     }
  74.     /**
  75.      * @Route("/block/calendar", name="block_calendar", methods={"GET"})
  76.      * @Template("Block/calendar.twig")
  77.      */
  78.     public function index(Request $request)
  79.     {
  80.         $reservable '⚪︎'//予約可能(表示用)
  81.         $reservationNot '×'//予約不可能(表示用) setReservationDayメソッドに影響があるため変更不可。表示変更ならJqueryを修正する
  82.         $few '△'//予約残りわずか(表示用)
  83.         $undefined '設定なし';
  84.         $area $this->areaInfomation->getArea(); //対応エリアを取得する
  85.         $reservationDetail = []; //各カテゴリ対応スタッフの予約状況を表示
  86.         $subStaffReservationDetail = []; //エコキュートの場合のサブ担当者の予約状況を表示
  87.         $subStaffFlg false;
  88.         $hidden_bakusoku '0'// 爆速商品フラグの初期セット
  89.         //カレンダーの曜日表示用で使用。getDayOfWeekStringの$weekdayから参照
  90.         $displayWeek = ['Sun' => '日''Mon' => '月''Tue' => '火''Wed' => '水''Thu' => '木''Fri' => '金''Sat' => '土'];
  91.         //本日の日付を設定する
  92.         //デバッグしやすいように/app/config/eccube/packages/kntn_calendar_date_set.yamlの設定情報で変更される
  93.         //defaultなら本日、default以外なら指定日
  94.         $dateSet $this->getParameter('calendar_block_date_set');
  95.         if ($dateSet === 'default') {
  96.             //通常通り本日
  97.             $today Carbon::now();
  98.         } else {
  99.             //デバッグモード 指定日
  100.             $today Carbon::createFromDate($dateSet);
  101.             dump($today);
  102.             echo '本日は' $today->copy()->format('Y-m-d H:i:s') . 'です。';
  103.         }
  104.         log_debug('今日は' $today);
  105.         $todayStr $today->toDateString();
  106.         $nextMonthDate $today->copy()->addMonth()->startOfMonth(); //来月の月初
  107.         $holidayListOfTwoMonths = []; //定休日リスト
  108.         $holiday ''//祝日(GoogleAPI)
  109.         //商品カテゴリごとにスタッフを選ぶため
  110.         $category $this->getProductCategory->getCategoryFromURL();
  111.         $categoryName $category['name'];
  112.         if ($categoryName === 'エコキュート') {
  113.             $subStaffFlg true;
  114.         }
  115.         //爆速商品対応か非対応でカレンダーの予約可能日を変更するため
  116.         $bakusokuflg $this->judgmentBakusokuKoukan->judge();
  117.         log_debug('false(1以外)は爆速商品' print_r($bakusokuflgtrue));
  118.         if ($bakusokuflg == true) {
  119.             $hidden_bakusoku '1';
  120.         }
  121.         //GoogleAPIリクエスト数節約の為、祝日ファイルを読み込む
  122.         $csvfile $this->kernel->getProjectDir() . '/php/file/holiday.csv';
  123.         $holiday file_get_contents($csvfile);
  124.         $holiday explode(','$holiday);
  125.         //祝日ファイルを読み込んでいない場合はGoogleから取得
  126.         if ($holiday[0] !== $todayStr) {
  127.             //dump('祝日CSV書き込み');
  128.             $this->getJapanholiday->getHoliday($today->copy());
  129.             $holiday file_get_contents($csvfile);
  130.             $holiday explode(','$holiday);
  131.         }
  132.         array_shift($holiday); //$holiday[0]はフラグの為、削除する
  133.         $firstDateOfThisMonth $today->copy()->startOfMonth();
  134.         $firstDateOfNextMonth $today->copy()->startOfMonth()->addMonth()->startOfMonth();
  135.         $endDateOfNextMonth $today->copy()->startOfMonth()->addMonth()->endOfMonth();
  136.         // 2ヶ月間の定休日を取得
  137.         $RegularHolidaysOfTwoMonths $this->calendarRepository->getHolidayList($firstDateOfThisMonth$endDateOfNextMonth);
  138.         // 今月のカレンダー配列を取得
  139.         $thisMonthCalendar $this->createCalendar($firstDateOfThisMonth);
  140.         // 来月のカレンダー配列を取得
  141.         dump($firstDateOfNextMonth);
  142.         $nextMonthCalendar $this->createCalendar($firstDateOfNextMonth);
  143.         dump($nextMonthCalendar);
  144.         // 定休日リストを取得
  145.         foreach ($RegularHolidaysOfTwoMonths as $RegularHoliday) {
  146.             $holidayListOfTwoMonths[] = $RegularHoliday->getHoliday()->setTime(000);
  147.         }
  148.         $todayCopy $today->copy();
  149.         $todayCopyNextMonth $today->copy()->startOfMonth()->addMonth();
  150.         //今月用
  151.         // 今月のカレンダー配列に定休日フラグを設定
  152.         $thisMonthCalendar $this->setHolidayAndTodayFlag($thisMonthCalendar$holidayListOfTwoMonths$todayCopy);
  153.         //カレンダーに各エリアごとの予約枠を作成する(初期化)
  154.         //$thisMonthCalendar = $this->calendar->initializationTargetMonthCalendar($thisMonthCalendar, $area, $undefined);
  155.         //各エリアに応じた平日の予約状況の詳細を作成(参照渡し)
  156.         $this->calendar->makeReservationDetail($todayCopy$area$categoryName$reservationDetail$subStaffReservationDetail$subStaffFlg);
  157.         dump($reservationDetail);
  158.         //カレンダーに平日の予約不可日を設定(ECCUBEカレンダーとスタッフの予約状況詳細$reservationDetail、$subStaffReservationDetailを連携)
  159.         $thisMonthCalendar $this->calendar->setReservationDay($thisMonthCalendar$reservationDetail$subStaffReservationDetail$todayCopy$area$category$reservable$reservationNot$few$subStaffFlg);
  160.         dump($thisMonthCalendar);
  161.         //ECCUBEカレンダーの定休日と祝日を予約不可にする
  162.         $thisMonthCalendar $this->calendar->setReservationHoliDay($thisMonthCalendar$holidayListOfTwoMonths$holiday$todayCopy$area$reservationNot);
  163.         //ECCUBEカレンダーの本日以前、本日から4日を予約不可にする。商品の在庫が存在する場合は本日を予約可能にする(当日のAMは選択不可だが当日の10時まではPMの選択を可能にする)
  164.         $notReservationDay = []; //予約不可日
  165.         $noReservationDayEnd ''// 爆速商品以外の場合に不可にする日付の最終日
  166.         $this->calendar->setNotReservationToday($thisMonthCalendar$bakusokuflg$area$holidayListOfTwoMonths$holiday$todayCopy$reservationNot$notReservationDay$noReservationDayEnd); //今月のカレンダーと予約不可日が返される
  167.         dump($noReservationDayEnd);
  168.         //来月用
  169.         // 来月のカレンダー配列に定休日フラグを設定
  170.         $nextMonthCalendar $this->setHolidayAndTodayFlag($nextMonthCalendar$holidayListOfTwoMonths$todayCopyNextMonth);
  171.         //カレンダーに各エリアごとの予約枠を作成する(初期化)
  172.         //$nextMonthCalendar = $this->calendar->initializationTargetMonthCalendar($nextMonthCalendar, $area, $undefined);
  173.         //カレンダーに平日の予約不可日を設定
  174.         $nextMonthCalendar $this->calendar->setReservationDay($nextMonthCalendar$reservationDetail$subStaffReservationDetail$nextMonthDate$area$category$reservable$reservationNot$few$subStaffFlg);
  175.         //ECCUBEカレンダーの定休日と祝日を予約不可にする
  176.         $nextMonthCalendar $this->calendar->setReservationHoliDay($nextMonthCalendar$holidayListOfTwoMonths$holiday$todayCopyNextMonth$area$reservationNot);
  177.         //ECCUBEカレンダーの本日以前、本日から4日を予約不可にする。商品の在庫が存在する場合は本日を予約可能にする(当日のAMは選択不可だが当日の10時まではPMの選択を可能にする)
  178.         $nextMonthCalendar $this->calendar->setNotReservationNextCalendar($nextMonthCalendar$area$todayCopy$reservationNot$notReservationDay$noReservationDayEnd);
  179.         //$nextMonthCalendar = $this->calendar->setNotReservationNextCalendar($thisMonthCalendar, $bakusokuflg, $area, $holidayListOfTwoMonths, $holiday, $todayCopy, $reservationNot, $notReservationDay);
  180.         dump($thisMonthCalendar);
  181.         // 各カレンダータイトルを作成
  182.         $monthFormat $this->translator->trans('front.block.calendar.month_format');
  183.         $thisMonthTitle $firstDateOfThisMonth->format($monthFormat);
  184.         $nextMonthTitle $firstDateOfNextMonth->format($monthFormat);
  185.         return [
  186.             'ThisMonthTitle' => $thisMonthTitle,
  187.             'NextMonthTitle' => $nextMonthTitle,
  188.             'ThisMonthCalendar' => $thisMonthCalendar,
  189.             'NextMonthCalendar' => $nextMonthCalendar,
  190.             'debug' => $reservationDetail//debug
  191.             'subdebug' => $subStaffReservationDetail//debug
  192.             'area' => $area,
  193.             'subStaffFlg' => $subStaffFlg//debug
  194.             'displayWeek' => $displayWeek,
  195.             'hidden_bakusoku' => $hidden_bakusoku //爆速商品フラグ
  196.         ];
  197.     }
  198.     /**
  199.      * カレンダー配列に定休日と今日フラグを設定します
  200.      *
  201.      * @param array $targetMonthCalendar カレンダー配列
  202.      * @param array $holidayListOfTwoMonths 定休日リスト
  203.      * @param Carbon $targetDate ターゲット日
  204.      *
  205.      * @return array カレンダーの配列
  206.      */
  207.     private function setHolidayAndTodayFlag($targetMonthCalendar$holidayListOfTwoMonthsCarbon $targetDate)
  208.     {
  209.         for ($i 0$i count($targetMonthCalendar); $i++) {
  210.             // カレンダー配列の日が空の場合は処理をスキップ
  211.             if ($targetMonthCalendar[$i]['day'] == '') {
  212.                 $targetMonthCalendar[$i]['holiday'] = false;
  213.                 $targetMonthCalendar[$i]['today'] = false;
  214.                 continue;
  215.             }
  216.             $targetYmdDateTime = new \DateTime($targetDate->copy()->format('Y-n') . '-' $targetMonthCalendar[$i]['day']);
  217.             // カレンダーの日付が定休日リストに存在するかを確認
  218.             $result array_search($targetYmdDateTime$holidayListOfTwoMonths);
  219.             // 定休日フラグを設定
  220.             if ($result !== false) {
  221.                 $targetMonthCalendar[$i]['holiday'] = true;
  222.             } else {
  223.                 $targetMonthCalendar[$i]['holiday'] = false;
  224.             }
  225.             // 今日フラグを設定
  226.             if ($targetYmdDateTime == new \DateTime($targetDate->copy()->format('Y-n-j'))) {
  227.                 $targetMonthCalendar[$i]['today'] = true;
  228.             } else {
  229.                 $targetMonthCalendar[$i]['today'] = false;
  230.             }
  231.         }
  232.         return $targetMonthCalendar;
  233.     }
  234.     /**
  235.      * カレンダーの配列を生成します
  236.      *
  237.      * @param Carbon $firstDateOfTargetMonth 月初日
  238.      *
  239.      * @return array カレンダーの配列
  240.      */
  241.     private function createCalendar(Carbon $firstDateOfTargetMonth)
  242.     {
  243.         $year $firstDateOfTargetMonth->copy()->year;
  244.         $month $firstDateOfTargetMonth->copy()->month;
  245.         // 週のうちの何日目か 0 (日曜)から 6 (土曜)を取得
  246.         $firstDayOfWeek $firstDateOfTargetMonth->dayOfWeek;
  247.         $targetMonthCalendar = [];
  248.         // 1日目の曜日の位置手前まで空文字を追加
  249.         for ($i 0$i <= $firstDayOfWeek$i++) {
  250.             $targetMonthCalendar[$i]['day'] = '';
  251.             $targetMonthCalendar[$i]['dayOfWeek'] = '';
  252.         }
  253.         // 1日目の曜日の位置+月の日数
  254.         $loopCount $firstDayOfWeek $firstDateOfTargetMonth->daysInMonth;
  255.         // 月の日数に合わせて日と曜日を追加
  256.         //年月日を追加
  257.         $dayNumber 1;
  258.         $dayOfWeekNumber $firstDayOfWeek;
  259.         for ($i $firstDayOfWeek$i $loopCount$i++) {
  260.             $targetMonthCalendar[$i]['day'] = $dayNumber;
  261.             $targetMonthCalendar[$i]['dayOfWeek'] = $this->getDayOfWeekString($dayOfWeekNumber);
  262.             $month2digits sprintf('%02d'$month);
  263.             $dayNumber2digits sprintf('%02d'$dayNumber);
  264.             $targetMonthCalendar[$i]['yearMonthDay'] = $year '-' $month2digits '-' $dayNumber2digits;
  265.             $dayNumber++;
  266.             $dayOfWeekNumber++;
  267.             // 曜日のおりかえし: 0 (日曜)へ
  268.             if ($dayOfWeekNumber == 7) {
  269.                 $dayOfWeekNumber 0;
  270.             }
  271.         }
  272.         // 1日目の曜日の位置+月の日数に合わせて後に空文字を追加
  273.         // 7日*4週=28日(日曜始まりでうるう年じゃない2月)
  274.         if ($loopCount === 28) {
  275.             // 後に空文字追加はスキップ
  276.             return $targetMonthCalendar;
  277.         }
  278.         // 7日*6週=42日、7日*5週=35日
  279.         $paddingLoopCount 35;
  280.         if ($loopCount 35) {
  281.             $paddingLoopCount 42;
  282.         }
  283.         for ($i $loopCount$i $paddingLoopCount$i++) {
  284.             $targetMonthCalendar[$i]['day'] = '';
  285.             $targetMonthCalendar[$i]['dayOfWeek'] = '';
  286.         }
  287.         return $targetMonthCalendar;
  288.     }
  289.     /**
  290.      * 曜日を数値から文字列へ変換します
  291.      *
  292.      * @param int $dayOfWeekNumber 曜日の番号 : 0 (日曜)から 6 (土曜)
  293.      *
  294.      * @return string 曜日の文字 : Sun(日曜)からSat(土曜)
  295.      */
  296.     private function getDayOfWeekString($dayOfWeekNumber)
  297.     {
  298.         $weekday = ['Sun''Mon''Tue''Wed''Thu''Fri''Sat'];
  299.         return $weekday[$dayOfWeekNumber];
  300.     }
  301. }