/** * @param boolean $insert * @param array $changedAttributes * @return bool */ public function afterSave($insert, $changedAttributes) { $topic = Topic::findOne($this->topic_id); $topic->reply = $topic->reply + 1; $topic->last_reply_time = time(); $topic->updated_at = time(); $topic->last_reply_user = Yii::$app->user->identity->username; $rst = $topic->update(); Yii::$app->cache->delete('HotTopic8'); //给用户发回复或者@通知,回复自己的不通知 if ($rst && Yii::$app->user->id != $topic->user_id) { $notice = new Notice(); $notice->from_user_id = $this->user_id; $notice->to_user_id = $topic->user_id; $notice->topic_id = $this->topic_id; $notice->type = 1; $notice->msg = $this->content; $notice->created = time(); $notice->save(); } //回复中提到其他人,通知其他人 if (strstr($this->content, '@')) { preg_match_all('/@(.*?)\\s/', $this->content, $match); if (isset($match[1]) && count($match[1]) > 0) { $notice_user = array_unique($match[1]); foreach ($notice_user as $v) { $to_user = User::findOne(['username' => $v]); if ($v == $topic->user->username || $v == Yii::$app->user->identity->username || empty($to_user->id)) { continue; } $notice = new Notice(); $notice->from_user_id = $this->user_id; $notice->to_user_id = $to_user->id; $notice->topic_id = $this->topic_id; $notice->type = 2; $notice->msg = $this->content; $notice->created = time(); $notice->save(); } } } return parent::afterSave($insert, $changedAttributes); }
public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Notice::find()]); $noticeDataProviders = array(); $category = ConfigParam::getConfig('category', 'title'); foreach ($category as $key => $value) { $data = new ActiveDataProvider(['totalCount' => 10, 'pagination' => ['pageSize' => 10], 'query' => Notice::find()->where(['category' => $key])->orderBy('publishdate desc')]); $noticeDataProviders[$key] = ['title' => $value, 'dataProvider' => $data]; } return $this->render('index', ['dataProvider' => $dataProvider, 'notice' => $noticeDataProviders]); }
/** * 获取当前登陆的用户的通知总数 * @return array|\yii\db\ActiveRecord[] */ static function Count() { $NoticeCount = Notice::find()->where(['to_user_id' => Yii::$app->user->id, 'is_read' => 0])->count(); $NoticeCount = empty($NoticeCount) ? null : $NoticeCount; return $NoticeCount; }
NavBar::begin(['brandLabel' => Yii::$app->name, 'brandUrl' => Yii::$app->homeUrl, 'options' => ['class' => 'navbar-default']]); $keyword = isset($this->context->keyword) ? $this->context->keyword : ''; echo '<form class="navbar-form navbar-left" role="search" action="/search" method="get"> <div class="form-group"> <input type="text" value="' . $keyword . '" name="keyword" class="form-control search_input" id="navbar-search" placeholder="搜索..." data-placement="bottom" data-content="请输入要搜索的关键词!"> </div> </form>'; if (Yii::$app->user->isGuest) { $menuItems[] = ['label' => '注册', 'url' => ['/account/signup']]; $menuItems[] = ['label' => '登录', 'url' => ['/account/login']]; } else { $menuItems[] = ['label' => Yii::$app->user->identity->username, 'url' => ['/member/' . Yii::$app->user->identity->username]]; if (Yii::$app->user->identity->email_status == 1) { $menuItems[] = ['label' => '创作新主题', 'url' => ['/topic/create']]; } $menuItems[] = ['label' => '通知 <span class="badge badge-important">' . Notice::Count() . '</span>', 'url' => ['/account/notice']]; $menuItems[] = ['label' => '设置', 'url' => ['/account/setting']]; $menuItems[] = ['label' => '退出', 'url' => ['/account/logout'], 'linkOptions' => ['data-method' => 'post']]; } echo Nav::widget(['encodeLabels' => false, 'activateItems' => false, 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems]); NavBar::end(); ?> </header> <main id="Wrapper" style="<?php if (!empty($this->context->bg)) { echo 'background-image:url(' . Yii::$app->params['bgUrl'] . $this->context->bg . ');background-position: 0 0, 0 0;background-repeat: repeat;'; } if (!empty($this->context->bg_color)) { echo 'background-color:' . $this->context->bg_color; } ?>
public function actionNoticeDelete($id) { if (Yii::$app->user->isGuest) { return $this->redirect('/account/login?next=/account/notice-delete?id=' . $id); } if (empty($id)) { return $this->goHome(); } $notice = Notice::findOne($id); if (!empty($notice)) { $notice->delete(); } return $this->redirect('/account/notice'); }
public function afterSave($insert, $changedAttributes) { if ($this->type == 1) { Yii::$app->cache->delete('UserCount' . $this->user_id); Yii::$app->cache->delete('FollowUserId' . Yii::$app->user->id); Yii::$app->cache->delete('FollowUser' . Yii::$app->user->id); } if ($this->type == 2) { Yii::$app->cache->delete('NodeCount' . $this->user_id); Yii::$app->cache->delete('FollowNodeId' . Yii::$app->user->id); Yii::$app->cache->delete('FollowNode' . Yii::$app->user->id); } if ($this->type == 3) { Yii::$app->cache->delete('TopicCount' . $this->user_id); Yii::$app->cache->delete('NoticeCount' . $this->follow_id); Yii::$app->cache->delete('FollowTopic' . Yii::$app->user->id); $topic = Topic::findOne($this->follow_id); $topic->follow = $topic->follow + 1; $topic->save(); if ($topic->user_id != $this->user_id) { $notice = new Notice(); $notice->from_user_id = $this->user_id; $notice->to_user_id = $topic->user_id; $notice->topic_id = $this->follow_id; $notice->type = 3; $notice->msg = ''; $notice->created = time(); $notice->save(); } } return parent::afterSave($insert, $changedAttributes); }
<?php use yii\helpers\Html; use yii\bootstrap\Nav; use yii\bootstrap\NavBar; use frontend\assets\AppAsset; use common\models\Notice; /* @var $this \yii\web\View */ /* @var $content string */ AppAsset::register($this); $noticeCount = Notice::Count(); $this->beginPage(); ?> <!DOCTYPE html> <html lang="<?php echo Yii::$app->language; ?> "> <head> <meta charset="<?php echo Yii::$app->charset; ?> "> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/> <title><?php echo Html::encode($this->context->title); if ($noticeCount > 0) { echo ' (' . $noticeCount . ')'; } ?>
public function actionList($category) { $dataProvider = new ActiveDataProvider(['query' => Notice::find()->where(['category' => $category])]); return $this->render('list', ['dataProvider' => $dataProvider]); }