/**
  * @param UserId     $uid
  * @param ETokenType $type
  */
 public static function getUserTokensByType(UserId $uid, ETokenType $type)
 {
     $tokens = [];
     $tkns = UserTokens::findAll(['user_id' => $uid->getId(), 'token_type' => $type->getValue()]);
     foreach ($tkns as $var) {
         $tokenType = ETokenType::search($var->token_type);
         $token = new Token($var->token, ETokenType::$tokenType(), new UserId($var->user_id), $var->token_expiration_date);
         $tokens[] = $token;
     }
     return $tokens;
 }
 public static function getUserEvents(UserId $uid, $sort = true)
 {
     if ($sort) {
         $e = Event::find()->where(['event_owner' => $uid->getId()])->orderBy(['date' => SORT_DESC])->all();
     } else {
         $e = Event::findAll(['event_owner' => $uid->getId()]);
     }
     $events = [];
     foreach ($e as $event) {
         $type = EEvent::search($event->event_type);
         $uconnected = $event->event_user_connected == null ? null : new UserId($event->event_user_connected);
         $ev = new UserEvent($event->event_id, EEvent::$type(), $uid, new \DateTime($event->date), $uconnected, $event->event_data_connected);
         $events[] = $ev;
     }
     return $events;
 }
 public function getUserData()
 {
     $id = Yii::$app->user->getId();
     $uid = null;
     try {
         $uid = new UserId($id);
     } catch (InvalidUserException $e) {
         Yii::$app->session->setFlash("error", "User does not exists");
         Yii::$app->user->logout(true);
     }
     $user = $uid->getUser();
     $this->view->params['userInfo'] = $user;
     ////////////////////////////////////////////////////// request service
     $notification = RequestService::getMyRequests($uid);
     $this->view->params['notification_data'] = $notification;
     $this->view->params['notification_count'] = count($notification);
 }
 public function actionAboutedit()
 {
     $id = Yii::$app->user->getId();
     $uid = new components\UserId($id);
     ////////////////////////////
     $loggedUser = $uid->getUser();
     if (Yii::$app->request->isPost) {
         $loggedUser->setCity(Yii::$app->request->post('inputLocation'));
         $loggedUser->setEducation(Yii::$app->request->post('inputEducation'));
         $loggedUser->setAbout(Yii::$app->request->post('inputNotes'));
         try {
             $bdate = Yii::$app->request->post('inputDate');
             if (strtotime($bdate) - time() > 0) {
                 Yii::$app->session->setFlash('error', 'Hello! It\'s date from future!');
                 return $this->redirect('/profile/aboutedit');
             }
             $loggedUser->setBirthDate(new \DateTime($bdate));
             UserService::saveUser($loggedUser);
         } catch (\common\components\exceptions\InvalidDateException $e) {
             Yii::$app->session->setFlash('error', 'Invalid date');
             return $this->redirect('/profile/aboutedit');
         }
         EventService::createEvent(components\EEvent::ACCOUNT_INFO_CHANGED(), $uid);
         Yii::$app->session->setFlash('success', 'Profile\'s been Succesfuly Updated');
         return $this->redirect('/profile');
     }
     $this->getUserData();
     // refresh
     return $this->render('aboutEdit', []);
 }
 public static function createPost(UserId $receiverId, $text)
 {
     $author_id = Yii::$app->user->getId();
     $receiver_id = $receiverId->getId();
     try {
         if (!AccessService::hasAccess($receiver_id, ObjectCheckType::Post)) {
             Yii::$app->session->setFlash('error', 'Access Denied');
             return false;
         }
     } catch (Exception $ex) {
         Yii::$app->session->setFlash('warning', 'Something went wrong, contact Administrator');
         return false;
     }
     $post = new Post();
     $post->owner_id = $author_id;
     $post->user_id = $receiver_id;
     $post->post_text = $text;
     $post->post_date = date('Y-m-d H:i:s');
     $post->post_type = EPostType::text;
     $post->post_visibility = "visible";
     if ($post->save()) {
         return $post->post_id;
     } else {
         return false;
     }
 }
 public static function isRequestBetween(UserId $user1_id, UserId $user2_id, $req_type)
 {
     if (!RequestType::isValid($req_type)) {
         throw new InvalidEnumKeyException("Value: " . $req_type);
     }
     return Request::find()->where(['user1_id' => $user1_id->getId(), 'user2_id' => $user2_id->getId()])->orWhere(['user1_id' => $user2_id->getId(), 'user2_id' => $user1_id->getId()])->exists();
 }
 public static function isUserAccountActivated(UserId $uid)
 {
     $user = User::findOne(['id' => $uid->getId()]);
     return $user->status == EAccountStatus::STATUS_ACTIVE && $user->status != EAccountStatus::STATUS_NOTACTIVATED;
 }
 /**
  * Do not use that function (Only for setRelation purposes)
  */
 private static function saveRelation(UserId $user1_id, UserId $user2_id, $rel_type)
 {
     $relation = new Relationship();
     $relation->user1_id = $user1_id->getId();
     $relation->user2_id = $user2_id->getId();
     $relation->relation_type = $rel_type;
     if ($relation->save()) {
         return true;
     } else {
         return false;
     }
 }