diff --git a/protected/humhub/modules/space/controllers/SpaceController.php b/protected/humhub/modules/space/controllers/SpaceController.php index 779e49d05c..bc4ee77d49 100644 --- a/protected/humhub/modules/space/controllers/SpaceController.php +++ b/protected/humhub/modules/space/controllers/SpaceController.php @@ -14,7 +14,6 @@ use humhub\modules\user\models\User; use humhub\modules\user\widgets\UserListBox; use humhub\modules\stream\actions\ContentContainerStream; - /** * SpaceController is the main controller for spaces. * @@ -61,10 +60,10 @@ class SpaceController extends \humhub\modules\content\components\ContentContaine { $space = $this->getSpace(); - if(Yii::$app->request->get('tour')) { + if (Yii::$app->request->get('tour')) { return $this->actionHome(); } - + if (!$space->isMember()) { $defaultPageUrl = \humhub\modules\space\widgets\Menu::getGuestsDefaultPageUrl($space); if ($defaultPageUrl != null) { @@ -105,14 +104,17 @@ class SpaceController extends \humhub\modules\content\components\ContentContaine { $this->forcePostRequest(); $space = $this->getSpace(); + + $success = false; + if (!$space->isMember()) { // follow without notifications by default - $space->follow(null, false); + $success = $space->follow(null, false); } if (Yii::$app->request->isAjax) { Yii::$app->response->format = 'json'; - return ['success' => true]; + return ['success' => $success]; } return $this->redirect($space->getUrl()); @@ -125,11 +127,12 @@ class SpaceController extends \humhub\modules\content\components\ContentContaine { $this->forcePostRequest(); $space = $this->getSpace(); - $space->unfollow(); + + $success = $space->unfollow(); if (Yii::$app->request->isAjax) { Yii::$app->response->format = 'json'; - return ['success' => true]; + return ['success' => $success]; } return $this->redirect($space->getUrl()); diff --git a/protected/humhub/modules/user/events/FollowEvent.php b/protected/humhub/modules/user/events/FollowEvent.php new file mode 100644 index 0000000000..8e8c304585 --- /dev/null +++ b/protected/humhub/modules/user/events/FollowEvent.php @@ -0,0 +1,32 @@ + 100] ]; } @@ -73,17 +85,6 @@ class Follow extends \yii\db\ActiveRecord /** * @inheritdoc */ - public function attributeLabels() - { - return [ - 'id' => 'ID', - 'object_model' => 'Object Model', - 'object_id' => 'Object ID', - 'user_id' => 'User ID', - 'send_notifications' => 'Send Notifications', - ]; - } - public function afterSave($insert, $changedAttributes) { if ($insert && $this->object_model == User::className()) { @@ -99,11 +100,17 @@ class Follow extends \yii\db\ActiveRecord ->save(); } - return parent::afterSave($insert, $changedAttributes); + $this->trigger(Follow::EVENT_FOLLOWING_CREATED, new FollowEvent(['user' => $this->user, 'target' => $this->getTarget()])); + + parent::afterSave($insert, $changedAttributes); } + /** + * @inheritdoc + */ public function beforeDelete() { + $this->trigger(Follow::EVENT_FOLLOWING_REMOVED, new FollowEvent(['user' => $this->user, 'target' => $this->getTarget()])); // ToDo: Handle this via event of User Module if ($this->object_model == User::className()) { @@ -137,12 +144,12 @@ class Follow extends \yii\db\ActiveRecord * Returns all followed spaces of the given user as ActiveQuery. * * @param \humhub\modules\user\models\User $user + * @param boolean|null $withNotifications by notification setting (default is null without notification handling) * @return \yii\db\ActiveQuery Space query of all followed spaces */ public static function getFollowedSpacesQuery(User $user, $withNotifications = null) { - $query = Space::find()->leftJoin('user_follow', 'user_follow.user_id=:userId AND user_follow.object_model=:spaceModel', - [':userId' => $user->id, ':spaceModel' => Space::class]); + $query = Space::find()->leftJoin('user_follow', 'user_follow.user_id=:userId AND user_follow.object_model=:spaceModel', [':userId' => $user->id, ':spaceModel' => Space::class]); if ($withNotifications === true) { $query->where(['user_follow.send_notifications' => 1]); @@ -155,15 +162,14 @@ class Follow extends \yii\db\ActiveRecord public static function getFollowersQuery(\yii\db\ActiveRecord $target, $withNotifications = null) { - $query = User::find()->leftJoin('user_follow', 'user.id=user_follow.user_id AND user_follow.object_model=:spaceModel AND user_follow.object_id=:spaceId', - [':spaceModel' => $target->className(), ':spaceId' => $target->getPrimaryKey()]); + $query = User::find()->leftJoin('user_follow', 'user.id=user_follow.user_id AND user_follow.object_model=:spaceModel AND user_follow.object_id=:spaceId', [':spaceModel' => $target->className(), ':spaceId' => $target->getPrimaryKey()]); if ($withNotifications === true) { $query->where(['user_follow.send_notifications' => 1]); } else if ($withNotifications === false) { $query->where(['user_follow.send_notifications' => 0]); } - + return $query; }