Example #1
0
 /**
  * Creates a new Main model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Main();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Example #2
0
 /**
  * 新增问题.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Main();
     $post = Yii::$app->request->post();
     if (isset($post['Main']['tags']) && isset($post['Main']['title']) && isset($post['Main']['content'])) {
         $tagReplace = Tag::dealTag($post['Main']['tags'], false);
         Tag::addTag(explode(',', $tagReplace));
         $model->load($post);
         $model->userId = Yii::$app->user->identity->getId();
         $model->createTime = date('Y-m-d H:i:s', time());
         $model->status = 3 == $post['Main']['type'] ? $post['Main']['status'] : 1;
         $model->scanNum = 0;
         $model->cellectNum = 0;
         $model->attentionNum = 0;
         $model->tags = $tagReplace;
         if ($model->save()) {
             if (1 == $post['Main']['type']) {
                 return $this->redirect(['index']);
             } elseif (2 == $post['Main']['type']) {
                 return $this->redirect(['article']);
             } else {
                 return $this->redirect(['note']);
             }
         }
     } else {
         $get = Yii::$app->request->get();
         $type = 1;
         if (isset($get['type'])) {
             if (2 == $get['type']) {
                 $title = '发表文章';
                 $type = 2;
             } else {
                 $title = '发表笔记';
                 $type = 3;
             }
         } else {
             $title = '发表问题';
         }
         return $this->render('create', ['model' => $model, 'title' => $title, 'type' => $type]);
     }
 }
 public function actionMain()
 {
     $params = Yii::$app->request->queryParams;
     $region = 1;
     if (!empty($params['MainSearch']['region'])) {
         $region = $params['MainSearch']['region'];
     }
     if (!empty($params['id'])) {
         $model = Main::find()->where(['id' => $params['id']])->one();
         $arrayParams = ['MainSearch' => ['region' => $model->region]];
     } else {
         $model = new Main();
         $model->region = $region;
         $arrayParams = ['MainSearch' => ['region' => $region]];
     }
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             if (!$model->isNewRecord) {
                 MainTrashPlace::deleteAll(['main_id' => $model->id]);
                 MainTrashMan::deleteAll(['main_id' => $model->id]);
                 MainTrashRelation::deleteAll(['main_id' => $model->id]);
                 MainTrashRecycle::deleteAll(['main_id' => $model->id]);
                 MainRubberItems::deleteAll(['main_id' => $model->id]);
             }
             $model->save();
             $main = Yii::$app->request->post()['Main'];
             if (!empty($main['places'])) {
                 foreach ($main['places'] as $value) {
                     $relationModel = new MainTrashPlace();
                     $relationModel->main_id = $model->id;
                     $relationModel->trash_place_id = $value;
                     $relationModel->save();
                 }
             }
             if (!empty($main['men'])) {
                 foreach ($main['men'] as $value) {
                     $relationModel = new MainTrashMan();
                     $relationModel->main_id = $model->id;
                     $relationModel->trash_man_id = $value;
                     $relationModel->save();
                 }
             }
             if (!empty($main['relations'])) {
                 foreach ($main['relations'] as $value) {
                     $relationModel = new MainTrashRelation();
                     $relationModel->main_id = $model->id;
                     $relationModel->trash_relation_id = $value;
                     $relationModel->save();
                 }
             }
             if (!empty($main['recycles'])) {
                 foreach ($main['recycles'] as $value) {
                     $relationModel = new MainTrashRecycle();
                     $relationModel->main_id = $model->id;
                     $relationModel->trash_recycle_id = $value;
                     $relationModel->save();
                 }
             }
             if (!empty($main['rubberItemsData'])) {
                 foreach ($main['rubberItemsData'] as $value) {
                     $relationModel = new MainRubberItems();
                     $relationModel->main_id = $model->id;
                     $relationModel->rubber_item_id = $value;
                     $relationModel->save();
                 }
             }
             $arrayParams['id'] = $model->id;
             $params = array_merge(["site/index"], $arrayParams);
             $url = Yii::$app->urlManager->createUrl($params);
             return $this->redirect($url);
         }
     }
     $model->places = ArrayHelper::getColumn($model->mainTrashPlaces, 'trash_place_id');
     $model->men = ArrayHelper::getColumn($model->mainTrashMen, 'trash_man_id');
     $model->relations = ArrayHelper::getColumn($model->mainTrashRelations, 'trash_relation_id');
     $model->recycles = ArrayHelper::getColumn($model->mainTrashRecycles, 'trash_recycle_id');
     $model->rubberItemsData = ArrayHelper::getColumn($model->mainRubberItems, 'rubber_item_id');
     return $this->render('main', ['model' => $model, 'region' => $region]);
 }