From a1a60fa5fada8cfe21ee36b39474f9d3ad275472 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 26 Aug 2016 15:27:54 +0300 Subject: [PATCH] big commti --- backend/controllers/BrandController.php | 24 ++++++++++++++++++------ backend/views/brand/_form.php | 2 +- common/models/Customer.php | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- common/models/LoginForm.php | 2 ++ common/modules/product/models/Export.php | 31 ++++++++++++++++--------------- common/modules/rubrication/controllers/TaxOptionController.php | 21 +++++---------------- frontend/controllers/SiteController.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- frontend/models/ContactForm.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/PasswordResetRequestForm.php | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/ResetPasswordForm.php | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/models/SignupForm.php | 38 +++----------------------------------- frontend/views/modal/registration_window_model_window.php | 59 +++++++++++++++++++++++++++-------------------------------- 12 files changed, 406 insertions(+), 328 deletions(-) mode change 100755 => 100644 common/models/Customer.php create mode 100644 frontend/models/ContactForm.php create mode 100644 frontend/models/PasswordResetRequestForm.php create mode 100644 frontend/models/ResetPasswordForm.php diff --git a/backend/controllers/BrandController.php b/backend/controllers/BrandController.php index 058a1a0..c30fd00 100755 --- a/backend/controllers/BrandController.php +++ b/backend/controllers/BrandController.php @@ -81,12 +81,18 @@ class BrandController extends Controller { $model = new Brand(); if ($model->load(Yii::$app->request->post())) { - if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + if ( ($image = UploadedFile::getInstance($model, 'image')) ) { $model->image = $image->name; } - if ($model->save() && $image) { - $image->saveAs(Yii::getAlias('@imagesDir/brands/' . $image->name)); + + $imgDir = Yii::getAlias('@storage/brand/'); + + if(!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } + + $image->saveAs(Yii::getAlias('@storage/brand/' . $image->name)); } return is_null(Yii::$app->request->post('create_and_new')) ? $this->redirect(['view', 'id' => $model->brand_id]) : $this->redirect(array_merge(['create'], Yii::$app->request->queryParams)); @@ -108,12 +114,18 @@ class BrandController extends Controller $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { - if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + if ( ($image = UploadedFile::getInstance($model, 'image')) ) { $model->image = $image->name; } - if ($model->save() && $image) { - $image->saveAs(Yii::getAlias('@imagesDir/brands/' . $image->name)); + + $imgDir = Yii::getAlias('@storage/brands/'); + + if(!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } + + $image->saveAs(Yii::getAlias('@storage/brands/' . $image->name)); } return $this->redirect(['view', 'id' => $model->brand_id]); diff --git a/backend/views/brand/_form.php b/backend/views/brand/_form.php index 993a69b..6ae3467 100755 --- a/backend/views/brand/_form.php +++ b/backend/views/brand/_form.php @@ -19,7 +19,7 @@ use yii\widgets\ActiveForm; field($model, 'alias')->textInput(['maxlength' => true]) ?> - field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ + field($model, 'image')->widget(\kartik\file\FileInput::classname(), [ 'language' => 'ru', 'options' => [ 'accept' => 'image/*', diff --git a/common/models/Customer.php b/common/models/Customer.php old mode 100755 new mode 100644 index 1b225d7..8cb51e4 --- a/common/models/Customer.php +++ b/common/models/Customer.php @@ -1,257 +1,128 @@ ['person','company','customer']], - [['verifyCode'], 'captcha','captchaAction'=>'reg/captcha','on'=>['person','company','customer']], - [['password_repeat'], 'required','on'=>['person','company','customer','edit_person']], - [['password_repeat'], 'password_repeat','on'=>['person','company','customer','edit_person']], - [['username'], 'is_username','on'=>['person','company','customer']], - [['username'], 'email','on'=>['person','company','customer','edit_person']], - [['company'], 'required','on'=>['company']], - - [['username', 'password','phone','role','name','surname'], 'required','on'=>['edit_person','edit_customer','edit_company']], - [['company'], 'required','on'=>['edit_company']], - [['sex','body','birth_day','birth_mouth','birth_year'], 'safe','on'=>['edit_person']], - [['sex','status','children','body','old_image','birth_day','birth_mouth','birth_year'], 'safe','on'=>['edit_customer']], - [['body'], 'safe','on'=>['edit_company']], - // [['image'], 'file', 'extensions'=>'jpg, gif, png','skipOnEmpty'=>true,'on'=>['edit_person','edit_customer','edit_company']], - ]; - } - - public function attributeLabels() - { - return [ - 'username'=>'Логин (E-mail)', - 'password'=>'Пароль', - 'password_repeat'=>'Повторить пароль', - 'phone'=>'Телефон', - 'verifyCode'=>'Код проверки', - 'name'=>'Имя', - 'surname'=>'Фамилия', - 'company'=>'Компания', - 'sex'=>'Пол', - 'status'=>'Семейное положение', - 'children'=>'Дети', - 'edu'=>'Образование', - 'work'=>'Работа', - 'langs'=>'Иностранные языки', - 'prava'=>'Водительское удостоверение', - 'body'=>'О себе', - 'image'=>'Изображения', - ]; - } - - public function password_repeat($attribute){ - if($this->password != $this->password_repeat) - $this->addError('password_repeat','Не правильный повтор пароля.'); - } - - public function is_username($attribute) - { - if(Customer::find() - //->where( ['username' => $this->username],['id!='.$_GET['id']] ) - ->where('username = :username', [':username' => $this->username]) - ->exists()) - $this->addError('username','Такой пользователь уже есть.'); - } - - - public function afterSave($insert, $changedAttributes) - { - parent::afterSave($insert, $changedAttributes); - - // установка роли пользователя - $auth = Yii::$app->authManager; - $role = $auth->getRole($this->role); - if (!$insert) { - $auth->revokeAll($this->id); - } - $auth->assign($role, $this->id); - - - } - - public function beforeSave($insert) { - - - $this->date_time = new \yii\db\Expression('NOW()'); - - /** - if($image = UploadedFile::getInstance($this,'image')){ - - $this->deleteImage($this->old_image); - //$this->image = $image; - $this->image = time() . '_' . rand(1, 1000) . '.' . $image->extension; - $image->saveAs('upload/profile/'.$this->image); - - $resizeObj = new resize('upload/profile/'.$this->image); - $resizeObj -> resizeImage(240, 240, 'crop'); - $resizeObj -> saveImage('upload/profile/ico/'.$this->image, 100); - }elseif(!empty($this->old_image)) $this->image = $this->old_image; - **/ - - return parent::beforeSave($insert); - } - - public function beforeDelete() { - //$this->deleteImage($this->image); - return parent::beforeDelete(); - } - - public function deleteImage($file){ - if(!empty($file)){ - @unlink('upload/profile/'.$file); - @unlink('upload/profile/ico/'.$file); - // @unlink('upload/fotos/big/'.$file); - } - } - - - public function getOld(){ - if(empty($this->birth_year) || empty($this->birth_mouth) || empty($this->birth_day))return; - $birthday = $this->birth_year.'-'.$this->birth_mouth.'-'.$this->birth_day; - if($birthday=="0000-00-00")return; - $birthday_timestamp = strtotime($birthday); - $age = date('Y') - date('Y', $birthday_timestamp); - if (date('md', $birthday_timestamp) > date('md')) { - $age--; - } - return $age; - - } - /** * @inheritdoc */ - public static function findIdentity($id) + public static function tableName() { - - /**Yii::$app->db->createCommand() - ->update('user', [ - 'time_online' => (time() + (10*60)), - ], 'id = '.$id) - ->execute(); **/ - return static::find()->select(['customer.*','auth_assignment.item_name as role'])->where(['id'=>$id])->join('LEFT JOIN','auth_assignment','auth_assignment.user_id = customer.id')->one(); + return 'customer'; } /** * @inheritdoc */ - public static function findIdentityByAccessToken($token, $type = null) - { - return static::findOne(['access_token' => $token]); - } - - /** - * Finds user by username - * - * @param string $username - * @return static|null - */ - public static function findByUsername($username) + public function rules() { - return static::findOne(['username' => $username]); + return [ + [['username', 'password_hash'], 'required'], + [['date_time'], 'safe'], + [['birth_day', 'birth_month', 'birth_year', 'group_id', 'status', 'created_at', 'updated_at'], 'integer'], + [['body'], 'string'], + [['status'],'default', 'value' => '10'], + [['username', 'name', 'surname', 'phone', 'email', 'password_reset_token'], 'string', 'max' => 255], + [['gender', 'auth_key'], 'string', 'max' => 32], + ]; } /** * @inheritdoc */ - public function getId() + public function attributeLabels() { - return $this->id; + return [ + 'id' => 'ID', + 'username' => 'Username', + 'name' => 'Name', + 'surname' => 'Surname', + 'phone' => 'Phone', + 'date_time' => 'Date Time', + 'gender' => 'Gender', + 'birth_day' => 'Birth Day', + 'birth_month' => 'Birth Month', + 'birth_year' => 'Birth Year', + 'body' => 'Body', + 'group_id' => 'Group ID', + 'email' => 'Email', + 'auth_key' => 'Auth Key', + 'password_reset_token' => 'Password Reset Token', + 'status' => 'Status', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + ]; } /** - * @inheritdoc + * @return \yii\db\ActiveQuery */ - public function getAuthKey() + public function getArtboxComments() { - return $this->authKey; + return $this->hasMany(ArtboxComment::className(), ['user_id' => 'id']); } /** - * @inheritdoc + * @return \yii\db\ActiveQuery */ - public function validateAuthKey($authKey) + public function getArtboxCommentRatings() { - return $this->authKey === $authKey; + return $this->hasMany(ArtboxCommentRating::className(), ['user_id' => 'id']); } /** - * Validates password - * - * @param string $password password to validate - * @return boolean if password provided is valid for current user + * @return \yii\db\ActiveQuery */ - public function validatePassword($password) + public function getArtboxLikes() { - return $this->password === $password; - } - - public function getImageProfile(){ - return !empty($this->image) ? $this->image : 'user_photo.png'; - } - - public static function findByPasswordResetToken($token) - { - if (!static::isPasswordResetTokenValid($token)) { - return null; - } - - return static::findOne([ - 'password_reset_token' => $token, - 'status' => self::STATUS_ACTIVE, - ]); + return $this->hasMany(ArtboxLike::className(), ['user_id' => 'id']); } /** - * Finds out if password reset token is valid - * - * @param string $token password reset token - * @return boolean + * @return \yii\db\ActiveQuery */ - public static function isPasswordResetTokenValid($token) + public function getShares() { - if (empty($token)) { - return false; - } - - $timestamp = (int) substr($token, strrpos($token, '_') + 1); - $expire = Yii::$app->params['user.passwordResetTokenExpire']; - return $timestamp + $expire >= time(); + return $this->hasMany(Share::className(), ['user_id' => 'id']); } /** - * Generates new password reset token + * Finds user by email + * + * @param string $email + * @return static|null */ - public function generatePasswordResetToken() - { - $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); - } - public static function findByEmail($email) { return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]); @@ -262,11 +133,4 @@ class Customer extends User implements \yii\web\IdentityInterface } - /** - * Removes password reset token - */ - public function removePasswordResetToken() - { - $this->password_reset_token = null; - } } diff --git a/common/models/LoginForm.php b/common/models/LoginForm.php index 152ca97..35004c9 100644 --- a/common/models/LoginForm.php +++ b/common/models/LoginForm.php @@ -24,6 +24,7 @@ class LoginForm extends Model return [ // username and password are both required [['email', 'password'], 'required'], + [['email'], 'email'], // rememberMe must be a boolean value ['rememberMe', 'boolean'], // password is validated by validatePassword() @@ -56,6 +57,7 @@ class LoginForm extends Model public function login() { if ($this->validate()) { + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); } else { return false; diff --git a/common/modules/product/models/Export.php b/common/modules/product/models/Export.php index 0cdf722..5bbaf37 100755 --- a/common/modules/product/models/Export.php +++ b/common/modules/product/models/Export.php @@ -21,8 +21,9 @@ class Export extends Model { ->with(['variantsWithFilters','brand','categories','filters'])->all(); - $i = 0; - foreach ($products as $product) + + $count = count($products); + for ($i=0; $i<=$count; $i++) { @@ -32,9 +33,9 @@ class Export extends Model { }*/ $mods = []; - $filterString = $this->convertFilterToString($product->filters); + $filterString = $this->convertFilterToString($products[$i]->filters); - foreach ($product->variantsWithFilters as $variant) + foreach ($products[$i]->variantsWithFilters as $variant) { $color = $variant->name; @@ -56,7 +57,7 @@ class Export extends Model { // $filters = $product->properties; $categories = []; - foreach($product->categories as $value){ + foreach($products[$i]->categories as $value){ $categories[] = $value->name; @@ -68,18 +69,18 @@ class Export extends Model { $list = [ $categories, - $product->brand->name, - $product->name, + $products[$i]->brand->name, + $products[$i]->name, '', - ((! empty($product->description)) ? $product->description : ''), + ((! empty($products[$i]->description)) ? $products[$i]->description : ''), $filterString, - (!empty($product->variant)) ? $product->variant->price_old : '', - (!empty($product->variant)) ? $product->variant->price : '', - intval($product->akciya), + (!empty($products[$i]->variant)) ? $products[$i]->variant->price_old : '', + (!empty($products[$i]->variant)) ? $products[$i]->variant->price : '', + intval($products[$i]->akciya), '', - intval($product->is_new), - intval($product->is_top), - $product->video, + intval($products[$i]->is_new), + intval($products[$i]->is_top), + $products[$i]->video, implode (',', $fotos), ]; @@ -90,7 +91,7 @@ class Export extends Model { fputcsv($handle, $to_write, ';'); - unset($product); + unset($products[$i]); } diff --git a/common/modules/rubrication/controllers/TaxOptionController.php b/common/modules/rubrication/controllers/TaxOptionController.php index 71dacd3..12bf38a 100755 --- a/common/modules/rubrication/controllers/TaxOptionController.php +++ b/common/modules/rubrication/controllers/TaxOptionController.php @@ -79,21 +79,10 @@ class TaxOptionController extends Controller $valueModel = new $valueModelName; if ($model->load(Yii::$app->request->post())) { - - if ($model->save() && ($image = UploadedFile::getInstance($model, 'image')) ) { - $imageModel = TaxOptionImage::find()->where(['tax_option_id' => $model->tax_option_id])->one(); - - if($imageModel instanceof TaxOptionImage) { - $imageModel->product_variant_id = $model->product_variant_id; - $imageModel->image = $image->name; - $imageModel->save(); - } else { - $imageModel = new TaxOptionImage(); - $imageModel->product_variant_id = $model->product_variant_id; - $imageModel->image = $image->name; - $imageModel->save(); - } - + if ( ($image = UploadedFile::getInstance($model, 'image')) ) { + $model->image = $image->name; + } + if ($model->save() && $image) { $imgDir = Yii::getAlias('@storage/tax_option/'); @@ -154,7 +143,7 @@ class TaxOptionController extends Controller $image->saveAs(Yii::getAlias('@storage/tax_option/' . $image->name)); } - //TaxOption::find()->rebuildMP($model->tax_group_id); + TaxOption::find()->rebuildMP($model->tax_group_id); return $this->redirect(['view', 'id' => $model->tax_option_id]); } else { diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index e0733f6..bd3fb80 100755 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -2,17 +2,20 @@ namespace frontend\controllers; -use common\modules\product\models\Category; -use common\modules\product\models\CategorySearch; -use common\modules\product\models\Product; -use common\modules\product\models\ProductVariant; +use common\components\Mailer; use Yii; +use common\models\LoginForm; +use frontend\models\PasswordResetRequestForm; +use frontend\models\ResetPasswordForm; +use frontend\models\SignupForm; +use frontend\models\ContactForm; +use yii\base\InvalidParamException; +use yii\web\BadRequestHttpException; use yii\web\Controller; -use common\models\Page; -use common\models\News; -use common\models\Catalog; -use common\models\Products; - +use yii\filters\VerbFilter; +use yii\filters\AccessControl; +use yii\web\Response; +use yii\widgets\ActiveForm; class SiteController extends Controller { @@ -28,5 +31,57 @@ class SiteController extends Controller } + /** + * Logs in a user. + * + * @return mixed + */ + public function actionLogin() + { + if (!\Yii::$app->user->isGuest) { + return $this->goHome(); + } + + $model = new LoginForm(); + if ($model->load(Yii::$app->request->post()) && $model->login()) { + return $this->goBack(); + } else { + return $this->render('index', [ + 'model' => $model, + ]); + } + } + + + /** + * Signs user up. + * + * @return mixed + */ + public function actionSignup() + { + + if(Yii::$app->request->post()){ + if (Yii::$app->request->isAjax) { + Yii::$app->response->format = Response::FORMAT_JSON; + $model = new SignupForm(); + $model->load(Yii::$app->request->post()); + return ActiveForm::validate($model); + } else { + $model = new SignupForm(); + $model->load(Yii::$app->request->post()); + if ($user = $model->signup()) { + if (Yii::$app->getUser()->login($user)) { + return $this->goHome(); + } + } + } + } + return $this->render('signup', [ + 'model' => $model, + ]); + } + + } diff --git a/frontend/models/ContactForm.php b/frontend/models/ContactForm.php new file mode 100644 index 0000000..613abb5 --- /dev/null +++ b/frontend/models/ContactForm.php @@ -0,0 +1,59 @@ + 'Verification Code', + ]; + } + + /** + * Sends an email to the specified email address using the information collected by this model. + * + * @param string $email the target email address + * @return boolean whether the email was sent + */ + public function sendEmail($email) + { + return Yii::$app->mailer->compose() + ->setTo($email) + ->setFrom([$this->email => $this->name]) + ->setSubject($this->subject) + ->setTextBody($this->body) + ->send(); + } +} diff --git a/frontend/models/PasswordResetRequestForm.php b/frontend/models/PasswordResetRequestForm.php new file mode 100644 index 0000000..c09f6f7 --- /dev/null +++ b/frontend/models/PasswordResetRequestForm.php @@ -0,0 +1,68 @@ + 'trim'], + ['email', 'required'], + ['email', 'email'], + ['email', 'exist', + 'targetClass' => '\common\models\User', + 'filter' => ['status' => User::STATUS_ACTIVE], + 'message' => 'There is no user with such email.' + ], + ]; + } + + /** + * Sends an email with a link, for resetting the password. + * + * @return boolean whether the email was send + */ + public function sendEmail() + { + /* @var $user User */ + $user = User::findOne([ + 'status' => User::STATUS_ACTIVE, + 'email' => $this->email, + ]); + + if (!$user) { + return false; + } + + if (!User::isPasswordResetTokenValid($user->password_reset_token)) { + $user->generatePasswordResetToken(); + } + + if (!$user->save()) { + return false; + } + + return Yii::$app + ->mailer + ->compose( + ['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], + ['user' => $user] + ) + ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) + ->setTo($this->email) + ->setSubject('Password reset for ' . \Yii::$app->name) + ->send(); + } +} diff --git a/frontend/models/ResetPasswordForm.php b/frontend/models/ResetPasswordForm.php new file mode 100644 index 0000000..dd48f52 --- /dev/null +++ b/frontend/models/ResetPasswordForm.php @@ -0,0 +1,65 @@ +_user = User::findByPasswordResetToken($token); + if (!$this->_user) { + throw new InvalidParamException('Wrong password reset token.'); + } + parent::__construct($config); + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + ['password', 'required'], + ['password', 'string', 'min' => 6], + ]; + } + + /** + * Resets password. + * + * @return boolean if password was reset. + */ + public function resetPassword() + { + $user = $this->_user; + $user->setPassword($this->password); + $user->removePasswordResetToken(); + + return $user->save(false); + } +} diff --git a/frontend/models/SignupForm.php b/frontend/models/SignupForm.php index 7115378..5644391 100755 --- a/frontend/models/SignupForm.php +++ b/frontend/models/SignupForm.php @@ -18,9 +18,6 @@ class SignupForm extends Model public $surname; public $phone; - const SCENARIO_AJAX = 'ajax'; - const SCENARIO_SUBMIT = 'submit'; - /** * @inheritdoc */ @@ -35,11 +32,11 @@ class SignupForm extends Model ['email', 'required'], ['email', 'email'], [['email','phone'], 'string', 'max' => 255], - ['email', 'unique', 'targetClass' => '\common\models\Customers', 'message' => Yii::t('app','message',[ + ['email', 'unique', 'targetClass' => '\common\models\Customer', 'message' => Yii::t('app','message',[ 'field' => 'Email' ])], - [['phone'], 'unique', 'targetClass' => '\common\models\Customers', 'message' => Yii::t('app','message',[ + [['phone'], 'unique', 'targetClass' => '\common\models\Customer', 'message' => Yii::t('app','message',[ 'field' => 'Телефон' ])], @@ -48,43 +45,14 @@ class SignupForm extends Model ['password', 'required'], ['password', 'string', 'min' => 6], - - [ - 'verifyCode', - 'safe', - 'on'=>[SignupForm::SCENARIO_AJAX] - ], - [ - 'verifyCode', - 'captcha', - 'on'=>[SignupForm::SCENARIO_SUBMIT] - ], - [ - 'verifyCode', - 'captcha', - 'on'=>[SignupForm::SCENARIO_DEFAULT] - ], - - - ]; } /** - * @inheritdoc - */ - public function attributeLabels() - { - return [ - 'verifyCode' => 'Verification Code', - ]; - } - - /** * Signs user up. * - * @return Customers|null the saved model or null if saving fails + * @return Customer|null the saved model or null if saving fails */ public function signup() { diff --git a/frontend/views/modal/registration_window_model_window.php b/frontend/views/modal/registration_window_model_window.php index 1456a03..4150c79 100644 --- a/frontend/views/modal/registration_window_model_window.php +++ b/frontend/views/modal/registration_window_model_window.php @@ -1,6 +1,7 @@ + Url::toRoute('site/signup'), + ]); ?> -
-
- - -
-
- - -
- -
- - -
-
- -
- - -
-
- - -
-
-
- - - -
-

На ваш email будет отправлено письмо для подтверждения данных.

-
+ + +
+ + field(new SignupForm(), 'email', ['enableAjaxValidation' => true]) ?> + + field(new SignupForm(), 'username')->textInput() ?> + + field(new SignupForm(), 'surname')->textInput() ?> + + field(new SignupForm(), 'phone', ['enableAjaxValidation' => true, + ])->textInput() ?> + + + field(new SignupForm(), 'password')->passwordInput() ?> + + field(new SignupForm(), 'password_repeat')->passwordInput() ?> + +
+ 'my_cust_btn', 'name' => 'signup-button']) ?> +
+ +