/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Scoutrelation::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, 'parentid' => $this->parentid, 'scoutid' => $this->scoutid, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); return $dataProvider; }
/** * Creates a new Scout model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Scout(); if ($model->load(Yii::$app->request->post())) { if (!Yii::$app->user->isGuest && !Yii::$app->user->identity->getIsScout() && !Yii::$app->user->identity->getIsParent()) { $auth = Yii::$app->authManager; $authorRole = $auth->getRole('scout'); $auth->assign($authorRole, Yii::$app->user->getId()); } $model->setAttribute('userid', Yii::$app->user->getId()); if ($model->save()) { if (Yii::$app->user->identity->getIsParent()) { $scoutrelation = new Scoutrelation(); $scoutrelation->scoutid = $model->id; $thisuser = Yii::$app->user->getId(); $parent = Scoutparent::findOne(['userid' => $thisuser]); $scoutrelation->parentid = $parent->id; $scoutrelation->save(); } $post = Yii::$app->request->post(); if ($post['where'] == 'fromparent') { return $this->redirect(['/scoutrelation/create']); } else { return $this->redirect(['/site/index']); } } else { return $this->render('create', ['model' => $model]); } } else { $context = Yii::$app->request->get(); if (in_array('where', $context) && $context['where'] == 'fromparent') { return $this->render('create', ['model' => $model, 'where' => 'fromparent']); } else { return $this->render('create', ['model' => $model, 'where' => '']); } } }
/** * @return \yii\db\ActiveQuery */ public function getScoutrelations() { return $this->hasMany(Scoutrelation::className(), ['scoutid' => 'id']); }
/** * Updates an existing Order model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model1 = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $model->order_amount = number_format($model->number_bales * 4.5, 2, '.', ''); } if ($model1->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['/order/index', 'id' => $model->id]); } else { if (Yii::$app->user->identity->getIsScout()) { $thisuser = Yii::$app->user->getId(); $scout = Scout::findOne(['userid' => $thisuser]); $scoutlist = ArrayHelper::map(Scout::findAll(['id' => $scout->id]), 'id', 'name'); } elseif (Yii::$app->user->identity->getIsLeader()) { $scoutlist = ArrayHelper::map(Scout::find()->all(), 'id', 'name'); } elseif (Yii::$app->user->identity->getIsParent()) { $thisuser = Yii::$app->user->getId(); $thisparent = Scoutparent::findOne(['userid' => $thisuser]); $myscouts = Scoutrelation::find()->where(['parentid' => $thisparent->id])->all(); $scoutlist = ArrayHelper::map(Scout::find()->where(['id' => ArrayHelper::getColumn($myscouts, 'scoutid')])->all(), 'id', 'name'); } return $this->render('update', ['model' => $model, 'scoutlist' => $scoutlist]); } }
/** * Finds the Scoutrelation model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Scoutrelation the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Scoutrelation::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }