Skip to content

Commit 2a3a169

Browse files
committed
preview next step
1 parent f2ac032 commit 2a3a169

File tree

9 files changed

+307
-57
lines changed

9 files changed

+307
-57
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace backend\controllers;
4+
5+
use Yii;
6+
use common\models\Interview;
7+
use yii\data\ActiveDataProvider;
8+
use yii\web\Controller;
9+
use yii\web\NotFoundHttpException;
10+
use yii\filters\VerbFilter;
11+
12+
/**
13+
* InterviewController implements the CRUD actions for Interview model.
14+
*/
15+
class InterviewController extends Controller
16+
{
17+
public function behaviors()
18+
{
19+
return [
20+
'verbs' => [
21+
'class' => VerbFilter::className(),
22+
'actions' => [
23+
'delete' => ['post'],
24+
],
25+
],
26+
];
27+
}
28+
29+
/**
30+
* Lists all Interview models.
31+
* @return mixed
32+
*/
33+
public function actionIndex()
34+
{
35+
$dataProvider = new ActiveDataProvider([
36+
'query' => Interview::find(),
37+
]);
38+
39+
return $this->render('index', [
40+
'dataProvider' => $dataProvider,
41+
]);
42+
}
43+
44+
/**
45+
* Displays a single Interview model.
46+
* @param integer $id
47+
* @return mixed
48+
*/
49+
public function actionView($id)
50+
{
51+
return $this->render('view', [
52+
'model' => $this->findModel($id),
53+
]);
54+
}
55+
56+
/**
57+
* Creates a new Interview model.
58+
* If creation is successful, the browser will be redirected to the 'view' page.
59+
* @return mixed
60+
*/
61+
public function actionCreate()
62+
{
63+
$model = new Interview();
64+
65+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
66+
return $this->redirect(['view', 'id' => $model->id]);
67+
} else {
68+
return $this->render('create', [
69+
'model' => $model,
70+
]);
71+
}
72+
}
73+
74+
/**
75+
* Updates an existing Interview model.
76+
* If update is successful, the browser will be redirected to the 'view' page.
77+
* @param integer $id
78+
* @return mixed
79+
*/
80+
public function actionUpdate($id)
81+
{
82+
$model = $this->findModel($id);
83+
84+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
85+
return $this->redirect(['view', 'id' => $model->id]);
86+
} else {
87+
return $this->render('update', [
88+
'model' => $model,
89+
]);
90+
}
91+
}
92+
93+
/**
94+
* Deletes an existing Interview model.
95+
* If deletion is successful, the browser will be redirected to the 'index' page.
96+
* @param integer $id
97+
* @return mixed
98+
*/
99+
public function actionDelete($id)
100+
{
101+
$this->findModel($id)->delete();
102+
103+
return $this->redirect(['index']);
104+
}
105+
106+
/**
107+
* Finds the Interview model based on its primary key value.
108+
* If the model is not found, a 404 HTTP exception will be thrown.
109+
* @param integer $id
110+
* @return Interview the loaded model
111+
* @throws NotFoundHttpException if the model cannot be found
112+
*/
113+
protected function findModel($id)
114+
{
115+
if (($model = Interview::findOne($id)) !== null) {
116+
return $model;
117+
} else {
118+
throw new NotFoundHttpException('The requested page does not exist.');
119+
}
120+
}
121+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\widgets\ActiveForm;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model common\models\Interview */
8+
/* @var $form yii\widgets\ActiveForm */
9+
?>
10+
11+
<div class="interview-form">
12+
13+
<?php $form = ActiveForm::begin(); ?>
14+
15+
<?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>
16+
17+
<?= $form->field($model, 'sex')->checkbox() ?>
18+
19+
<?= $form->field($model, 'planets')->textInput(['maxlength' => 255]) ?>
20+
21+
<?= $form->field($model, 'astronauts')->textInput(['maxlength' => 255]) ?>
22+
23+
<?= $form->field($model, 'planet')->textInput() ?>
24+
25+
<div class="form-group">
26+
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
27+
</div>
28+
29+
<?php ActiveForm::end(); ?>
30+
31+
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model common\models\Interview */
8+
9+
$this->title = 'Create Interview';
10+
$this->params['breadcrumbs'][] = ['label' => 'Interviews', 'url' => ['index']];
11+
$this->params['breadcrumbs'][] = $this->title;
12+
?>
13+
<div class="interview-create">
14+
15+
<h1><?= Html::encode($this->title) ?></h1>
16+
17+
<?= $this->render('_form', [
18+
'model' => $model,
19+
]) ?>
20+
21+
</div>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\grid\GridView;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $dataProvider yii\data\ActiveDataProvider */
8+
9+
$this->title = 'Опрос';
10+
$this->params['breadcrumbs'][] = $this->title;
11+
$planets = ['Меркурий', 'Венера', 'Земля', 'Марс', 'Юпитер', 'Сатурн', 'Уран', 'Нептун'];
12+
$astronauts = [
13+
'Юрий Гагарин',
14+
'Алексей Леонов',
15+
'Нил Армстронг',
16+
'Валентина Терешкова',
17+
'Эдвин Олдрин',
18+
'Анатолий Соловьев'
19+
];
20+
?>
21+
<div class="interview-index">
22+
23+
<h1><?= Html::encode($this->title) ?></h1>
24+
25+
<?= GridView::widget([
26+
'dataProvider' => $dataProvider,
27+
'columns' => [
28+
['class' => 'yii\grid\SerialColumn'],
29+
'name',
30+
[
31+
'attribute' => 'sex',
32+
'value' => function ($model) {
33+
return $model->sex ? 'Мужчина' : 'Женщина';
34+
}
35+
],
36+
[
37+
'attribute' => 'planets',
38+
'value' => function ($model) use ($planets) {
39+
$result = null;
40+
$numbers = explode(',', $model->planets);
41+
foreach ($numbers as $number) {
42+
$result .= $planets[$number] . ' ';
43+
}
44+
return $result;
45+
}
46+
],
47+
[
48+
'attribute' => 'astronauts',
49+
'value' => function ($model) use ($astronauts) {
50+
$result = null;
51+
$numbers = explode(',', $model->astronauts);
52+
foreach ($numbers as $number) {
53+
$result .= $astronauts[$number] . ' ';
54+
}
55+
return $result;
56+
}
57+
],
58+
[
59+
'attribute' => 'planet',
60+
'value' => function ($model) use ($planets) {
61+
return $planets[$model->planet];
62+
}
63+
],
64+
65+
['class' => 'yii\grid\ActionColumn'],
66+
],
67+
]); ?>
68+
69+
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
5+
/* @var $this yii\web\View */
6+
/* @var $model common\models\Interview */
7+
8+
$this->title = 'Update Interview: ' . ' ' . $model->name;
9+
$this->params['breadcrumbs'][] = ['label' => 'Interviews', 'url' => ['index']];
10+
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
11+
$this->params['breadcrumbs'][] = 'Update';
12+
?>
13+
<div class="interview-update">
14+
15+
<h1><?= Html::encode($this->title) ?></h1>
16+
17+
<?= $this->render('_form', [
18+
'model' => $model,
19+
]) ?>
20+
21+
</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\widgets\DetailView;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model common\models\Interview */
8+
9+
$this->title = $model->name;
10+
$this->params['breadcrumbs'][] = ['label' => 'Interviews', 'url' => ['index']];
11+
$this->params['breadcrumbs'][] = $this->title;
12+
?>
13+
<div class="interview-view">
14+
15+
<h1><?= Html::encode($this->title) ?></h1>
16+
17+
<p>
18+
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
19+
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
20+
'class' => 'btn btn-danger',
21+
'data' => [
22+
'confirm' => 'Are you sure you want to delete this item?',
23+
'method' => 'post',
24+
],
25+
]) ?>
26+
</p>
27+
28+
<?= DetailView::widget([
29+
'model' => $model,
30+
'attributes' => [
31+
'id',
32+
'name',
33+
'sex:boolean',
34+
'planets',
35+
'astronauts',
36+
'planet',
37+
],
38+
]) ?>
39+
40+
</div>
Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace frontend\models; // измените на common\models
3+
namespace common\models;
44

