/** * Finds the News model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return News the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($link) { if (($model = News::find()->where('name=:link AND lang_id=:lang', [':link' => $link, ':lang' => Language::$current->id])->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Lists all News models. * @return mixed */ public function actionIndex() { //$model = News::find()->orderBy(['id' => SORT_DESC])->all(); //return $this->render('index', ['models' => $model,]); $query = News::find()->where(['category_id' => 1])->orderBy(['id' => SORT_DESC]); $pages = new \loveorigami\pagination\ReversePagination(['totalCount' => $query->count(), 'pageSize' => 10]); $pages->pageSizeParam = false; $models = $query->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('index', ['models' => $models, 'pages' => $pages]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = News::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, 'category_id' => $this->category_id, 'date_created' => $this->date_created, 'date_published' => $this->date_published, 'user_id' => $this->user_id]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'images', $this->images])->andFilterWhere(['like', 'text', $this->text])->andFilterWhere(['like', 'url_source', $this->url_source])->andFilterWhere(['like', 'name_source', $this->name_source]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = News::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'status' => $this->status]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'desc', $this->desc])->andFilterWhere(['like', 'ext_url', $this->ext_url]); return $dataProvider; }
public function actionIndex() { $todayStart = strtotime(date('Y-m-d')); $newsViews = NewsViews::find()->select("SUM(`views`)")->where(['date' => $todayStart])->scalar(); if (!$newsViews) { $newsViews = 0; } $todayViewedNews = NewsViews::find()->select('newsID')->where(['date' => $todayStart])->orderBy('views DESC')->asArray()->all(); $popularFiveToday = $popularToday = $todayPopularNews = []; if ($todayViewedNews) { $popularFiveToday = ArrayHelper::getColumn(array_slice($todayViewedNews, 0, 5), 'newsID'); } $sViews = NewsViews::find()->select(['title' => 'date', 'views' => 'COUNT(`views`)'])->groupBy('date')->limit(30)->asArray()->all(); array_walk($sViews, function (&$item, $key) { $item['title'] = \Yii::$app->formatter->asDate($item['title']); }); return $this->render('index', ['monthlyViews' => $sViews, 'todayViewedNewsCount' => count($todayViewedNews), 'lastNews' => new ActiveDataProvider(['query' => News::find(), 'pagination' => ['pageSize' => 5], 'sort' => ['defaultOrder' => ['publishDate' => SORT_DESC]]]), 'popularToday' => new ActiveDataProvider(['query' => News::find()->select(['news.*', 'todayViews' => 'newsViews.views'])->where(['in', 'id', $popularFiveToday])->with('todayViews')->joinWith('todayViews')->orderBy(['todayViews' => SORT_DESC]), 'pagination' => ['pageSize' => 5], 'sort' => false]), 'lastAuth' => new ActiveDataProvider(['query' => Authorization::find()->with('user'), 'pagination' => ['pageSize' => 5], 'sort' => ['defaultOrder' => ['date' => SORT_DESC]]]), 'newsCount' => News::find()->where("publishDate >= '{$todayStart}'")->count(), 'newsViews' => $newsViews, 'commentsCount' => Comment::find()->where("date >= '{$todayStart}'")->count()]); }
/** * Lists all News models. * @return mixed */ public function actionIndex() { $model = News::find()->where(['category_id' => 2])->orderBy(['id' => SORT_DESC])->all(); return $this->render('index', ['models' => $model]); }
public function actionGetList() { $query = News::find()->where(['deleted' => 0]); if ($excluded = \Yii::$app->request->get("excludedCategories")) { if (is_array($excluded)) { $query->andWhere(['not in', 'categoryID', $excluded]); } } if ($excluded = \Yii::$app->request->get("excludedNews")) { $query->andWhere(['not in', 'id', $excluded]); } if ($onlyCategories = \Yii::$app->request->get("onlyCategories")) { $query->andWhere(['in', 'categoryID', $onlyCategories]); } if ($searchQuery = \Yii::$app->request->get("q")) { $query->andWhere(['like', 'title', $searchQuery]); } $newsList = []; foreach ($query->limit(50)->each() as $news) { $newsList[] = ['id' => $news->id, 'text' => $news->title]; } if (\Yii::$app->request->isAjax) { \Yii::$app->response->format = 'json'; } return \Yii::$app->response->format ? ['results' => $newsList] : $newsList; }
use kartik\select2\Select2; use yii\bootstrap\Html; use yii\helpers\ArrayHelper; use yii\web\JsExpression; $form = \kartik\form\ActiveForm::begin(['type' => \kartik\form\ActiveForm::TYPE_HORIZONTAL, 'id' => 'edit-rss-form', 'action' => empty($action) ? $model->isNew ? '/rss/add-feed' : '/rss/edit-feed/' . $model->id : $action, 'options' => ['data-pjax' => true, 'class' => 'editRssForm']]); ?> <div class="modal-body"> <?php if (\Yii::$app->session->getFlash('saved', false)) { echo \yii\bootstrap\Alert::widget(['body' => \Yii::$app->session->getFlash('saved'), 'options' => ['class' => 'alert alert-success alert-dismissible']]); } else { if (\Yii::$app->session->getFlash('error', false)) { echo \yii\bootstrap\Alert::widget(['body' => \Yii::$app->session->getFlash('error'), 'options' => ['class' => 'alert alert-danger alert-dismissible row col-xs-10 col-xs-offset-1']]); } } echo $form->field($model, 'name'), $form->field($model, 'description')->textarea(), $form->field($model, 'categories')->widget(Select2::className(), ['options' => ['multiple' => true], 'data' => !$model->isNew ? ArrayHelper::map(Category::find()->select('id, title')->where(['deleted' => 0])->andWhere(['in', 'id', $model->categories])->asArray()->all(), 'id', 'title') : [], 'pluginOptions' => ['ajax' => ['url' => '/categories/get-list', 'data-type' => 'json', 'data' => new JsExpression("function(params){ return {q:params.term} }")]]]), $form->field($model, 'articles')->widget(Select2::className(), ['options' => ['multiple' => true], 'data' => !$model->isNew ? ArrayHelper::map(News::find()->select('id, title')->where(['deleted' => 0])->andWhere(['in', 'id', $model->articles])->asArray()->all(), 'id', 'title') : [], 'pluginOptions' => ['ajax' => ['url' => '/news/get-list', 'data-type' => 'json', 'data' => new JsExpression('function(params){ var data = {q:params.term}, categories = $("#newsfeedform-categories").val(); if(categories !== null && categories.length > 0){ data.excludedCategories = categories;} return data; }')]]]), $form->field($model, 'published')->checkbox([], false), $form->field($model, 'id', ['options' => ['style' => 'display: none']])->hiddenInput()->label(false); ?> </div> <div class="modal-footer"> <?php if (\Yii::$app->request->isAjax) { ?> <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Закрыть</button><?php } ?> <?php echo Html::tag('div', Html::button($model->isNew ? 'Добавить ленту' : 'Сохранить изменения', ['class' => 'btn btn-success', 'type' => 'submit']), ['class' => 'text-center']); ?> </div> <?php $form->end();
<?php use backend\helpers\ArrayHelper; use backend\models\Category; use backend\models\News; use kartik\form\ActiveForm; use kartik\select2\Select2; use yii\web\JsExpression; $form = ActiveForm::begin(['type' => ActiveForm::TYPE_HORIZONTAL, 'id' => 'edit-theme-form', 'options' => ['data-pjax' => true]]); echo $form->field($model, 'name'), $form->field($model, 'categories')->widget(Select2::className(), ['options' => ['multiple' => true], 'data' => !$model->isNew ? ArrayHelper::map(Category::find()->select('id, title')->where(['deleted' => 0])->andWhere(['in', 'id', $model->categories])->asArray()->all(), 'id', 'title') : [], 'pluginOptions' => ['ajax' => ['url' => '/categories/get-list', 'data-type' => 'json', 'data' => new JsExpression("function(params){ return {q:params.term} }")]]]), $form->field($model, 'articles')->widget(Select2::className(), ['options' => ['multiple' => true], 'data' => !$model->isNew ? ArrayHelper::map(News::find()->select('id, title')->where(['deleted' => 0])->andWhere(['in', 'id', $model->articles])->asArray()->all(), 'id', 'title') : [], 'pluginOptions' => ['ajax' => ['url' => '/news/get-list', 'data-type' => 'json', 'data' => new JsExpression('function(params){ var data = {q:params.term}, categories = $("#mainthemeform-categories").val(); if(categories !== null && categories.length > 0){ data.excludedCategories = categories;} return data; }')]]]), $form->field($model, 'color')->dropDownList($model->getColors()), $form->field($model, 'titleWords', ['inputOptions' => ['format' => 'number']]), $form->field($model, 'textWords', ['inputOptions' => ['format' => 'number']]), $form->field($model, 'enabled')->checkbox([], false), \yii\bootstrap\Html::tag('div', \yii\bootstrap\Html::button('Сохранить', ['class' => 'btn btn-success btn-lg', 'type' => 'submit']), ['class' => 'text-center']); $form->end();