/** * 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($id) { if (($model = News::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 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.'); } }
public function actionCommentsWidget($id, $showButtons = true) { $news = News::findOne($id); if (!$news) { throw new NotFoundHttpException("Новость с идентификатром {$id} не найдена!"); } return $this->renderPartial('comments-widget', ['news' => $news, 'showButtons' => $showButtons, 'comments' => new ActiveDataProvider(['query' => $news->getComments(), 'pagination' => ['pageSize' => 0], 'sort' => ['defaultOrder' => ['date' => SORT_ASC]]])]); }
public function save() { $news = new News(); if (!empty($this->id)) { $news = News::findOne($this->id); } $news->setAttributes(['title' => $this->title, 'meta_keywords' => $this->metaKeywords, 'meta_description' => $this->metaDescription, 'categoryID' => $this->category, 'favorite' => $this->favorite, 'published' => $this->published, 'publishDate' => strtotime($this->publishDate), 'deleted' => $this->deleted, 'textPreview' => preg_replace('/"=""/', '', $this->textPreview), 'text' => preg_replace('/"=""/', '', $this->textExtension), 'author' => $this->author, 'link' => $this->link, 'moderatedComments' => $this->moderatedComments, 'language' => $this->language, 'genre' => $this->genre], false); if (!$news->save(false)) { return false; } $this->id = $news->id; return true; }
/** * 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()]); }
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();