55
use Yii;
66

@@ -16,8 +16,6 @@
1616
*/
1717
class Interview extends \yii\db\ActiveRecord
1818
{
19-
public $verifyCode; // можно удалить, т.к. необходимо только для frontend части
20-
2119
/**
2220
* @inheritdoc
2321
*/
@@ -26,33 +24,6 @@ public static function tableName()
2624
return 'interview';
2725
}
2826

29-
/**
30-
* @inheritdoc
31-
*/
32-
public function rules() // метод удалить, т.к. необходим только для frontend части
33-
{
34-
return [
35-
[['name', 'sex', 'planets', 'astronauts', 'planet'], 'required'],
36-
['name', 'string'],
37-
['sex', 'boolean', 'message' => 'Пол выбран не верно.'],
38-
[
39-
['planets', 'planet'],
40-
'in',
41-
'range' => range(0, 7),
42-
'message' => 'Выбран не корректный список планет.',
43-
'allowArray' => 1
44-
],
45-
[
46-
'astronauts',
47-
'in',
48-
'range' => range(0, 5),
49-
'message' => 'Выбран не корректный список космонавтов.',
50-
'allowArray' => 1
51-
],
52-
['verifyCode', 'captcha'],
53-
];
54-
}
55-
5627
/**
5728
* @inheritdoc
5829
*/
@@ -64,7 +35,6 @@ public function attributeLabels()
6435
'planets' => 'Какие планеты обитаемы?',
6536
'astronauts' => 'Какие космонавты известны?',
6637
'planet' => 'На какую планету хотели бы полететь?',
67-
'verifyCode' => 'Проверочный код',
6838
];
6939
}
7040
}

0 commit comments

Comments
 (0)