Commit 32162c173c173480ca5bbd7b215929267ae8c0b3
1 parent
3930107c
- add buy a book
Showing
11 changed files
with
464 additions
and
9 deletions
Show diff stats
1 | +<?php | ||
2 | + /** | ||
3 | + * Created by PhpStorm. | ||
4 | + * User: stes | ||
5 | + * Date: 23.05.18 | ||
6 | + * Time: 12:55 | ||
7 | + */ | ||
8 | + | ||
9 | + namespace backend\controllers; | ||
10 | + | ||
11 | + use artbox\core\admin\actions\Delete; | ||
12 | + use artbox\core\admin\actions\Index; | ||
13 | + use artbox\core\admin\actions\View; | ||
14 | + use artbox\core\admin\widgets\Form; | ||
15 | + use common\models\Buy; | ||
16 | + use common\models\Comment; | ||
17 | + use common\models\Support; | ||
18 | + use yii\filters\AccessControl; | ||
19 | + use yii\filters\VerbFilter; | ||
20 | + use yii\web\Controller; | ||
21 | + use yii\web\NotFoundHttpException; | ||
22 | + | ||
23 | + class BuyController extends Controller | ||
24 | + { | ||
25 | + public function behaviors() | ||
26 | + { | ||
27 | + return [ | ||
28 | + 'verbs' => [ | ||
29 | + 'class' => VerbFilter::className(), | ||
30 | + 'actions' => [ | ||
31 | + 'delete' => [ 'POST' ], | ||
32 | + ], | ||
33 | + ], | ||
34 | + 'access' => [ | ||
35 | + 'class' => AccessControl::className(), | ||
36 | + 'rules' => [ | ||
37 | + [ | ||
38 | + 'allow' => true, | ||
39 | + 'roles' => [ '@' ], | ||
40 | + ], | ||
41 | + ], | ||
42 | + ], | ||
43 | + ]; | ||
44 | + } | ||
45 | + public function actions() | ||
46 | + { | ||
47 | + return [ | ||
48 | + 'index' => [ | ||
49 | + 'class' => Index::className(), | ||
50 | + 'columns' => [ | ||
51 | + 'name' => [ | ||
52 | + 'type' => Index::ACTION_COL, | ||
53 | + ], | ||
54 | + 'email' => [ | ||
55 | + 'type' => Index::STRING_COL | ||
56 | + ], | ||
57 | + 'status' => [ | ||
58 | + 'type' => Index::STATUS_COL, | ||
59 | + ], | ||
60 | + 'created_at' => [ | ||
61 | + 'type' => Index::DATETIME_COL, | ||
62 | + ] | ||
63 | + ], | ||
64 | + 'model' => Buy::className(), | ||
65 | + 'hasLanguage' => false, | ||
66 | + 'enableMassDelete' => true, | ||
67 | + 'modelPrimaryKey' => 'id', | ||
68 | + 'defaultSort' => [ | ||
69 | + 'created_at' => 'DESC', | ||
70 | + ], | ||
71 | + 'create' => false | ||
72 | + ], | ||
73 | + 'view' => [ | ||
74 | + 'class' => View::className(), | ||
75 | + 'model' => Buy::className(), | ||
76 | + 'hasAlias' => false, | ||
77 | + 'hasGallery' => false, | ||
78 | + 'languageFields' => [ | ||
79 | + ], | ||
80 | + 'fields' => [ | ||
81 | + [ | ||
82 | + 'name' => 'name', | ||
83 | + 'type' => Form::STRING, | ||
84 | + ], | ||
85 | + [ | ||
86 | + 'name' => 'email', | ||
87 | + 'type' => Form::STRING, | ||
88 | + ], | ||
89 | + [ | ||
90 | + 'name' => 'created_at', | ||
91 | + 'type' => Form::STRING, | ||
92 | + ], | ||
93 | + | ||
94 | + ], | ||
95 | + ], | ||
96 | + 'delete' => [ | ||
97 | + 'class' => Delete::className(), | ||
98 | + ], | ||
99 | + ]; | ||
100 | + } | ||
101 | + | ||
102 | + public function findModel($id) | ||
103 | + { | ||
104 | + | ||
105 | + $model = Buy::find() | ||
106 | + ->where([ 'id' => $id ]) | ||
107 | + ->one(); | ||
108 | + if ($model !== null) { | ||
109 | + return $model; | ||
110 | + } else { | ||
111 | + throw new NotFoundHttpException('The requested page does not exist.'); | ||
112 | + } | ||
113 | + } | ||
114 | + | ||
115 | + public function deleteModel($id) | ||
116 | + { | ||
117 | + $category = Buy::find() | ||
118 | + ->where( | ||
119 | + [ | ||
120 | + 'id' => $id, | ||
121 | + ] | ||
122 | + ) | ||
123 | + ->one(); | ||
124 | + | ||
125 | + | ||
126 | + return $category->delete(); | ||
127 | + } | ||
128 | + | ||
129 | + public function actionUpdate($id) | ||
130 | + { | ||
131 | + $model = $this->findModel($id); | ||
132 | + if ($model->load(\Yii::$app->request->post()) && $model->save()) { | ||
133 | + return $this->redirect('index'); | ||
134 | + } else { | ||
135 | + return $this->render( | ||
136 | + 'update', | ||
137 | + [ | ||
138 | + 'model' => $model, | ||
139 | + ] | ||
140 | + ); | ||
141 | + } | ||
142 | + } | ||
143 | + } | ||
0 | \ No newline at end of file | 144 | \ No newline at end of file |
1 | +<?php | ||
2 | + | ||
3 | + use artbox\core\admin\assets\Select2; | ||
4 | + use artbox\core\admin\assets\Switchery; | ||
5 | + use yii\helpers\Html; | ||
6 | + use yii\web\View; | ||
7 | + use yii\widgets\ActiveForm; | ||
8 | + | ||
9 | + /* @var $this yii\web\View */ | ||
10 | + /* @var $model \common\models\Comment */ | ||
11 | + /* @var $form yii\widgets\ActiveForm */ | ||
12 | + Switchery::register($this); | ||
13 | + $js = <<< JS | ||
14 | +$('.switchery').each(function(idx, elem) { | ||
15 | + new Switchery(elem, { | ||
16 | + color:'#46b749', | ||
17 | + secondaryColor:'#e2e2e2' | ||
18 | + }); | ||
19 | +}); | ||
20 | + | ||
21 | +$(".select_service").select2(); | ||
22 | +JS; | ||
23 | + | ||
24 | + Select2::register($this); | ||
25 | + $this->registerJs($js, View::POS_READY); | ||
26 | + | ||
27 | +?> | ||
28 | + | ||
29 | +<div class="feedback-form"> | ||
30 | + | ||
31 | + <?php $form = ActiveForm::begin(); ?> | ||
32 | + <?=$model->book->title?> Цена: <?=$model->book->price?> грн. | ||
33 | + | ||
34 | + <?= $form->field($model, 'name') | ||
35 | + ->textInput([ 'maxlength' => true ]) ?> | ||
36 | + | ||
37 | + <?= $form->field($model, 'email') | ||
38 | + ->textInput([ 'maxlength' => true ]) ?> | ||
39 | + | ||
40 | + | ||
41 | + <?= $form->field($model, 'status') | ||
42 | + ->checkbox( | ||
43 | + [ | ||
44 | + 'class' => 'switchery', | ||
45 | + ] | ||
46 | + ) ?> | ||
47 | + | ||
48 | + <div class="form-group"> | ||
49 | + <?= Html::submitButton( | ||
50 | + $model->isNewRecord ? Yii::t('core', 'Create') : Yii::t('core', 'Update'), | ||
51 | + [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ] | ||
52 | + ) ?> | ||
53 | + </div> | ||
54 | + | ||
55 | + <?php ActiveForm::end(); ?> | ||
56 | + | ||
57 | +</div> |
1 | +<?php | ||
2 | + | ||
3 | + use yiister\gentelella\widgets\Panel; | ||
4 | + | ||
5 | + /* @var $this yii\web\View */ | ||
6 | + /* @var $model \common\models\Comment */ | ||
7 | + | ||
8 | + $this->title = Yii::t( | ||
9 | + 'app', | ||
10 | + 'Update {modelClass}: ', | ||
11 | + [ | ||
12 | + 'modelClass' => 'Comment', | ||
13 | + ] | ||
14 | + ) . $model->name; | ||
15 | + $this->params[ 'breadcrumbs' ][] = [ | ||
16 | + 'label' => Yii::t('app', 'Comments'), | ||
17 | + 'url' => [ 'index' ], | ||
18 | + ]; | ||
19 | + $this->params[ 'breadcrumbs' ][] = [ | ||
20 | + 'label' => $model->name, | ||
21 | + 'url' => [ | ||
22 | + 'view', | ||
23 | + 'id' => $model->id, | ||
24 | + ], | ||
25 | + ]; | ||
26 | + $this->params[ 'breadcrumbs' ][] = Yii::t('core', 'Update'); | ||
27 | +?> | ||
28 | + | ||
29 | +<?php $panel = Panel::begin( | ||
30 | + [ | ||
31 | + 'header' => $this->title, | ||
32 | + 'options' => [ | ||
33 | + 'class' => 'x_panel comment-update', | ||
34 | + ], | ||
35 | + ] | ||
36 | +) ?> | ||
37 | + | ||
38 | +<?= $this->render( | ||
39 | + '_form', | ||
40 | + [ | ||
41 | + 'model' => $model, | ||
42 | + ] | ||
43 | +) ?> | ||
44 | + | ||
45 | +<?php $panel::end(); ?> |
backend/views/layouts/menu_items.php
common/models/Book.php
1 | +<?php | ||
2 | + | ||
3 | + namespace common\models; | ||
4 | + | ||
5 | + use Yii; | ||
6 | + use yii\behaviors\TimestampBehavior; | ||
7 | + | ||
8 | + /** | ||
9 | + * This is the model class for table "buy". | ||
10 | + * | ||
11 | + * @property int $id | ||
12 | + * @property int $book_id | ||
13 | + * @property string $name | ||
14 | + * @property string $email | ||
15 | + * @property int $created_at | ||
16 | + * @property int $status | ||
17 | + * @property Book $book | ||
18 | + */ | ||
19 | + class Buy extends \yii\db\ActiveRecord | ||
20 | + { | ||
21 | + /** | ||
22 | + * {@inheritdoc} | ||
23 | + */ | ||
24 | + public static function tableName() | ||
25 | + { | ||
26 | + return 'buy'; | ||
27 | + } | ||
28 | + public function behaviors() | ||
29 | + { | ||
30 | + return [ | ||
31 | + [ | ||
32 | + 'class' => TimestampBehavior::className(), | ||
33 | + 'updatedAtAttribute' => false, | ||
34 | + ], | ||
35 | + ]; | ||
36 | + | ||
37 | + } | ||
38 | + /** | ||
39 | + * {@inheritdoc} | ||
40 | + */ | ||
41 | + public function rules() | ||
42 | + { | ||
43 | + return [ | ||
44 | + [ | ||
45 | + [ | ||
46 | + 'book_id', | ||
47 | + 'created_at', | ||
48 | + 'status', | ||
49 | + ], | ||
50 | + 'default', | ||
51 | + 'value' => null, | ||
52 | + ], | ||
53 | + [ | ||
54 | + [ | ||
55 | + 'book_id', | ||
56 | + 'created_at', | ||
57 | + 'status', | ||
58 | + ], | ||
59 | + 'integer', | ||
60 | + ], | ||
61 | + [ | ||
62 | + [ | ||
63 | + 'name', | ||
64 | + 'email', | ||
65 | + ], | ||
66 | + 'string', | ||
67 | + 'max' => 255, | ||
68 | + ], | ||
69 | + [ | ||
70 | + [ 'book_id' ], | ||
71 | + 'exist', | ||
72 | + 'skipOnError' => true, | ||
73 | + 'targetClass' => Book::className(), | ||
74 | + 'targetAttribute' => [ 'book_id' => 'id' ], | ||
75 | + ], | ||
76 | + [ | ||
77 | + ['email'], 'required' | ||
78 | + ] | ||
79 | + ]; | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * {@inheritdoc} | ||
84 | + */ | ||
85 | + public function attributeLabels() | ||
86 | + { | ||
87 | + return [ | ||
88 | + 'id' => Yii::t('app', 'ID'), | ||
89 | + 'book_id' => Yii::t('app', 'Book ID'), | ||
90 | + 'name' => Yii::t('app', 'Name'), | ||
91 | + 'email' => Yii::t('app', 'Email'), | ||
92 | + 'created_at' => Yii::t('app', 'Created At'), | ||
93 | + 'status' => Yii::t('app', 'Status'), | ||
94 | + ]; | ||
95 | + } | ||
96 | + | ||
97 | + /** | ||
98 | + * @return \yii\db\ActiveQuery | ||
99 | + */ | ||
100 | + public function getBook() | ||
101 | + { | ||
102 | + return $this->hasOne(Book::className(), [ 'id' => 'book_id' ]); | ||
103 | + } | ||
104 | + } |
console/migrations/m180704_105641_create_buy_table.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +use yii\db\Migration; | ||
4 | + | ||
5 | +/** | ||
6 | + * Handles the creation of table `buy`. | ||
7 | + */ | ||
8 | +class m180704_105641_create_buy_table extends Migration | ||
9 | +{ | ||
10 | + /** | ||
11 | + * {@inheritdoc} | ||
12 | + */ | ||
13 | + public function safeUp() | ||
14 | + { | ||
15 | + $this->createTable('buy', [ | ||
16 | + 'id' => $this->primaryKey(), | ||
17 | + 'book_id' => $this->integer(), | ||
18 | + 'name' => $this->string(), | ||
19 | + 'email' => $this->string(), | ||
20 | + 'created_at' => $this->integer(), | ||
21 | + 'status' => $this->integer() | ||
22 | + ]); | ||
23 | + | ||
24 | + $this->addForeignKey('buy_book_fk', | ||
25 | + 'buy', | ||
26 | + 'book_id', | ||
27 | + 'book', | ||
28 | + 'id', | ||
29 | + 'CASCADE', | ||
30 | + 'CASCADE'); | ||
31 | + } | ||
32 | + | ||
33 | + /** | ||
34 | + * {@inheritdoc} | ||
35 | + */ | ||
36 | + public function safeDown() | ||
37 | + { | ||
38 | + $this->dropForeignKey('buy_book_fk', 'buy'); | ||
39 | + $this->dropTable('buy'); | ||
40 | + } | ||
41 | +} |
frontend/controllers/SupportController.php
@@ -9,6 +9,7 @@ | @@ -9,6 +9,7 @@ | ||
9 | namespace frontend\controllers; | 9 | namespace frontend\controllers; |
10 | 10 | ||
11 | use common\models\Book; | 11 | use common\models\Book; |
12 | + use common\models\Buy; | ||
12 | use common\models\Support; | 13 | use common\models\Support; |
13 | use frontend\helpers\Url; | 14 | use frontend\helpers\Url; |
14 | use frontend\models\SearchModel; | 15 | use frontend\models\SearchModel; |
@@ -68,4 +69,21 @@ | @@ -68,4 +69,21 @@ | ||
68 | } | 69 | } |
69 | } | 70 | } |
70 | } | 71 | } |
72 | + | ||
73 | + | ||
74 | + public function actionBuy($book_id = null){ | ||
75 | + $model = new Buy([ | ||
76 | + 'book_id' => $book_id, | ||
77 | + 'status' => 0 | ||
78 | + ]); | ||
79 | + $book = Book::find()->with('author')->where(['id' => $book_id])->one(); | ||
80 | + if ($model->load(\Yii::$app->request->post()) and $model->save()){ | ||
81 | + \Yii::$app->session->setFlash('success', 'Дякуємо за ваш запит. Найближчим часом, наш менеджер зв\'яжеться з Вами для уточнення деталей'); | ||
82 | + return $this->redirect(['site/index']); | ||
83 | + } | ||
84 | + return $this->render('buy', [ | ||
85 | + 'model' => $model, | ||
86 | + 'book' => $book | ||
87 | + ]); | ||
88 | + } | ||
71 | } | 89 | } |
72 | \ No newline at end of file | 90 | \ No newline at end of file |
frontend/views/book/_book.php
@@ -22,17 +22,16 @@ | @@ -22,17 +22,16 @@ | ||
22 | 400 | 22 | 400 |
23 | ) ?> | 23 | ) ?> |
24 | </div> | 24 | </div> |
25 | - <?php if ($model->price == null){?> | ||
26 | <div class="style books-btn books-btn-new"> | 25 | <div class="style books-btn books-btn-new"> |
27 | <a href="<?=Url::to(['support/index', 'book_id' => $model->id])?>" class="btn_">Підтримати</a> | 26 | <a href="<?=Url::to(['support/index', 'book_id' => $model->id])?>" class="btn_">Підтримати</a> |
28 | </div> | 27 | </div> |
29 | - <?php } else{?> | 28 | + <?php if ($model->price != null){?> |
30 | <div class="style price-books-catalog"><div>вартість <b><?=$model->price?></b> грн.</div></div> | 29 | <div class="style price-books-catalog"><div>вартість <b><?=$model->price?></b> грн.</div></div> |
31 | - | 30 | + <div class="style books-btn books-btn-buy"> |
31 | + <a href="<?=Url::to(['support/buy', 'book_id' => $model->id])?>" class="btn_">придбати</a> | ||
32 | + </div> | ||
32 | 33 | ||
33 | <?php } ?> | 34 | <?php } ?> |
34 | - <div class="style books-btn books-btn-buy"> | ||
35 | - <a href="<?=Url::to(['support/index', 'book_id' => $model->id])?>" class="btn_">придбати</a> | ||
36 | - </div> | 35 | + |
37 | </div> | 36 | </div> |
38 | </div> | 37 | </div> |
1 | +<?php | ||
2 | + /** | ||
3 | + * @var \common\models\Book $book | ||
4 | + * @var \common\models\Support $model | ||
5 | + */ | ||
6 | + use yii\helpers\Html; | ||
7 | + use yii\widgets\ActiveForm; | ||
8 | + | ||
9 | +?> | ||
10 | +<section class="section-books-support"> | ||
11 | + <div class="container"> | ||
12 | + <div class="row"> | ||
13 | + <div class="col-xs-12"> | ||
14 | + <div class="title-blocks title-support">Придбати</div> | ||
15 | + </div> | ||
16 | + </div> | ||
17 | + <div class="row"> | ||
18 | + <div class="hidden-xs col-sm-4"></div> | ||
19 | + <div class="col-xs-12 col-sm-4 support-form-col"> | ||
20 | + <div class="style support-form-wr"> | ||
21 | + <?php $form = ActiveForm::begin()?> | ||
22 | + <div class="style title-support-form"><?=$book->title?></div> | ||
23 | + <div class="style autor-support-form"> Автор: <?=$book->author->name?> <?=$book->author->secondname?></div> | ||
24 | + <div class="input-wr"> | ||
25 | + <?=$form->field($model, 'name')->label('Ім\'я')?> | ||
26 | + </div> | ||
27 | + <div class="input-wr"> | ||
28 | + <?=$form->field($model, 'email')->label('Пошта')?> | ||
29 | + </div> | ||
30 | + | ||
31 | + <div style="margin-top: 8px;" class="style title-support-form">Ціна - <?=$book->price?> грн.</div> | ||
32 | + <?=$form->field($model, 'book_id')->hiddenInput()->label(false);?> | ||
33 | + <?=$form->field($model, 'status')->hiddenInput()->label(false);?> | ||
34 | + <div class="button-wr"> | ||
35 | + <?=Html::submitButton('Придбати')?> | ||
36 | + </div> | ||
37 | + <?php ActiveForm::end()?> | ||
38 | + </div> | ||
39 | + </div> | ||
40 | + </div> | ||
41 | + </div> | ||
42 | +</section> |
frontend/views/support/index.php
@@ -11,7 +11,7 @@ | @@ -11,7 +11,7 @@ | ||
11 | <div class="container"> | 11 | <div class="container"> |
12 | <div class="row"> | 12 | <div class="row"> |
13 | <div class="col-xs-12"> | 13 | <div class="col-xs-12"> |
14 | - <div class="title-blocks title-support">Придбати заздалегіть</div> | 14 | + <div class="title-blocks title-support">Підтримати</div> |
15 | </div> | 15 | </div> |
16 | </div> | 16 | </div> |
17 | <div class="row"> | 17 | <div class="row"> |
@@ -36,7 +36,7 @@ | @@ -36,7 +36,7 @@ | ||
36 | <?=$form->field($model, 'book_id')->hiddenInput()->label(false);?> | 36 | <?=$form->field($model, 'book_id')->hiddenInput()->label(false);?> |
37 | <?=$form->field($model, 'status')->hiddenInput()->label(false);?> | 37 | <?=$form->field($model, 'status')->hiddenInput()->label(false);?> |
38 | <div class="button-wr"> | 38 | <div class="button-wr"> |
39 | - <?=Html::submitButton('Придбати')?> | 39 | + <?=Html::submitButton('Підтримати')?> |
40 | </div> | 40 | </div> |
41 | <?php ActiveForm::end()?> | 41 | <?php ActiveForm::end()?> |
42 | </div> | 42 | </div> |