コード例 #1
0
ファイル: ProfileForm.php プロジェクト: alexletsu/kiytes
 /**
  * Save user profile
  * @return boolean whether the user is created successfully
  */
 public function saveProfile()
 {
     if (YII_DEBUG) {
         error_log("[ProfileForm::saveProfile]");
     }
     if (YII_DEBUG) {
         error_log("[ProfileForm::saveProfile] attributes : " . json_encode($this->getAttributes()));
     }
     if ($this->isEditable) {
         $formData = Yii::$app->request->post('ProfileForm');
         if ($formData) {
             $this->processFileField('photo', $this->_user);
             $_email = isset($formData['email']) ? strval($formData['email']) : null;
             if ($_email && $this->_user->email !== $_email) {
                 $v = new EmailValidator();
                 $err = null;
                 if ($v->validate($_email)) {
                     $u = User::findOne(['email' => $_email]);
                     if (null !== $u) {
                         $err = 'Email already exists';
                     } else {
                         $this->_user->email = $_email;
                     }
                 } else {
                     $err = 'Not a valid email address';
                 }
                 if ($err) {
                     Yii::$app->session->addFlash('email', $err);
                 }
             }
             $_phone = isset($formData['phone']) ? strval($formData['phone']) : null;
             if ($_phone && 0 == $this->_user->phone_verified && $this->_user->phone !== $_phone) {
                 $err = User::validatePhone($_phone);
                 if (true === $err) {
                     $u = User::findOne(['phone' => $_phone]);
                     if (null !== $u) {
                         $err = 'Phone already exists';
                     } else {
                         $this->_user->phone = $_phone;
                         $this->_user->phone_verified = 0;
                         $this->_user->generatePhoneToken();
                     }
                 }
                 if (is_string($err)) {
                     Yii::$app->session->addFlash('phone', $err);
                 }
             }
             $_address = isset($formData['address']) ? strval($formData['address']) : null;
             if ($_address && 0 < strlen($_address)) {
                 $addr = Address::findOne(['address' => $_address]);
                 if (!$addr) {
                     $addr = new Address();
                     $addr->address = $_address;
                     $addr->save();
                 }
                 if (User::$_TYPE_CUSTOMER === $this->_user->user_type) {
                     $rua = RelUserAddress::findOne(['user_id' => $this->_user->id, 'address_id' => $addr->id]);
                     if (!$rua) {
                         $rua = new RelUserAddress();
                         $rua->user_id = $this->_user->id;
                         $rua->address_id = $addr->id;
                         $rua->save();
                     }
                 } else {
                     $rua = RelUserAddress::findOne(['user_id' => $this->_user->id]);
                     if (!$rua) {
                         $rua = new RelUserAddress();
                         $rua->user_id = $this->_user->id;
                     }
                     $rua->address_id = $addr->id;
                     $rua->save();
                 }
             }
             $_fName = isset($formData['firstName']) ? strval($formData['firstName']) : null;
             if (strlen($_fName) > 0 && $this->_user->first_name !== $_fName) {
                 $this->_user->first_name = $_fName;
             }
             $_lName = isset($formData['lastName']) ? strval($formData['lastName']) : null;
             if (strlen($_lName) > 0 && $this->_user->last_name !== $_lName) {
                 $this->_user->last_name = $_lName;
             }
             $_ccInfo = isset($formData['creditCardInfo']) ? strval($formData['creditCardInfo']) : null;
             if ($this->_user->ccard_info !== $_ccInfo) {
                 $this->_user->ccard_info = $_ccInfo;
             }
             if (User::$_TYPE_DRIVER === $this->_user->user_type) {
                 $this->_car = Car::findOne(['driver_id' => $this->_user->id]);
                 if (!$this->_car) {
                     $this->_car = new Car();
                     $this->_car->driver_id = $this->_user->id;
                     $this->_car->save();
                 }
                 $this->processFileField('car_photo', $this->_car);
                 $this->processFileField('license_photo', $this->_user);
                 $this->processFileField('insurance_photo', $this->_user);
                 $_priceMile = isset($formData['priceMile']) ? strval($formData['priceMile']) : null;
                 if (strlen($_priceMile) > 0 && $this->_car->price_mile !== $_priceMile) {
                     $this->_car->price_mile = $_priceMile;
                 }
                 $_carMake = isset($formData['carMake']) ? strval($formData['carMake']) : null;
                 if (strlen($_carMake) > 0 && $this->_car->make !== $_carMake) {
                     $this->_car->make = $_carMake;
                 }
                 $_carModel = isset($formData['carModel']) ? strval($formData['carModel']) : null;
                 if (strlen($_carModel) > 0 && $this->_car->model !== $_carModel) {
                     $this->_car->model = $_carModel;
                 }
                 $_carYear = isset($formData['carYear']) ? strval($formData['carYear']) : null;
                 if (strlen($_carYear) > 0 && $this->_car->year !== $_carYear) {
                     $this->_car->year = $_carYear;
                 }
                 $_licensePlateNumber = isset($formData['licensePlateNumber']) ? strval($formData['licensePlateNumber']) : null;
                 $_existing_car = Car::findOne(['license_plate' => $_licensePlateNumber]);
                 if ($_existing_car) {
                     Yii::$app->session->addFlash('license_plate', 'License plate number already registered');
                 } elseif (strlen($_licensePlateNumber) > 0 && $this->_car->license_plate !== $_licensePlateNumber) {
                     $this->_car->license_plate = $_licensePlateNumber;
                 }
                 $this->_car->save();
             }
         }
         $this->_user->save();
     }
     $this->loadData();
     return true;
 }
