
Личные данные
+ +
diff --git a/common/models/Customers.php b/common/models/Customers.php index fdfc10f..1e15eb5 100644 --- a/common/models/Customers.php +++ b/common/models/Customers.php @@ -58,6 +58,22 @@ class Customers extends User } /** + * Finds user by email + * + * @param string $email + * @return static|null + */ + public static function findByEmail($email) + { + return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]); + } + + public function getName(){ + return $this->username. ' '.$this->surname; + } + + + /** * @inheritdoc */ public function attributeLabels() diff --git a/common/models/LoginForm.php b/common/models/LoginForm.php index afc1c23..11b4ec5 100644 --- a/common/models/LoginForm.php +++ b/common/models/LoginForm.php @@ -9,9 +9,9 @@ use yii\base\Model; */ class LoginForm extends Model { - public $username; public $password; public $rememberMe = true; + public $email; private $_user; @@ -23,7 +23,7 @@ class LoginForm extends Model { return [ // username and password are both required - [['username', 'password'], 'required'], + [['email', 'password'], 'required'], // rememberMe must be a boolean value ['rememberMe', 'boolean'], // password is validated by validatePassword() @@ -70,9 +70,19 @@ class LoginForm extends Model protected function getUser() { if ($this->_user === null) { - $this->_user = User::findByUsername($this->username); + $this->_user = Customers::findByEmail($this->email); } return $this->_user; } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'rememberMe' => Yii::t('app', 'rememberMe'), + ]; + } } diff --git a/common/translation/ru/app.php b/common/translation/ru/app.php new file mode 100644 index 0000000..efcee4a --- /dev/null +++ b/common/translation/ru/app.php @@ -0,0 +1,25 @@ + 'ID', + 'username' => 'Имя', + 'surname' => 'Фамилия', + 'auth_key' => 'Auth Key', + 'password_hash' => 'Password Hash', + 'password_reset_token' => 'Password Reset Token', + 'email' => 'Логин (e-mail)', + 'phone' => 'Телефон', + 'status' => 'Status', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + 'verifyCode' => 'Код проверки', + 'password' => 'Пароль', + 'password_repeat' => 'Повторить пароль', + 'registration' => 'Регистрация', + 'message' => 'Этот {field} уже занят', + 'message_match_password' => 'Пароли не совпадают', + 'exit' => 'Выход', + 'enter' => 'Войти', + 'your_personal_area' => 'Вход в личный кабинет', + 'forgot_password' => 'Забыли пароль?', + 'rememberMe' => 'Запомнить меня', +]; \ No newline at end of file diff --git a/console/migrations/m160320_174258_customer.php b/console/migrations/m160320_174258_customer.php index 58c0c4a..f0b22ca 100644 --- a/console/migrations/m160320_174258_customer.php +++ b/console/migrations/m160320_174258_customer.php @@ -14,7 +14,7 @@ class m160320_174258_customer extends Migration $this->createTable('{{%customers}}', [ 'id' => $this->primaryKey(), - 'username' => $this->string()->notNull()->unique(), + 'username' => $this->string()->notNull(), 'surname' => $this->string(), 'auth_key' => $this->string(32)->notNull(), 'password_hash' => $this->string()->notNull(), diff --git a/console/migrations/m160321_232402_orders.php b/console/migrations/m160321_232402_orders.php new file mode 100644 index 0000000..0cec26a --- /dev/null +++ b/console/migrations/m160321_232402_orders.php @@ -0,0 +1,34 @@ +db->driverName === 'mysql') { + // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%orders}}', [ + 'id' => $this->primaryKey(), + 'name' => $this->string()->notNull(), + 'email' => $this->string()->notNull(), + 'phone' => $this->string(32)->notNull(), + 'delivery' => $this->integer(), + 'payment' => $this->integer(), + 'code' => $this->string(), + 'status' => $this->smallInteger(), + 'created_at' => $this->integer()->notNull(), + 'updated_at' => $this->integer()->notNull(), + ], $tableOptions); + } + + public function down() + { + $this->dropTable('{{%orders}}'); + } + +} diff --git a/frontend/controllers/OrderController.php b/frontend/controllers/OrderController.php new file mode 100644 index 0000000..3c8a520 --- /dev/null +++ b/frontend/controllers/OrderController.php @@ -0,0 +1,190 @@ +request->post()) { + $item = $items_id = array(); + + $i = 0; + $order = Yii::$app->request->post(); + + $orderData['Order'] = $order['OrderForm']; + + foreach($order['OrderForm']['one_item'] as $k => $v ){ + $item[$k]['num'] = $v['num']; + $items_id[] = $k; + $i++; + } + + $items = Items::find()->where(['id'=>$items_id])->all(); + + + $orderModel = new Order(); + $orderModel->load($orderData); + $orderModel->save(); + + + foreach($items as $one_item){ + $ItemOrderModel = new ItemOrder(); + $ItemOrderModel->order_id = $orderModel->id; + $ItemOrderModel->num = $item[$one_item->id]['num']; + $ItemOrderModel->item_id = $one_item->id; + $ItemOrderModel->price = $one_item->price * $item[$one_item->id]['num']; + $ItemOrderModel->item_name = $one_item->name; + $ItemOrderModel->save(); + } + Yii::$app->session->set('order', [] ); + return $this->redirect(['order/complete']); + } + $total_price = 0; + + $items_id = []; + + $orders = Yii::$app->session->get('order'); + + if(!empty($orders)){ + foreach($orders as $k => $v) { + $items_id[] = $k; + } + } + + + $items = Items::find()->where(['id'=>$items_id])->all(); + + foreach($items as $item) { + $total_price += $orders[$item['id']]['num'] * $item['price']; + } + + + $dataProvider = new ArrayDataProvider([ + 'allModels' => $items + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + 'total_price'=> $total_price, + 'model' => new OrderForm() + ]); + } + + + public function actionComplete() + { + return $this->render('complete', [ + ]); + } + + public function actionBuyItems(){ + $data = Yii::$app->request->post(); + $sessionData = Yii::$app->session->get('order'); + if(isset($sessionData) && !array_search($data['id'],Yii::$app->session->get('order')) ){ + $array = Yii::$app->session->get('order'); + $array[$data['id']] = $data; + Yii::$app->session->set('order', $array ); + } else { + $array[$data['id']] = $data; + Yii::$app->session->set('order', $array ); + } + echo count(Yii::$app->session->get('order')); + + } + /** + * Displays a single Order model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Order model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Order(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing Order model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Order model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete() + { + $data = Yii::$app->request->post(); + $sessionData = Yii::$app->session->get('order'); + unset($sessionData[$data['id']]); + Yii::$app->session->set('order', $sessionData); + return count(Yii::$app->session->get('order')); + } + + /** + * Finds the Order model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Order the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Order::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/frontend/controllers/PuttyController.php b/frontend/controllers/PuttyController.php index f3adaef..5103c6e 100644 --- a/frontend/controllers/PuttyController.php +++ b/frontend/controllers/PuttyController.php @@ -35,9 +35,6 @@ class PuttyController extends Controller return $this->render('basket-step-02'); } - public function actionCabinet(){ - return $this->render('cabinet'); - } public function actionContacts(){ return $this->render('contacts'); diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 1366251..edcedb2 100644 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -12,12 +12,15 @@ use yii\web\BadRequestHttpException; use yii\web\Controller; use yii\filters\VerbFilter; use yii\filters\AccessControl; +use yii\web\Response; +use yii\widgets\ActiveForm; /** * Site controller */ class SiteController extends Controller { + /** * @inheritdoc */ @@ -52,6 +55,18 @@ class SiteController extends Controller /** * @inheritdoc */ + public function beforeAction($action) + { + if ($action->id == 'signup') { + Yii::$app->controller->enableCsrfValidation = false; + } + + return true; + } + + /** + * @inheritdoc + */ public function actions() { return [ @@ -149,15 +164,23 @@ class SiteController extends Controller */ public function actionSignup() { - $model = new SignupForm(); - if ($model->load(Yii::$app->request->post())) { - if ($user = $model->signup()) { - if (Yii::$app->getUser()->login($user)) { - return $this->goHome(); - } - } - } + if(Yii::$app->request->post()){ + if (Yii::$app->request->isAjax) { + Yii::$app->response->format = Response::FORMAT_JSON; + $model = new SignupForm(['scenario' => SignupForm::SCENARIO_AJAX]); + $model->load(Yii::$app->request->post()); + return ActiveForm::validate($model); + } else { + $model = new SignupForm(['scenario' => SignupForm::SCENARIO_SUBMIT]); + $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, ]); @@ -211,4 +234,9 @@ class SiteController extends Controller 'model' => $model, ]); } + + + public function actionCabinet(){ + return $this->render('cabinet'); + } } diff --git a/frontend/models/SignupForm.php b/frontend/models/SignupForm.php index 9a781d4..dca4b88 100644 --- a/frontend/models/SignupForm.php +++ b/frontend/models/SignupForm.php @@ -18,6 +18,8 @@ class SignupForm extends Model public $surname; public $phone; + const SCENARIO_AJAX = 'ajax'; + const SCENARIO_SUBMIT = 'submit'; /** * @inheritdoc @@ -33,16 +35,38 @@ class SignupForm extends Model ['email', 'required'], ['email', 'email'], [['email','phone'], 'string', 'max' => 255], - ['email', 'unique', 'targetClass' => '\common\models\Customers', 'message' => 'This email address has already been taken.'], + ['email', 'unique', 'targetClass' => '\common\models\Customers', 'message' => Yii::t('app','message',[ + 'field' => 'Email' + ])], + [['phone'], 'unique', 'targetClass' => '\common\models\Customers', 'message' => Yii::t('app','message',[ + 'field' => 'Телефон' + ])], ['password_repeat', 'required'], - ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match" ], + ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=> Yii::t('app', 'message_match_password') ], ['password', 'required'], ['password', 'string', 'min' => 6], - ['verifyCode', 'captcha'], + [ + 'verifyCode', + 'safe', + 'on'=>[SignupForm::SCENARIO_AJAX] + ], + [ + 'verifyCode', + 'captcha', + 'on'=>[SignupForm::SCENARIO_SUBMIT] + ], + [ + 'verifyCode', + 'captcha', + 'on'=>[SignupForm::SCENARIO_DEFAULT] + ], + + + ]; } @@ -65,17 +89,18 @@ class SignupForm extends Model public function signup() { - if (!$this->validate()) { return null; } $user = new Customers(); $user->username = $this->username; + $user->surname = $this->surname; $user->email = $this->email; $user->phone = $this->phone; $user->setPassword($this->password); $user->generateAuthKey(); + $user->validate(); return $user->save() ? $user : null; } } diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index 325055f..fffbc77 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -5,6 +5,8 @@ use frontend\assets\AppAsset; +use yii\helpers\Html; +use yii\helpers\Url; AppAsset::register($this); @@ -35,15 +37,20 @@ AppAsset::register($this);