/** * Renames a class * * This is often required because some classes are also stored in database * e.g. for polymorphic relations. * * This method is also required for 0.20 namespace migration! * * @param string $oldClass * @param string $newClass */ protected function renameClass($oldClass, $newClass) { $this->updateSilent('activity', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('activity', ['class' => $newClass], ['class' => $oldClass]); $this->updateSilent('comment', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('content', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('file', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('like', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('notification', ['source_class' => $newClass], ['source_class' => $oldClass]); $this->updateSilent('user_mentioning', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('user_follow', ['object_model' => $newClass], ['object_model' => $oldClass]); $this->updateSilent('wall', ['object_model' => $newClass], ['object_model' => $oldClass]); /** * Looking up "NewLike" activities with this className * Since 0.20 the className changed to Like (is not longer the target object e.g. post) * * Use raw query for better performace. */ /* $likes = (new \yii\db\Query())->select("activity.*, like.id as likeid")->from('activity') ->leftJoin('like', 'like.object_model=activity.object_model AND like.object_id=activity.object_id') ->where(['class' => 'humhub\modules\like\activities\Liked'])->andWhere('like.id IS NOT NULL')->andWhere(['!=', 'activity.object_model', \humhub\modules\like\models\Like::className()]); foreach ($likes->each() as $like) { Yii::$app->db->createCommand()->update('activity', ['object_model' => \humhub\modules\like\models\Like::className(), 'object_id' => $like['likeid']], ['id' => $like['id']])->execute(); } */ $updateSql = "\n UPDATE activity \n LEFT JOIN `like` ON like.object_model=activity.object_model AND like.object_id=activity.object_id\n SET activity.object_model=:likeModelClass, activity.object_id=like.id\n WHERE activity.class=:likedActivityClass AND like.id IS NOT NULL and activity.object_model != :likeModelClass\n "; Yii::$app->db->createCommand($updateSql, [':likeModelClass' => \humhub\modules\like\models\Like::className(), ':likedActivityClass' => \humhub\modules\like\activities\Liked::className()])->execute(); }
/** * Executes the widget. */ public function run() { $currentUserLiked = false; $likes = Like::GetLikes($this->object->className(), $this->object->id); foreach ($likes as $like) { if ($like->user->id == Yii::$app->user->id) { $currentUserLiked = true; } } return $this->render('likeLink', array('object' => $this->object, 'likes' => $likes, 'currentUserLiked' => $currentUserLiked, 'id' => $this->object->getUniqueId(), 'likeUrl' => Url::to(['/like/like/like', 'contentModel' => $this->object->className(), 'contentId' => $this->object->id]), 'unlikeUrl' => Url::to(['/like/like/unlike', 'contentModel' => $this->object->className(), 'contentId' => $this->object->id]), 'userListUrl' => Url::to(['/like/like/user-list', 'contentModel' => $this->object->className(), 'contentId' => $this->object->getPrimaryKey()]), 'title' => $this->generateLikeTitleText($currentUserLiked, $likes))); }
/** * Like Count for specifc model */ public static function GetLikes($objectModel, $objectId) { $cacheId = "likes_" . $objectModel . "_" . $objectId; $cacheValue = Yii::$app->cache->get($cacheId); if ($cacheValue === false) { $newCacheValue = Like::findAll(array('object_model' => $objectModel, 'object_id' => $objectId)); Yii::$app->cache->set($cacheId, $newCacheValue, Yii::$app->settings->get('cache.expireTime')); return $newCacheValue; } else { return $cacheValue; } }
/** * Returns an JSON with current like informations about a target */ public function actionShowLikes() { Yii::$app->response->format = 'json'; // Some Meta Infos $currentUserLiked = false; $likes = Like::GetLikes($this->contentModel, $this->contentId); foreach ($likes as $like) { if ($like->user->id == Yii::$app->user->id) { $currentUserLiked = true; } } return ['currentUserLiked' => $currentUserLiked, 'likeCounter' => count($likes)]; }
/** * Callback to validate module database records. * * @param Event $event */ public static function onIntegrityCheck($event) { $integrityController = $event->sender; $integrityController->showTestHeadline("Like (" . Like::find()->count() . " entries)"); foreach (Like::find()->all() as $like) { if ($like->source === null) { if ($integrityController->showFix("Deleting like id " . $like->id . " without existing target!")) { $like->delete(); } } // User exists if ($like->user === null) { if ($integrityController->showFix("Deleting like id " . $like->id . " without existing user!")) { $like->delete(); } } } }
public function up() { $this->renameClass('Like', Like::className()); }