/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TopicContent::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['topic_id' => $this->topic_id]); $query->andFilterWhere(['like', 'topic_text', $this->topic_text])->andFilterWhere(['like', 'topic_text_short', $this->topic_text_short])->andFilterWhere(['like', 'topic_text_source', $this->topic_text_source])->andFilterWhere(['like', 'topic_extra', $this->topic_extra]); return $dataProvider; }
public function afterDelete() { (new History(['user_id' => $this->user_id, 'action' => History::ACTION_DELETE_TOPIC, 'target' => $this->id]))->save(false); TopicContent::deleteAll(['topic_id' => $this->id]); Node::updateCounterInfo('deleteTopic', $this->node_id); UserInfo::updateCounterInfo('deleteTopic', $this->user_id); $count = Comment::afterTopicDelete($this->id); Siteinfo::updateCountersInfo(['topics' => -1, 'comments' => -$count]); Favorite::afterTopicDelete($this->id); Notice::afterTopicDelete($this->id); return parent::afterDelete(); }
public function actionEdit($id) { $request = Yii::$app->getRequest(); $me = Yii::$app->getUser()->getIdentity(); $model = $this->findTopicModel($id, ['content', 'node']); if (!$me->canEdit($model)) { throw new ForbiddenHttpException('您没有权限修改或已超过可修改时间。'); } if ($me->isAdmin()) { $model->scenario = Topic::SCENARIO_ADMIN_EDIT; } else { $model->scenario = Topic::SCENARIO_AUTHOR_EDIT; } if (!($content = $model->content)) { $content = new TopicContent(['topic_id' => $model->id]); } $oldTags = $model->tags; if ($model->load($request->post()) && $model->validate() && $content->load($request->post()) && $content->validate()) { // $model->tags = Tag::editTags($model->tags, $oldTags); $model->tags = Tag::getTags($model->tags); $model->save(false) && $content->save(false); Tag::afterTopicEdit($model->id, $model->tags, $oldTags); (new History(['user_id' => $me->id, 'action' => History::ACTION_EDIT_TOPIC, 'action_time' => $model->updated_at, 'target' => $model->id]))->save(false); return $this->redirect(Topic::getRedirectUrl($id, 0, $request->get('ip', 1), $request->get('np', 1))); } return $this->render('edit', ['model' => $model, 'content' => $content]); }
/** * @return \yii\db\ActiveQuery */ public function getTopicContent() { return $this->hasOne(TopicContent::className(), ['topic_id' => 'topic_id']); }
/** * Finds the TopicContent model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return TopicContent the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = TopicContent::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }