diff --git a/Events.php b/Events.php index 7a3c08e..b97c702 100644 --- a/Events.php +++ b/Events.php @@ -11,6 +11,8 @@ use humhub\modules\fcmPush\services\DriverService; use humhub\modules\fcmPush\widgets\PushNotificationInfoWidget; use humhub\modules\notification\targets\MobileTargetProvider; +use humhub\modules\ui\menu\MenuLink; +use humhub\modules\user\widgets\AccountTopMenu; use humhub\modules\user\widgets\AuthChoice; use humhub\modules\web\pwa\controllers\ManifestController; use humhub\modules\web\pwa\controllers\ServiceWorkerController; @@ -20,9 +22,6 @@ class Events { - private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout'; - private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin'; - public static function onBeforeRequest() { /** @var Module $module */ @@ -88,16 +87,30 @@ public static function onNotificationInfoWidget($event) public static function onLayoutAddonInit($event) { - if (Yii::$app->session->has(self::SESSION_VAR_LOGOUT)) { - MobileAppHelper::unregisterNotificationScript(); // Before Logout - MobileAppHelper::registerLogoutScript(); - Yii::$app->session->remove(self::SESSION_VAR_LOGOUT); + // If the mobile app Opener page is open (after login and switching instance) + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_HIDE_OPENER)) { + MobileAppHelper::registerHideOpenerScript(); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_HIDE_OPENER); + } elseif (MobileAppHelper::openerState()) { + MobileAppHelper::registerHideOpenerScript(); } - if (Yii::$app->session->has(self::SESSION_VAR_LOGIN)) { - MobileAppHelper::registerLoginScript(); + // After login + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION)) { MobileAppHelper::registerNotificationScript(); - Yii::$app->session->remove(self::SESSION_VAR_LOGIN); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION); + } + + // Before logout + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION)) { + MobileAppHelper::unregisterNotificationScript(); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION); + } + + // After logout + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_SHOW_OPENER)) { + MobileAppHelper::registerShowOpenerScript(); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_SHOW_OPENER); } if (Yii::$app->user->isGuest) { @@ -115,14 +128,16 @@ public static function onLayoutAddonInit($event) FirebaseAsset::register(Yii::$app->view); } - public static function onAfterLogout() + public static function onAfterLogin() { - Yii::$app->session->set(self::SESSION_VAR_LOGOUT, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_HIDE_OPENER, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION, 1); } - public static function onAfterLogin() + public static function onAfterLogout() { - Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1); } public static function onAuthChoiceBeforeRun(Event $event) @@ -137,4 +152,26 @@ public static function onAuthChoiceBeforeRun(Event $event) $sender->setClients([]); } } + + public static function onAccountTopMenuInit(Event $event) + { + if (!MobileAppHelper::isMultiInstanceApp()) { + return; + } + + /** @var AccountTopMenu $menu */ + $menu = $event->sender; + + $menu->addEntry(new MenuLink([ + 'label' => Yii::t('FcmPushModule.base', 'Switch network'), + 'url' => ['/fcm-push/mobile-app/instance-opener'], + 'icon' => 'arrows-h', + 'sortOrder' => 699, // Just before "Logout" + 'isActive' => true, + 'isVisible' => true, + 'htmlOptions' => [ + 'data-pjax' => '0', // Force full page refresh to trigger the onLayoutAddonInit event + ], + ])); + } } diff --git a/components/MailerMessage.php b/components/MailerMessage.php index dcd3b58..b3c8709 100644 --- a/components/MailerMessage.php +++ b/components/MailerMessage.php @@ -1,4 +1,5 @@ 'fcm-push', @@ -20,10 +22,11 @@ ['humhub\modules\web\pwa\controllers\ServiceWorkerController', Controller::EVENT_INIT, [Events::class, 'onServiceWorkerControllerInit']], [LayoutAddons::class, LayoutAddons::EVENT_INIT, [Events::class, 'onLayoutAddonInit']], [Application::class, Application::EVENT_BEFORE_REQUEST, [Events::class, 'onBeforeRequest']], - [User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']], [User::class, User::EVENT_AFTER_LOGIN, [Events::class, 'onAfterLogin']], + [User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']], [AuthChoice::class, AuthChoice::EVENT_BEFORE_RUN, [Events::class, 'onAuthChoiceBeforeRun']], - //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']] + //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']], + [AccountTopMenu::class, AccountTopMenu::EVENT_INIT, [Events::class, 'onAccountTopMenuInit']], ], 'consoleControllerMap' => [ 'firebase' => 'humhub\modules\fcmPush\commands\SendController', diff --git a/controllers/AdminController.php b/controllers/AdminController.php index b072f02..e4c5045 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -3,8 +3,10 @@ namespace humhub\modules\fcmPush\controllers; use humhub\modules\admin\components\Controller; -use humhub\modules\fcmPush\models\ConfigureForm; +use humhub\modules\admin\notifications\NewVersionAvailable; +use humhub\modules\fcmPush\models\FcmUser; use humhub\modules\fcmPush\Module; +use humhub\modules\user\models\User; use Yii; /** @@ -26,4 +28,34 @@ public function actionIndex() return $this->render('index', ['model' => $model]); } + /** + * @return string + */ + public function actionMobileApp() + { + if (Yii::$app->request->get('triggerNotification') == 1) { + + /** @var User $user */ + $user = Yii::$app->user->getIdentity(); + + $updateNotification = new NewVersionAvailable(); + $updateNotification->sendBulk(User::find()->where(['user.id' => $user->id])); + $this->view->setStatusMessage('success', 'Notification queued!'); + return $this->redirect('mobile-app'); + } + + if (Yii::$app->request->get('deleteToken') != "") { + $t = FcmUser::findOne(['id' => Yii::$app->request->get('deleteToken')]); + if ($t->delete() !== false) { + $this->view->setStatusMessage('success', 'Token deleted!'); + return $this->redirect('mobile-app'); + } + + $this->view->setStatusMessage('warning', 'Token NOT deleted!'); + return $this->redirect('mobile-app'); + } + + return $this->render('mobile-app'); + } + } diff --git a/controllers/MobileAppController.php b/controllers/MobileAppController.php index 068d498..2c8a16b 100644 --- a/controllers/MobileAppController.php +++ b/controllers/MobileAppController.php @@ -2,45 +2,18 @@ namespace humhub\modules\fcmPush\controllers; -use humhub\modules\admin\components\Controller; -use humhub\modules\admin\notifications\NewVersionAvailable; -use humhub\modules\fcmPush\models\FcmUser; -use humhub\modules\user\models\User; +use humhub\components\Controller; +use humhub\modules\fcmPush\helpers\MobileAppHelper; use Yii; class MobileAppController extends Controller { - /** - * Renders the index view for the module - * - * @return string - */ - public function actionIndex() + public function actionInstanceOpener() { + // Send to the mobile app to display the instance opener + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1); - if (Yii::$app->request->get('triggerNotification') == 1) { - - /** @var User $user */ - $user = Yii::$app->user->getIdentity(); - - $updateNotification = new NewVersionAvailable(); - $updateNotification->sendBulk(User::find()->where(['user.id' => $user->id])); - $this->view->setStatusMessage('success', 'Notification queued!'); - return $this->redirect('index'); - } - - if (Yii::$app->request->get('deleteToken') != "") { - $t = FcmUser::findOne(['id' => Yii::$app->request->get('deleteToken')]); - if ($t->delete() !== false) { - $this->view->setStatusMessage('success', 'Token deleted!'); - return $this->redirect('index'); - } else { - $this->view->setStatusMessage('warning', 'Token NOT deleted!'); - return $this->redirect('index'); - } - } - - return $this->render('index'); + // Stay on the same page, because when we come back from the mobile app to this instance + return $this->htmlRedirect(Yii::$app->request->referrer); } - } diff --git a/controllers/WellKnownController.php b/controllers/WellKnownController.php index 281c91e..ef74625 100644 --- a/controllers/WellKnownController.php +++ b/controllers/WellKnownController.php @@ -1,4 +1,5 @@ request->headers->get('x-humhub-app-is-ios'); } + + /** + * True if the mobile app supports multi instance to display the Opener landing page without logout for switching instance + * + * @since HumHub mobile app v1.0.124 + */ + public static function isMultiInstanceApp(): bool + { + return + static::isAppRequest() + && Yii::$app->request->headers->get('x-humhub-app-is-multi-instance'); + } + + /** + * True if the mobile app Opener landing page is visible and should be hidden. + * + * @since HumHub mobile app v1.0.124 + */ + public static function openerState(): bool + { + return + static::isAppRequest() + && Yii::$app->request->headers->get('x-humhub-app-opener-state'); + } } diff --git a/migrations/uninstall.php b/migrations/uninstall.php index f1cff4f..55f1e18 100644 --- a/migrations/uninstall.php +++ b/migrations/uninstall.php @@ -1,4 +1,5 @@ - 'pull-right']); ?> + 'pull-right']) ?> diff --git a/views/mobile-app/index.php b/views/admin/mobile-app.php similarity index 95% rename from views/mobile-app/index.php rename to views/admin/mobile-app.php index 508dff7..e043837 100644 --- a/views/mobile-app/index.php +++ b/views/admin/mobile-app.php @@ -43,7 +43,7 @@ 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'showOpener'])]); ?> 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'hideOpener'])]); ?> - 'POST', 'class' => 'btn btn-default']); ?> + 'POST', 'class' => 'btn btn-default']); ?> @@ -56,7 +56,7 @@ Administrative Notifications!. It may take a few minutes.

- 1], ['class' => 'btn btn-primary pull-right']) ?> + 1], ['class' => 'btn btn-primary pull-right']) ?> @@ -107,7 +107,7 @@ sender_id ?> · - $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?> + $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?> @@ -215,4 +215,4 @@ } }); - \ No newline at end of file +