app/Customize/Service/CommonService.php line 171

Open in your IDE?
  1. <?php
  2. /**
  3.  * @version EC=CUBE4
  4.  * @copyright 株式会社 翔 kakeru.co.jp
  5.  * @author
  6.  * 2021年11月16日作成
  7.  *
  8.  * app\Customize\Service\CommonService.php
  9.  *
  10.  *
  11.  * 共通サービス
  12.  *
  13.  * YAML・EC-CUBE CONFIG の読み込み・サービス
  14.  *
  15.  *                               C= C= C= ┌(;・_・)┘トコトコ
  16.  ******************************************************/
  17. namespace Customize\Service;
  18. use Doctrine\ORM\EntityManager;
  19. use Eccube\Entity\Cart;
  20. use Eccube\Entity\Master\DeviceType;
  21. use Eccube\Entity\Order;
  22. use Eccube\Entity\Page;
  23. use Eccube\Entity\PageLayout;
  24. use Eccube\Repository\BaseInfoRepository;
  25. use Lm\Engine\EC\Entity\GoodsWithRelated;
  26. use Detection\MobileDetect as MobileDetector;
  27. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  28. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  29. use Symfony\Component\HttpFoundation\RequestStack;
  30. use Symfony\Component\DependencyInjection\ContainerInterface;
  31. use Symfony\Component\HttpFoundation\RedirectResponse;
  32. use Eccube\Common\EccubeConfig;
  33. use Symfony\Component\Yaml\Yaml;
  34. use Twig\Environment as Twig;
  35. use Eccube\Entity\Layout;
  36. use Eccube\Entity\Payment;
  37. use Eccube\Repository\DeliveryFeeRepository;
  38. use Lm\Entity\OrderHeader;
  39. use Lm\Engine\NoukiKeisan\NoukiKeisan as NoukiEngine;
  40. class CommonService
  41. {
  42.     const RouteingFilename 'Routeing.yaml';
  43.     const OliginalToken 'OliginalToken';
  44.     const Session_Junction_Mode 'Session_Junction_Mode';
  45.     const Lm_Order_Option_Session 'Lm_Order_Option_Session';
  46.     const Lm_Order_Option 'LmOrderOption';
  47.     const Lm_Option_Survey 'survey';
  48.     const Lm_Option_Survey_Data 'survey_data';
  49.     const Lm_Option_Coupon 'coupon';
  50.     const Lm_Option_OrderId 'order_id';
  51.     const Lm_Option_Catalog 'catalog';
  52.     const Lm_Option_Receipt 'receipt';
  53.     const RemoveCartItme    'RemoveCartItme';
  54.     const Matrix_Token      'matrix';
  55.     const MetaTagData 'MetaTag.yaml';
  56.     const TOP_DIR 'front-public/files/top/';
  57.     protected $Config;
  58.     protected $Container;
  59.     protected $Twig;
  60.     protected $PageRepository;
  61.     protected $RouteingYamlPath;
  62.     protected $LmPayments = [];
  63.     protected $BaseInfo;
  64.     protected $BaseInfoRepository;
  65.     protected $Session;
  66.     protected $Request;
  67.     protected $TokenStorage;
  68.     protected $LmHelper;
  69.     protected $MobileDetector;
  70.     protected $DeliveryFeeRepository;
  71.     /**
  72.      * @var OrderOptionsService
  73.      */
  74.     protected $OrderOptionsService;
  75.     /**
  76.      * @var YamlService
  77.      */
  78.     protected $YamlService;
  79.     public function __construct(
  80.         ContainerInterface    $Container,
  81.         EccubeConfig          $eccubeConfig,
  82.         BaseInfoRepository    $BaseInfoRepository,
  83.         Twig                  $Twig,
  84.         SessionInterface      $Session,
  85.         RequestStack          $RequestStack,
  86.         TokenStorageInterface $TokenStorage,
  87.         LmHelper              $LmHelper,
  88.         MobileDetector        $mobileDetector,
  89.         DeliveryFeeRepository $DeliveryFeeRepository,
  90.         OrderOptionsService   $OrderOptionsService,
  91.         YamlService           $YamlService
  92.     )
  93.     {
  94.         $this->Container $Container;
  95.         $this->Config $eccubeConfig;
  96.         $this->RouteingYamlPath $this->GetConfig('eccube_config_routes_dir');
  97.         $this->SetTwig($Twig);
  98.         $this->BaseInfoRepository $BaseInfoRepository;
  99.         $this->Session $Session;
  100.         $this->Request $RequestStack->getCurrentRequest();
  101.         $this->TokenStorage $TokenStorage;
  102.         $this->Router $Container->get('router');
  103.         $this->LmHelper $LmHelper;
  104.         $this->MobileDetector $mobileDetector;
  105.         $this->DeliveryFeeRepository $DeliveryFeeRepository;
  106.         $this->OrderOptionsService $OrderOptionsService;
  107.         $this->YamlService $YamlService;
  108.     }
  109.     /**
  110.      * コンフィグ取得する
  111.      * @param str コンフィグ名 null 全ての配列を返す
  112.      * @return {string,int,array} $Config
  113.      */
  114.     public function GetConfig($Str null)
  115.     {
  116.         if (!$Str) {
  117.             return $this->Config;
  118.         } else {
  119.             return $this->Config[$Str] ?? $Str;
  120.         }
  121.     }
  122.     /**
  123.      * Ymlファイルを配列で取得する
  124.      * @param $Yml str YMLファイル名
  125.      * @param %flg boolean true form 逆
  126.      * @param $Path str パス
  127.      * @return Array
  128.      */
  129.     public function GetYaml($Yaml$Flg false$Path null)
  130.     {
  131.         return $this->YamlService->GetYaml($Yaml$Flg$Path);
  132.     }
  133.     /**
  134.      * Ymlファイルを書き込む
  135.      * @param $Add array 書き込む配列
  136.      * @param $Yml str YMLファイル名
  137.      * @param $Path str パス
  138.      * @return Array
  139.      */
  140.     public function MergeYaml(array $Adds$Yaml$Path null)
  141.     {
  142.         return $this->YamlService->MergeYaml($Adds$Yaml$Path);
  143.     }
  144.     /**
  145.      * Ymlファイルを書き込む
  146.      * @param $Data array 書き込む配列
  147.      * @param $Yml str YMLファイル名
  148.      * @param $Path str パス
  149.      */
  150.     public function WriteYml(array $Data$Yaml$Path null)
  151.     {
  152.         return $this->YamlService->WriteYml($Data$Yaml$Path);
  153.     }
  154.     /**
  155.      * BaseINfo
  156.      */
  157.     public function GetBaseInfo($Param null)
  158.     {
  159.         if (!is_array($this->BaseInfo)) {
  160.             #連想配列で格納
  161.             $BaseInfos $this->BaseInfoRepository->get();
  162.             $SetName = function ($Keys) {
  163.                 $Re '';
  164.                 foreach (explode('_'$Keys) as $Key) {
  165.                     $Re .= ucfirst($Key);
  166.                 }
  167.                 return $Re;
  168.             };
  169.             foreach ($BaseInfos->ToNormalizedArray() as $Key => $BaseInfo) {
  170.                 $Name $SetName($Key);
  171.                 switch ($Key) {
  172.                     case 'postal_code':
  173.                         $this->BaseInfo[$Name] = substr_replace($BaseInfo'-'30);
  174.                         break;
  175.                     case 'Pref':
  176.                         $Pref $BaseInfos->getPref();
  177.                         $this->BaseInfo[$Name] = $Pref->getName() ;
  178.                         break;
  179.                     case 'phone_number';
  180.                         $this->BaseInfo[$Name] = $this->GetPhoneNumber($BaseInfo,false) ;
  181.                         $this->BaseInfo['Tel'] = $BaseInfo;
  182.                         break;
  183.                     case 'fax_number';
  184.                         $this->BaseInfo[$Name] = $this->GetPhoneNumber($BaseInfo,false) ;
  185.                         $this->BaseInfo['Fax'] = $BaseInfo;
  186.                         break;
  187.                     case 'delivery_free_amount':
  188.                         $this->BaseInfo['delivery_free_amounte'] = (int)$BaseInfo;
  189.                         $this->BaseInfo[$Name] = number_format((int)$BaseInfo);
  190.                         break;
  191.                     default:
  192.                         $this->BaseInfo[$Name] = $BaseInfo;
  193.                         break;
  194.                 }
  195.             }
  196.         }
  197.         if (!$Param) {
  198.             return $this->BaseInfo;
  199.         }
  200.         return $this->BaseInfo[$Param] ?? '' ;
  201.     }
  202.     /**
  203.      * ルーティンファイルを書き込む
  204.      * @param $Yml str YMLファイル名
  205.      * @param $Add array 書き込む配列
  206.      * @param $flg boolean true 追加 false 上書き
  207.      * @param $Path str パス
  208.      * @return Array
  209.      */
  210.     public function RouteYml(array $Data$Yaml self::RouteingFilename)
  211.     {
  212.         $Path $this->RouteingYamlPath;
  213.         $Path .= $Yaml;
  214.         $Yml '#' date('Ymd') . ' ' $Yaml ' RouteYml ' PHP_EOL;
  215.         $Yml .= Yaml::dump($Data);
  216.         $fp fopen($Path'w');
  217.         fwrite($fp$Yml);
  218.         fclose($fp);
  219.     }
  220.     /**
  221.      * Twig\Environment のセット
  222.      * @param Twig $Twig
  223.      */
  224.     public function SetTwig(Twig $Twig)
  225.     {
  226.         $this->Twig $Twig;
  227.         return $this;
  228.     }
  229.     /**
  230.      * Twig\Environment のゲット
  231.      *
  232.      * @return Twig
  233.      */
  234.     public function GetTwig()
  235.     {
  236.         return $this->Twig;
  237.     }
  238.     /**
  239.      * ページレイアウトの設定
  240.      * @param $Nmae string URL の設定
  241.      * @param $isMobile
  242.      * @param $MetaTags メタタグ array() Author Description Keyword Robots MetaTags
  243.      * @param $LayoutId int レイアウトIDの変更
  244.      *
  245.      */
  246.     public function SetPageLayout($Name$isMobile false$MetaTags = [], $LayoutId null)
  247.     {
  248.         $Globals $this->Twig->getGlobals();
  249.         if (empty($Globals['Page']) || is_null($Globals['Page']->getId())) {
  250.             $Page $this->GetPageByRoute($Name);
  251.         } else {
  252.             $Page $Globals['Page'];
  253.         }
  254.         if (count($MetaTags) > 0) {
  255.             #メタタグの設定
  256.             //Author
  257.             if ($Author $MetaTags['Author'] ?? null) {
  258.                 $Page->setAuthor($Author);
  259.             }
  260.             //Description
  261.             if ($Description $MetaTags['Description'] ?? null) {
  262.                 $Page->setDescription($Description);
  263.             }
  264.             //Keyword
  265.             if ($Keyword $MetaTags['Keyword'] ?? null) {
  266.                 $Page->setKeyword($Keyword);
  267.             }
  268.             //Robots
  269.             if ($Robots $MetaTags['Robots'] ?? null) {
  270.                 $Page->setMetaRobots($Robots);
  271.             }
  272.             #MetaTags
  273.             if ($MetaTagss $MetaTags['MetaTags'] ?? null) {
  274.                 $Page->setMetaTags($MetaTagss);
  275.             }
  276.         }
  277.         #ページレイアウト
  278.         /**
  279.          * @var PageLayout[] $Layouts
  280.          */
  281.         $Layouts $Page->getPageLayouts();
  282.         if (empty($LayoutId)) {
  283.             //
  284.             $LayoutId $Layouts[0]->getLayoutId() ?? Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE;
  285.             //
  286.             if ($isMobile) {
  287.                 //
  288.                 foreach ($Layouts as $pageLayout) {
  289.                     //
  290.                     if ($pageLayout->getDeviceTypeId() === DeviceType::DEVICE_TYPE_MB) {
  291.                         //
  292.                         $LayoutId $pageLayout->getLayoutId();
  293.                     }
  294.                 }
  295.             }
  296.         }
  297.         $em $this->Container->get('doctrine.orm.entity_manager');
  298.         $Layout $em->getRepository(Layout::class)->find($LayoutId);
  299.         $this->Twig->addGlobal('Layout'$Layout);
  300.         $this->Twig->addGlobal('Page'$Page);
  301.     }
  302.     /**
  303.      * ページレイアウトの設定
  304.      * Eccube\Repository\PageRepository
  305.      * GetPageByRoute のコピー
  306.      *
  307.      * @param $Nmae string URL の設定
  308.      * @return Page
  309.      */
  310.     protected function GetPageByRoute($Name)
  311.     {
  312.         /**
  313.          * @var EntityManager $em
  314.          */
  315.         $em $this->Container->get('doctrine.orm.entity_manager');
  316.         $Qb $em->createQueryBuilder();
  317.         $Page $Qb
  318.             ->select(['p''pl''l'])
  319.             ->from("Eccube\\Entity\\page"'p')
  320.             ->leftJoin('p.PageLayouts''pl')
  321.             ->leftJoin('pl.Layout''l')
  322.             ->where('p.url = :url')
  323.             ->setParameter('url'$Name)
  324.             ->getQuery()
  325.             ->useResultCache(true3600)
  326.             ->getSingleResult();
  327.         return $Page;
  328.     }
  329.     /**
  330.      * EC-CUBEの時間をセットする 通常EC-Cubeの時間はUTC
  331.      * 9時間引いたUTC
  332.      *
  333.      * @param $DataTime string 2016-10-18 16:19:02
  334.      * @param Flg boolean
  335.      *
  336.      * @return Flg=true UTC object
  337.      */
  338.     public function SetEcTime($DataTime null$Flg false)
  339.     {
  340.         $Data = new \DateTime($DataTime);
  341.         return $Flg $Data->modify('-9 hours') : $Data;
  342.     }
  343.     /**
  344.      * EC-CUBE URLのリダイレクト
  345.      *
  346.      *
  347.      * @param URL string
  348.      * @param param array()
  349.      *
  350.      */
  351.     public function Redirect($Url$Param = [])
  352.     {
  353.         $Router $this->Container->get('router');
  354.         $Rout $Router->generate($Url$Param);
  355.         return new RedirectResponse($Rout);
  356.         exit;
  357.     }
  358.     /**
  359.      * EC-CUBE URLのリダイレクト
  360.      *  shimfony のRedirectResponse ではリダイレクトしない場合がある
  361.      *
  362.      * @param URL string
  363.      * @param param array()
  364.      *
  365.      */
  366.     public function Redirect2($Url$Param = [])
  367.     {
  368.         $Router $this->Container->get('router');
  369.         $Rout $Router->generate($Url$Param);
  370.         header('Location: ' $Rout);
  371.         exit;
  372.     }
  373.     public function GetPhoneNumber($PhoneNumber$Flg=true)
  374.     {
  375.         $PhoneNumber str_replace(['-'], ''$PhoneNumber);
  376.         if (!$PhoneNumber) {
  377.             return  $Flg ? [null,null,null] : null;
  378.         }
  379.         $RePhon[2] = substr($PhoneNumber, -4);
  380.         $AreaCodes $this->GetYaml('PhoneNumber.yaml');
  381.         for ($i 5$i >= 2$i--) {
  382.             $Code substr($PhoneNumber0$i);
  383.             if (isset($AreaCodes[$Code])) {
  384.                 $RePhon[0] = $Code;
  385.                 break;
  386.             }
  387.         }
  388.         $Len1 strlen($PhoneNumber);
  389.         if (isset($RePhon[0])) {
  390.             $len2 strlen($RePhon[0]);
  391.             $RePhon[1] = substr($PhoneNumber$len2$Len1 $len2 4);
  392.         } else {
  393.             $RePhon[0] = substr($PhoneNumber0$Len1 8);
  394.             $RePhon[1] = substr($PhoneNumber$Len1 8,  4);
  395.         }
  396.         for ($i 0$i <= 2$i++) {
  397.             if (!isset($RePhon[$i])) {
  398.                 $RePhon[$i] = null;
  399.             }
  400.         }
  401.         return $Flg $RePhon $RePhon[0] .'-' .$RePhon[1] .'-' $RePhon[2];
  402.     }
  403.     /**
  404.      * 乱数を取得する
  405.      *
  406.      * @param int 桁数
  407.      * @return string  乱数
  408.      */
  409.     public static function GetRand($Num)
  410.     {
  411.         $ReRand '';
  412.         $Arr array_merge(range('a''z'), range('A''Z'), range(09));
  413.         for ($i 0$i $Num$i++) {
  414.             $ReRand .= $Arr[rand(0count($Arr) - 1)];
  415.         }
  416.         return $ReRand;
  417.     }
  418.     public function GetLayout($LayoutId$Page)
  419.     {
  420.         $em $this->Container->get('doctrine.orm.entity_manager');
  421.         $Layout $em->getRepository(Layout::class)->find($LayoutId);
  422.         $this->Container;
  423.         $T $Page->getPageLayouts();
  424.         //foreach ($Page->getPageLayouts() as $key =>$d){
  425.         //echo $ke;
  426.         //}
  427.         return $Layout;
  428.     }
  429.     /**
  430.      * LM の支払いIDで EC-Cubuno支払い先をGETする
  431.      *
  432.      * @param inr LN paymentId
  433.      *
  434.      * @return ID`が  NULLの場合 全ての配列を返す  array()
  435.      * @return ID が ある場合 名前を返す
  436.      */
  437.     public function GetLmPayment($Id null)
  438.     {
  439.         if (count($this->LmPayments) < 1) {
  440.             $em $this->Container->get('doctrine.orm.entity_manager');
  441.             $Payments $em->getRepository(Payment::class)->findAll();
  442.             $Map $this->GetYaml('PaymentMapping.yaml');
  443.             foreach ($Payments as $Payment) {
  444.                 if (!$LmId = ($Map[$Payment->getId()] ?? 0)) {
  445.                     continue;
  446.                 }
  447.                 $this->LmPayments[$LmId] = $Payment->getMethod();
  448.             }
  449.         }
  450.         return $Id $this->LmPayments[$Id] ?? NULL $this->LmPayments;
  451.     }
  452.     /**
  453.      *  オリジナルトークンを作成
  454.      *
  455.      * @param string $Cd トークン名
  456.      * @param string $name
  457.      * @param string $Value メッセージなど
  458.      *
  459.      * @param this
  460.      */
  461.     public function SetToken($Cd$Name 'token'$Value null)
  462.     {
  463.         $Token $this->Session->get(self::OliginalToken) ?? [];
  464.         $Token[$Cd][$Name] = $Value ?? md5($this->GetRand(8));
  465.         $this->Session->set(self::OliginalToken$Token);
  466.         return $this;
  467.     }
  468.     public function ResetToken($Cd$Name 'token'$Value null){
  469.         return $this->RemoveToken($Cd)
  470.                     ->SetToken($Cd,$Name,$Value)
  471.                     ->GetToken($Cd,$Name);
  472.     }
  473.     /**
  474.      *  オリジナルトークントークンを返す
  475.      *
  476.      * @param string $Cd
  477.      * @param string $Token
  478.      *
  479.      * @param string
  480.      */
  481.     public function GetToken($Cd$Name 'token')
  482.     {
  483.         $Data $this->Session->get(self::OliginalToken);
  484.         return $Data[$Cd][$Name] ?? null;
  485.     }
  486.     /**
  487.      *   オリジナルトークントークンをリセット
  488.      * @param string $Cd
  489.      *
  490.      * @param string
  491.      */
  492.     public function RemoveToken($Cd)
  493.     {
  494.         $Token $this->Session->get(self::OliginalToken);
  495.         Unset($Token[$Cd]);
  496.         $this->Session->set(self::OliginalToken,$Token);
  497.         return $this;
  498.     }
  499.     /**
  500.      * YAML からアンケートを作成する
  501.      * @param string $YAML
  502.      * @param string $Param
  503.      */
  504.     public function SetSurvey($Yaml 'survey.yaml'$Param self::Lm_Order_Option$Option null)
  505.     {
  506.         if (!$Option) {
  507.             $Option $this->GetLmOrderOption(self::Lm_Option_Survey);
  508.             #1 初期化
  509.             $this->InitializeLmOrderOption(self::Lm_Option_Survey);
  510.         }
  511.         $Html = function ($flg$Type$Title$Val$Parameter$Sub '') use ($Param$Option) {
  512.             if (!= $flg) {
  513.                 $Name "name=\"{$Param}[" self::Lm_Option_Survey "][{$Title}][{$Val['name']}]\"";
  514.             } else {
  515.                 $Name "name=\"{$Param}[" self::Lm_Option_Survey "][$Title]\"";
  516.             }
  517.             switch ($Type) {
  518.                 case 'select':
  519.                     $Re "<select {$Name} >";
  520.                     $Datas $Val['data'] ?? [];
  521.                     foreach ($Datas as $Data) {
  522.                         $Selected '';
  523.                         if ($Option) {
  524.                             $Selected $Data == ($Option[$Title][$Val['name']] ?? '') ? 'selected' '';
  525.                         }
  526.                         $Re .= "<option value=\"{$Data}\" {$Selected}>{$Data}</oprion>";
  527.                     }
  528.                     $Re .= '</select>';
  529.                     break;
  530.                 case 'for':
  531.                     $Name rtrim($Name'"') . '[]"';
  532.                     $Re '';
  533.                     $Unit $Parameter[$Val['name']]['unit'] ?? '';
  534.                     foreach ($Val['data'] as $Key => $Data) {
  535.                         if (is_array($Data)) {
  536.                             $Re .= "<select {$Name}><option value=\"\"></option>";
  537.                             foreach ($Data as $i) {
  538.                                 $Selected '';
  539.                                 if ($Option) {
  540.                                     $Selected $i == ($Option[$Title][$Val['name']][$Key] ?? '999') ? 'selected' '';
  541.                                 }
  542.                                 $Re .= "<option value=\"{$i}\" {$Selected}>{$i}</option>";
  543.                             }
  544.                             $Re .= "</select>" . ($Unit[$Key] ?? '');
  545.                         } else {
  546.                             $Da explode('-'$Data);
  547.                             $Re .= "<select {$Name}><option value=\"\"></oprion>";
  548.                             for ($i $Da[0]; $i <= $Da[1]; $i++) {
  549.                                 $Selected '';
  550.                                 if ($Option) {
  551.                                     $Selected $i == ($Option[$Title][$Val['name']][$Key] ?? '999') ? 'selected' '';
  552.                                 }
  553.                                 $Re .= "<option value=\"{$i}\" $Selected>{$i}</oprion>";
  554.                             }
  555.                             $Re .= "</select>" $Unit[$Key] ?? '';
  556.                         }
  557.                     }
  558.                     break;
  559.                 case 'text':
  560.                     $Unit $Parameter[$Val['name']]['unit'] ?? '';
  561.                     $Value $Option[$Title][$Val['name']] ?? $Val['data'];
  562.                     $Re "<input type=\"text\" {$Name} value=\"{$Value}\">" $Unit;
  563.                     break;
  564.                 case 'radio';
  565.                     deffault:
  566.                     $Name rtrim($Name'"') . '[0]"';
  567.                     $Checked '';
  568.                     if ($Option) {
  569.                         $Checked $Val == ($Option[$Title][0] ?? '') ? 'checked="chekced"' '';
  570.                     }
  571.                     $Re "<label><input type=\"radio\" {$Name} value=\"{$Val}\" {$Checked}>{$Val}</label>";
  572.             }
  573.             if (== $flg) {
  574.                 $Re "<p>{$Re}{$Sub}</p>";
  575.             }
  576.             return $Re;
  577.         };
  578.         $Re '';
  579.         foreach ($this->GetYaml($Yaml) as $key => $Value) {
  580.             $Re .= "<div class=\"{$key}\"><p class =\"title\">{$Value['title']}</p>" PHP_EOL;
  581.             foreach ($Value['data'] as $Data) {
  582.                 $Sub '';
  583.                 if (is_array($Data)) {
  584.                     $Val key($Data);
  585.                     $Arr $Data[$Val];
  586.                     $Type $Arr['type'] ?? 'select';
  587.                     $Parameter $Value['parameter'] ?? '';
  588.                     $Sub $Html(1$Arr['type'], $key$Arr$Parameter);
  589.                 } else {
  590.                     $Val $Data;
  591.                 }
  592.                 $Type $Data['type'] ?? 'radio';
  593.                 $Parameter '';
  594.                 $Re .= $Html(0$Type$key$Val$Parameter$Sub);
  595.             }
  596.             foreach ($Value['span'] ?? [] as $span) {
  597.                 $Re .= "<span>{$span}</span>";
  598.             }
  599.             $Re .= '</div>' PHP_EOL;
  600.         }
  601.         return $Re;
  602.     }
  603.     /**
  604.      * 2022/03/15
  605.      * LM orede option を初期化する
  606.      * アマゾンぺがあるために コントローラーではなくここでセット
  607.      *         
  608.      */
  609.     public function InitializeLmOrderOption($Name null)
  610.     {
  611.         #1 初期化
  612.         if (is_null($Name)) {
  613.             $this->Session->set(self::Lm_Order_Option_Sessionnull);
  614.             return;
  615.         }
  616.         #2 $Name のみ
  617.         $Option $this->Session->Get(self::Lm_Order_Option_Session);
  618.         unset($Option[$Name]);
  619.         $this->Session->set(self::Lm_Order_Option_Session$Option);
  620.         return;
  621.     }
  622.     /**
  623.      * 2022/03/18
  624.      * LM orede option を初期化する
  625.      * アマゾンぺイがあるために コントローラーではなくここでセット
  626.      *         
  627.      */
  628.     public function SetInitiarizeLmOrederOption($OrderId)
  629.     {
  630.         $OptionOrderid $this->GetLmOrderOption(self::Lm_Option_OrderId);
  631.         switch (true) {
  632.             case is_null($OptionOrderid):
  633.             case $OptionOrderid != $OrderId:
  634.                 $Catalog $this->GetLmOrderOption(self::Lm_Option_Catalog);
  635.                 $this->InitializeLmOrderOption();
  636.                 $this->AddLmOrderOption(self::Lm_Option_Catalog$Catalog);
  637.                 $this->AddLmOrderOption(self::Lm_Option_OrderId$OrderId);
  638.             case $OptionOrderid == $OrderId:
  639.             default:
  640.                 ##
  641.                 break;
  642.         }
  643.         return;
  644.     }
  645.     /**
  646.      * 2022/03/15
  647.      * LM orede option トする
  648.      * Shopping でできたDATAをconfirmでせっと
  649.      * 貸出サンプルのカタログ同梱 sample_catalog
  650.      * アンケート
  651.      *
  652.      *
  653.      * アマゾンぺイがあるために コントローラーではなくここでセット
  654.      *         
  655.      */
  656.     public function RequestLmOrderOption()
  657.     {
  658.         #2022/03/15 kakeru Order OPtion の取得
  659.         if ($Datas $this->Request->get(self::Lm_Order_Option)) {
  660.             foreach ($Datas as $Name => $Data) {
  661.                 $this->AddLmOrderOption($Name$Data);
  662.             }
  663.         }
  664.     }
  665.     /**
  666.      * 2022/03/17
  667.      * LM orede option をセットする
  668.      * アマゾンぺイがあるために コントローラーではなくここでセット
  669.      *         
  670.      */
  671.     public function AddLmOrderOption($Name$Data)
  672.     {
  673.         #2022/03/15 kakeru Order OPtion の取得
  674.         #1 Dataの取得
  675.         $Option $this->Session->get(self::Lm_Order_Option_Session);
  676.         #2 設定
  677.         $Option[$Name] = $Data;
  678.         $this->Session->set(self::Lm_Order_Option_Session$Option);
  679.         //
  680.         $this->OrderOptionsService->addOrderOptions($Name$Data);
  681.     }
  682.     /**
  683.      * 2022/03/15
  684.      * LM orede option をセットする
  685.      * アマゾンぺイイがあるために コントローラーではなくここでセット
  686.      *         
  687.      */
  688.     public function GetLmOrderOption($Name null)
  689.     {
  690.         return $this->OrderOptionsService->getOrderOptions($Name) ?: $this->_GetLmOrderOption($Name);
  691.     }
  692.     protected function _GetLmOrderOption($Name null)
  693.     {
  694.         return $this->OrderOptionsService->_GetLmOrderOption($Name);
  695.     }
  696.     /**
  697.      * 確認画面にアンケートを表示する
  698.      * アンケートのDATaはバラバラなので ここで保存する
  699.      *
  700.      */
  701.     public function SetSurveyData()
  702.     {
  703.         $Servey $this->GetLmOrderOption(self::Lm_Option_Survey);
  704.         $Re = [];
  705.         foreach ($this->GetYaml('survey.yaml') as $key => $Value) {
  706.             if (!isset($Servey[$key][0])) {
  707.                 continue;
  708.             }
  709.             $Selected $Servey[$key][0];
  710.             $Text '';
  711.             $Parameters $Value['parameter'] ?? [];
  712.             foreach ($Value['data'] as $Data) {
  713.                 if (is_array($Data)) {
  714.                     $Label key($Data);
  715.                     $Config $Data[$Label];
  716.                     if ($Label !== $Selected) {
  717.                         continue;
  718.                     }
  719.                     $Text $Label;
  720.                     $Name $Config['name'] ?? null;
  721.                     if ($Name && isset($Servey[$key][$Name])) {
  722.                         $Answer $Servey[$key][$Name];
  723.                         $Unit $Parameters[$Name]['unit'] ?? '';
  724.                         if (is_array($Answer)) {
  725.                             foreach ($Answer as $i => $Da) {
  726.                                 if ($Da === '' || $Da === null) {
  727.                                     continue;
  728.                                 }
  729.                                 $UnitText is_array($Unit) ? ($Unit[$i] ?? '') : $Unit;
  730.                                 $Text .= ' ' $Da $UnitText;
  731.                             }
  732.                         } elseif ($Answer !== '' && $Answer !== null) {
  733.                             $UnitText is_array($Unit) ? ($Unit[0] ?? '') : $Unit;
  734.                             $Text .= ' ' $Answer $UnitText;
  735.                         }
  736.                     }
  737.                     break;
  738.                 }
  739.                 if ($Data === $Selected) {
  740.                     $Text $Selected;
  741.                     break;
  742.                 }
  743.             }
  744.             if ($Text === '') {
  745.                 $Text $Selected;
  746.             }
  747.             $Re[] = $Value['title'];
  748.             $Re[] = trim($Text);
  749.         }
  750.         $this->AddLmOrderOption(self::Lm_Option_Survey_Data$Re);
  751.         return $Re;
  752.     }
  753.     /**
  754.      * デバイスの判定
  755.      *
  756.      * @param $strict
  757.      * @return string
  758.      *  SP ・・・ スマホ
  759.      *  PC ・・・ パソコン
  760.      *  TB ・・・ タブレット ※パラメータ $strict = true 時のみ
  761.      */
  762.     public function GetDevice($strict falseMobileDetector $mobileDetector null)
  763.     {
  764.         //
  765.         if ($mobileDetector === null) {
  766.             $mobileDetector $this->MobileDetector;
  767.         }
  768.         //
  769.         if ($strict && $mobileDetector->isTablet()) {
  770.             // タブレット
  771.             return 'TB';
  772.         } else if ($mobileDetector->isMobile()) {
  773.             // スマホ
  774.             // $strict = false のとき、タブレットも含む。
  775.             return 'SP';
  776.         } else {
  777.             // PC
  778.             return 'PC';
  779.         }
  780.     }
  781.     /**
  782.      * 会員を取得する
  783.      *
  784.      */
  785.     public function getUser()
  786.     {
  787.         if (null === $token $this->TokenStorage->getToken()) {
  788.             return;
  789.         }
  790.         if (!is_object($user $token->getUser())) {
  791.             // e.g. anonymous authentication
  792.             return;
  793.         }
  794.         return $user;
  795.     }
  796.     /**
  797.      * メッセージのbaseinfo config Messegeを 変換する
  798.      * 例 
  799.      *  {{BaseInfo.shop_name}}
  800.      *  {{Config.Payment_Charge_free}}
  801.      *  {{message.common.ResetPassward_messeg02}} or {{trans.common.ResetPassward_messeg02}}
  802.      */
  803.     public function BaseInfo($Text){
  804.         if (preg_match('/^[A-Za-z0-9_]+$/',$Text)){
  805.             return $this->GetBaseInfo($Text);
  806.         }
  807.         while(preg_match("/\{\{(.+?)\}\}/",$Text,$Param)){
  808.             $Pa =explode('.',$Param[1]);
  809.             $Parameter trim($Pa[1]);
  810.             $Value='';
  811.             switch (true) {
  812.                 case preg_match('/BaseInfo\./i',$Param[1]):
  813.                     $Value $this->GetBaseInfo($Parameter);
  814.                     break;
  815.                 case preg_match('/Config\./i'$Param[1]):
  816.                     $Value $this->GetConfig($Parameter);
  817.                     break;
  818.                 case preg_match('/Message\.|trans\./i'$Param[1]):
  819.                     $Parameter preg_replace('/message\.|trans\./i','',$Param[1]);
  820.                     $Value trans($Parameter) ?? '' ;
  821.                     break;
  822.                 case preg_match('/url\./i'$Param[1]):
  823.                     $Value $this->Router->generate($Parameter);
  824.                     break;
  825.                 case preg_match('/Other\./i'$Param[1]):
  826.                     switch ($Parameter){
  827.                         case 'LargeDiscount':
  828.                             $Value $this->BaseInfoLageDiscount();
  829.                         break;
  830.                         case 'DeliveryFree';
  831.                             $Value $this->BaseInfoDeliveryFree();
  832.                         break;
  833.                         case 'Charge':
  834.                             $Value $this->BaseInfoCharge();
  835.                         break;
  836.                     }
  837.                     break;
  838.                  default:
  839.                     $Value $Parameter;
  840.                     break;
  841.             }
  842.             if (!$Value){$Value=$Parameter;}
  843.             $Text str_replace($Param[0],$Value,$Text);
  844.         }
  845.         return $Text;
  846.     }
  847.     /**
  848.      * 大口割引のDATAをもとに文字化する
  849.      *  
  850.      * @return string
  851.      */
  852.     protected function BaseInfoLageDiscount(){
  853.         $Re= [];
  854.         $LargeDiscount $this->GetConfig('LargeDiscount');
  855.         foreach ($LargeDiscount as $Discount){
  856.             $Re[] =$Discount['more']/10000 .'万円以上で'.$Discount['discount'].'%';
  857.         }
  858.         return implode('、',$Re);
  859.     }
  860.     /**
  861.      * 全国一律 東京の運賃を算出
  862.      *
  863.      */
  864.     protected function BaseInfoDeliveryFree(){
  865.         $DeliveryFee $this->DeliveryFeeRepository->find(13);
  866.         return number_format($DeliveryFee->getFee());
  867.     }
  868.     protected function BaseInfoCharge(){
  869.         $em $this->Container->get('doctrine.orm.entity_manager');
  870.         $Payment $em->getRepository(Payment::class)->find(4);
  871.         return number_format($Payment->getCharge());
  872.     }
  873.     public function getSpOtherCategoryPulldownMap()
  874.     {
  875.         return array(
  876.             'item' => array(
  877.                 'group_label' => '商品で探す',
  878.                 'map' => array(
  879.                     "/polo/"                => 'ポロシャツ',
  880.                     "/shirt/"            => 'シャツ',
  881.                     "/tshirt/"            => 'Tシャツ',
  882.                     "/staff/"            => 'ジャンパー',
  883.                     "/vest/"                => 'ベスト',
  884.                     "/warm/"                => '防寒服',
  885.                     "/sweat/"            => 'トレーナー',
  886.                     "/pants/"            => 'パンツ',
  887.                     "/food-uniform/cook-coat/"            => 'コックコート',
  888.                     "/apron/"            => 'エプロン',
  889.                     "/shirt/aloha/"        => 'アロハシャツ',
  890.                     "/happi/"            => 'はっぴ',
  891.                     "/cap/"                => 'キャップ',
  892.                     "/overall/"            => 'つなぎ',
  893.                     "/anzengutsu/"        => '安全靴',
  894.                     "/towel/"            => 'タオル',
  895.                     "/scrub/"            => 'スクラブ',
  896.                     "/bag/"                => 'バッグ'
  897.                 )
  898.             ),
  899.             'use' => array(
  900.                 'group_label' => '目的・用途で探す',
  901.                 'map' => array(
  902.                     "/kucho-fuku/"            => '空調服®・エアークラフト',
  903.                     "/work/"                => '作業服',
  904.                     "/jimufuku/"            => '事務服',
  905.                     "/hakui/"                => '医療白衣',
  906.                     "/food-uniform/"        => '飲食店',
  907.                     "/esthe-uniform/"    => 'エステ',
  908.                     "/election-item/"    => '選挙用品',
  909.                     "/care-wear/"        => '介護服',
  910.                     "/patrol/"            => '防犯パトロール',
  911.                     "/food-uniform/formal-vest/"    => 'フォーマル',
  912.                 )
  913.             )
  914.         );
  915.     }
  916.     /**
  917.      * @param $src
  918.      * @param $attrname
  919.      * @param array $dst
  920.      * @return string
  921.      */
  922.     public static function addAttrRegexp($src$attrname, array $dst)
  923.     {
  924.         $attrs preg_replace_callback("/{$attrname}=\"([^\"]+)\"/", function ($matches) {
  925.             return $matches[1];
  926.         }, $src);
  927.         $attrs explode(' '$attrs);
  928.         $attrs $dst $attrs;
  929.         $attrs implode(' '$attrs);
  930.         return $attrs;
  931.     }
  932.     public function GetJunctionMode(){
  933.         return $this->Session->get(self::Session_Junction_Mode);
  934.     }
  935.     public function GetExclusionControl($Type$Message null)
  936.     {
  937.         $Message $Message ?? $this->SetExclusionControl($Type1);
  938.         return ['errid' => 'message' => $Message ,'class_id'=>,'add_lines' => null];
  939.     }
  940.     /**
  941.      * カート排他制御
  942.      *
  943.      */
  944.     public function SetExclusionControl($Type ,$Flg)
  945.     {
  946.         $TypeName01 $this->SetTypeName($this->LmHelper->GetCartType());
  947.         $TypeName02 $this->SetTypeName($Type);
  948.         //$Url = $this->Router->generate('cart');
  949.         $Message trans('front.ExclusionControl.message0' $Flg);
  950.         $Message str_replace('@TypeName01'$TypeName01$Message);
  951.         $Message str_replace('@TypeName02'$TypeName02$Message);
  952.         //$Message = str_replace('@URL', $Url, $Message);
  953.         return $Message;
  954.     }
  955.     protected function SetTypeName($Type)
  956.     {
  957.         switch ($Type) {
  958.             case 3#CartService::CartTypeSample:
  959.                 $TypeName trans('front.cart_Sample');
  960.                 break;
  961.             case 4:  #CartService::CartTypeCatalog:
  962.                 $TypeName trans('front.cart_catalog');
  963.                 break;
  964.             case 101:
  965.                 $TypeName trans('front.Cart.Repeat');
  966.                 break;
  967.             default:
  968.                 $TypeName trans('front.ExclusionControl.message03');
  969.                 break;
  970.         }
  971.         return $TypeName;
  972.     }
  973.     /**
  974.      * 配列に一個でもNULLか空有白 がある
  975.      * @param array
  976.      *
  977.      * @return boolean ある true ない false
  978.      */
  979.     public function IsNull($Data){
  980.        if(in_array(null$Data,true)){return true;};
  981.        return  in_array(''$Data,true);
  982.     }
  983.     /**
  984.      * app/Customize/Twig/Extension/ServiceExtension.php から移動
  985.      *
  986.      */
  987.     public function GetPagename($UrlName)
  988.     {
  989.        $MetaTags $this->GetYaml(self::MetaTagData);
  990.        return  !empty($MetaTags[$UrlName]['PageName']) ? $MetaTags[$UrlName]['PageName'] : '';
  991.     }
  992.     public function generateSetSiteTypeValueForCriteoReturn()
  993.     {
  994.         if ($this->MobileDetector->isTablet()) {
  995.             return 't';
  996.         } else if ($this->MobileDetector->isMobile()) {
  997.             return 'm';
  998.         } else {
  999.             return 'd';
  1000.         }
  1001.     }
  1002.     public function get_page_type ($route null) {
  1003.         //
  1004.         if (empty($route)) {
  1005.             $route $this->Request->get('_route');
  1006.         }
  1007.         $yahoo_retargeting_page_type '';
  1008.         // PC
  1009.         switch (true) {
  1010.             case $route === 'homepage':
  1011.                 // トップページ
  1012.                 $yahoo_retargeting_page_type 'home';
  1013.                 break;
  1014.             case $route === 'page/main-category':
  1015.             case $route === 'page/sub-category':
  1016.                 // 商品一覧ページ - 親カテ・子カテ・コラム
  1017.                 $yahoo_retargeting_page_type 'category';
  1018.                 break;
  1019.             case $route === 'item_searchlist':
  1020.                 // 検索結果ページ
  1021.                 $yahoo_retargeting_page_type 'search';
  1022.                 break;
  1023.             case $route === 'item/detail':
  1024.                 // 商品詳細ページ
  1025.                 $yahoo_retargeting_page_type 'detail';
  1026.                 break;
  1027.             case $route === 'cart':
  1028.             case $route === 'shopping_login':
  1029.             case $route === 'shopping':
  1030.             case $route === 'amazon_pay_shopping':
  1031.                 // 購入
  1032.                 // カートページ
  1033.                 $yahoo_retargeting_page_type 'cart';
  1034.                 break;
  1035.             case $route === 'shopping_confirm':
  1036.             case $route === 'amazon_pay_shopping_confirm':
  1037.                 // 購入
  1038.                 // 購入、申込ページ(コンバージョン直前)
  1039.                 $yahoo_retargeting_page_type 'conversionintent';
  1040.                 break;
  1041.             case $route === 'shopping_complete':
  1042.                 // 購入
  1043.                 // コンバージョンページ
  1044.                 $yahoo_retargeting_page_type 'conversion';
  1045.                 break;
  1046.         }
  1047.         return $yahoo_retargeting_page_type;
  1048.     }
  1049.     public function get_yahoo_retargeting_page_type ($route null) {
  1050.         //
  1051.         return $this->get_page_type($route);
  1052.     }
  1053.     public function get_google_retargeting_item($goodsId$google_business_vertical 'retail')
  1054.     {
  1055.         try {
  1056.             return GoodsWithRelated::getFeedGoodsByIdForGDN($goodsId$google_business_vertical);
  1057.         } catch (\Exception $e) {
  1058.             return null;
  1059.         }
  1060.     }
  1061.     public function get_yahoo_retargeting_item($goodsId$quantity 1)
  1062.     {
  1063.         try {
  1064.             return GoodsWithRelated::getFeedGoodsByIdForYDN($goodsId$quantity);
  1065.         } catch (\Exception $e) {
  1066.             return null;
  1067.         }
  1068.     }
  1069.     public function EigyoubiCalendar()
  1070.     {
  1071.         $cal="";
  1072.         // Google PSI対応
  1073.         // ファイル・キャッシュ化
  1074.         // / $s3 = new FileStoreService(env('S3_LM_DOC'), self::TOP_DIR);
  1075.         // / $Filename = "1day4cal.inc";
  1076.         // / $cal = $s3->getObject($Filename);
  1077.         if (file_exists($filename 'files/top/1day4cal.inc')) {
  1078.             //
  1079.             $cal file_get_contents($filename);
  1080.         }
  1081.         //
  1082.         return $cal;
  1083.     }
  1084.     /**
  1085.      * @param Cart[] $carts
  1086.      * @return int
  1087.      */
  1088.     public function calculateTotalQuantityForCarts(array $carts = [])
  1089.     {
  1090.         //
  1091.         $totalQuantity 0;
  1092.         //
  1093.         foreach ($carts as $cart) {
  1094.             //
  1095.             $totalQuantity += $cart->getQuantity();
  1096.         }
  1097.         //
  1098.         return $totalQuantity;
  1099.     }
  1100.     /**
  1101.      * @param Order $order
  1102.      * @return int
  1103.      */
  1104.     public function calculateTotalQuantityForOrder(Order $order)
  1105.     {
  1106.         //
  1107.         $totalQuantity 0;
  1108.         //
  1109.         foreach ($order->getProductOrderItems() as $orderItem) {
  1110.             //
  1111.             $totalQuantity += $orderItem->getQuantity();
  1112.         }
  1113.         //
  1114.         return $totalQuantity;
  1115.     }
  1116.     public function getSendScheduleDate($orderHeader, &$message null)
  1117.     {
  1118.         if ($oh OrderHeader::factory($orderHeader)) {
  1119.             return (new NoukiEngine())->calculate($ohnull$message);
  1120.         }
  1121.         return null;
  1122.     }
  1123. }