/trunk/backend/modules/user/controllers/DefaultController.php

https://gitlab.com/Sang240892/real-estate-system · PHP · 231 lines · 145 code · 29 blank · 57 comment · 18 complexity · 380c8c5f5f0fed1c90488ee508511512 MD5 · raw file

  1. <?php
  2. namespace backend\modules\user\controllers;
  3. use backend\commons\forms\SignUpForm;
  4. use backend\modules\auth\controllers\AuthenticateController;
  5. use backend\modules\payment\models\SearchPaymentHistoryModel;
  6. use common\models\entities\PaymentHistory;
  7. use common\models\UserIdentity;
  8. use Yii;
  9. use common\models\entities\RealEstateUser;
  10. use backend\modules\user\models\SearchRealEstateUser;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. /**
  15. * DefaultController implements the CRUD actions for RealEstateUser model.
  16. */
  17. class DefaultController extends AuthenticateController
  18. {
  19. /**
  20. * @inheritdoc
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['POST'],
  29. ],
  30. ],
  31. ];
  32. }
  33. /**
  34. * Lists all RealEstateUser models.
  35. * @return mixed
  36. */
  37. public function actionIndex()
  38. {
  39. $searchModel = new SearchRealEstateUser();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41. return $this->render('index', [
  42. 'searchModel' => $searchModel,
  43. 'dataProvider' => $dataProvider,
  44. ]);
  45. }
  46. /**
  47. * Displays a single RealEstateUser model.
  48. * @param integer $id
  49. * @return mixed
  50. */
  51. public function actionView($id)
  52. {
  53. return $this->render('view', [
  54. 'model' => $this->findModel($id),
  55. ]);
  56. }
  57. /**
  58. * Creates a new RealEstateUser model.
  59. * If creation is successful, the browser will be redirected to the 'view' page.
  60. * @return mixed
  61. */
  62. public function actionCreate()
  63. {
  64. $model = new SignUpForm(['scenario' => SignUpForm::SCENARIO_REGISTER]);
  65. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  66. $pw = Yii::$app->security->generatePasswordHash(Yii::$app->request->post()['SignUpForm']['password']);
  67. $model->password = $pw;
  68. $model->save(false);
  69. return $this->redirect(['view', 'id' => $model->id]);
  70. } else {
  71. return $this->render('create', [
  72. 'model' => $model,
  73. ]);
  74. }
  75. }
  76. /**
  77. * Updates an existing RealEstateUser model.
  78. * If update is successful, the browser will be redirected to the 'view' page.
  79. * @param integer $id
  80. * @return mixed
  81. */
  82. public function actionUpdate($id)
  83. {
  84. // $historySearchModel = new SearchPaymentHistoryModel();
  85. $model = new SignUpForm(['scenario' => SignUpForm::SCENARIO_UPDATE]);
  86. $model = $model->findOne(['id'=>$id]);
  87. $oldPw = $model->password;
  88. if(!$model){
  89. throw new NotFoundHttpException('The requested page does not exist.');
  90. }
  91. //$historyDataProvider = $historySearchModel->search(Yii::$app->request->queryParams,$id);
  92. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  93. if(isset(Yii::$app->request->post()['SignUpForm']['password']) && !empty(Yii::$app->request->post()['SignUpForm']['password'])){
  94. $pw = Yii::$app->security->generatePasswordHash(Yii::$app->request->post()['SignUpForm']['password']);
  95. $model->password = $pw;
  96. }else{
  97. $model->password = $oldPw;
  98. }
  99. $model->save(false);
  100. return $this->redirect(['view', 'id' => $model->id]);
  101. } else {
  102. $model->password = null;
  103. return $this->render('update', [
  104. 'model' => $model,
  105. // 'historySearchModel' => $historySearchModel,
  106. // 'historyDataProvider' => $historyDataProvider,
  107. ]);
  108. }
  109. }
  110. /**
  111. * Deletes an existing RealEstateUser model.
  112. * If deletion is successful, the browser will be redirected to the 'index' page.
  113. * @param integer $id
  114. * @return mixed
  115. */
  116. public function actionDelete($id)
  117. {
  118. $this->findModel($id)->delete();
  119. return $this->redirect(['index']);
  120. }
  121. /**
  122. * Deletes an existing PaymentHistory model.
  123. * If deletion is successful, the browser will be redirected to the 'index' page.
  124. * @param integer $id
  125. * @return mixed
  126. */
  127. public function actionDeleteHistory($user_id)
  128. {
  129. $h_id = Yii::$app->request->get('h_id');
  130. $user = $this->findModel($user_id);
  131. $this->findModelPaymentHistory($h_id)->delete();
  132. return $this->redirect(['create-history','user_id'=>$user_id]);
  133. }
  134. public function actionCreateHistory($user_id)
  135. {
  136. $model = new PaymentHistory();
  137. $h_id = Yii::$app->request->get('h_id');
  138. $user = $this->findModel($user_id);
  139. if($h_id != 0){
  140. $model = $this->findModelPaymentHistory($h_id);
  141. }else{
  142. $model->start_date = isset(Yii::$app->request->post()['PaymentHistory']['start_date'])
  143. ?Yii::$app->request->post()['PaymentHistory']['start_date']:null;
  144. $model->end_date = isset(Yii::$app->request->post()['PaymentHistory']['end_date'])
  145. ?Yii::$app->request->post()['PaymentHistory']['end_date']:null;
  146. }
  147. $model->user_id = $user->id;
  148. $searchModel = new SearchPaymentHistoryModel();
  149. $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$user_id);
  150. //die(var_dump(Yii::$app->request->post()['PaymentHistory']));
  151. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  152. return $this->redirect(['create-history', 'user_id' => $user->id]);
  153. } else {
  154. return $this->render('create-history', [
  155. 'model' => $model,
  156. 'user'=>$user,
  157. 'searchModel' => $searchModel,
  158. 'dataProvider' => $dataProvider,
  159. ]);
  160. }
  161. }
  162. /**
  163. * Finds the PaymentHistory model based on its primary key value.
  164. * If the model is not found, a 404 HTTP exception will be thrown.
  165. * @param integer $id
  166. * @return PaymentHistory the loaded model
  167. * @throws NotFoundHttpException if the model cannot be found
  168. */
  169. protected function findModelPaymentHistory($id)
  170. {
  171. if (($model = PaymentHistory::findOne($id)) !== null) {
  172. return $model;
  173. } else {
  174. throw new NotFoundHttpException('The requested page does not exist.');
  175. }
  176. }
  177. public function actionPaymentHistory($user_id){
  178. $user = $this->findModel($user_id);
  179. $searchModel = new SearchPaymentHistoryModel();
  180. $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$user_id);
  181. return $this->render('payment-history', [
  182. 'searchModel' => $searchModel,
  183. 'dataProvider' => $dataProvider,
  184. 'user'=>$user
  185. ]);
  186. }
  187. /**
  188. * Finds the RealEstateUser model based on its primary key value.
  189. * If the model is not found, a 404 HTTP exception will be thrown.
  190. * @param integer $id
  191. * @return RealEstateUser the loaded model
  192. * @throws NotFoundHttpException if the model cannot be found
  193. */
  194. protected function findModel($id)
  195. {
  196. if (($model = RealEstateUser::findOne($id)) !== null) {
  197. return $model;
  198. } else {
  199. throw new NotFoundHttpException('The requested page does not exist.');
  200. }
  201. }
  202. }