/** * @inheritdoc */ public function afterSave($insert, $changedAttributes) { Yii::$app->search->update($this); $user = \humhub\modules\user\models\User::findOne(['id' => $this->created_by]); if ($insert) { // Auto add creator as admin $membership = new Membership(); $membership->space_id = $this->id; $membership->user_id = $user->id; $membership->status = Membership::STATUS_MEMBER; $membership->group_id = self::USERGROUP_ADMIN; $membership->save(); $activity = new \humhub\modules\space\activities\Created(); $activity->source = $this; $activity->originator = $user; $activity->create(); } Yii::$app->cache->delete('userSpaces_' . $user->id); return parent::afterSave($insert, $changedAttributes); }
/** * After Save Addons */ public function afterSave($insert, $changedAttributes) { Yii::$app->search->update($this); $user = \humhub\modules\user\models\User::findOne(['id' => $this->created_by]); if ($insert) { // Create new wall record for this space $wall = new Wall(); $wall->object_model = $this->className(); $wall->object_id = $this->id; $wall->save(); $this->wall_id = $wall->id; $this->update(false, ['wall_id']); // Auto add creator as admin $membership = new Membership(); $membership->space_id = $this->id; $membership->user_id = $user->id; $membership->status = Membership::STATUS_MEMBER; $membership->invite_role = 1; $membership->admin_role = 1; $membership->share_role = 1; $membership->save(); $activity = new \humhub\modules\space\activities\Created(); $activity->source = $this; $activity->originator = $user; $activity->create(); } Yii::$app->cache->delete('userSpaces_' . $user->id); return parent::afterSave($insert, $changedAttributes); }
/** * Adds an member to this space. * * This can happens after an clicking "Request Membership" Link * after Approval or accepting an invite. * Reputation id is 1 for joining * * Reputation is 5 for those who invited them * @param type $userId */ public function addMember($userId) { $user = User::findOne(['id' => $userId]); $membership = $this->getMembership($userId); if ($membership == null) { // Add Membership $reputation_id = 1; $membership = new Membership(); $membership->space_id = $this->owner->id; $membership->user_id = $userId; $membership->status = Membership::STATUS_MEMBER; $membership->invite_role = 0; $membership->admin_role = 0; $membership->share_role = 0; $userInvite = Invite::findOne(['email' => $user->email]); if ($userInvite !== null && $userInvite->source == Invite::SOURCE_INVITE) { $reputation_id = 5; $notification = new \humhub\modules\space\notifications\InviteAccepted(); $notification->originator = $user; $notification->source = $this->owner; $notification->send(User::findOne(['id' => $userInvite->user_originator_id])); ReputationHistory::addReputation($membership->originator_user_id, $reputation_id); } } else { // User is already member if ($membership->status == Membership::STATUS_MEMBER) { return true; } // User requested membership if ($membership->status == Membership::STATUS_APPLICANT) { $notification = new \humhub\modules\space\notifications\ApprovalRequestAccepted(); $notification->source = $this->owner; $notification->originator = Yii::$app->user->getIdentity(); $notification->send($user); } // User was invited if ($membership->status == Membership::STATUS_INVITED) { $notification = new \humhub\modules\space\notifications\InviteAccepted(); $notification->source = $this->owner; $notification->originator = $user; $notification->send(User::findOne(['id' => $membership->originator_user_id])); } // Update Membership $membership->status = Membership::STATUS_MEMBER; } $membership->save(); ReputationHistory::addReputation($userId, $reputation_id); $activity = new \humhub\modules\space\activities\MemberAdded(); $activity->source = $this->owner; $activity->originator = $user; $activity->create(); // Members can't also follow the space $this->owner->unfollow($userId); // Delete invite notification for this user $notificationInvite = new \humhub\modules\space\notifications\Invite(); $notificationInvite->source = $this->owner; $notificationInvite->delete($user); // Delete pending approval request notifications for this user $notificationApprovalRequest = new \humhub\modules\space\notifications\ApprovalRequest(); $notificationApprovalRequest->source = $this->owner; $notificationApprovalRequest->originator = $user; $notificationApprovalRequest->delete(); }