コード例 #2
0
ファイル: SiteController.php プロジェクト: alexletsu/kiytes
 public function actionInvitation()
 {
     $_currUser = Yii::$app->user->isGuest ? null : User::findOne(['id' => Yii::$app->user->id]);
     $_request = Yii::$app->request;
     if (!$_currUser || User::$_TYPE_DRIVER === $_currUser->user_type) {
         return $this->goHome();
     }
     $_driver = $_request->get('driver') ? User::findOne(['id' => $_request->get('driver')]) : null;
     if ('1' === $_request->get('send_invite')) {
         $errorMsg = null;
         if (!$_currUser) {
             $errorMsg = "Please log in";
         } elseif (!$_request->get('address_start')) {
             $errorMsg = "Start Address Not Specified";
         } elseif (!$_request->get('address_dest')) {
             $errorMsg = "Destination Address Not Specified";
         } elseif (!$_request->get('time_start')) {
             $errorMsg = "Ride Date And Time Not Specified";
         } elseif (!$_driver) {
             $errorMsg = "Driver Not Choosen";
         } else {
             $addressStart = Address::findOne(['id' => $_request->get('address_start')]);
             $addressDest = Address::findOne(['id' => $_request->get('address_dest')]);
             if (!$addressStart) {
                 $errorMsg = "Start Address Not Found";
             } elseif (!$addressDest) {
                 $errorMsg = "Destination Address Not Found";
             } else {
                 $ruaStart = RelUserAddress::findOne(['user_id' => $_currUser->id, 'address_id' => $addressStart->id]);
                 $ruaDest = RelUserAddress::findOne(['user_id' => $_currUser->id, 'address_id' => $addressDest->id]);
                 if (!$ruaStart) {
                     $errorMsg = "Start Address Is Not In Your Addresses List";
                 } elseif (!$ruaDest) {
                     $errorMsg = "Destination Address Is Not In Your Addresses List";
                 } else {
                     $timeStart = \DateTime::createFromFormat("d/m/Y H:i", $_request->get('time_start'));
                     $dtNowPlsHour = new \DateTime();
                     $dtNowPlsHour->add(new \DateInterval("PT1H"));
                     if (YII_DEBUG) {
                         error_log("datetime now + 1 hour : {$dtNowPlsHour->format("d/m/Y H:i")}");
                         error_log("datetime start : {$timeStart->format("d/m/Y H:i")}");
                     }
                     if ($dtNowPlsHour > $timeStart) {
                         $errorMsg = "Ride Start Time Should Be At Least In Next Hour";
                     } else {
                         if (YII_DEBUG) {
                             error_log("ready to create ride invite : ");
                             error_log("*** address start : {$addressStart->address} [{$addressStart->id}]");
                             error_log("*** address dest : {$addressDest->address} [{$addressDest->id}]");
                             error_log("*** time start : {$timeStart->format('d/m/Y H:i')}");
                             error_log("*** driver : {$_driver->email} [{$_driver->id}]");
                             error_log("*** current user : {$_currUser->email} [{$_currUser->id}]");
                             $ride = new Ride();
                             $ride->client_id = $_currUser->id;
                             $ride->driver_id = $_driver->id;
                             $ride->status = Ride::$_STATUS_INVITATION;
                             $ride->message = $_request->get('message', null);
                             $ride->time_start = $timeStart->getTimestamp();
                             $ride->address_start = $addressStart->id;
                             $ride->address_end = $addressDest->id;
                             $ride->generateRideToken();
                             try {
                                 $ride->save();
                                 Yii::$app->session->addFlash('home_notification', 'Invitation has been sent successfully');
                                 return $this->goHome();
                             } catch (\Exception $ex) {
                                 $errorMsg = "Failed to create new invitation, try again later";
                             }
                         }
                     }
                 }
             }
         }
         if ($errorMsg) {
             Yii::$app->session->addFlash('invite_error', $errorMsg, false);
         }
     }
     $_addresses = [];
     $addresses = new Query();
     //$addresses = $addresses->select('adr.id as id, adr.address as address')->from('addresses adr')->all();
     $addresses = $addresses->select('adr.id as id, adr.address as address')->from('addresses adr')->leftJoin('rel_user_address rua', 'rua.address_id = adr.id')->leftJoin('users u', 'u.id = rua.user_id')->where('u.id = :uid', [':uid' => $_currUser->id])->all();
     foreach ($addresses as $address) {
         $_addresses[$address['id']] = $address['address'];
     }
     $context = ['hire' => $_request->get('hire', 0), 'customer' => $_currUser, 'driver' => $_driver, 'address_start' => $_request->get('address_start', null), 'address_dest' => $_request->get('address_dest', null), 'addresses' => json_encode($_addresses), 'time_start' => $_request->get('time_start', null), 'message' => $_request->get('message', null)];
     return $this->render('invitation', ['context' => $context, 'history' => []]);
 }
