/** * 将文章插入数据库 * @param $title * @param $content * @param $publish_at * @param $tag * @return bool */ public static function insert($title, $content, $publish_at, $tag = '') { //插入标签(搜索的分类) $article = new Article(); $article->title = $title; $article->content = $content; $article->author = 'yang'; $article->status = Article::STATUS_GATHER; $article->publish_at = $publish_at; $res = $article->save(false); if ($tag) { try { $tagModel = Tag::find()->where(['name' => $tag])->one(); if (!$tagModel) { $tagModel = new Tag(); $tagModel->name = $tag; $tagModel->article_count = 0; $tagModel->save(false); } $articleTag = new ArticleTag(); $articleTag->article_id = $article->id; $articleTag->tag_id = $tagModel->id; $articleTag->save(false); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; } } return $res ? true : false; }
public function actionView($id) { $article = Article::findOne($id); $comment = new Comment(); $comments = $article->comments; return $this->render("view", ["article" => $article, "comment" => $comment, "comments" => $comments]); }
/** * 是否显示状态修改 * @param $article_id * @param int $status * @return bool|int */ public function setStatus($article_id, $status = 1) { if (!in_array($status, [0, 1])) { return false; } return Article::updateAll(['status' => $status], 'article_id = :article_id', [':article_id' => $article_id]); }
public function actionView($id) { $this->view_data['article'] = $article = Article::find()->where("id = {$id}")->asArray()->one(); //获取这篇文章的所有留言 $this->view_data['comments'] = Article::getComments($article['id']); return $this->render('view', $this->view_data); }
public function actionAddStars($id) { if (\Yii::$app->request->isAjax) { $model = Article::findOne($id); $model->addStars(); return json_encode(1); } }
/** * Finds the Article model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Article the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Article::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @param $slug * @return string * @throws NotFoundHttpException */ public function actionView($slug) { $model = Article::find()->published()->andWhere(['slug' => $slug])->one(); if (!$model) { throw new NotFoundHttpException(); } $viewFile = $model->view ?: 'view'; return $this->render($viewFile, ['model' => $model]); }
public function actionDetail($id) { if (!($article = Article::findOne($id))) { throw new NotFoundHttpException('ID 为 ' . $id . ' 的文章没有找到'); } //文章标签 $tags = $article->getArticleTag(); //相关文章 return $this->render('detail', ['article' => $article, 'tags' => $tags]); }
/** * 生成 404 静态页面 * @author gaoqing * @date 2016-04-20 * @param string $cacheKey 缓存的唯一标识 * @return boolean$generateFlag是否生成成功 */ public static function cache404Page($cacheKey, $param = [], $forceCache = false) { $generateFlag = false; $frontend = \Yii::getAlias('@frontend'); $page404FileName = $frontend . '/web/404.shtml'; if (!file_exists($page404FileName)) { $forceCache = true; } if ($forceCache) { $view = "404"; //获取最新的资讯文章 $article = new Article(); $where = ' status=20'; $order = ' articleid DESC'; $lastestNews = $article->List_Articles($where, $order, 5, 0); //获取 疾病健康 文章 $darticle = new \common\models\disease\Article(); $lastestJibingArticle = $darticle->getLatestArticle(5, 0); //精彩问答 $ask = new Ask(); $where1 = ' 1'; $order1 = ' id DESC'; $lastestAsk = $ask->getList($where1, $order1, 5, 0); //字母部分 $letters = range('A', 'Z'); $condition = array('typeid' => array(0, 2, 3, 4, 5, 6, 7, 8, 9)); $rand_words = KeyWords::getCacheRandWords(100, $condition); //热门疾病、热门部位 $commonDisDep = CacheHelper::getCache('frontend_article_detail_rmksbw_404', []); $params = ['lastestNews' => $lastestNews, 'lastestJibingArticle' => $lastestJibingArticle, 'lastestAsk' => $lastestAsk, 'letters' => $letters, 'rand_words' => $rand_words, 'commonDisDep' => $commonDisDep, 'searchurl' => 'http://www.9939.com/zhuanti/']; $controller = new BaseController('base404', null); $controller->id = "base404"; $page404FilePath = $frontend . '/views/site'; $controller->viewPath = $page404FilePath; $page404 = $controller->renderPartial($view, $params); if (isset($page404) && !empty($page404)) { if (file_put_contents($page404FileName, $page404)) { $generateFlag = true; } } } return $generateFlag; }
/** * Finds the ArticleDownload model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ArticleDownload the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $ArticleDownload = ArticleDownload::findOne($id); $Article = Article::findOne($id); if ($ArticleDownload !== null && $Article !== null) { return ['ArticleDownload' => $ArticleDownload, 'Article' => $Article]; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Deletes an existing ArticleCategory model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $articleModel = Article::find()->andWhere(['category_id' => $id])->one(); if (null === $articleModel) { $this->findModel($id)->delete(); } else { Yii::$app->session->setFlash('alert', ['body' => \Yii::t('backend', 'Can not delete category #' . $id . '. It used in other table. Change category for article #' . $articleModel->id . ' before delete.'), 'options' => ['class' => 'alert-error']]); } return $this->redirect(['index']); }
public static function getMetaTags() { $tags = []; $locale = null; $arr = self::parseUrl(Yii::$app->request->pathInfo); if (!empty($arr[0]) and !empty($arr[1]) and !empty($arr[2]) and !empty($arr[3])) { $shortLocale = $arr[0]; $controller = $arr[1]; $action = $arr[2]; $slug = $arr[3]; foreach (Yii::$app->params['availableLocales'] as $k => $v) { if ($shortLocale == explode('-', $k)[0]) { $locale = $k; } } switch ($controller) { case 'page': $model = self::find()->published()->andWhere(['slug' => $slug, 'locale' => $locale])->one(); break; case 'article': $model = Article::find()->published()->andWhere(['slug' => $slug, 'locale' => $locale])->one(); break; case 'promo': //$model = Promo::find()->published()->andWhere(['slug' => $slug, 'locale' => $locale])->one(); break; case 'project': $model = Project::find()->published()->andWhere(['slug' => $slug, 'locale' => $locale])->one(); break; } //hardcore :) json to array if (!empty($model) and !empty($model->head)) { $arr = json_decode($model->head, true); foreach ($arr as $key => $value) { foreach ($value as $key2 => $value2) { //custom meta tag if (4 == count($value2)) { $value2 = array_values($value2); $value2 = [$value2[0] => $value2[1], $value2[2] => $value2[3]]; } $tags[] = $value2; } } } if (empty($model)) { throw new NotFoundHttpException('The requested page does not exist.'); } if (!empty($tags[0]) and !empty($tags[0]['name']) and 'title' == !empty($tags[0]['name']) and !empty($tags[0]['content'])) { Yii::$app->view->title = $tags[0]['content']; } else { Yii::$app->view->title = $model->title; } } return $tags; }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = Article::find()->published(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'slug' => $this->slug, 'category_id' => $this->category_id]); $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = Article::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'slug' => $this->slug, 'author_id' => $this->author_id, 'category_id' => $this->category_id, 'updater_id' => $this->updater_id, 'status' => $this->status, 'published_at' => $this->published_at, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'weight', $this->weight])->andFilterWhere(['like', 'body', $this->body]); return $dataProvider; }
/** * Lists the Article models in a specific category $slug. * * @param $slug * @return mixed */ public function actionTag($slug) { $model = Tag::find()->andWhere(['slug' => $slug])->one(); if (!$model) { throw new NotFoundHttpException(Yii::t('frontend', 'Page not found.')); } $query = Article::find()->with('category')->joinWith('tags')->where('{{%tag}}.slug = :slug', [':slug' => $slug])->published(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 10]]); $dataProvider->sort = ['defaultOrder' => ['created_at' => SORT_DESC]]; return $this->render('tag', ['model' => $model, 'dataProvider' => $dataProvider, 'menuItems' => self::getMenuItems()]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Article::find()->orderBy('created_at DESC'); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'author', $this->author]); return $dataProvider; }
public function actionSearchList($q = null) { $out = []; if ($q) { $query = Article::find()->where(['LIKE', 'title', $q])->orWhere(['LIKE', 'body', $q]); $data = $query->limit(10)->all(); } foreach ($data as $d) { $out[] = ['value' => $d['title']]; } echo Json::encode($out); }
public function actionIndex() { if (\Yii::$app->user->isGuest) { $this->redirect(['user/sign-in/login']); } $article = (new Query())->from('article')->all(); $query = Article::find(); $provide = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC, 'title' => 'simon']]]); $provide->prepare(); $pagination = $provide->getPagination(); return $this->render('index', ['article' => $provide->getModels(), 'pageCount' => $pagination->pageCount, 'page' => $pagination->page, 'links' => $pagination->getLinks()]); }
public function init() { parent::init(); if ($this->id) { $this->_item = Article::findOne($this->id); } else { throw new InvalidParamException(\Yii::t('front', 'No required parameter given') . ' - id'); } if (!$this->articleType) { $this->articleType = 'normal'; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Article::find()->where(['in', 'status', [Article::STATUS_GATHER, Article::STATUS_DISPLAY]]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 100]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } if ($this->status) { $query->andFilterWhere(['like', 'status', $this->status]); } return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Article::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(['id' => $this->id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'body', $this->body]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ArticleModel::find()->orderBy('id desc'); $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(['id' => $this->id, 'category_id' => $this->category_id, 'status' => $this->status]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'category', $this->category])->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'cover', $this->cover]); return $dataProvider; }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = Article::find(); if (!\Yii::$app->user->can('administrator')) { $query->forDomain(); } $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'slug' => $this->slug, 'author_id' => $this->author_id, 'category_id' => $this->category_id, 'updater_id' => $this->updater_id, 'status' => $this->status, 'published_at' => $this->published_at, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'domain_id' => $this->domain_id]); $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'weight', $this->weight])->andFilterWhere(['like', 'body', $this->body])->andFilterWhere(['like', 'before_body', $this->before_body])->andFilterWhere(['like', 'after_body', $this->after_body])->andFilterWhere(['like', 'on_scenario', $this->on_scenario]); return $dataProvider; }
/** * @param string $category * @param string $slug * @return string * @throws NotFoundHttpException */ public function actionView($category, $slug) { $model = Article::find()->published()->andWhere(['slug' => $slug])->one(); if (!$model) { throw new NotFoundHttpException(); } //Мета-теги foreach (['description', 'keywords'] as $name) { $attr = 'meta_' . $name; \Yii::$app->view->registerMetaTag(['name' => $name, 'content' => strlen($model->{$attr}) > 0 ? $model->{$attr} : Yii::$app->keyStorage->get('frontend.' . $attr)], $name); } $viewFile = $model->view ?: 'view'; return $this->render($viewFile, ['model' => $model]); }
/** * Creates data provider instance with search query applied. * * @param array $params * @return ActiveDataProvider */ public function search($params) { $query = Article::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pagesize' => 30]]); $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'category_id' => $this->category_id, 'author_id' => $this->author_id, 'updater_id' => $this->updater_id, 'published_at' => $this->published_at, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'keywords', $this->keywords])->andFilterWhere(['like', 'body', $this->body])->andFilterWhere(['like', 'preview', $this->body]); return $dataProvider; }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = Article::find(); $query->joinWith(['author']); if (!\Yii::$app->user->can('administrator')) { $query->forDomain(); } $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'slug' => $this->slug, 'category_id' => $this->category_id, 'updater_id' => $this->updater_id, 'status' => $this->status, 'domain_id' => $this->domain_id]); if ($this->search_date_published != '') { $this->data_begin_published = strtotime($this->search_date_published); $this->data_end_published = strtotime($this->search_date_published) + 24 * 60 * 60; } $query->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'weight', $this->weight])->andFilterWhere(['like', 'body', $this->body])->andFilterWhere(['like', 'before_body', $this->before_body])->andFilterWhere(['like', 'after_body', $this->after_body])->andFilterWhere(['like', 'user.username', $this->author])->andFilterWhere(['between', 'article.published_at', $this->data_begin_published, $this->data_end_published])->andFilterWhere(['like', 'on_scenario', $this->on_scenario]); return $dataProvider; }
public function actionGenerate() { $webUrl = Yii::getAlias('@frontendUrl'); // проверка наличия слеша в конце ссылки if (!StringHelper::endsWith($webUrl, '/', false)) { $webUrl .= '/'; } $webPath = Yii::getAlias('@frontend/web/'); // create sitemap $sitemap = new Sitemap($webPath . 'sitemap.xml'); // add some URLs foreach (Article::find()->published()->all() as $item) { $sitemap->addItem($webUrl . 'article/' . $item->slug, time(), Sitemap::DAILY); } // write it $sitemap->write(); // get URLs of sitemaps written $sitemapFileUrls = $sitemap->getSitemapUrls($webUrl); // create sitemap for static files $staticSitemap = new Sitemap($webPath . 'sitemap_static.xml'); // add some URLs $staticSitemap->addItem($webUrl . 'article/index'); $staticSitemap->addItem($webUrl . 'site/contact'); // write it $staticSitemap->write(); // get URLs of sitemaps written $staticSitemapUrls = $staticSitemap->getSitemapUrls($webUrl); // create sitemap index file $index = new Index($webPath . 'sitemap_index.xml'); // add URLs foreach ($sitemapFileUrls as $sitemapUrl) { $index->addSitemap($sitemapUrl); } // add more URLs foreach ($staticSitemapUrls as $sitemapUrl) { $index->addSitemap($sitemapUrl); } // write it $index->write(); Console::output('The sitemap generated successfully.'); }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = Article::find()->published(); $dataProvider = new ActiveDataProvider(['query' => $query]); //q=word if (!empty($params['q'])) { $q = $params['q']; //$q="иб"; //$query2=Article::find(); //$query2->andWhere(['LIKE', 'title', $q]); //$t=$query2->all(); $query->andWhere(['LIKE', 'title', $q])->orWhere(['LIKE', 'body', $q]); return $dataProvider; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'slug' => $this->slug, 'category_id' => $this->category_id]); $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * redis保存文章浏览数,定时同步到数据表 * `crontab -e` * `0 2 * * * yii article/sync-view` */ public function actionSyncView() { $redis = \Yii::$app->redis; $keys = $redis->keys('article:view:*'); foreach ($keys as $v => $key) { $id = join('', array_slice(explode(':', $key), -1)); $article = Article::find()->where(['id' => $id])->one(); if (empty($article)) { $redis->del($v); continue; } $view = $redis->get($key); // 更新浏览数 if ($article->view < $view) { $article->view = $redis->get($key); $article->save(false); } else { $redis->set($v, $article->view); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Article::find()->where(['in', 'status', [Article::STATUS_HIDDEN, Article::STATUS_DISPLAY]]); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { return $dataProvider; } if ($this->publish_at) { $query->andFilterWhere(['like', 'publish_at', $this->publish_at]); } if ($this->title) { $query->andFilterWhere(['like', 'title', $this->title]); } if ($this->author) { $query->andFilterWhere(['like', 'author', $this->author]); } if ($this->category_id) { $query->andFilterWhere(['category_id' => $this->category_id]); } return $dataProvider; }