public function actionSingle()
 {
     $model = new CommentForm();
     $blog = Blog::getInstance();
     $uri = \Yii::$app->request->get('uri');
     $article = Articles::findOne(['id' => \Yii::$app->request->get('id')]);
     if (!$article) {
         \Yii::$app->session->setFlash('articleNotFound', 'warning');
         // Look at UrlManager configurations
         // Real address is['/blog/category?uri=' . $uri]
         return $this->redirect(['/category/' . $uri], 302);
     }
     $articleCatUri = $article->category->uri;
     $articleId = $article->id;
     if ($articleCatUri != $uri) {
         // Look at UrlManager configurations
         // Real address is ['/blog/single?uri=' . $articleCatUri . '&id=' . $articleId]
         return $this->redirect(['/category/' . $articleCatUri . '/' . $articleId], 302);
     }
     if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
         $model->article_id = $articleId;
         $answer = $model->addComment() ? ['name' => 'commentSuccessSending', 'type' => 'success'] : ['name' => 'commentWarningSending', 'type' => 'error'];
         \Yii::$app->session->setFlash($answer['name'], $answer['type']);
         return $this->refresh();
     }
     $data = $blog->getComments($articleId);
     return $this->render('single', ['article' => $article, 'model' => $model, 'data' => $data]);
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  * @param string $id
  *
  * @return ActiveDataProvider
  */
 public function search($params, $id = null)
 {
     $query = is_null($id) ? Articles::find()->with('category') : Articles::find()->where(['category_id' => $id])->with('category');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pagesize' => 10, 'forcePageParam' => false, 'pageSizeParam' => false]]);
     $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, 'created' => $this->created, 'updated' => $this->updated, 'category_id' => $this->category_id, 'comments_status' => $this->comments_status, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content]);
     return $dataProvider;
 }
Beispiel #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Articles::find()->where(['publish_status' => 'publish']);
     $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, 'author_id' => $this->author_id, 'publish_date' => $this->publish_date, 'rank' => $this->rank]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'anons', $this->anons])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'picture_path', $this->picture_path])->andFilterWhere(['like', 'publish_status', $this->publish_status]);
     return $dataProvider;
 }
Beispiel #4
0
 public function getArticlesPage($category = false)
 {
     $condition = ['category_id' => $category->id, 'status' => 10];
     if (!$category) {
         $condition = array_splice($condition, 1, 1);
     }
     $query = Articles::find()->where($condition)->with('category');
     if (!$query->count()) {
         return false;
     }
     $pagination = new Pagination(['defaultPageSize' => 10, 'totalCount' => $query->count(), 'forcePageParam' => false, 'pageSizeParam' => false]);
     $articles = $query->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all();
     return ['articles' => $articles, 'pagination' => $pagination];
 }
Beispiel #5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getArticles()
 {
     return $this->hasMany(Articles::className(), ['category_id' => 'id']);
 }
Beispiel #6
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getArticle()
 {
     return $this->hasOne(Articles::className(), ['id' => 'article_id']);
 }
 /**
  * Finds the Articles model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Articles the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Articles::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('Сторінки, яку Ви шукаєте не існує.');
     }
 }
Beispiel #8
0
 /**
  * Finds the Articles model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return Articles the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Articles::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #9
0
 public static function getModelForAutocomplete()
 {
     return Articles::find()->select(['id as value', 'title as value'])->asArray()->all();
 }
 /**
  * Updates an existing Comments model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $model->updated = date('Y-m-d');
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     }
     $data = ArrayHelper::map(Articles::find()->all(), 'id', 'title');
     return $this->render('update', ['data' => $data, 'model' => $model, 'items' => $this->getItems()]);
 }