InquirerController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace manager\controllers;
  3. use app\models\Inquirer;
  4. use app\models\InquirerAnswer;
  5. use Yii;
  6. class InquirerController extends \yii\web\Controller
  7. {
  8. public function actionIndex()
  9. {
  10. return $this->render('index');
  11. }
  12. public function actionUpdate($id)
  13. {
  14. $model = $this->findModel($id);
  15. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  16. if( $model->load($post, 'Inquirer') && $model->save() ){
  17. if( isset($post['Inquirer']['text_answer']) ){
  18. $answer_do = InquirerAnswer::findAll(['idq'=>$id]);
  19. $a = array();
  20. foreach( $answer_do as $item ){
  21. $a[$item->id] = $item->id;
  22. }
  23. foreach( $post['Inquirer']['text_answer'] as $key => $item ){
  24. if( isset( $a[$key] ) ){ unset($a[$key]); }
  25. $answer = InquirerAnswer::findOne(['id'=>$key]);
  26. if( $answer ){
  27. $answer->text = $item;
  28. $answer->idq = $id;
  29. $answer->save();
  30. }
  31. }
  32. foreach( $a as $item ){
  33. $answer = InquirerAnswer::findOne(['id'=>$item]);
  34. $answer->delete();
  35. }
  36. }
  37. if( isset($post['Inquirer']['text_new']) ){
  38. foreach( $post['Inquirer']['text_new'] as $item ){
  39. $answer = new InquirerAnswer();
  40. $answer->text = $item;
  41. $answer->idq = $id;
  42. $answer->save();
  43. }
  44. }
  45. }
  46. }else{
  47. return $this->render('Update', ['model'=>$model]);
  48. }
  49. return $this->redirect('index');
  50. }
  51. public function actionCreate()
  52. {
  53. $model = new Inquirer();
  54. if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){
  55. if( $model->load($post, 'Inquirer') && $model->save() ){
  56. if( isset($post['Inquirer']['text_new']) ){
  57. foreach( $post['Inquirer']['text_new'] as $item ){
  58. $answer = new InquirerAnswer();
  59. $answer->text = $item;
  60. $answer->idq = $model->id;
  61. $answer->save();
  62. }
  63. }
  64. }
  65. }else{
  66. return $this->render('Create', ['model'=>$model]);
  67. }
  68. return $this->redirect('index');
  69. }
  70. public function actionShow($id)
  71. {
  72. $model = $this->findModel($id);
  73. if($model) $model->Show($id);
  74. return $this->redirect('index');
  75. }
  76. public function actionActive($id)
  77. {
  78. $model = $this->findModel($id);
  79. if($model) $model->Active($id);
  80. return $this->redirect('index');
  81. }
  82. protected function findModel($id)
  83. {
  84. if (($model = Inquirer::findOne($id)) !== null) {
  85. return $model;
  86. }
  87. throw new NotFoundHttpException('Ой! рубрика не найдена.');
  88. return false;
  89. }
  90. }