/** * Validate and save wiki page. * @return Wiki|false */ public function save() { $this->trigger(self::EVENT_BEFORE_UPDATE); if (!$this->validate()) { return false; } $transaction = Yii::$app->db->beginTransaction(); try { $isNew = $this->isNew(); // Preserve New status for later checking. $this->_wiki->title = $this->title; $this->_wiki->slug = $this->slug; if (!$this->_wiki->save()) { throw new \yii\db\Exception('Cannot save Wiki model.'); } // Don't save wiki if content not modified. if ($isNew || $this->content != $this->getHistoryContent()) { $history = new History(); $history->wiki_id = $this->_wiki->id; $history->content = $this->content; $history->host_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $history->summary = $this->summary; if (!$history->save()) { throw new \yii\db\Exception('Cannot save History model.'); } } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); return false; } $this->trigger(self::EVENT_AFTER_UPDATE); return $this->_wiki; }
/** * @return \yii\db\ActiveQuery */ public function getHistoryLatest() { return $this->hasOne(History::className(), ['wiki_id' => 'id'])->orderBy('rev DESC'); }
/** * Update wiki page. * @param integer $id wiki page id * @param integer $rev history revision id */ public function actionUpdate($id, $rev = null) { /** @var $wiki Wiki */ $wiki = $this->findModel(Wiki::className(), $id); if (!Yii::$app->user->can('updateWiki', ['wiki' => $wiki])) { throw new ForbiddenHttpException(); } $editor = new Editor($wiki); if ($rev) { /** @var $history History */ $history = History::findOne(['wiki_id' => $id, 'rev' => $rev]); if (!$history) { $this->addFlash(self::FLASH_WARNING, Yii::t('app', 'Revision not found.')); } $editor->content = $history->content; } $historyProvider = new ActiveDataProvider(['query' => History::find()->where(['wiki_id' => $id])->orderBy('created_at DESC'), 'pagination' => ['pageSize' => 5]]); if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); if ($editor->load($post) && $editor->save()) { return $this->redirect(['page/view', 'id' => $editor->getWiki()->id]); } } return $this->render('update', ['editor' => $editor, 'historyProvider' => $historyProvider]); }