Exemple #1
0
 /**
  * Displays a single Items model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $commentsData = comment::getComments($id);
     $model = $this->findModel($id);
     $newComment = new Comment();
     $addToBasket = new basket();
     $description = new Description();
     $description->find()->where(['item_id' => $id])->all();
     $itemRating = ItemsRating::findOne($id);
     if ($model->count == 0) {
         Yii::$app->db->createCommand(" UPDATE `items` SET items.is_aviable=0 WHERE items.id =" . $id)->execute();
     } else {
         if (!Yii::$app->user->isGuest) {
             $uid = Yii::$app->user->identity->getId();
             $get_user = \dektrium\user\models\User::getUser($uid)->getModels()[0]['username'];
             if (isset($_POST['Comment'])) {
                 //            print_r($_POST['Comment']['text']);
                 $newComment = new Comment();
                 $newComment->author = $get_user;
                 $newComment->item_id = $id;
                 $newComment->user_id = $uid;
                 $newComment->date = date("Y-m-d h:i:s");
                 $newComment->text = $_POST['Comment']['text'];
                 $newComment->save();
             }
             if (isset($_POST['Basket'])) {
                 if ($model->is_aviable) {
                     if ($_POST['Basket']['count'] <= $model->count) {
                         $addToBasket = new Basket();
                         $have = $addToBasket->find()->where(['item_id' => $id, 'user_id' => $uid])->all();
                         if (!$have) {
                             $addToBasket->item_id = $id;
                             $addToBasket->user_id = $uid;
                             $addToBasket->name = $model->name;
                             $addToBasket->price = $model->price;
                             $addToBasket->count = $_POST['Basket']['count'];
                             $addToBasket->sum = $model->price * $_POST['Basket']['count'];
                             $addToBasket->save();
                         }
                     }
                 }
                 return $this->redirect(['view', 'id' => $id]);
             }
         }
     }
     return $this->render('view', ['model' => $model, 'commentsData' => $commentsData, 'newComment' => $newComment, 'itemRating' => $itemRating, 'addToBasket' => $addToBasket, 'description' => $description]);
 }
 public function actionCreate()
 {
     $id = Yii::$app->user->identity->getId();
     $get_user = \dektrium\user\models\User::getUser($id);
     $model = new Post();
     $tag = new Tag();
     $model->date = date("Y-m-d h:i:s");
     $model->author = $get_user->getModels()[0]['username'];
     $model->user_id = $id;
     $model->rating_minus = 0;
     $model->rating_plus = 0;
     $tags = array();
     $tmp = array();
     $upload = new UploadForm();
     if (Yii::$app->request->isPost) {
     }
     if (isset($_POST['Upload'])) {
         print_r($_POST['Post']);
     }
     //            $model->image_url="s";
     if (isset($_POST['Tag'])) {
         $pieces = explode(",", Yii::$app->request->post()['Tag']['name']);
         $dataProvider = Tag::getAllTags();
         //            //print_r(Yii::$app->request->post() );
         $count = $dataProvider->getModels();
         for ($i = 0; $i < count($count); $i++) {
             for ($j = 0; $j < count($pieces); $j++) {
                 if ($dataProvider->getModels()[$i]['name'] == $pieces[$j]) {
                     array_push($tmp, $pieces[$j]);
                     //                        //print_r($tmp);
                     $pieces[$j] = "";
                     //                        echo $pieces[$j];
                 }
             }
         }
         $ids = "'" . implode("','", $tmp) . "'";
         //            //print_r($tmp);
         //            //print_r($ids);
         $dataProvider2 = Tag::getTagsIn($ids);
         //print_r($dataProvider2->getModels());
         for ($i = 0; $i < count($pieces); $i++) {
             $tag = new Tag();
             $tag->name = $pieces[$i];
             $tag->save();
             array_push($tags, $tag->tag_id);
         }
         for ($i = 0; $i < count($dataProvider2->getModels()); $i++) {
             array_push($tags, $dataProvider2->getModels()[$i]['tag_id']);
         }
     }
     if ($model->load(Yii::$app->request->post())) {
         //            print_r();
         if ($_POST['UploadForm']) {
             //                $random = '';
             //            for ($i = 0; $i < 30; $i++) {
             //                $random .= chr(mt_rand(33, 126));
             //            }
             $upload->imageFile = UploadedFile::getInstance($upload, 'imageFile');
             if ($upload->upload()) {
                 $model->image_url = 'uploads/' . $upload->imageFile->name;
                 $model->save();
                 //                    return;
             }
         }
         $model->save();
         for ($i = 0; $i < count($tags); $i++) {
             $postTags = new Post_tags();
             $postTags->tag_id = $tags[$i];
             $postTags->post_id = $model->post_id;
             $postTags->save();
         }
         //            $upload->file = UploadedFile::getInstance($model ,'image_url');
         //
         ////            echo $upload->file=$model->formName();
         //            $upload->file->saveAs();
         //            if ($model->validate()) {
         //                $upload->file->saveAs('uploads' );
         //            }
         //            $file->saveAs('@app/uploads');
         return $this->redirect(['/post/view', 'id' => $model->post_id]);
     } else {
         return $this->render('create', ['model' => $model, 'tag' => $tag, 'upload' => $upload]);
     }
 }