/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->status = User::STATUS_NOTACTIVATED; $b = $user->save(); $activationToken = new UserTokens(); $activationToken->user_id = $user->id; $activationToken->token_type = ETokenType::ACCOUNT_ACTIVATION; $activationToken->token = sha1(mt_rand(10000, 99999) . time() . $user->email); $activationToken->save(); $auth = Yii::$app->authManager; $userRole = $auth->getRole('user'); $auth->assign($userRole, $user->id); if ($b) { $x = new UserInfo(); $x->user_id = $user->id; $x->save(); EventService::createEvent(EEvent::ACCOUNT_CREATE(), new UserId($user->id)); $this->sendActivationMail($user, $activationToken->token); return $user; } } return null; }
/** * answering on request, accept or dismiss * * @param type $req_id * @param type $answer answer on request true or false. */ public static function answerRequest($req_id, $answer) { $user1_id = RequestService::getUser1Id($req_id); $user2_id = RequestService::getUser2Id($req_id); $uid1 = new UserId($user1_id); $uid2 = new UserId($user2_id); ///AccessService try { if (!AccessService::hasAccess($user2_id, ObjectCheckType::Request)) { \Yii::$app->session->setFlash('error', 'Access Denied'); return false; } } catch (Exception $ex) { Yii::$app->session->setFlash('warning', 'Something went wrong, contact Administrator'); return false; } ///end AccessService if ($answer) { RelationService::setRelation($uid1, $uid2, RelationType::Friend); RelationService::setRelation($uid1, $uid2, RelationType::Follower); // for default friend is followed RelationService::setRelation($uid2, $uid1, RelationType::Follower); EventService::createEvent(EEvent::FRIEND_REQUEST_ACCEPTED(), $uid2, true, $uid1); EventService::createEvent(EEvent::FRIEND_REQUEST_ACCEPTED(), $uid1, false, $uid2); } else { EventService::createEvent(EEvent::FRIEND_REQUEST_DENIED(), $uid2, true, $uid1); EventService::createEvent(EEvent::FRIEND_REQUEST_DENIED(), $uid1, false, $uid2); } self::dropRequest($req_id); //TODO Przemek popraw to! $check = Request::find()->select('req_id')->where(['user1_id' => $user2_id, 'user2_id' => $user1_id, 'req_type' => 'friend'])->one(); if (!is_null($check)) { self::dropRequest($check['req_id']); } }
public function actionComment($cid) { $idu = Yii::$app->user->getId(); $uid = new components\UserId($idu); $comment = PostsService::getCommentById($cid); $nothing = $comment->getAuthor(); $commentOwner = $nothing->getId(); //die(var_dump($commentOwner)); if (!Yii::$app->user->can('admin')) { if ($commentOwner != $idu) { return $this->redirect('/intouch/accessdenied'); } } if (Yii::$app->request->isPost) { try { $id = Yii::$app->request->post('comment_id'); $comment = PostsService::getCommentById($id); $comment->changeContent(Yii::$app->request->post('inputContent')); PostsService::saveComment($comment); } catch (exception $e) { } EventService::createEvent(components\EEvent::ACCOUNT_INFO_CHANGED(), $uid); Yii::$app->session->setFlash('success', 'Comment\'s been Succesfuly Updated'); return $this->redirect('/profile'); } return $this->render('comment', ['comment' => $comment]); }
public function actionView($uname) { /////////////////////////--- Profile Infos ---////////////////////////// $id = UserService::getUserIdByName($uname); if ($id === false) { throw new \yii\web\NotFoundHttpException("User cannot be found"); } $uid = new components\UserId($id); $myId = Yii::$app->user->getId(); $myUid = new components\UserId($myId); if ($id == $myId) { return $this->redirect('/profile'); } if (Yii::$app->request->isPost || Yii::$app->request->isPjax) { if (Yii::$app->user->can('relations-manage-own')) { $request = Yii::$app->request; if (!is_null($request->post('follow-btn')) && Yii::$app->user->can('relations-follow')) { RelationService::setRelation($myUid, $uid, RelationType::Follower); components\EventService::createEvent(components\EEvent::FOLLOWS(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::FOLLOWS(), $myUid, true, $uid); } if (!is_null($request->post('friend-btn')) && Yii::$app->user->can('relations-friend')) { RequestService::createRequest($myUid, $uid, RequestType::FriendRequest, date('Y-m-d H:i:s')); //to tutaj components\EventService::createEvent(components\EEvent::FRIEND_REQUEST_SENT(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::FRIEND_REQUEST_SENT(), $myUid, true, $uid); } if (!is_null($request->post('unfriend-btn'))) { RelationService::removeRelation($myUid, $uid, RelationType::Friend); components\EventService::createEvent(components\EEvent::UNFRIEND(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::UNFRIEND(), $myUid, true, $uid); } if (!is_null($request->post('unfollow-btn'))) { RelationService::removeRelation($myUid, $uid, RelationType::Follower); components\EventService::createEvent(components\EEvent::UNFOLLOWS(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::UNFOLLOWS(), $myUid, true, $uid); } if (!is_null(Yii::$app->request->post('type'))) { switch (Yii::$app->request->post('type')) { case 'newpost': $post_id = PostsService::createPost($uid, Yii::$app->request->post('inputText')); $pliks = $_FILES['kawaiiPicture']['tmp_name']; if ($pliks[0] != '') { PhotoService::addPostAttachmentPhoto($pliks, $post_id); } components\EventService::createEvent(components\EEvent::POST_CREATE(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::POST_CREATE(), $myUid, true, $uid); break; case 'newcomment': PostsService::createComment(PostsService::getPostById(Yii::$app->request->post('post_id')), Yii::$app->request->post('inputText')); components\EventService::createEvent(components\EEvent::COMMENT_CREATE(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::COMMENT_CREATE(), $myUid, true, $uid); break; case 'delete_post': $rep_post_id = Yii::$app->request->post('post_id'); PostsService::deletePost(PostsService::getPostById($rep_post_id)); components\EventService::createEvent(components\EEvent::POST_DELETE(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::POST_DELETE(), $myUid, true, $uid); break; case 'delete_comment': $rep_comment_id = Yii::$app->request->post('comment_id'); PostsService::deleteComment(PostsService::getCommentById($rep_comment_id)); components\EventService::createEvent(components\EEvent::COMMENT_DELETE(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::COMMENT_DELETE(), $myUid, true, $uid); break; } } } else { $this->redirect(["intouch/accessdenied"]); } if (!is_null(Yii::$app->request->post('type'))) { switch (Yii::$app->request->post('type')) { case 'like': $like_form_post_id = Yii::$app->request->post('post_id'); $like_form_score_elem = Yii::$app->request->post('score_elem'); $like_form_user_id = Yii::$app->request->post('user_id'); $score = new components\Score(EScoreType::like(), null, EScoreElem::$like_form_score_elem(), $like_form_post_id, new components\UserId($like_form_user_id)); $existing_scores = ScoreService::getScoresByElem(EScoreElem::post(), $like_form_post_id); $found = false; foreach ($existing_scores as $var) { $user = $var->getPublisher(); $userId = $user->getId(); if ((int) $like_form_user_id == $userId && (int) $like_form_post_id == $var->getElementId()) { $found = true; $found_score_id = $var->getScoreId(); } } if (!$found) { ScoreService::addScore($score); components\EventService::createEvent(components\EEvent::POST_LIKED(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::POST_LIKED(), $myUid, true, $uid); } else { ScoreService::revokeScore($found_score_id); components\EventService::createEvent(components\EEvent::POST_UNLIKED(), $uid, false, $myUid); components\EventService::createEvent(components\EEvent::POST_UNLIKED(), $myUid, true, $uid); } break; } } } $user = $uid->getUser(); $followers = count(RelationService::getUsersWhoFollowMe($uid)); $following = count(RelationService::getUsersWhoIFollow($uid)); $friends = count(RelationService::getFriendsList($uid)); /////$$$$$ FORMS $$$$$////////////////////////////////////////////////// ////////////////////////////--- Other stuff ---///////////////////////// $UserRelations = RelationService::getRelations($myUid, $uid); $isFriend = $UserRelations[RelationType::Friend]; if (!$isFriend) { if (RequestService::isRequestBetween($uid, $myUid, RequestType::FriendRequest)) { $isFriend = "Friend Request Sent"; } } $IFollow = $UserRelations[RelationType::Follower]; //***Do not add anything new below this line (except for the render)**** //$this->getUserData(); $posts = PostsService::getUserPosts($uid); $shared = ['user' => $user, 'followers' => $followers, 'following' => $following, 'friends' => $friends, 'UserFollowState' => $IFollow, 'UserFriendshipState' => $isFriend, 'posts' => $posts]; return $this->render('view', $shared); }
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', []); }