/** * Follows the owner object * * @param int $userId * @return boolean */ public function follow($userId = "", $withNotifications = true) { if ($userId == "") { $userId = Yii::$app->user->id; } // User cannot follow himself if ($this->owner->className() == \humhub\modules\user\models\User::className() && $this->owner->getPrimaryKey() == $userId) { return false; } $follow = $this->getFollowRecord($userId); if ($follow === null) { $follow = new \humhub\modules\user\models\Follow(); $follow->user_id = $userId; $follow->object_id = $this->owner->getPrimaryKey(); $follow->object_model = $this->owner->className(); } if ($withNotifications) { $follow->send_notifications = 1; } else { $follow->send_notifications = 0; } if (!$follow->save()) { return false; } return true; }
/** * Executes the widgets */ public function run() { // Some member stats $statsTotalUsers = User::find()->active()->count(); $statsUserOnline = \humhub\modules\user\components\Session::getOnlineUsers()->count(); $statsUserFollow = Follow::find()->where(['object_model' => User::className()])->count(); // Render widgets view return $this->render('memberStats', array('statsTotalUsers' => $statsTotalUsers, 'statsUserOnline' => $statsUserOnline, 'statsUserFollow' => $statsUserFollow)); }
/** * Before Delete of a User * */ public function beforeDelete() { // We don't allow deletion of users who owns a space - validate that foreach (\humhub\modules\space\models\Membership::GetUserSpaces($this->id) as $space) { if ($space->isSpaceOwner($this->id)) { throw new Exception("Tried to delete a user which is owner of a space!"); } } // Disable all enabled modules foreach ($this->getAvailableModules() as $moduleId => $module) { if ($this->isModuleEnabled($moduleId)) { $this->disableModule($moduleId); } } // Delete profile image $this->getProfileImage()->delete(); // Remove from search index Yii::$app->search->delete($this); // Cleanup related tables Invite::deleteAll(['user_originator_id' => $this->id]); Follow::deleteAll(['user_id' => $this->id]); Follow::deleteAll(['object_model' => $this->className(), 'object_id' => $this->id]); Password::deleteAll(['user_id' => $this->id]); Profile::deleteAll(['user_id' => $this->id]); GroupUser::deleteAll(['user_id' => $this->id]); Session::deleteAll(['user_id' => $this->id]); return parent::beforeDelete(); }
/** * @inheritdoc */ public function beforeDelete() { foreach (Setting::findAll(['space_id' => $this->id]) as $spaceSetting) { $spaceSetting->delete(); } foreach ($this->getAvailableModules() as $moduleId => $module) { if ($this->isModuleEnabled($moduleId)) { $this->disableModule($moduleId); } } Yii::$app->search->delete($this); $this->getProfileImage()->delete(); \humhub\modules\user\models\Follow::deleteAll(['object_id' => $this->id, 'object_model' => 'Space']); foreach (Membership::findAll(['space_id' => $this->id]) as $spaceMembership) { $spaceMembership->delete(); } \humhub\modules\user\models\Invite::deleteAll(['space_invite_id' => $this->id]); // When this workspace is used in a group as default workspace, delete the link foreach (\humhub\modules\user\models\Group::findAll(['space_id' => $this->id]) as $group) { $group->space_id = ""; $group->save(); } return parent::beforeDelete(); }
/** * Callback to validate module database records. * * @param \yii\base\Event $event */ public static function onIntegrityCheck($event) { $integrityController = $event->sender; $integrityController->showTestHeadline("User Module - Users (" . User::find()->count() . " entries)"); foreach (User::find()->joinWith(['profile'])->all() as $user) { if ($user->profile == null) { $integrityController->showWarning("User with id " . $user->id . " has no profile record!"); } } foreach (GroupUser::find()->joinWith(['user'])->all() as $groupUser) { if ($groupUser->user == null) { if ($integrityController->showFix("Deleting group admin " . $groupUser->id . " without existing user!")) { $groupUser->delete(); } } } $integrityController->showTestHeadline("User Module - Password (" . Password::find()->count() . " entries)"); foreach (Password::find()->joinWith(['user'])->all() as $password) { if ($password->user == null) { if ($integrityController->showFix("Deleting password " . $password->id . " without existing user!")) { $password->delete(); } } } $integrityController->showTestHeadline("User Module - Profile (" . Profile::find()->count() . " entries)"); foreach (Profile::find()->joinWith(['user'])->all() as $profile) { if ($profile->user == null) { if ($integrityController->showFix("Deleting profile " . $profile->user_id . " without existing user!")) { $profile->delete(); } } } $integrityController->showTestHeadline("User Module - Mentioning (" . Mentioning::find()->count() . " entries)"); foreach (Mentioning::find()->joinWith(['user'])->all() as $mentioning) { if ($mentioning->user == null) { if ($integrityController->showFix("Deleting mentioning " . $mentioning->id . " of non existing user!")) { $mentioning->delete(); } } if ($mentioning->getPolymorphicRelation() == null) { if ($integrityController->showFix("Deleting mentioning " . $mentioning->id . " of non target!")) { $mentioning->delete(); } } } $integrityController->showTestHeadline("User Module - Follow (" . Follow::find()->count() . " entries)"); foreach (Follow::find()->joinWith(['user'])->all() as $follow) { if ($follow->user == null) { if ($integrityController->showFix("Deleting follow " . $follow->id . " of non existing user!")) { $follow->delete(); } } if ($follow->getTarget() == null) { if ($integrityController->showFix("Deleting follow " . $follow->id . " of non target!")) { $follow->delete(); } } } $integrityController->showTestHeadline("User Module - Modules (" . models\Module::find()->count() . " entries)"); foreach (models\Module::find()->joinWith(['user'])->all() as $module) { if ($module->user == null) { if ($integrityController->showFix("Deleting user-module " . $module->id . " of non existing user!")) { $module->delete(); } } } }
/** * On delete of a Content or ContentAddon * * @param \yii\base\Event $event */ public static function onContentDelete($event) { models\Mentioning::deleteAll(['object_model' => $event->sender->className(), 'object_id' => $event->sender->getPrimaryKey()]); models\Follow::deleteAll(['object_model' => $event->sender->className(), 'object_id' => $event->sender->getPrimaryKey()]); }
/** * Returns the number of follows the owner object performed. * This is only affects User owner objects! * * @param string $objectModel HActiveRecord Classname to restrict Object Classes to (e.g. User) * @return int */ public function getFollowingCount($objectModel) { #if (!class_exists($objectModel)) { # throw new CException("Invalid objectModel parameter given!"); #} return Follow::find()->where(['user_id' => $this->owner->getPrimaryKey(), 'object_model' => $objectModel])->count(); }