/** * Creates a new Route model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $rootPath = Yii::$app->params['route_img_dir']; $model = new Route(); $status = Lookup::items("RouteStatus"); if ($model->load(Yii::$app->request->post())) { $image = UploadedFile::getInstance($model, 'img'); $ext = $image->getExtension(); $randName = time() . rand(1000, 9999) . "." . $ext; $path = abs(crc32($randName) % 500); $rootPath = $rootPath . "img/"; if (!file_exists($rootPath)) { mkdir($rootPath, 0777, true); } $image->saveAs($rootPath . $randName); $model->img = $rootPath . $randName; if ($model->save()) { $is_joined = Join::find()->andWhere(['user_id' => $model->author_id, 'route_id' => $model->id])->all() == null; if ($is_joined) { $join = new Join(); $join->user_id = $model->author_id; $join->route_id = $model->id; $join->save(); } return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('create', ['model' => $model, 'status' => $status]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Join::find(); $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, 'user_id' => $this->user_id, 'route_id' => $this->route_id]); return $dataProvider; }
/** * Deletes an existing Route model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionJoinus() { $model = new Join(); $result['msg'] = ""; $in = Yii::$app->request->post(); //print_r($in); $model->user_id = $in["user_id"]; $model->route_id = $in["route_id"]; if (Yii::$app->request->post()) { $is_joined = Join::find()->andWhere(['user_id' => $model->user_id, 'route_id' => $model->route_id])->all() !== null; if ($is_joined) { $result['msg'] = "You have successfully joined, please do not repeat to join!"; } else { if ($model->save()) { $result['msg'] = "successfully joined!"; } } } exit(json_encode($result)); }