diff --git a/src/enums/Options.php b/src/enums/Options.php index 6b54421..682fc65 100644 --- a/src/enums/Options.php +++ b/src/enums/Options.php @@ -22,9 +22,10 @@ abstract class Options * Available event types. */ public const EVENT_TYPE = [ - 'users' => 'Users', - 'entries' => 'Entries', - 'assets' => 'Assets', + 'users' => 'Users', + 'entries' => 'Entries', + 'assets' => 'Assets', + 'commerce-orders' => 'Commerce Orders', ]; /** @@ -82,6 +83,18 @@ abstract class Options // 'value' => 'after-delete-asset', //// 'class' => 'craft\elements\Asset::EVENT_AFTER_PROPAGATE' // ] + ], + 'commerce-orders' => [ + [ + 'label' => 'When an order is completed (placed)', + 'value' => 'after-complete-order', + 'class' => 'craft\commerce\elements\Order::EVENT_AFTER_COMPLETE_ORDER' + ], + [ + 'label' => 'When an order is fully paid', + 'value' => 'after-order-paid', + 'class' => 'craft\commerce\elements\Order::EVENT_AFTER_ORDER_PAID' + ], ] ]; diff --git a/src/helpers/events/CommerceOrderEvents.php b/src/helpers/events/CommerceOrderEvents.php new file mode 100644 index 0000000..d6347d6 --- /dev/null +++ b/src/helpers/events/CommerceOrderEvents.php @@ -0,0 +1,84 @@ +sender instanceof Order)) { + return; + } + + $order = $event->sender; + + // Get all notifications for this event + $notifications = Notification::find() + ->where([ + 'eventType' => 'commerce-orders', + 'event' => 'after-complete-order', + ]) + ->all(); + + // Send all matching notifications + NotifierPlugin::getInstance()->messages->sendAll($notifications, $event, [ + 'object' => $order, + 'order' => $order, + ]); + } + + /** + * When an order is fully paid. + * + * @param Event $event + * @return void + */ + public static function afterOrderPaid(Event $event): void + { + if (!($event->sender instanceof Order)) { + return; + } + + $order = $event->sender; + + // Get all notifications for this event + $notifications = Notification::find() + ->where([ + 'eventType' => 'commerce-orders', + 'event' => 'after-order-paid', + ]) + ->all(); + + // Send all matching notifications + NotifierPlugin::getInstance()->messages->sendAll($notifications, $event, [ + 'object' => $order, + 'order' => $order, + ]); + } + +} diff --git a/src/models/Dispatch.php b/src/models/Dispatch.php index e48704b..bc01664 100644 --- a/src/models/Dispatch.php +++ b/src/models/Dispatch.php @@ -174,6 +174,7 @@ public function filterByEventType(): bool return $this->_filterEntries(); case 'users': case 'assets': + case 'commerce-orders': // No further filters return true; } @@ -593,7 +594,8 @@ private function _parseTwig(array $config, string $text): string $vars = [ // Event Variables 'event' => $this->event, - 'object' => $this->event->sender, + // Unique case for User Activated events + 'object' => $this->event->user ?? $this->event->sender, // People Variables 'recipient' => $recipient, // Element Variables diff --git a/src/services/Events.php b/src/services/Events.php index b934b25..43af429 100644 --- a/src/services/Events.php +++ b/src/services/Events.php @@ -15,6 +15,7 @@ use craft\elements\Asset; use craft\elements\Entry; use craft\elements\User; +use craft\commerce\elements\Order; use craft\events\RegisterComponentTypesEvent; use craft\services\Users; use doublesecretagency\notifier\filters\DraftFilter; @@ -23,6 +24,7 @@ use doublesecretagency\notifier\filters\ProvisionalDraftFilter; use doublesecretagency\notifier\filters\RevisionFilter; use doublesecretagency\notifier\helpers\events\AssetEvents; +use doublesecretagency\notifier\helpers\events\CommerceOrderEvents; use doublesecretagency\notifier\helpers\events\EntryEvents; use doublesecretagency\notifier\helpers\events\UserEvents; use yii\base\Event; @@ -72,6 +74,10 @@ public function registerNotificationEvents(): void $this->_registerEntryEvents(); $this->_registerAssetEvents(); $this->_registerUserEvents(); + + if (class_exists(Order::class)) { + $this->_registerCommerceOrderEvents(); + } } // ========================================================================= // @@ -151,6 +157,28 @@ private function _registerUserEvents(): void ); } + /** + * Register all events for Commerce Orders. + * + * @return void + */ + private function _registerCommerceOrderEvents(): void + { + // When an order is completed (placed) + Event::on( + Order::class, + Order::EVENT_AFTER_COMPLETE_ORDER, + [CommerceOrderEvents::class, 'afterCompleteOrder'] + ); + + // When an order is fully paid + Event::on( + Order::class, + Order::EVENT_AFTER_ORDER_PAID, + [CommerceOrderEvents::class, 'afterOrderPaid'] + ); + } + // ========================================================================= // /** diff --git a/src/templates/notifications/_edit/event/commerce-orders.twig b/src/templates/notifications/_edit/event/commerce-orders.twig new file mode 100644 index 0000000..4381c89 --- /dev/null +++ b/src/templates/notifications/_edit/event/commerce-orders.twig @@ -0,0 +1,12 @@ +{% import '_includes/forms' as forms %} + + diff --git a/src/templates/notifications/_edit/event/index.twig b/src/templates/notifications/_edit/event/index.twig index 7ca8f8d..675b98b 100644 --- a/src/templates/notifications/_edit/event/index.twig +++ b/src/templates/notifications/_edit/event/index.twig @@ -17,3 +17,4 @@ {% include 'notifier/notifications/_edit/event/users' %} {% include 'notifier/notifications/_edit/event/entries' %} {% include 'notifier/notifications/_edit/event/assets' %} +{% include 'notifier/notifications/_edit/event/commerce-orders' %} diff --git a/src/utilities/NotificationLog.php b/src/utilities/NotificationLog.php index 77cfa26..5bf4cc2 100644 --- a/src/utilities/NotificationLog.php +++ b/src/utilities/NotificationLog.php @@ -15,6 +15,7 @@ use craft\base\Utility; use craft\helpers\DateTimeHelper; use DateTime; +use DateTimeZone; use doublesecretagency\notifier\records\Log; use Exception; @@ -131,15 +132,17 @@ private static function _getLogs(string $date): array $dayLog = []; // Get system timezone - $timeZone = Craft::$app->timeZone; + $systemTimeZone = new DateTimeZone(Craft::$app->timeZone); + $dateObj = new DateTime($date, $systemTimeZone); - // Convert dateCreated to the system timezone - $dateCreated = "DATE(CONVERT_TZ(dateCreated, 'UTC', '{$timeZone}'))"; + // Start and end of the day in system timezone + $startOfDay = (clone $dateObj)->setTime(0, 0, 0)->format('Y-m-d H:i:s'); + $endOfDay = (clone $dateObj)->setTime(23, 59, 59)->format('Y-m-d H:i:s'); // Get all envelopes on selected day $envelopes = Log::find() - ->where([$dateCreated => $date]) - ->andWhere(['type' => 'envelope']) + ->where(['type' => 'envelope']) + ->andWhere(['between', 'dateCreated', $startOfDay, $endOfDay]) ->orderBy('id') ->all(); diff --git a/src/web/twig/Extension.php b/src/web/twig/Extension.php index e3a4dad..4d2202c 100755 --- a/src/web/twig/Extension.php +++ b/src/web/twig/Extension.php @@ -54,12 +54,21 @@ public function getGlobals(): array // Generate available field options $fieldOptions = $this->_fieldOptions(); + // Configure available events by installed plugins + $eventTypes = Options::EVENT_TYPE; + $allEvents = Options::ALL_EVENTS; + + // Hide Commerce events if Craft Commerce is not installed + if (!class_exists('craft\\commerce\\elements\\Order')) { + unset($eventTypes['commerce-orders'], $allEvents['commerce-orders']); + } + // Return globally accessible variables return [ 'notifier' => new Notifier(), 'notificationOptions' => [ - 'eventType' => Options::EVENT_TYPE, - 'allEvents' => Options::ALL_EVENTS, + 'eventType' => $eventTypes, + 'allEvents' => $allEvents, 'messageType' => Options::MESSAGE_TYPE, 'emailField' => $fieldOptions['email'], 'smsField' => $fieldOptions['sms'],