Commit db1feeef461c5533eaa37f4d1c6433f982e01769
1 parent
1d3eec92
author page
Showing
6 changed files
with
324 additions
and
2 deletions
Show diff stats
common/models/Author.php
... | ... | @@ -6,6 +6,7 @@ |
6 | 6 | use yii\base\NotSupportedException; |
7 | 7 | use yii\behaviors\TimestampBehavior; |
8 | 8 | use yii\db\ActiveRecord; |
9 | + use yii\helpers\FileHelper; | |
9 | 10 | use yii\web\IdentityInterface; |
10 | 11 | |
11 | 12 | /** |
... | ... | @@ -82,6 +83,7 @@ |
82 | 83 | 'password_hash', |
83 | 84 | 'password_reset_token', |
84 | 85 | 'auth_key', |
86 | + 'avatar' | |
85 | 87 | ], |
86 | 88 | 'string', |
87 | 89 | 'max' => 255, |
... | ... | @@ -276,4 +278,22 @@ |
276 | 278 | $this->password_reset_token = null; |
277 | 279 | } |
278 | 280 | |
281 | + public function saveImage($file){ | |
282 | + /** | |
283 | + * @var \yii\web\UploadedFile $file; | |
284 | + */ | |
285 | + if (!empty($file)){ | |
286 | + if (!file_exists(\Yii::getAlias('@storage/author/') . $this->id)) { | |
287 | + FileHelper::createDirectory(\Yii::getAlias('@storage/author/') . $this->id); | |
288 | + } | |
289 | + if ($file->saveAs(\Yii::getAlias('@storage/author/').$this->id.'/'.$this->id.'.'.$file->extension)){ | |
290 | + $this->avatar = $this->id.'.'.$file->extension; | |
291 | + return $this->save(); | |
292 | + } | |
293 | + return false; | |
294 | + | |
295 | + } | |
296 | + return true; | |
297 | + } | |
298 | + | |
279 | 299 | } | ... | ... |
frontend/controllers/AuthorController.php
... | ... | @@ -8,11 +8,50 @@ |
8 | 8 | |
9 | 9 | namespace frontend\controllers; |
10 | 10 | |
11 | + use common\models\Book; | |
12 | + use frontend\helpers\Url; | |
13 | + use frontend\models\ChangePassword; | |
14 | + use yii\data\ActiveDataProvider; | |
11 | 15 | use yii\web\Controller; |
12 | - | |
16 | + use yii\web\Response; | |
17 | + use yii\web\UploadedFile; | |
18 | + | |
13 | 19 | class AuthorController extends Controller |
14 | 20 | { |
15 | - public function actionIndex(){ | |
21 | + public function actionIndex() | |
22 | + { | |
23 | + if (\Yii::$app->user->isGuest) { | |
24 | + return $this->redirect([ Url::home() ]); | |
25 | + } | |
26 | + /* @var \common\models\Author $user */ | |
27 | + $user = \Yii::$app->user->identity; | |
28 | + $dataProvider = new ActiveDataProvider( | |
29 | + [ | |
30 | + 'query' => Book::find() | |
31 | + ->where([ 'author_id' => $user->id ]), | |
32 | + 'pagination' => [ | |
33 | + 'pageSize' => 10, | |
34 | + ], | |
35 | + ] | |
36 | + ); | |
37 | + if ($user->load(\Yii::$app->request->post()) and $user->save()) { | |
38 | + $user->saveImage(UploadedFile::getInstanceByName('avatar')); | |
39 | + } | |
40 | + return $this->render( | |
41 | + 'index', | |
42 | + [ | |
43 | + 'user' => $user, | |
44 | + 'dataProvider' => $dataProvider, | |
45 | + ] | |
46 | + ); | |
47 | + } | |
48 | + | |
16 | 49 | |
50 | + public function actionChangePassword(){ | |
51 | + \Yii::$app->response->format = Response::FORMAT_JSON; | |
52 | + $model = new ChangePassword(); | |
53 | + if ($model->load(\Yii::$app->request->post(), '')){ | |
54 | + return $model->changePassword(); | |
55 | + } | |
17 | 56 | } |
18 | 57 | } |
19 | 58 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | + /** | |
3 | + * Created by PhpStorm. | |
4 | + * User: stes | |
5 | + * Date: 26.06.18 | |
6 | + * Time: 10:40 | |
7 | + */ | |
8 | + | |
9 | + namespace frontend\models; | |
10 | + | |
11 | + use yii\base\Model; | |
12 | + | |
13 | + class ChangePassword extends Model | |
14 | + { | |
15 | + public $password; | |
16 | + | |
17 | + public $confirmPassword; | |
18 | + | |
19 | + public function rules() | |
20 | + { | |
21 | + return [ | |
22 | + [ | |
23 | + ['password', 'confirmPassword'], 'string', | |
24 | + ],[ | |
25 | + | |
26 | + ['confirmPassword'], 'compare', 'compareAttribute' => 'password' | |
27 | + ] | |
28 | + ]; | |
29 | + } | |
30 | + | |
31 | + public function changePassword(){ | |
32 | + /* @var \common\models\Author $user*/ | |
33 | + $user = \Yii::$app->user->identity; | |
34 | + $user->setPassword($this->password); | |
35 | + return $user->save(); | |
36 | + } | |
37 | + } | |
0 | 38 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | + /** | |
3 | + * Created by PhpStorm. | |
4 | + * User: stes | |
5 | + * Date: 26.06.18 | |
6 | + * Time: 10:32 | |
7 | + */ | |
8 | + ?> | |
9 | +<div class="account-columns-row"> | |
10 | + <div class="account-number">№1</div> | |
11 | + <div class="account-title"><b>Нова українська артилерія</b></div> | |
12 | + <div class="account-edit"><a href="#">редагувати</a></div> | |
13 | +</div> | ... | ... |
1 | +<?php | |
2 | + /** | |
3 | + * @var \common\models\Author $user | |
4 | + * @var \common\models\Book $books | |
5 | + * @var \yii\web\View $this | |
6 | + */ | |
7 | + | |
8 | + use frontend\helpers\Url; | |
9 | + use frontend\models\ChangePassword; | |
10 | + use yii\web\View; | |
11 | + use yii\widgets\ActiveForm; | |
12 | + use yii\widgets\LinkPager; | |
13 | + use yii\widgets\ListView; | |
14 | + | |
15 | + $this->params[ 'breadcrumbs'][] = 'Особистий кабінет'; | |
16 | + $js = <<<JS | |
17 | + var maxFileSize = 3000000; | |
18 | + var types = [ | |
19 | + 'image/png', | |
20 | + 'image/jpeg', | |
21 | + 'image/jpg', | |
22 | + 'image/gif' | |
23 | + ]; | |
24 | + var removeFileSelector = document.getElementsByClassName('remove-img-file')[0]; | |
25 | + var fileInput = document.getElementById('qa'); | |
26 | + fileInput.addEventListener('change', function(event){ | |
27 | + event.preventDefault(); | |
28 | + var file = event.target.files[0]; | |
29 | + if(validateFiles(file)){ | |
30 | + displayImage(file); | |
31 | + } | |
32 | + | |
33 | + }); | |
34 | + function validateFiles(file) { | |
35 | + if (types.indexOf(file.type) !== -1 && file.size < maxFileSize) { | |
36 | + return true; | |
37 | + }else{ | |
38 | + return false; | |
39 | + | |
40 | + } | |
41 | + } | |
42 | + | |
43 | + function displayImage(file) { | |
44 | + | |
45 | + var fr = new FileReader(); | |
46 | + console.log(fr); | |
47 | + fr.onload = (function(file) { | |
48 | + return function() { | |
49 | + console.log(this.result); | |
50 | + var selector = $(".img-file"); | |
51 | + selector.find('img').attr('src', this.result); | |
52 | + selector.addClass('vis_'); | |
53 | + } | |
54 | + })(file); | |
55 | + fr.readAsDataURL(file); | |
56 | +// imageFile = file; | |
57 | + } | |
58 | + | |
59 | + removeFileSelector.addEventListener('click', function(event) { | |
60 | + console.log('remove'); | |
61 | + event.preventDefault(); | |
62 | + document.getElementsByClassName('img-file')[0].classList.remove('vis_'); | |
63 | + fileInput.value = ''; | |
64 | + }); | |
65 | +JS; | |
66 | +$this->registerJs($js, View::POS_READY); | |
67 | +$model = new ChangePassword(); | |
68 | +?> | |
69 | + | |
70 | + | |
71 | +<section class="section-books-support section-block-account"> | |
72 | + <div class="container"> | |
73 | + <div class="row"> | |
74 | + <div class="col-xs-12"> | |
75 | + <div class="title-blocks title-support">Особистий кабінет</div> | |
76 | + </div> | |
77 | + </div> | |
78 | + <div class="row"> | |
79 | + <div class="hidden-xs col-sm-2"></div> | |
80 | + <div class="col-xs-12 col-sm-8 support-form-col"> | |
81 | + <div class="style tabs-account"> | |
82 | + <ul> | |
83 | + <li class="active"><span>Налаштування</span></li> | |
84 | + <li><span>Проекти</span></li> | |
85 | + </ul> | |
86 | + </div> | |
87 | + <div class="style card-form-add-wrapp"> | |
88 | + <div class="active style tabs-account-forms"> | |
89 | + <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']])?> | |
90 | + <div class="add-book-form-column"> | |
91 | + <div class="input-wr"> | |
92 | + <?=$form->field($user, 'name')->label('Ім\'я')?> | |
93 | + </div> | |
94 | + | |
95 | + <div class="input-wr"> | |
96 | + <?=$form->field($user, 'secondname')->label('Прізвище')?> | |
97 | + </div> | |
98 | + | |
99 | + <div class="input-wr"> | |
100 | + <?=$form->field($user, 'email')->label('Пошта')?> | |
101 | + </div> | |
102 | + | |
103 | + <div class="input-wr phones_mask"> | |
104 | + <?=$form->field($user, 'phone')->label('Телефон')?> | |
105 | + </div> | |
106 | + <div class="input-wr"> | |
107 | + <?=$form->field($model, 'password')->passwordInput()->label('Пароль')?> | |
108 | + </div> | |
109 | + | |
110 | + <div class="input-wr"> | |
111 | + <?=$form->field($model, 'confirmPassword')->passwordInput()->label('Пароль ще раз')?> | |
112 | + </div> | |
113 | + | |
114 | + <div class="input-wr change-password-link"> | |
115 | + <span>змінити пароль</span> | |
116 | + </div> | |
117 | + </div> | |
118 | + <div class="add-book-form-column" style="text-align: center;"> | |
119 | + <div class="input-wr-file-wrapp"> | |
120 | + <label for="">Фото користувача (аватар)</label> | |
121 | + <div class="input-wr-file"> | |
122 | + <label for="qa"></label> | |
123 | + <input type="hidden" name="avatar" value=""> | |
124 | + <input type="file" id="qa" name="avatar"> | |
125 | + | |
126 | + <div class="img-size">182 x 182</div> | |
127 | + | |
128 | + <!--если картинка загруженна то добавить для .img-file класс ".vis_"--> | |
129 | + <div class="img-file <?=(!empty($user->avatar)) ? 'vis_': ''?>" style="display: none;"> | |
130 | + <!--124x84--> | |
131 | + <div class="remove-img-file"></div> | |
132 | + <img src="<?=(!empty($user->avatar)) ? '/storage/author/'.$user->id.'/'.$user->avatar : ''?>" alt=""> | |
133 | + </div> | |
134 | + | |
135 | + </div> | |
136 | + </div> | |
137 | + </div> | |
138 | + | |
139 | + | |
140 | + | |
141 | + <div class="button-wr account-save"> | |
142 | + <span class="account-cancel"><b>відмінити</b></span> | |
143 | + <button type="submit">зберегти</button> | |
144 | + </div> | |
145 | + <?php ActiveForm::end()?> | |
146 | + </div> | |
147 | + <div class="style tabs-account-forms"> | |
148 | + <div class="style add-project-btn"> | |
149 | + <a href="<?=Url::to(['book/add'])?>">додати проект</a> | |
150 | + </div> | |
151 | + <div class="style"> | |
152 | + <?php | |
153 | + /* @var \yii\data\ActiveDataProvider $dataProvider*/ | |
154 | + $count = round($dataProvider->getCount()/2, 0, PHP_ROUND_HALF_DOWN); | |
155 | + | |
156 | + ListView::widget([ | |
157 | + 'dataProvider' => $dataProvider, | |
158 | + 'itemView' => '_book', | |
159 | + 'options' => ['class' => 'account-columns'], | |
160 | + 'afterItem' => function ($model, $key, $index) use ($count) { | |
161 | + if ($index == $count){ | |
162 | + return " | |
163 | + </div> | |
164 | + <div class=\"account-columns\"> | |
165 | + "; | |
166 | + } | |
167 | + }, | |
168 | + 'layout' => '{items}', | |
169 | + ])?> | |
170 | + <div class="style account-pagination"> | |
171 | + <?=LinkPager::widget([ | |
172 | + 'pagination' => $dataProvider->pagination, | |
173 | + 'maxButtonCount' => 5, | |
174 | + ])?> | |
175 | + </div> | |
176 | + </div> | |
177 | + </div> | |
178 | + </div> | |
179 | + </div> | |
180 | + </div> | |
181 | + </div> | |
182 | +</section> | ... | ... |
frontend/web/js/script.js
... | ... | @@ -254,6 +254,37 @@ $(document).ready(function() { |
254 | 254 | } |
255 | 255 | }); |
256 | 256 | |
257 | + $(document).on('click', '.change-password-link span', function(e) { | |
258 | + e.preventDefault(); | |
259 | + var password = $('#changepassword-password'); | |
260 | + var passwordConfirm = $('#changepassword-confirmpassword'); | |
261 | + if (password.parent().hasClass('has-error') || passwordConfirm.parent().hasClass('has-error')){ | |
262 | + return false; | |
263 | + } | |
264 | + if (password.val() == '' || passwordConfirm.val() == ''){ | |
265 | + return false; | |
266 | + } | |
267 | + | |
268 | + $.ajax( | |
269 | + { | |
270 | + url: '/author/change-password', | |
271 | + type: "POST", | |
272 | + data: { | |
273 | + 'password': password.val(), | |
274 | + 'passwordConfirm': passwordConfirm.val() | |
275 | + }, | |
276 | + success: function(data) { | |
277 | + if (data){ | |
278 | + password.val(''); | |
279 | + passwordConfirm.val(''); | |
280 | + passwordConfirm.parent().parent().after('<p>Пароль змінено</p>') | |
281 | + } | |
282 | + } | |
283 | + } | |
284 | + ); | |
285 | + | |
286 | + }) | |
287 | + | |
257 | 288 | }); |
258 | 289 | function success(message) { |
259 | 290 | document.querySelector('#success_form .txt-success').innerHTML = message; | ... | ... |