keepSnapshots(true); $this->addBehavior( new SoftDelete([ 'field' => 'deleted', 'value' => 1, ]) ); } public function beforeCreate() { if (empty($this->cover)) { $this->cover = kg_default_article_cover_path(); } elseif (Text::startsWith($this->cover, 'http')) { $this->cover = self::getCoverPath($this->cover); } if (is_array($this->tags)) { $this->tags = kg_json_encode($this->tags); } $this->create_time = time(); } public function beforeUpdate() { if (time() - $this->update_time > 3 * 3600) { $sync = new ArticleIndexSync(); $sync->addItem($this->id); } if (Text::startsWith($this->cover, 'http')) { $this->cover = self::getCoverPath($this->cover); } if (is_array($this->tags)) { $this->tags = kg_json_encode($this->tags); } if ($this->deleted == 1) { $this->published = 0; } $this->update_time = time(); } public function afterCreate() { $cache = new MaxArticleIdCache(); $cache->rebuild(); } public function afterFetch() { if (!Text::startsWith($this->cover, 'http')) { $this->cover = kg_cos_article_cover_url($this->cover); } if (is_string($this->tags)) { $this->tags = json_decode($this->tags, true); } } public static function getCoverPath($url) { if (Text::startsWith($url, 'http')) { return parse_url($url, PHP_URL_PATH); } return $url; } public static function sourceTypes() { return [ self::SOURCE_ORIGIN => '原创', self::SOURCE_REPRINT => '转载', self::SOURCE_TRANSLATE => '翻译', ]; } public static function sortTypes() { return [ 'latest' => '最新', 'popular' => '最热', 'featured' => '推荐', ]; } }