/** * Returns ID of user responsible for logged action. * @return integer|null */ public static function blame() { if (Yii::$app instanceof Application && !Yii::$app->user->isGuest) { return User::loggedId(); } return null; }
/** * Searches for subscription * @param array $params * @return ActiveDataProvider */ public function search($params) { $query = self::find()->where(['user_id' => User::loggedId()]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 10, 'forcePageParam' => false]]); $dataProvider->sort->defaultOrder = ['post_seen' => SORT_ASC, 'id' => SORT_DESC]; $dataProvider->pagination->pageSize = Yii::$app->session->get('per-page', 20); return $dataProvider; }
/** * Searches for threads with unread posts. * @param array $params * @return ActiveDataProvider */ public function search() { $loggedId = User::loggedId(); $query = Thread::find()->joinWith('threadView')->where(['or', ['and', ['user_id' => $loggedId], new Expression('`new_last_seen` < `new_post_at`')], ['and', ['user_id' => $loggedId], new Expression('`edited_last_seen` < `edited_post_at`')], ['user_id' => null]]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 10, 'forcePageParam' => false]]); $dataProvider->sort->defaultOrder = ['edited_post_at' => SORT_ASC, 'id' => SORT_ASC]; $dataProvider->pagination->pageSize = Yii::$app->session->get('per-page', 20); return $dataProvider; }
/** * Searches for subscription * @param array $params * @return ActiveDataProvider */ public function search($params) { $query = self::find()->where(['user_id' => User::loggedId()]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 10, 'forcePageParam' => false]]); $dataProvider->sort->defaultOrder = ['post_seen' => SORT_ASC, 'id' => SORT_DESC]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } return $dataProvider; }
/** * Searches for deleted messages. * @param array $params * @return ActiveDataProvider */ public function searchDeleted($params) { $dataProvider = $this->search(); $dataProvider->query->where(['or', ['and', ['sender_id' => User::loggedId()], ['sender_status' => Message::getDeletedStatuses()]], ['and', ['receiver_id' => User::loggedId()], ['receiver_status' => Message::getDeletedStatuses()]]]); if (!($this->load($params) && $this->validate())) { $dataProvider->query->joinWith(['receiverUser' => function ($q) { $q->from(User::tableName() . ' pdu_receiver'); }, 'senderUser' => function ($q) { $q->from(User::tableName() . ' pdu_sender'); }]); return $dataProvider; } $dataProvider->query->andFilterWhere(['like', 'topic', $this->topic]); $dataProvider->query->joinWith(['receiverUser' => function ($q) { $q->from(User::tableName() . ' pdu_receiver')->where(['like', 'pdu_receiver.username', $this->receiverName]); }, 'senderUser' => function ($q) { $q->from(User::tableName() . ' pdu_sender')->where(['like', 'pdu_sender.username', $this->senderName]); }]); return $dataProvider; }
/** * Searches for sent messages. * @param array $params * @return ActiveDataProvider */ public function search($params) { // not very proud of this query - slow for sure // let me know if it can be done better. $subquery = (new Query())->select(['m2.replyto'])->from(['m1' => Message::tableName()])->leftJoin(['m2' => Message::tableName()], '`m1`.`replyto` = `m2`.`id`')->where(['is not', 'm2.replyto', null]); $query = self::find()->where(['and', ['sender_id' => User::loggedId(), 'sender_status' => Message::getSentStatuses()], ['not in', Message::tableName() . '.id', $subquery]]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => ['id', 'topic', 'created_at']]]); $dataProvider->sort->defaultOrder = ['id' => SORT_DESC]; $dataProvider->pagination->pageSize = Yii::$app->session->get('per-page', 20); if (!($this->load($params) && $this->validate())) { $dataProvider->query->joinWith(['messageReceivers' => function ($q) { $q->joinWith(['receiver']); }]); return $dataProvider; } $dataProvider->query->andFilterWhere(['like', 'topic', $this->topic]); if (preg_match('/^(forum|orum|rum|um|m)?#([0-9]+)$/', strtolower($this->receiverName), $matches)) { $dataProvider->query->joinWith(['messageReceivers' => function ($q) use($matches) { $q->joinWith(['receiver' => function ($q) use($matches) { $q->andFilterWhere(['username' => ['', null], User::tableName() . '.id' => $matches[2]]); }]); }]); } elseif (preg_match('/^([0-9]+)$/', $this->receiverName, $matches)) { $dataProvider->query->joinWith(['messageReceivers' => function ($q) use($matches) { $q->joinWith(['receiver' => function ($q) use($matches) { $q->andFilterWhere(['or', ['like', 'username', $this->receiverName], ['username' => ['', null], 'id' => $matches[1]]]); }]); }]); } else { $dataProvider->query->joinWith(['messageReceivers' => function ($q) { $q->joinWith(['receiver' => function ($q) { $q->andFilterWhere(['like', User::tableName() . '.username', $this->receiverName]); }]); }]); } return $dataProvider; }
/** * Updates friend status for the user. * @return boolean * @since 0.2 */ public function updateFriend() { try { if ($this->isBefriendedBy(User::loggedId())) { Yii::$app->db->createCommand()->delete('{{%podium_user_friend}}', 'user_id = :uid AND friend_id = :iid', [':uid' => User::loggedId(), ':iid' => $this->id])->execute(); Log::info('User unfriended', $this->id, __METHOD__); } else { Yii::$app->db->createCommand()->insert('{{%podium_user_friend}}', ['user_id' => User::loggedId(), 'friend_id' => $this->id])->execute(); Log::info('User befriended', $this->id, __METHOD__); } Cache::getInstance()->deleteElement('user.friends', $this->id); return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Deleting the user of given ID. * @param integer $id * @return \yii\web\Response */ public function actionDelete($id = null) { if (User::can(Rbac::PERM_DELETE_USER)) { $model = User::findOne((int) $id); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find Member with this ID.')); } elseif ($model->id == User::loggedId()) { $this->error(Yii::t('podium/flash', 'Sorry! You can not delete your own account.')); } else { if ($model->delete()) { Cache::getInstance()->delete('members.fieldlist'); Cache::getInstance()->delete('forum.memberscount'); Activity::deleteUser($model->id); Log::info('User deleted', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'User has been deleted.')); } else { Log::error('Error while deleting user', $model->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was some error while deleting the user.')); } } } else { $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.')); } return $this->redirect(['admin/members']); }
/** * Removes message. * @param integer $perm Permanent removal flag * @return boolean */ public function remove($perm = 0) { $clearCache = false; if ($this->receiver_status == self::STATUS_NEW) { $clearCache = true; } if ($this->receiver_id == User::loggedId()) { $this->receiver_status = $perm ? self::STATUS_REMOVED : self::STATUS_DELETED; } if ($this->sender_id == User::loggedId()) { $this->sender_status = $perm ? self::STATUS_REMOVED : self::STATUS_DELETED; } if ($this->receiver_status == self::STATUS_REMOVED && $this->sender_status == self::STATUS_REMOVED) { if ($this->delete()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } return true; } else { return false; } } if ($this->save()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } return true; } else { return false; } }
/** * Viewing the message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionView($id = null) { $model = Message::find()->where(['and', ['id' => $id], ['or', 'receiver_id' => User::loggedId(), 'sender_id' => User::loggedId()]])->limit(1)->one(); if ($model) { if ($model->receiver_id == User::loggedId() && $model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } } return $this->render('view', ['model' => $model]); } else { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.')); return $this->redirect(['messages/inbox']); } }
/** * Voting on the post. * @return string|\yii\web\Response */ public function actionThumb() { if (Yii::$app->request->isAjax) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while voting on this post!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { $postId = Yii::$app->request->post('post'); $thumb = Yii::$app->request->post('thumb'); if (is_numeric($postId) && $postId > 0 && in_array($thumb, ['up', 'down'])) { $post = Post::findOne((int) $postId); if ($post) { if ($post->thread->locked) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'This thread is locked.'), ['class' => 'text-info'])]; } else { if ($post->author_id == User::loggedId()) { return Json::encode(['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You can not vote on your own post!'), ['class' => 'text-info'])]); } $count = 0; $votes = Cache::getInstance()->get('user.votes.' . User::loggedId()); if ($votes !== false) { if ($votes['expire'] < time()) { $votes = false; } elseif ($votes['count'] >= 10) { return Json::encode(['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', '10 votes per hour limit reached!'), ['class' => 'text-danger'])]); } else { $count = $votes['count']; } } if ($post->thumb) { if ($post->thumb->thumb == 1 && $thumb == 'down') { $post->thumb->thumb = -1; if ($post->thumb->save()) { $post->updateCounters(['likes' => -1, 'dislikes' => 1]); } } elseif ($post->thumb->thumb == -1 && $thumb == 'up') { $post->thumb->thumb = 1; if ($post->thumb->save()) { $post->updateCounters(['likes' => 1, 'dislikes' => -1]); } } } else { $postThumb = new PostThumb(); $postThumb->post_id = $post->id; $postThumb->user_id = User::loggedId(); $postThumb->thumb = $thumb == 'up' ? 1 : -1; if ($postThumb->save()) { if ($thumb == 'up') { $post->updateCounters(['likes' => 1]); } else { $post->updateCounters(['dislikes' => 1]); } } } $data = ['error' => 0, 'likes' => '+' . $post->likes, 'dislikes' => '-' . $post->dislikes, 'summ' => $post->likes - $post->dislikes, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'Your vote has been saved!'), ['class' => 'text-success'])]; if ($count == 0) { Cache::getInstance()->set('user.votes.' . User::loggedId(), ['count' => 1, 'expire' => time() + 3600]); } else { Cache::getInstance()->setElement('user.votes.' . User::loggedId(), 'count', $count + 1); } } } } } else { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to vote on this post'), ['class' => 'text-info'])]; } return Json::encode($data); } else { return $this->redirect(['default/index']); } }
/** * 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']); } }
Pjax::begin(); echo PageSizer::widget(); echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'filterSelector' => 'select#per-page', 'tableOptions' => ['class' => 'table table-striped table-hover'], 'columns' => [['attribute' => 'username', 'label' => Yii::t('podium/view', 'Username') . Helper::sortOrder('username'), 'encodeLabel' => false, 'format' => 'raw', 'value' => function ($model) { return Html::a($model->podiumName, ['members/view', 'id' => $model->id, 'slug' => $model->podiumSlug], ['data-pjax' => '0']); }], ['attribute' => 'role', 'label' => Yii::t('podium/view', 'Role') . Helper::sortOrder('role'), 'encodeLabel' => false, 'format' => 'raw', 'filter' => User::getRoles(), 'value' => function ($model) { return Helper::roleLabel($model->role); }], ['attribute' => 'created_at', 'label' => Yii::t('podium/view', 'Joined') . Helper::sortOrder('created_at'), 'encodeLabel' => false, 'value' => function ($model) { return Yii::$app->formatter->asDatetime($model->created_at); }], ['attribute' => 'threads_count', 'label' => Yii::t('podium/view', 'Threads'), 'encodeLabel' => false, 'value' => function ($model) { return $model->threadsCount; }], ['attribute' => 'posts_count', 'label' => Yii::t('podium/view', 'Posts'), 'encodeLabel' => false, 'value' => function ($model) { return $model->postsCount; }], ['class' => ActionColumn::className(), 'header' => Yii::t('podium/view', 'Actions'), 'contentOptions' => ['class' => 'text-right'], 'headerOptions' => ['class' => 'text-right'], 'template' => '{view}' . (!Yii::$app->user->isGuest ? ' {pm}' : ''), 'buttons' => ['view' => function ($url, $model) { return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['members/view', 'id' => $model->id, 'slug' => $model->podiumSlug], ['class' => 'btn btn-default btn-xs', 'data-pjax' => '0', 'data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::t('podium/view', 'View Member')]); }, 'pm' => function ($url, $model) { if ($model->id !== User::loggedId()) { return Html::a('<span class="glyphicon glyphicon-envelope"></span>', ['messages/new', 'user' => $model->id], ['class' => 'btn btn-default btn-xs', 'data-pjax' => '0', 'data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::t('podium/view', 'Send Message')]); } else { return Html::a('<span class="glyphicon glyphicon-envelope"></span>', '#', ['class' => 'btn btn-xs disabled text-muted']); } }]]]]); Pjax::end(); ?> <div class="panel panel-default"> <div class="panel-body small"> <ul class="list-inline pull-right"> <li><a href="<?php echo Url::to(['default/index']); ?> " data-toggle="tooltip" data-placement="top" title="<?php echo Yii::t('podium/view', 'Go to the main page');
/** * Voting on the post. * @return string|\yii\web\Response */ public function actionThumb() { if (!Yii::$app->request->isAjax) { return $this->redirect(['default/index']); } $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while voting on this post!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to vote on this post'), ['class' => 'text-info']); return Json::encode($data); } $postId = Yii::$app->request->post('post'); $thumb = Yii::$app->request->post('thumb'); if (is_numeric($postId) && $postId > 0 && in_array($thumb, ['up', 'down'])) { $post = Post::find()->where(['id' => $postId])->limit(1)->one(); if ($post) { if ($post->thread->locked) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'This thread is locked.'), ['class' => 'text-info']); return Json::encode($data); } if ($post->author_id == User::loggedId()) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You can not vote on your own post!'), ['class' => 'text-info']); return Json::encode($data); } $count = 0; $votes = Cache::getInstance()->get('user.votes.' . User::loggedId()); if ($votes !== false) { if ($votes['expire'] < time()) { $votes = false; } elseif ($votes['count'] >= 10) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', '{max} votes per hour limit reached!', ['max' => 10]), ['class' => 'text-danger']); return Json::encode($data); } else { $count = $votes['count']; } } if ($post->podiumThumb($thumb == 'up', $count)) { $data = ['error' => 0, 'likes' => '+' . $post->likes, 'dislikes' => '-' . $post->dislikes, 'summ' => $post->likes - $post->dislikes, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'Your vote has been saved!'), ['class' => 'text-success'])]; } } } return Json::encode($data); }
/** * 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(); } } } }
/** * 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; }
/** * Ignoring the user of given ID. * @param integer $id * @return \yii\web\Response */ public function actionIgnore($id = null) { if (!Yii::$app->user->isGuest) { try { $model = User::find()->where(['and', ['id' => (int) $id], ['!=', 'status', User::STATUS_REGISTERED]])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find Member with this ID.')); } elseif ($model->id == User::loggedId()) { $this->error(Yii::t('podium/flash', 'Sorry! You can not ignore your own account.')); } elseif ($model->id == User::ROLE_ADMIN) { $this->error(Yii::t('podium/flash', 'Sorry! You can not ignore Administrator.')); } else { if ($model->isIgnoredBy(User::loggedId())) { Yii::$app->db->createCommand()->delete('{{%podium_user_ignore}}', 'user_id = :uid AND ignored_id = :iid', [':uid' => User::loggedId(), ':iid' => $model->id])->execute(); Log::info('User unignored', !empty($model->id) ? $model->id : '', __METHOD__); $this->success(Yii::t('podium/flash', 'User has been unignored.')); } else { Yii::$app->db->createCommand()->insert('{{%podium_user_ignore}}', ['user_id' => User::loggedId(), 'ignored_id' => $model->id])->execute(); Log::info('User ignored', !empty($model->id) ? $model->id : '', __METHOD__); $this->success(Yii::t('podium/flash', 'User has been ignored.')); } } } catch (Exception $e) { $this->error(Yii::t('podium/flash', 'Sorry! There was some error while performing this action.')); Log::error($e->getMessage(), null, __METHOD__); } } return $this->redirect(['members/index']); }
/** * Searches for thread. * @param integer $forum_id * @return ActiveDataProvider */ public function search($forum_id = null, $filters = null) { $query = self::find(); if ($forum_id) { $query->where(['forum_id' => (int) $forum_id]); } if (!empty($filters)) { if (!empty($filters['pin']) && $filters['pin'] == 1) { $query->andWhere(['pinned' => 1]); } if (!empty($filters['lock']) && $filters['lock'] == 1) { $query->andWhere(['locked' => 1]); } if (!empty($filters['hot']) && $filters['hot'] == 1) { $query->andWhere(['>=', 'posts', Config::getInstance()->get('hot_minimum')]); } if (!empty($filters['new']) && $filters['new'] == 1 && !Yii::$app->user->isGuest) { $query->joinWith(['view' => function ($q) { $q->andWhere(['or', ['and', ['user_id' => User::loggedId()], new Expression('`new_last_seen` < `new_post_at`')], ['user_id' => null]]); }]); } if (!empty($filters['edit']) && $filters['edit'] == 1 && !Yii::$app->user->isGuest) { $query->joinWith(['view' => function ($q) { $q->andWhere(['or', ['and', ['user_id' => User::loggedId()], new Expression('`edited_last_seen` < `edited_post_at`')], ['user_id' => null]]); }]); } } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false]); $dataProvider->sort->defaultOrder = ['pinned' => SORT_DESC, 'updated_at' => SORT_DESC, 'id' => SORT_ASC]; return $dataProvider; }
/** * Subscribing the thread of given ID. * @param integer $id * @return \yii\web\Response */ public function actionAdd($id = null) { if (!Yii::$app->request->isAjax) { return $this->redirect(['default/index']); } $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while adding this subscription!'), ['class' => 'text-danger'])]; if (Yii::$app->user->isGuest) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to subscribe to this thread'), ['class' => 'text-info']); } if (is_numeric($id) && $id > 0) { $subscription = Subscription::find()->where(['thread_id' => $id, 'user_id' => User::loggedId()])->limit(1)->one(); if (!empty($subscription)) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are already subscribed to this thread.'), ['class' => 'text-info']); } else { if (Subscription::add((int) $id)) { $data = ['error' => 0, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'You have subscribed to this thread!'), ['class' => 'text-success'])]; } } } return Json::encode($data); }
/** * Checks if user is moderator for this forum. * @param integer|null $user_id User's ID or null for current signed in. * @return boolean */ public function isMod($user_id = null) { if (in_array($user_id ?: User::loggedId(), $this->getMods())) { return true; } return false; }
/** * Loads older messages in thread. * @return string */ public function actionLoad() { if (!Yii::$app->request->isAjax) { return $this->redirect(['default/index']); } $result = ['messages' => '', 'more' => 0]; if (!Yii::$app->user->isGuest) { $loggedId = User::loggedId(); $id = Yii::$app->request->post('message'); $message = Message::find()->where(['id' => $id])->limit(1)->one(); if ($message && ($message->sender_id == $loggedId || $message->isMessageReceiver($loggedId))) { $stack = 0; $reply = clone $message; while ($reply->reply && $stack < 5) { $result['more'] = 0; if ($reply->reply->sender_id == $loggedId && $reply->reply->sender_status == Message::STATUS_DELETED) { $reply = $reply->reply; continue; } $result['messages'] .= $this->renderPartial('load', ['reply' => $reply]); $reply = $reply->reply; if ($reply) { $result['more'] = $reply->id; } $stack++; } } } return Json::encode($result); }
/** * Sends message. * @return boolean */ public function send() { $transaction = static::getDb()->beginTransaction(); try { $this->sender_id = User::loggedId(); $this->sender_status = self::STATUS_READ; if ($this->save()) { $count = count($this->receiversId); foreach ($this->receiversId as $receiver) { if (!(new Query())->select('id')->from(User::tableName())->where(['id' => $receiver, 'status' => User::STATUS_ACTIVE])->exists()) { if ($count == 1) { throw new Exception('No active receivers to send message to!'); } else { continue; } } $message = new MessageReceiver(); $message->message_id = $this->id; $message->receiver_id = $receiver; $message->receiver_status = self::STATUS_NEW; if ($message->save()) { Cache::getInstance()->deleteElement('user.newmessages', $receiver); } else { throw new Exception('MessageReceiver saving error!'); } } $transaction->commit(); $sessionKey = 'messages.' . $this->sender_id; if (Yii::$app->session->has($sessionKey)) { $sentAlready = explode('|', Yii::$app->session->get($sessionKey)); $sentAlready[] = time(); Yii::$app->session->set($sessionKey, implode('|', $sentAlready)); } else { Yii::$app->session->set($sessionKey, time()); } return true; } else { throw new Exception('Message saving error!'); } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), $this->id, __METHOD__); } return false; }
/** * Performs vote processing. * @param boolean $up whether this is up or downvote * @param integer $count number of user's cached votes * @return boolean * @since 0.2 */ public function podiumThumb($up = true, $count = 0) { try { if ($this->thumb) { if ($this->thumb->thumb == 1 && !$up) { $this->thumb->thumb = -1; if ($this->thumb->save()) { $this->updateCounters(['likes' => -1, 'dislikes' => 1]); } } elseif ($this->thumb->thumb == -1 && $up) { $this->thumb->thumb = 1; if ($this->thumb->save()) { $this->updateCounters(['likes' => 1, 'dislikes' => -1]); } } } else { $postThumb = new PostThumb(); $postThumb->post_id = $this->id; $postThumb->user_id = User::loggedId(); $postThumb->thumb = $up ? 1 : -1; if ($postThumb->save()) { if ($postThumb->thumb) { $this->updateCounters(['likes' => 1]); } else { $this->updateCounters(['dislikes' => 1]); } } } if ($count == 0) { Cache::getInstance()->set('user.votes.' . User::loggedId(), ['count' => 1, 'expire' => time() + 3600]); } else { Cache::getInstance()->setElement('user.votes.' . User::loggedId(), 'count', $count + 1); } return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Adds subscription for thread. * @param integer $thread thread's ID * @return boolean * @since 0.2 */ public static function add($thread) { if (!Yii::$app->user->isGuest) { $sub = new Subscription(); $sub->thread_id = $thread; $sub->user_id = User::loggedId(); $sub->post_seen = self::POST_SEEN; if ($sub->save()) { return true; } } return false; }
/** * Adding or removing user as a friend. * @param integer $id user ID * @return \yii\web\Response * @since 0.2 */ public function actionFriend($id = null) { if (Yii::$app->user->isGuest) { return $this->redirect(['default/index']); } $model = User::find()->where(['and', ['id' => $id], ['!=', 'status', User::STATUS_REGISTERED]])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find Member with this ID.')); return $this->redirect(['members/index']); } if ($model->id == User::loggedId()) { $this->error(Yii::t('podium/flash', 'Sorry! You can not befriend your own account.')); return $this->redirect(['members/view', 'id' => $model->id, 'slug' => $model->podiumSlug]); } if ($model->updateFriend()) { if ($model->isBefriendedBy(User::loggedId())) { $this->success(Yii::t('podium/flash', 'User is your friend now.')); } else { $this->success(Yii::t('podium/flash', 'User is not your friend anymore.')); } } else { $this->error(Yii::t('podium/flash', 'Sorry! There was some error while performing this action.')); } return $this->redirect(['members/view', 'id' => $model->id, 'slug' => $model->podiumSlug]); }
/** * Performs post report sending to moderators. * @param Post $post reported post * @return boolean * @since 0.2 */ public function podiumReport($post = null) { try { if (empty($post)) { throw new Exception('Reported post missing'); } $package = []; $mods = $post->forum->mods; foreach ($mods as $mod) { if ($mod != User::loggedId()) { $package[] = [User::loggedId(), $mod, Yii::t('podium/view', 'Complaint about the post #{id}', ['id' => $post->id]), $this->content . '<hr>' . Html::a(Yii::t('podium/view', 'Direct link to this post'), ['default/show', 'id' => $post->id]) . '<hr>' . '<strong>' . Yii::t('podium/view', 'Post contents') . '</strong><br><div class="blockquote">' . $post->content . '</div>', Message::STATUS_REMOVED, Message::STATUS_NEW, time(), time()]; } } if (empty($package)) { throw new Exception('No one to send report to'); } Yii::$app->db->createCommand()->batchInsert(Message::tableName(), ['sender_id', 'receiver_id', 'topic', 'content', 'sender_status', 'receiver_status', 'created_at', 'updated_at'], array_values($package))->execute(); Cache::getInstance()->delete('user.newmessages'); Log::info('Post reported', $post->id, __METHOD__); return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
echo Html::encode($reply->topic); ?> </div> <div class="popover-content"> <?php echo $reply->content; ?> </div> </div> </div> </div> <?php $stack = 0; while ($reply->reply && $stack < 4) { $loggedId = User::loggedId(); if ($reply->reply->receiver_id == $loggedId && $reply->reply->receiver_status == Message::STATUS_REMOVED || $reply->reply->sender_id == $loggedId && $reply->reply->sender_status == Message::STATUS_REMOVED) { $reply = $reply->reply; } else { ?> <div class="row"> <div class="col-sm-2 text-center"> <?php echo Avatar::widget(['author' => $reply->reply->senderUser]); ?> </div> <div class="col-sm-10"> <div class="popover right podium"> <div class="arrow"></div> <div class="popover-title"> <small class="pull-right"><span data-toggle="tooltip" data-placement="top" title="<?php
/** * Subscription relation. * @return Subscription */ public function getSubscription() { return $this->hasOne(Subscription::className(), ['thread_id' => 'id'])->where(['user_id' => User::loggedId()]); }
/** * Subscribing the thread of given ID. * @param integer $id * @return \yii\web\Response */ public function actionAdd($id = null) { if (Yii::$app->request->isAjax) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while adding this subscription!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { if (is_numeric($id) && $id > 0) { $thread = Thread::findOne((int) $id); if ($thread) { $subscription = Subscription::findOne(['thread_id' => $thread->id, 'user_id' => User::loggedId()]); if ($subscription) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are already subscribed to this thread.'), ['class' => 'text-info'])]; } else { $sub = new Subscription(); $sub->thread_id = $thread->id; $sub->user_id = User::loggedId(); $sub->post_seen = Subscription::POST_SEEN; if ($sub->save()) { $data = ['error' => 0, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'You have subscribed to this thread!'), ['class' => 'text-success'])]; } } } } } else { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to subscribe to this thread'), ['class' => 'text-info'])]; } return Json::encode($data); } else { return $this->redirect(['default/index']); } }
<br> <?php Pjax::begin(); echo PageSizer::widget(); echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'filterSelector' => 'select#per-page', 'tableOptions' => ['class' => 'table table-striped table-hover'], 'columns' => [['attribute' => 'senderName', 'label' => Yii::t('podium/view', 'From') . Helper::sortOrder('senderName'), 'encodeLabel' => false, 'format' => 'raw', 'value' => function ($model) { return $model->senderName; }], ['attribute' => 'receiverName', 'label' => Yii::t('podium/view', 'To') . Helper::sortOrder('receiverName'), 'encodeLabel' => false, 'format' => 'raw', 'value' => function ($model) { return $model->receiverName; }], ['attribute' => 'topic', 'label' => Yii::t('podium/view', 'Topic') . Helper::sortOrder('topic'), 'encodeLabel' => false, 'format' => 'raw', 'value' => function ($model) { return Html::a(Html::encode($model->topic), ['messages/view', 'id' => $model->id], ['data-pjax' => '0']); }], ['attribute' => 'created_at', 'label' => Yii::t('podium/view', 'Sent') . Helper::sortOrder('created_at'), 'encodeLabel' => false, 'format' => 'raw', 'value' => function ($model) { return Html::tag('span', Yii::$app->formatter->asRelativeTime($model->created_at), ['data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::$app->formatter->asDatetime($model->created_at, 'long')]); }], ['class' => ActionColumn::className(), 'header' => Yii::t('podium/view', 'Actions'), 'contentOptions' => ['class' => 'text-right'], 'headerOptions' => ['class' => 'text-right'], 'template' => '{view} {reply} {delete}', 'buttons' => ['view' => function ($url) { return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, ['class' => 'btn btn-default btn-xs', 'data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::t('podium/view', 'View Message')]); }, 'reply' => function ($url, $model) { if ($model->sender_id != User::loggedId() && $model->senderUser !== null) { return Html::a('<span class="glyphicon glyphicon-share-alt"></span>', $url, ['class' => 'btn btn-success btn-xs', 'data-pjax' => '0', 'data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::t('podium/view', 'Reply to Message')]); } else { return Html::a('<span class="glyphicon glyphicon-share-alt"></span>', '#', ['class' => 'btn btn-xs disabled text-muted']); } }, 'delete' => function ($url) { return Html::tag('span', Html::tag('button', '<span class="glyphicon glyphicon-trash"></span>', ['class' => 'btn btn-danger btn-xs', 'data-toggle' => 'tooltip', 'data-placement' => 'top', 'title' => Yii::t('podium/view', 'Delete Message')]), ['data-toggle' => 'modal', 'data-target' => '#podiumModal', 'data-url' => $url]); }]]]]); Pjax::end(); ?> </div> </div><br> <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="podiumModal" aria-hidden="true" id="podiumModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header">