/** * 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]); }
/** * Creates a new Feed model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($id = null) { $model = new Feed(); $model->setScenario('repost'); if ($id != null && $model->load(Yii::$app->request->post())) { $id = intval($id); $query = new Query(); $feed = $query->select('f.content, f.feed_data, f.template, u.username')->from('{{%home_feed}} as f')->join('LEFT JOIN', '{{%user}} as u', 'u.id=f.user_id')->where('f.id=:id', [':id' => $id])->one(); $postData = ['{comment}' => $model->content, '{content}' => $feed['content'], '{username}' => Html::a($feed['username'], ['/user/view', 'id' => $feed['username']]), '{feed_data}' => unserialize($feed['feed_data']), '{template}' => $feed['template']]; Feed::addFeed('repost', $postData); Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Create successfully.')); return $this->redirect(['/user/dashboard']); } $model->setScenario('create'); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->userData->updateKey('feed_count', Yii::$app->user->id); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }