/**
  * @param $id
  *
  * @return IntouchUser
  */
 public static function getUserById(UserId $uid)
 {
     $id = $uid->getId();
     $u = User::findOne($id);
     if (!is_null($u)) {
         $details = UserInfo::findOne($id);
         $intouchUser = new IntouchUser($id, $u->email, $u->username, $details->user_name, $details->user_surname, new \DateTime($details->user_birthdate), PhotoService::getProfilePhoto($id), $details->user_education, $details->user_city, $details->user_about);
         return $intouchUser;
     } else {
         return null;
     }
 }
 public function __construct($post_id)
 {
     $this->post_id = $post_id;
     $file_name = PostsService::getPostAttachmentPhoto($post_id);
     if (!$file_name) {
         $this->is_attachment = false;
     } else {
         $this->is_attachment = true;
         foreach ($file_name as $file) {
             $tmp_file = PhotoService::getPostAttachmentPhoto($file['file']);
             array_push($this->file, $tmp_file);
         }
     }
 }
 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 actionProfile()
 {
     $id = Yii::$app->user->getId();
     $uid = new components\UserId($id);
     $lUser = $uid->getUser();
     if (Yii::$app->request->isPost || Yii::$app->request->isPjax) {
         if (!is_null(Yii::$app->request->post('type'))) {
             switch (Yii::$app->request->post('type')) {
                 case 'settings':
                     //To upload profile photo
                     $plik = $_FILES['exampleInputFile']['tmp_name'];
                     if (strlen($plik) > 0) {
                         \common\components\PhotoService::setProfilePhoto($id, $plik);
                     }
                     $lUser->setName(Yii::$app->request->post('inputName'));
                     $lUser->setSurname(Yii::$app->request->post('inputSurname'));
                     $lUser->setEmail(Yii::$app->request->post('inputEmail'));
                     UserService::saveUser($lUser);
                     $pass1cnt = strlen(Yii::$app->request->post('inputPassword'));
                     $pass2cnt = strlen(Yii::$app->request->post('inputPasswordRepeat'));
                     if ($pass1cnt > 0 || $pass2cnt > 0) {
                         if ($pass1cnt != $pass2cnt) {
                             Yii::$app->session->setFlash('error', 'Passwords not match. Password\'s has not been changed');
                             return $this->redirect('/profile');
                         }
                         if ($pass1cnt < 6) {
                             Yii::$app->session->setFlash('error', 'Password is too short');
                             return $this->redirect('/profile');
                         }
                         EventService::createEvent(components\EEvent::ACCOUNT_PASSWORD_CHANGED(), $uid);
                     }
                     ////////////////////
                     Yii::$app->session->setFlash('success', 'Profile\'s been succesfuly updated');
                     break;
                 case 'newpost':
                     $pliks = $_FILES['kawaiiPicture']['tmp_name'];
                     $post_id = PostsService::createPost($uid, Yii::$app->request->post('inputText'));
                     if ($pliks[0] != '') {
                         PhotoService::addPostAttachmentPhoto($pliks, $post_id);
                     }
                     EventService::createEvent(components\EEvent::POST_CREATE(), $uid);
                     break;
                 case 'newcomment':
                     PostsService::createComment(PostsService::getPostById(Yii::$app->request->post('post_id')), Yii::$app->request->post('inputText'));
                     EventService::createEvent(components\EEvent::COMMENT_CREATE(), $uid);
                     break;
                 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);
                         EventService::createEvent(components\EEvent::POST_LIKED(), $uid);
                     } else {
                         EventService::createEvent(components\EEvent::POST_UNLIKED(), $uid);
                         ScoreService::revokeScore($found_score_id);
                     }
                     break;
                 case 'report':
                     $rep_form_post_id = Yii::$app->request->post('post_id');
                     $rep_form_score_elem = Yii::$app->request->post('score_elem');
                     $rep_form_user_id = Yii::$app->request->post('user_id');
                     $score = new components\Score(EScoreType::like(), null, EScoreElem::$rep_form_score_elem(), $rep_form_post_id, new components\UserId($rep_form_user_id));
                     ScoreService::addScore($score);
                     break;
                 case 'delete_post':
                     $rep_post_id = Yii::$app->request->post('post_id');
                     PostsService::deletePost(PostsService::getPostById($rep_post_id));
                     EventService::createEvent(components\EEvent::POST_DELETE(), $uid);
                     break;
                 case 'delete_comment':
                     $rep_comment_id = Yii::$app->request->post('comment_id');
                     PostsService::deleteComment(PostsService::getCommentById($rep_comment_id));
                     EventService::createEvent(components\EEvent::COMMENT_DELETE(), $uid);
                     break;
             }
         }
     }
     $posts = PostsService::getUserPosts($uid);
     $followers = count(RelationService::getUsersWhoFollowMe($uid));
     $following = count(RelationService::getUsersWhoIFollow($uid));
     $friends = count(RelationService::getFriendsList($uid));
     $timeline = components\EventService::getUserEvents($uid, true);
     //////////////////////////////////////////////////////////////////////////
     return $this->render('profile', ['userinfo' => $uid->getUser(), 'posts' => $posts, 'followers' => $followers, 'following' => $following, 'friends' => $friends, 'loggedUser' => $lUser, 'timeline' => $timeline]);
 }
                            <!-- User Account: style can be found in dropdown.less -->
                            <li class="dropdown user user-menu">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                   
                                   <i class="fa fa-sign-in"></i>
                                    <span class="hidden-xs"><?php 
echo Yii::t('app', 'Sign in');
?>
</span>
                                </a>
                                <ul class="dropdown-menu">
                                    <!-- User image -->
                                    <li class="user-header">
                                        <?php 
echo Html::img(\common\components\PhotoService::getProfilePhoto(0), ['class' => "img-circle"]);
?>
                                        <p>
                                            <?php 
echo Yii::t('app', "You're not signed in.");
?>
                                        </p>
                                    </li>

                                    <!-- Menu Footer-->
                                    <li class="user-footer">
                                        <div class="pull-left">
                                            <a href="/register" class="btn btn-default btn-flat"><?php 
echo Yii::t('app', 'Register');
?>
</a>
				</a>
				<!-- Header Navbar: style can be found in header.less -->
				<nav class="navbar navbar-static-top" role="navigation">
					<!-- Sidebar toggle button-->
					<a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
						<span class="sr-only">Toggle navigation</span>
					</a>
					<div class="navbar-custom-menu">
						<ul class="nav navbar-nav">
							
							<!-- User Account: style can be found in dropdown.less -->
							<li class="dropdown user user-menu">
								<a href="#" class="dropdown-toggle" data-toggle="dropdown">
									
									<!-- <?php 
echo Html::img(\common\components\PhotoService::getProfilePhoto(0), ['class' => "user-image"]);
?>
-->
									<i class="fa fa-sign-in"></i>
									<span class="hidden-xs"><?php 
echo Yii::t('app', 'Account');
?>
</span>
								</a>
								<ul class="dropdown-menu">
									<!-- User image -->
									
									<li class="user-header">
										
										<?php 
echo Html::img($userInfo->getImageUrl(), ['class' => 'img-circle', 'alt' => 'User Image']);