/** * Marks post as seen by current user. */ public function markSeen() { if (!Yii::$app->user->isGuest) { $threadView = ThreadView::findOne(['user_id' => User::loggedId(), 'thread_id' => $this->thread_id]); if (!$threadView) { $threadView = new ThreadView(); $threadView->user_id = User::loggedId(); $threadView->thread_id = $this->thread_id; $threadView->new_last_seen = $this->created_at; $threadView->edited_last_seen = !empty($this->edited_at) ? $this->edited_at : $this->created_at; $threadView->save(); $this->thread->updateCounters(['views' => 1]); } else { if ($this->edited) { if ($threadView->edited_last_seen < $this->edited_at) { $threadView->edited_last_seen = $this->edited_at; $threadView->save(); $this->thread->updateCounters(['views' => 1]); } } else { $save = false; if ($threadView->new_last_seen < $this->created_at) { $threadView->new_last_seen = $this->created_at; $save = true; } if ($threadView->edited_last_seen < max($this->created_at, $this->edited_at)) { $threadView->edited_last_seen = max($this->created_at, $this->edited_at); $save = true; } if ($save) { $threadView->save(); $this->thread->updateCounters(['views' => 1]); } } } if ($this->thread->subscription) { if ($this->thread->subscription->post_seen == Subscription::POST_NEW) { $this->thread->subscription->post_seen = Subscription::POST_SEEN; $this->thread->subscription->save(); } } } }
/** * Marking all unread posts as seen. * @return string|\yii\web\Response */ public function actionMarkSeen() { if (Yii::$app->user->isGuest) { $this->info(Yii::t('podium/flash', 'This action is available for registered users only.')); return $this->redirect(['account/login']); } try { $loggedId = User::loggedId(); $batch = []; $threadsPrevMarked = Thread::find()->joinWith('threadView')->where(['and', ['user_id' => User::loggedId()], ['or', new Expression('`new_last_seen` < `new_post_at`'), new Expression('`edited_last_seen` < `edited_post_at`')]]); $time = time(); foreach ($threadsPrevMarked->each() as $thread) { $batch[] = $thread->id; } if (!empty($batch)) { Yii::$app->db->createCommand()->update(ThreadView::tableName(), ['new_last_seen' => $time, 'edited_last_seen' => $time], ['thread_id' => $batch, 'user_id' => $loggedId])->execute(); } $batch = []; $threadsNew = Thread::find()->joinWith('threadView')->where(['user_id' => null]); foreach ($threadsNew->each() as $thread) { $batch[] = [$loggedId, $thread->id, $time, $time]; } if (!empty($batch)) { Yii::$app->db->createCommand()->batchInsert(ThreadView::tableName(), ['user_id', 'thread_id', 'new_last_seen', 'edited_last_seen'], $batch)->execute(); } $this->success(Yii::t('podium/flash', 'All unread threads have been marked as seen.')); return $this->redirect(['default/index']); } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was an error while marking threads as seen. Contact administrator about this problem.')); return $this->redirect(['default/unread-posts']); } }
/** * Performs marking all unread threads as seen for user. * @return boolean * @throws Exception * @since 0.2 */ public static function podiumMarkAllSeen() { try { $loggedId = User::loggedId(); if (empty($loggedId)) { throw new Exception('User ID missing'); } $batch = []; $threadsPrevMarked = Thread::find()->joinWith('threadView')->where(['and', ['user_id' => $loggedId], ['or', new Expression('`new_last_seen` < `new_post_at`'), new Expression('`edited_last_seen` < `edited_post_at`')]]); $time = time(); foreach ($threadsPrevMarked->each() as $thread) { $batch[] = $thread->id; } if (!empty($batch)) { Yii::$app->db->createCommand()->update(ThreadView::tableName(), ['new_last_seen' => $time, 'edited_last_seen' => $time], ['thread_id' => $batch, 'user_id' => $loggedId])->execute(); } $batch = []; $threadsNew = Thread::find()->joinWith('threadView')->where(['user_id' => null]); foreach ($threadsNew->each() as $thread) { $batch[] = [$loggedId, $thread->id, $time, $time]; } if (!empty($batch)) { Yii::$app->db->createCommand()->batchInsert(ThreadView::tableName(), ['user_id', 'thread_id', 'new_last_seen', 'edited_last_seen'], $batch)->execute(); } return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * ThreadView relation. * @return ThreadView */ public function getView() { return $this->hasOne(ThreadView::className(), ['thread_id' => 'id'])->where(['user_id' => User::loggedId()]); }
/** * ThreadView relation general. * @return ThreadView */ public function getThreadView() { return $this->hasMany(ThreadView::className(), ['thread_id' => 'id']); }
public function getView() { return $this->hasOne(ThreadView::className(), ['thread_id' => 'id'])->where(['user_id' => Yii::$app->user->id]); }