/**
  * Creates a new Posts model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Posts();
     /** @var Categories[] $categories */
     $categories = Categories::find()->all();
     /** @var [] $items [category_id => 'name',category_id => 'name',category_id => 'name'] */
     $items = [];
     foreach ($categories as $category) {
         $items[$category->id] = $category->title;
     }
     //var_dump($items);
     // exit;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         foreach ($model->categoriesID as $categoryID) {
             $categoryPost = new Relatives();
             $categoryPost->id_categories = $categoryID;
             $categoryPost->id_posts = $model->id;
             $categoryPost->save();
         }
         //var_dump(Yii::$app->request->post());
         // exit;
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'items' => $items]);
     }
 }
Exemple #2
0
 /**
  * Creates a new Posts model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Posts();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->post_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemple #3
0
 /**
  * Creates a new Posts model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Posts();
     if ($model->load(Yii::$app->request->post())) {
         $model->user_id = Yii::$app->user->getId();
         $model->create = date('Y-d-m h:i:s');
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->renderAjax('create', ['model' => $model]);
     }
 }
Exemple #4
0
 /**
  * Creates a new Posts model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Posts();
     $model->active = true;
     $model->allow_comments = true;
     $model->updated_at = date("Y-m-d H:i:s");
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         $upload_form_model = new UploadForm();
         $upload_form_model->post_id = $model->id;
         return $this->render('create', ['upload_form_model' => $upload_form_model, 'model' => $model]);
     }
 }
 public function actionBlog($id = null)
 {
     $user = Yii::$app->user->id;
     /** @var $profile \app\models\Profile */
     if ($id === null) {
         $profile = Profile::findOne(['user_id' => $user]);
         $queryPosts = Posts::find()->where(['user_id' => $user]);
     } else {
         $queryPosts = Posts::find()->where(['user_id' => $id]);
         $profile = Profile::findOne(['user_id' => $id]);
         if (!isset($profile)) {
             Yii::$app->getSession()->setFlash('error', 'that profile not found');
             return $this->goHome();
         }
     }
     $pagination = new Pagination(['defaultPageSize' => 5, 'totalCount' => $queryPosts->count()]);
     $posts = $queryPosts->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all();
     $model = new Posts();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         if ($model->createPost()) {
             $this->refresh();
         } else {
             $errors = $model->errors;
             Yii::$app->session->setFlash('error', 'Error: check the fields');
         }
     }
     return $this->render('blog', ['posts' => $posts, 'profile' => $profile, 'activePage' => Yii::$app->request->get('page', 1), 'countPages' => $pagination->getPageCount(), 'pagination' => $pagination, 'model' => $model]);
 }