public function actionIndex() { if (Yii::$app->user->isGuest) { $this->redirect(['/explore/index']); } $model = $this->findModel(); $newFeed = new Feed(); if ($newFeed->load(Yii::$app->request->post()) && $newFeed->save()) { Yii::$app->userData->updateKey('feed_count', Yii::$app->user->id); return $this->refresh(); } $query = new Query(); $query = $query->select('p.id, p.user_id, p.content, p.feed_data, p.template, p.created_at, u.username, u.avatar')->from('{{%home_feed}} as p')->join('LEFT JOIN', '{{%user_follow}} as f', 'p.user_id=f.people_id')->join('LEFT JOIN', '{{%user}} as u', 'u.id=p.user_id')->where('p.user_id=:user_id OR f.user_id=:user_id', [':user_id' => $model->id])->orderBy('p.created_at DESC'); $pages = Tools::Pagination($query); return $this->render('index', ['model' => $model, 'newFeed' => $newFeed, 'feeds' => $pages['result'], 'pages' => $pages['pages']]); }
/** * This is invoked before the record is saved. * @return boolean whether the record should be saved. */ public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->isNewRecord) { //插入记录(Feed) $title = Html::a(Html::encode($this->title), $this->url); preg_match_all("/<[img|IMG].*?src=\"([^^]*?)\".*?>/", $this->content, $images); $images = isset($images[0][0]) ? $images[0][0] : ''; $content = mb_substr(strip_tags($this->content), 0, 140, 'utf-8') . '... ' . Html::a(Yii::t('app', 'View Details'), $this->url) . '<br>' . $images; $postData = ['{title}' => $title, '{content}' => $content]; Feed::addFeed('blog', $postData); $this->user_id = Yii::$app->user->id; $this->created_at = time(); Yii::$app->userData->updateKey('post_count', Yii::$app->user->id); } //标签分割 $tags = trim($this->tags); $explodeTags = array_unique(explode(',', str_replace(',', ',', $tags))); $explodeTags = array_slice($explodeTags, 0, 10); $this->tags = implode(',', $explodeTags); return true; } else { return false; } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->userData->updateKey('post_count', Yii::$app->user->id); //插入记录(Feed) $title = Html::a(Html::encode($model->title), $model->url); preg_match_all("/<[img|IMG].*?src=\"([^^]*?)\".*?>/", $model->content, $images); $images = isset($images[0][0]) ? $images[0][0] : ''; $content = mb_substr(strip_tags($model->content), 0, 140, 'utf-8') . '... ' . Html::a(Yii::t('app', 'View Details'), $model->url) . '<br>' . $images; $postData = ['{title}' => $title, '{content}' => $content]; Feed::addFeed('blog', $postData); return $this->redirect(['/home/post']); } return $this->render('create', ['model' => $model]); }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $this->layout = '@app/modules/user/views/layouts/user'; $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->userData->updateKey('post_count', Yii::$app->user->id); Yii::$app->userData->updateKey('feed_count', Yii::$app->user->id); //插入记录(Feed) $title = Html::a(Html::encode($model->title), $model->url); preg_match_all("/<[img|IMG].*?src=\"([^^]*?)\".*?>/", $model->content, $images); $summary = mb_substr(strip_tags($model->content), 0, 140, 'utf-8') . '... ' . Html::a(Yii::t('app', 'View Details'), $model->url) . '<br>' . $images[0][0]; $postData = ['{title}' => $title, '{summary}' => $summary]; Feed::addFeed('blog', serialize($postData)); return $this->redirect(['/home/post']); } return $this->render('create', ['model' => $model]); }
/** * Finds the Feed model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Feed the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Feed::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * app\modules\home\models\Feed * @return array */ public function getFeeds() { $query = Feed::find()->where(['user_id' => $this->id])->orderBy('id desc'); $countQuery = clone $query; $pages = new \yii\data\Pagination(['totalCount' => $countQuery->count()]); $pages->defaultPageSize = 14; $feeds = $query->offset($pages->offset)->limit($pages->limit)->all(); return ['feeds' => $feeds, 'pages' => $pages]; }