public function actionCreate()
 {
     if (User::model()->isAuthor()) {
         $this->redirect('/');
     }
     if (isset($_GET['iframe']) and $_GET['iframe'] == 'yes') {
         $iframe = true;
     } else {
         $iframe = false;
     }
     $model = new Zakaz();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (Yii::app()->user->isGuest) {
         Yii::app()->theme = 'client';
         $user = new RegistrationForm();
         if (isset($_POST['RegistrationForm'])) {
             Yii::import('user.controllers.RegistrationController');
             if (RegistrationController::register($user, $_POST['RegistrationForm'])) {
             }
         }
     }
     $isGuest = Yii::app()->user->isGuest;
     $new = true;
     $agreementNotAccepted = null;
     $messageForCustomer = null;
     if (!$isGuest && self::createProject($model, $_POST['Zakaz'])) {
         if (User::model()->isManager()) {
             $this->redirect(Yii::app()->createUrl('/project/zakaz/update', array('id' => $model->id)));
         } else {
             $new = false;
             $agreementNotAccepted = Templates::model()->getTemplate(Templates::TYPE_FOR_MANAGER_AGREEMENT_NOT_ACCEPTED);
             $messageForCustomer = Templates::model()->getTemplate(Templates::TYPE_FOR_CUSTOMER_AGREEMENT_NOT_ACCEPTED);
         }
     } else {
         $model->attributes = $_POST['Zakaz'];
     }
     if (!isset($model->unixtime) || $model->unixtime == '') {
         $model->unixtime = time();
     }
     if ($iframe) {
         $this->layout = '//layouts/iframe';
     }
     $this->render('create', array('model' => $model, 'isGuest' => $isGuest, 'user' => $user, 'new' => $new, 'agreementNotAccepted' => $agreementNotAccepted, 'messageForCustomer' => $messageForCustomer));
 }
 public function processOrderPage()
 {
     $message = false;
     $login_ok = false;
     if (!Yii::app()->user->isGuest && User::model()->isCustomer()) {
         $login_ok = true;
     }
     if (!$login_ok && isset($_POST['Login'])) {
         if (Yii::app()->user->isGuest) {
             $model = new UserLogin();
             //$this->performAjaxValidation($model);
             // collect user input data
             $model->attributes = $_POST['Login'];
             // validate user input and redirect to previous page if valid
             if ($model->validate()) {
                 //$this->lastViset();
                 $login_ok = true;
             } else {
                 $message = 'Incorrect login or password';
                 //Yii::app()->end();
             }
         }
     }
     if (!$login_ok && isset($_POST['User'])) {
         $model = new User();
         $attributes = $_POST['User'];
         //$attributes['full_name'] =  $_POST['User']['first_name'].' '.$_POST['User']['last_name'];
         $pos = strpos($attributes['email'], '@');
         $attributes['username'] = str_replace(array('@', '.'), '_', $attributes['email']);
         // substr( $attributes['email'], 0, $pos);
         $attributes['full_name'] = $_POST['User']['first_name'] . ' ' . $_POST['User']['last_name'];
         unset($attributes['first_name']);
         unset($attributes['last_name']);
         $p = $_POST['Profile'];
         $country = $p['country'];
         $countryCodes = $this->getCountryCodes();
         $code = $countryCodes[$country];
         $attributes['phone_number'] = '+' . $code . $attributes['phone_number'];
         Yii::import('user.controllers.RegistrationController');
         if (RegistrationController::register($model, $attributes)) {
             $login_ok = true;
             $profile = new Profile();
             $profile->user_id = $model->id;
             $profile->country = $country;
             $profile->save();
         } else {
             if ($attributes['email'] != '') {
                 $message = 'Sorry, registration faild...<br>';
                 foreach ($model->errors as $err => $descr) {
                     $message .= $descr[0] . '<br>';
                 }
             }
         }
     }
     $model = new Zakaz();
     $model->attributes = $_POST['Zakaz'];
     // (unixtime)
     if ($login_ok) {
         Yii::import('project.controllers.ZakazController');
         if (ZakazController::createProject($model, $_POST['Project'])) {
             $cost = $this->calculateCost($model);
             $payment = new ProjectPayments();
             $payment->order_id = $model->id;
             $payment->project_price = $cost;
             $payment->received = 0;
             $payment->to_receive = $cost;
             $payment->work_price = 0;
             $payment->payed = 0;
             $payment->to_pay = 0;
             $payment->save();
             if (Campaign::getPayment2Chekout() != 0) {
                 $user = User::model()->with('profile')->findByPk($model->user_id);
                 $data = array('sid' => Campaign::getPayment2Chekout(), 'mode' => '2CO', 'li_0_type' => 'product', 'li_0_name' => 'order' . $model->id, 'li_0_price' => $cost, 'li_0_product_id' => $model->id, 'x_receipt_link_url' => 'http://' . $_SERVER["HTTP_HOST"] . '/project/payment/affiliatePayment', 'card_holder_name' => $user->full_name, 'country' => $user->profile->country, 'email' => $user->email, 'phone' => $user->phone_number);
                 $this->redirectWithPost('https://2checkout.com/checkout/purchase', $data);
             }
             echo 'Ok! Cost = ' . $cost;
             Yii::app()->end();
         } else {
             //echo 'Project is not valid!!<br>';
             //print_r($_POST['Project']);
             //Yii::app()->end();
             $message = 'Please complete all required fields.';
         }
     }
     if (!isset($model->unixtime) or $model->unixtime == '') {
         $model->unixtime = time();
     }
     Yii::app()->theme = explode('.', $_SERVER['SERVER_NAME'])[0];
     $this->render('page/order', array('logged' => $login_ok, 'message' => $message, 'project' => $model, 'countryCodes' => $this->getCountryCodes()));
     Yii::app()->end();
 }