/** * 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; }
protected function deleteAllChildren() { $children = $this->_wiki->getChildrenAll(); foreach ($children as $child) { if ($child->delete() === false) { throw new DbException('Cannot delete child wiki id: ' . $child->id); } } }
/** * Autocomplete wiki title. * @param integer $ign ignore suggests from wiki page id * @param string $q */ public function actionWikiSuggest($ign = '', $q = '') { Yii::$app->response->format = Response::FORMAT_JSON; $query = Wiki::find()->andFilterWhere(['like', 'title', $q])->orderBy('title')->limit(10); if ($ign) { /** @var $wiki Wiki */ $wiki = $this->findModel(Wiki::className(), $ign); $children = $wiki->getChildrenAll(function ($query) { $query->select('id'); }); $childrenIds = array_map(function ($child) { return $child->id; }, $children); $query->where(['!=', 'id', $wiki->id])->andWhere(['not in', 'id', $childrenIds]); } $wikis = $query->all(); return array_map(function (Wiki $wiki) { return ['id' => $wiki->id, 'text' => $wiki->title]; }, $wikis); }
/** * @return ActiveQuery */ public function getWiki() { return $this->hasOne(Wiki::className(), ['id' => 'wiki_id']); }