コード例 #3
0
ファイル: User.php プロジェクト: alexletsu/kiytes
 public function beforeSave($insert)
 {
     $blnMakeSave = parent::beforeSave($insert);
     /** @TODO: add checking on profile completion **/
     if (!$insert) {
         $blnIsComplete = true;
         $rua = RelUserAddress::findOne(['user_id' => $this->id]);
         if (!$rua || 0 == strlen($this->first_name) || 0 == strlen($this->last_name) || 0 == strlen($this->email) || 0 == strlen($this->phone) || 0 == $this->phone_verified || 0 == strlen($this->photo) || 0 == strlen($this->ccard_info)) {
             $blnIsComplete = false;
         }
         if ($blnIsComplete && User::$_TYPE_DRIVER === $this->user_type) {
             $_car = Car::findOne(['driver_id' => $this->id]);
             if (0 == strlen($this->license_photo) || 0 == strlen($this->insurance_photo) || !$_car || 0 == strlen($_car->make) || 0 == strlen($_car->model) || 0 == strlen($_car->year) || 0 == strlen($_car->license_plate) || 0 == strlen($_car->car_photo) || 0 == strlen($_car->price_mile)) {
                 $blnIsComplete = false;
             }
         }
         $this->is_complete = $blnIsComplete ? 1 : 0;
     }
     return $blnMakeSave;
 }