/**
  * @todo: remove creates from here
  * @todo: make it readable, use Vacations/Dates
  */
 public function actionCreate()
 {
     $model = new Posts();
     if ($model->load(Yii::$app->request->post())) {
         $model->UserID = \Yii::$app->user->getId();
         $model->PostType = Posts::TYPE_VACATIONS;
         if ($model->save()) {
             // todo: check the data!
             $hd = explode(",", Yii::$app->request->post()['holidaydates']);
             if (count($hd) == 2) {
                 PostDates::deleteAll(['PostID' => $model->PostID]);
                 $begin = new \DateTime($hd[0]);
                 $end = new \DateTime($hd[1]);
                 $end = $end->modify('+1 day');
                 $interval = new \DateInterval('P1D');
                 $daterange = new \DatePeriod($begin, $interval, $end);
                 foreach ($daterange as $date) {
                     $d = new PostDates();
                     $d->PostID = $model->PostID;
                     $d->PostDate = $date->format("Y-m-d");
                     $d->PostDateType = 0;
                     $d->save();
                 }
             }
             return $this->redirect(['vacations/index', 'username' => Yii::$app->user->identity->username]);
         }
     } else {
         // todo: ???
         return $this->render('create', ['model' => $model, 'postdates' => []]);
     }
 }
 public function actionAdd($ingredientId)
 {
     // less queries here
     $postType = Posts::TYPE_USER_INGREDIENTS;
     $post = Posts::find()->where(['and', ['PostType' => $postType], ['UserID' => Yii::$app->user->getId()]])->one();
     if (!$post) {
         $post = new Posts();
         $post->PostType = $postType;
         $post->UserID = Yii::$app->user->getId();
         $post->save();
     }
     $ingredient = Tags::findOne($ingredientId);
     $post->link('tags', $ingredient);
 }
 public function actionCreate()
 {
     $model = new Posts();
     if ($model->load(Yii::$app->request->post())) {
         list($model, $imgs, $urls) = Wishlist::prepareForSave($model);
         $model->UserID = \Yii::$app->user->getId();
         $str = iconv('UTF-8', 'windows-1251//TRANSLIT', $model->PostHeader);
         $str = iconv('windows-1251', 'UTF-8', $str);
         $model->PostURL = \frontend\models\PostWish::translit_url($str);
         $model->PostType = Posts::TYPE_WISH;
         if ($model->save()) {
             Wishlist::insertUrlsAndImgs($model->PostID, $imgs, $urls);
             return $this->redirect(['wishlist/index', 'username' => Yii::$app->user->identity->username]);
         }
     } else {
         return $this->render('create', ['model' => $model, 'posturls' => [], 'postimgs' => []]);
     }
 }
 public function getUserIngredients()
 {
     $post = Posts::find()->where(['and', ['PostType' => Posts::TYPE_USER_INGREDIENTS], ['UserID' => Yii::$app->user->getId()]])->one();
     if ($post) {
         $tags = $post->tags;
     } else {
         $tags = [];
     }
     $userTags = [];
     foreach ($tags as $tag) {
         $userTags[$tag->TagID] = $tag->Name;
     }
     return $userTags;
 }
 /**
  * Delete Post with dates.
  * 
  * @todo: use beforeDelete for global delete all Post relations
  */
 public function deletePastDates($postId)
 {
     PostDates::deleteAll(['PostID' => $postId]);
     Posts::deleteAll(['PostID' => $postId]);
 }
 /**
  * Finds the Posts model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Posts the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Posts::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPost()
 {
     return $this->hasOne(Posts::className(), ['PostID' => 'PostID']);
 }
 public static function archiveWish($postid)
 {
     $wish = Posts::findOne($postid);
     if ($wish) {
         $wish->PostType = Posts::TYPE_WISH_ARCHIVE;
         $wish->save();
     }
 }
 public function actionRead()
 {
     $read = Posts::find()->where(['id' => 1])->one();
     return $this->render('read', ['read' => $read]);
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPosts()
 {
     return $this->hasMany(Posts::className(), ['PostID' => 'PostID'])->viaTable('PostTags', ['TagID' => 'TagID']);
 }