/**
  * Creates a new FileGroup model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     if (!Yii::$app->user->can("admin")) {
         throw new HttpException(403, 'You are not allowed to perform this action.');
     }
     $model = new FileGroup();
     $model->is_deleted = 0;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function actionVerifyapprove($request_id, $file_id, $user_id, $group)
 {
     if (!Yii::$app->user->can("admin")) {
         throw new HttpException(403, 'You are not allowed to perform this action.');
     }
     $response = ['status' => 'fail'];
     // If we get a numeric group, then we are using an existing group
     if (is_numeric($group)) {
         // First check to see if a record exists because the
         // stupid front end is not very good at figuring this out
         $ufg = UserFileGroup::find()->where(['user_id' => $user_id])->andWhere(['file_group_id' => $group])->one();
         //var_dump($ufg);
         //exit;
         // If nothing is found, create the new entry
         if ($ufg === NULL) {
             // Now assign the user to this filegroup
             $ufg = new UserFileGroup();
             $ufg->user_id = $user_id;
             $ufg->file_group_id = $group;
             if ($ufg->save()) {
                 $rqf = RequestFile::find()->where(['request_id' => $request_id, 'file_id' => $file_id])->one();
                 $rqf->granted = 1;
                 if ($rqf->save()) {
                     $this->commitRequest($request_id);
                     $response = ['status' => 'success', 'message' => ''];
                 } else {
                     $response = ['status' => 'fail', 'message' => 'Error saving request file'];
                 }
             } else {
                 $response = ['status' => 'fail', 'message' => 'Error saving request file'];
             }
         } else {
             $response = ['status' => 'fail', 'message' => 'Record already exists'];
             $rqf = RequestFile::find()->where(['request_id' => $request_id, 'file_id' => $file_id])->one();
             $rqf->granted = 1;
             if ($rqf->save()) {
                 $this->commitRequest($request_id);
                 $response = ['status' => 'success', 'message' => ''];
             }
         }
     } else {
         // Need to create new the group and assign the file id
         $fg = new FileGroup();
         $fg->group_name = $group;
         $fg->description = "Auto Generated through Permission Tool, please provide a more descriptive group name if you want.";
         $fg->status = 'active';
         $fg->is_deleted = 0;
         if ($fg->save()) {
             // Associate the file group with this file
             $ffg = new FileFileGroup();
             $ffg->file_id = $file_id;
             $ffg->group_id = $fg->id;
             if ($ffg->save()) {
                 // Now assign the user to this filegroup
                 $ufg = new UserFileGroup();
                 $ufg->user_id = $user_id;
                 $ufg->file_group_id = $fg->id;
                 if ($ufg->save()) {
                     $rqf = RequestFile::find()->where(['request_id' => $request_id, 'file_id' => $file_id])->one();
                     $rqf->granted = 1;
                     if ($rqf->save()) {
                         $response = ['status' => 'success'];
                         $this->commitRequest($request_id);
                     } else {
                         $response = ['status' => 'fail'];
                     }
                 } else {
                     $response = ['status' => 'fail'];
                 }
             } else {
                 $response = ['status' => 'fail'];
             }
         } else {
             $response = ['status' => 'fail'];
         }
     }
     \Yii::$app->response->format = 'json';
     return $response;
 }