|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Plugin\OrderStatusColor42; |
| 4 | + |
| 5 | +use Eccube\Event\TemplateEvent; |
| 6 | +use Eccube\Repository\Master\OrderStatusRepository; |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 9 | + |
| 10 | +class Event implements EventSubscriberInterface |
| 11 | +{ |
| 12 | + /** |
| 13 | + * デフォルト色(グレー) |
| 14 | + */ |
| 15 | + const DEFAULT_COLOR = '#999999'; |
| 16 | + |
| 17 | + /** |
| 18 | + * 背景色の透明度(0.0〜1.0) |
| 19 | + */ |
| 20 | + const OPACITY = 0.1; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var OrderStatusRepository |
| 24 | + */ |
| 25 | + protected $orderStatusRepository; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var EntityManagerInterface |
| 29 | + */ |
| 30 | + protected $entityManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * Event constructor. |
| 34 | + * |
| 35 | + * @param OrderStatusRepository $orderStatusRepository |
| 36 | + * @param EntityManagerInterface $entityManager |
| 37 | + */ |
| 38 | + public function __construct(OrderStatusRepository $orderStatusRepository, EntityManagerInterface $entityManager) |
| 39 | + { |
| 40 | + $this->orderStatusRepository = $orderStatusRepository; |
| 41 | + $this->entityManager = $entityManager; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return array |
| 46 | + */ |
| 47 | + public static function getSubscribedEvents() |
| 48 | + { |
| 49 | + return [ |
| 50 | + '@admin/Order/index.twig' => 'onAdminOrderIndex', |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 受注一覧画面にJSを注入 |
| 56 | + * |
| 57 | + * @param TemplateEvent $event |
| 58 | + */ |
| 59 | + public function onAdminOrderIndex(TemplateEvent $event) |
| 60 | + { |
| 61 | + // mtb_order_status_colorテーブルからステータス色を取得 |
| 62 | + $statusColors = []; |
| 63 | + |
| 64 | + // mtb_order_status_colorテーブルから直接データを取得 |
| 65 | + // 注意: このテーブルでは色の値はnameカラムに格納されている |
| 66 | + // 注意: mtb_order_status_colorはEC-CUBE標準テーブルのため、エラーハンドリングは不要 |
| 67 | + $connection = $this->entityManager->getConnection(); |
| 68 | + $sql = 'SELECT id, name FROM mtb_order_status_color ORDER BY id'; |
| 69 | + $results = $connection->fetchAllAssociative($sql); |
| 70 | + |
| 71 | + foreach ($results as $row) { |
| 72 | + $statusId = (int)$row['id']; |
| 73 | + $color = $row['name'] ?: null; // nameカラムに色の値が格納されている |
| 74 | + |
| 75 | + // 色が設定されている場合 |
| 76 | + if ($color) { |
| 77 | + // HEX形式に変換(念の為、#がなければ追加) |
| 78 | + if (strpos($color, '#') !== 0) { |
| 79 | + $color = '#' . $color; |
| 80 | + } |
| 81 | + |
| 82 | + // カラーコードの形式を検証(#RRGGBB または #RGB 形式) |
| 83 | + // 正規表現: #の後に3桁または6桁の16進数(0-9, A-F, a-f) |
| 84 | + if (preg_match('/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/', $color)) { |
| 85 | + $statusColors[$statusId] = $color; |
| 86 | + } else { |
| 87 | + // 無効なカラーコードの場合はデフォルト色を使用 |
| 88 | + $statusColors[$statusId] = self::DEFAULT_COLOR; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // 全ステータスを取得して、色が設定されていないものにデフォルト色(グレー)を設定 |
| 94 | + // 注意: OrderStatusRepositoryはEC-CUBE標準のため、エラーハンドリングは不要 |
| 95 | + $OrderStatuses = $this->orderStatusRepository->findAll(); |
| 96 | + foreach ($OrderStatuses as $OrderStatus) { |
| 97 | + $statusId = $OrderStatus->getId(); |
| 98 | + |
| 99 | + // mtb_order_status_colorから取得した色がない場合、デフォルト色(グレー)を使用 |
| 100 | + if (!isset($statusColors[$statusId])) { |
| 101 | + $statusColors[$statusId] = self::DEFAULT_COLOR; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Shipping.id → OrderStatus.id のマッピングを作成 |
| 106 | + // テンプレートイベントから受注データを取得 |
| 107 | + $shippingIdToStatusIdMap = []; |
| 108 | + |
| 109 | + try { |
| 110 | + $Orders = $event->getParameter('pagination') ? $event->getParameter('pagination')->getItems() : []; |
| 111 | + |
| 112 | + // paginationがない場合は、直接Ordersパラメータを確認 |
| 113 | + if (empty($Orders)) { |
| 114 | + $Orders = $event->getParameter('Orders') ?: []; |
| 115 | + } |
| 116 | + |
| 117 | + // 各OrderからShippingsを取得し、Shipping.id → OrderStatus.id のマッピングを作成 |
| 118 | + foreach ($Orders as $Order) { |
| 119 | + try { |
| 120 | + if (method_exists($Order, 'getOrderStatus') && method_exists($Order, 'getShippings')) { |
| 121 | + $OrderStatus = $Order->getOrderStatus(); |
| 122 | + if ($OrderStatus && method_exists($OrderStatus, 'getId')) { |
| 123 | + $statusId = $OrderStatus->getId(); |
| 124 | + |
| 125 | + // OrderからShippingsを取得 |
| 126 | + $Shippings = $Order->getShippings(); |
| 127 | + if ($Shippings) { |
| 128 | + foreach ($Shippings as $Shipping) { |
| 129 | + if (method_exists($Shipping, 'getId')) { |
| 130 | + $shippingId = $Shipping->getId(); |
| 131 | + $shippingIdToStatusIdMap[$shippingId] = $statusId; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } catch (\Exception $e) { |
| 138 | + // 個別のOrder処理でエラーが発生した場合(ログに記録して続行) |
| 139 | + if (function_exists('log_error')) { |
| 140 | + $orderId = method_exists($Order, 'getId') ? $Order->getId() : 'unknown'; |
| 141 | + \log_error('OrderStatusColor42: Orderの処理中にエラーが発生しました', ['order_id' => $orderId, 'error' => $e->getMessage()]); |
| 142 | + } |
| 143 | + continue; |
| 144 | + } |
| 145 | + } |
| 146 | + } catch (\Exception $e) { |
| 147 | + // Orderの取得に失敗した場合 |
| 148 | + if (function_exists('log_error')) { |
| 149 | + \log_error('OrderStatusColor42: Orderの取得に失敗しました', [$e->getMessage()]); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // テンプレートにパラメータを追加 |
| 154 | + $event->setParameter('statusColors', $statusColors); |
| 155 | + $event->setParameter('shippingIdToStatusIdMap', $shippingIdToStatusIdMap); |
| 156 | + $event->setParameter('opacity', self::OPACITY); |
| 157 | + |
| 158 | + // Twigテンプレートをスニペットとして追加 |
| 159 | + $event->addSnippet('@OrderStatusColor42/admin/order_index_script.twig'); |
| 160 | + } |
| 161 | +} |
0 commit comments