OrganizationsController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Banks;
  4. use Yii;
  5. use app\models\Organizations;
  6. use app\models\OrganizationsSearch;
  7. use yii\helpers\ArrayHelper;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. /**
  12. * OrganizationsController implements the CRUD actions for Organizations model.
  13. */
  14. class OrganizationsController extends Controller
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function behaviors()
  20. {
  21. return [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. ];
  29. }
  30. /**
  31. * Lists all Organizations models.
  32. * @return mixed
  33. */
  34. public function actionIndex()
  35. {
  36. $searchModel = new OrganizationsSearch();
  37. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  38. return $this->render('index', [
  39. 'searchModel' => $searchModel,
  40. 'dataProvider' => $dataProvider->getModels(),
  41. ]);
  42. }
  43. /**
  44. * Displays a single Organizations model.
  45. * @param integer $id
  46. * @return mixed
  47. * @throws NotFoundHttpException if the model cannot be found
  48. */
  49. public function actionView($id)
  50. {
  51. $org = $this->findModel($id);
  52. $persons = $org->persons;
  53. $bank = Banks::findOne($org->bank_id);
  54. return $this->render('view', [
  55. 'model' => $org,
  56. 'persons' => $persons,
  57. 'bank' => $bank,
  58. ]);
  59. }
  60. /**
  61. * Creates a new Organizations model.
  62. * If creation is successful, the browser will be redirected to the 'view' page.
  63. * @return mixed
  64. */
  65. public function actionCreate()
  66. {
  67. $model = new Organizations();
  68. if (Yii::$app->request->isAjax) {
  69. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  70. return $this->asJson(['success' => true, 'id' => $model->getPrimaryKey()]);
  71. }
  72. $result = [];
  73. // The code below comes from ActiveForm::validate(). We do not need to validate the model
  74. // again, as it was already validated by save(). Just collect the messages.
  75. foreach ($model->getErrors() as $attribute => $errors) {
  76. $result[yii\helpers\Html::getInputId($model, $attribute)] = $errors;
  77. }
  78. return $this->asJson(['validation' => $result]);
  79. }
  80. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  81. return $this->redirect(['view', 'id' => $model->id]);
  82. }
  83. return $this->render('create', [
  84. 'model' => $model,
  85. ]);
  86. }
  87. /**
  88. * Updates an existing Organizations model.
  89. * If update is successful, the browser will be redirected to the 'view' page.
  90. * @param integer $id
  91. * @return mixed
  92. * @throws NotFoundHttpException if the model cannot be found
  93. */
  94. public function actionUpdate($id)
  95. {
  96. $model = $this->findModel($id);
  97. $banks = Banks::find()->asArray()->all();
  98. //$banks2 = Banks::find()->all();
  99. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  100. return $this->redirect(['view', 'id' => $model->id]);
  101. }
  102. $items = [];
  103. foreach ($banks as $bank){
  104. $mfo = isset($bank['mfo']) ? $bank['mfo'] . '<br>' : '';
  105. $name = isset($bank['name']) ? '<a>' . $bank['name'] .'</a>' : '';
  106. $address = isset($bank['address']) ? '<br>' . $bank['address'] : '';
  107. $items[$bank['id']] = $mfo . $name . $address;
  108. }
  109. return $this->render('update', [
  110. 'model' => $model,
  111. 'banks' => $items
  112. ]);
  113. }
  114. /**
  115. * Deletes an existing Organizations model.
  116. * If deletion is successful, the browser will be redirected to the 'index' page.
  117. * @param integer $id
  118. * @return mixed
  119. * @throws NotFoundHttpException if the model cannot be found
  120. */
  121. public function actionDelete($id)
  122. {
  123. $this->findModel($id)->delete();
  124. return $this->redirect(['index']);
  125. }
  126. /**
  127. * Finds the Organizations model based on its primary key value.
  128. * If the model is not found, a 404 HTTP exception will be thrown.
  129. * @param integer $id
  130. * @return Organizations the loaded model
  131. * @throws NotFoundHttpException if the model cannot be found
  132. */
  133. protected function findModel($id)
  134. {
  135. if (($model = Organizations::findOne($id)) !== null) {
  136. return $model;
  137. }
  138. throw new NotFoundHttpException('The requested page does not exist.');
  139. }
  140. }