/** * Before Save Addons * * @return type */ public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); // Handle mentioned users \humhub\modules\user\models\Mentioning::parse($this, $this->message); return true; }
/** * Parses a given text for mentioned users and creates an mentioning for them. * * @param HActiveRecordContent|HActiveRecordContentAddon $record * @param string $text */ public static function parse($record, $text) { if ($record instanceof ContentActiveRecord || $record instanceof ContentAddonActiveRecord) { preg_replace_callback('@\\@\\-u([\\w\\-]*?)($|\\s|\\.)@', function ($hit) use(&$record) { $user = User::findOne(['guid' => $hit[1]]); if ($user !== null) { // Check the user was already mentioned (e.g. edit) $mention = self::findOne(['object_model' => get_class($record), 'object_id' => $record->getPrimaryKey(), 'user_id' => $user->id]); if ($mention === null) { $mention = new Mentioning(); $mention->object_model = $record->className(); $mention->object_id = $record->getPrimaryKey(); $mention->user_id = $user->id; $mention->save(); $mention->setPolymorphicRelation($record); // Mentioned users automatically follows the content $record->content->getPolymorphicRelation()->follow($user->id); } } }, $text); } else { throw new Exception("Mentioning can only used in HActiveRecordContent or HActiveRecordContentAddon objects!"); } }
/** * After Saving of comments, fire an activity * * @return type */ public function afterSave($insert, $changedAttributes) { // flush the cache $this->flushCache(); $activity = new \humhub\modules\comment\activities\NewComment(); $activity->source = $this; $activity->create(); // Handle mentioned users // Execute before NewCommentNotification to avoid double notification when mentioned. \humhub\modules\user\models\Mentioning::parse($this, $this->message); if ($insert) { $notification = new \humhub\modules\comment\notifications\NewComment(); $notification->source = $this; $notification->originator = $this->user; $notification->sendBulk($this->content->getPolymorphicRelation()->getFollowers(null, true, true)); } $this->updateContentSearch(); return parent::afterSave($insert, $changedAttributes); }
/** * 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()]); }