<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.timeline.fetch");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	$systemSession = $systemHandler->loadHandler("SESSION");
	$systemSmilies = $systemHandler->loadHandler("SMILIES");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mProfileUserId = isset($_POST["userId"]) ? $p["userId"] : -1;
	$mIsTimeline = $mProfileUserId == "timeline";
	$mOwnUser = $systemHandler->loadHandler("USER")->mUserId;
	
	$mCurrentPosition = $p["currentPosition"];
	$a["containerHTML"] = null;
	
	if(!$mIsTimeline) {
		$mQuery = $systemExecution->runQuery("SELECT web_timeline.post_user_id, web_timeline.post_text, web_timeline.post_location, web_timeline.post_privacy, web_timeline.post_hashtags_list, web_timeline.post_mentions_list, web_timeline.post_submit_date, web_timeline.post_image_id, web_timeline.id, web_timeline.post_share_id, web_timeline.post_entry_location FROM web_timeline WHERE web_timeline.post_entry_location = '%s' ORDER BY web_timeline.id DESC LIMIT %s,15", array($mProfileUserId !== -1 ? $mProfileUserId : "{user.id}", $mCurrentPosition));
	}
	else {
		$mQuery = $systemExecution->runQuery("SELECT web_timeline.post_user_id, web_timeline.post_text, web_timeline.post_location, web_timeline.post_privacy, web_timeline.post_hashtags_list, web_timeline.post_mentions_list, web_timeline.post_submit_date, web_timeline.post_image_id, web_timeline.id, web_timeline.post_share_id, web_timeline.post_entry_location FROM web_timeline JOIN web_friends WHERE web_timeline.post_user_id = '%s' OR (web_timeline.post_privacy = '1' OR (web_timeline.post_privacy = '2' AND ((web_friends.user_one_id = web_timeline.post_user_id AND web_friends.user_two_id = '%s') OR (web_friends.user_one_id = '%s' AND web_friends.user_two_id = web_timeline.post_user_id))) OR (web_timeline.post_privacy = '3' AND web_timeline.post_user_id = '%s')) ORDER BY id DESC LIMIT %s,50", array($mOwnUser, $mOwnUser, $mOwnUser, $mOwnUser, $mCurrentPosition));
	}
		
	$mCollection = array();
	$mCounter = 0;
	
	while($mRow = $mQuery->fetch_array()) {
		$containerImageId = $mRow[7];
		$containerImageSet = $containerImageId !== "-1";
		$containerImageAlbum = -1;
		$containerImageHash = -1;
		$containerImagePath = null;
		
		if($containerImageSet) {
			$mAssociativeStorage = $systemExecution->runQuery("SELECT * FROM web_images WHERE id = '%s'", $containerImageId)->fetch_assoc();
			$containerImageAlbum = $mAssociativeStorage["image_album_id"];
			$containerImageHash = $mAssociativeStorage["image_hash"];
			$containerImagePath = vsprintf("system.uploads/%s", $containerImageHash);
		}
		
		$containerPostId = $mRow[0];
		$containerText = $mRow[1];
		$containerLocation = $mRow[2];
		$containerPrivacy = $mRow[3];
		$containerHashtags = $mRow[4];
		$containerMentions = $mRow[5];
		$containerSubmitDate = $mRow[6];
		$containerId = $mRow[8];
		$containerLocationSet = strlen($containerLocation) > 0;
		
		if(strlen($containerHashtags) > 0) {
			$mSplittedHashtags = explode(',', $containerHashtags);
			$containerHashtags = null;
			foreach($mSplittedHashtags as $mHashtag) {
				$containerHashtags .= "<span class=\"label label-primary\">#".htmlspecialchars($mHashtag)."</span> ";
			}
		}
		
		$mMentionsCount = 0;
		if(strlen($containerMentions) > 0) {
			$mSplittedMentions = explode(',', $containerMentions);
			$containerMentions = null;
			foreach($mSplittedMentions as $mUserId) {
				if($mMentionsCount == 6 && count($mSplittedMentions) > 7) {
					$containerMentions .= "<a data-event=\"display-mentions\" data-parameter=\"".$containerId."\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"".$l->getValue("showall")."\" class=\"mentionUser displayMentionsModal\" href=\"javascript:;\"><img src=\"system.assets/images/display_more.png\" /></a>";
					break;
				} else {
					$mUser = new systemUSER($mUserId);
					$containerMentions .= "<a data-toggle=\"tooltip\" data-placement=\"top\" title=\"".$mUser->mFullname."\" class=\"mentionUser\" href=\"page.profile.php?profile-id=".$mUser->mUserId."\"><img src=\"".$mUser->mProfilePicture."\" /></a>";
					$mMentionsCount++;
				}
			}
		}
		
		$containerLikeCount = $systemExecution->runQuery("SELECT COUNT(id) AS likeCount FROM web_likes WHERE container_id = '%s'", $containerId)->fetch_object()->likeCount;
		$containerLikeCountOwn = $systemExecution->runQuery("SELECT COUNT(id) AS likeCount FROM web_likes WHERE container_id = '%s' AND user_id = '%s'", array($containerId, $systemSession->loadSession("USER")))->fetch_object()->likeCount;
		$containerLike = $containerLikeCountOwn == 0 ? "<i class=\"glyphicon glyphicon-thumbs-up\"></i> <span class=\"\">".$l->getValue("like")."</span>" : "<i class=\"glyphicon glyphicon-thumbs-up\"></i> <span class=\"\">".$l->getValue("unlike")."</span>";
		
		$containerPostUser = new systemUSER($containerPostId);
		
		$containerCommentsCount = $systemExecution->runQuery("SELECT COUNT(id) AS commentsCount FROM web_comments WHERE container_id = '%s'", $containerId)->fetch_object()->commentsCount;
		
		$containerShareSet = $mRow[9] !== "-1";
		$containerShareId = $mRow[9];
		
		$containerExcludedTimelineSet = $mRow[10] !== $containerPostUser->mUserId;
		$containerExcludedUser = new systemUSER($mRow[10]);
		$containerExcludedText = vsprintf("<strong><a href=\"page.profile.php?profile-id=%s\">%s</a></strong> wrote on <strong><a href=\"page.profile.php?profile-id=%s\">%s</a></strong>'s timeline", array($containerPostUser->mUserId, $containerPostUser->mFullname, $containerExcludedUser->mUserId, $containerExcludedUser->mFullname));
		
		if($containerShareSet)
			$containerShareUser = new systemUSER($containerShareId);
		
		if(!array_key_exists($containerId, $mCollection)) {
			$mOkayGiven = true;
			$mProfileOwnUserId = $systemHandler->loadHandler("USER")->mUserId;
			
			if($mOkayGiven) {
				$mCollection[$containerId] = "<li container-id=\"".$containerId."\" style=\"z-index: 1\" ".($mCounter % 2 == 0 ? "class=\"timeline-inverted\"" : null).">
									<div class=\"timeline-panel\">
										".($containerShareSet ? "
										<div class=\"timeline-heading shareTitle\">
											<center><i class=\"glyphicon glyphicon-share-alt font14\"></i> <strong><a href\"page.profile.php?profile-id=".$containerPostUser->mUserId."\">".$containerPostUser->mFirstname."</a></strong> ".$l->getValue("sharedfrom")." <strong><a href=\"page.profile.php?profile-id=".$containerShareUser->mUserId."\">".$containerShareUser->mFullname."</a></strong></center>
										</div>" : null)."
										
										".($containerExcludedTimelineSet ? "
										<div class=\"timeline-heading shareTitle\">
											<center><i class=\"glyphicon glyphicon-share-alt font14\"></i> ".$containerExcludedText."</center>
										</div>" : null)."
										
										".($containerImageSet ? "
										<div class=\"timeline-heading\">
											<center><img data-event=\"display-fancy\" style=\"cursor: pointer\" data-parameter=\"".$containerImageId."\" container-id=\"".$containerId."\" class=\"img-responsive container-image thumbnail\" src=\"".$containerImagePath."\" /></center>
										</div>" : null)."
										<div container-id=\"".$containerId."\" class=\"timeline-body\">
											
											".(strlen($containerText) > 0 ? "
											<div class=\"wrap no_selection entryPadding\">".$systemSmilies->initializeSmilies($containerText).(strlen($containerMentions) > 0 ? "<br /><center>".$containerMentions."</center>" : null).(strlen($containerHashtags) > 0 ? "<div>".$containerHashtags."</div>" : null)."</div>" : null)."
											
											".($containerLocationSet ? "<div class=\"mapContainer\"><img style=\"width: 100%; height: 125px\" src=\"http://maps.googleapis.com/maps/api/staticmap?center=".$containerLocation."&zoom=13&size=409x150&maptype=roadmap\" /><div class=\"mapOverlay\"><span class=\"mapLocationText\"><i class=\"glyphicon glyphicon-map-marker textIcon\"></i> ".htmlspecialchars(strlen($containerLocation) < 35 ? $containerLocation : substr($containerLocation, 0, 35)."..")."</span></div></div>" : null)."
											
											<div class=\"containerAddons\">
												<i class=\"glyphicon glyphicon-".($containerPrivacy == 1 ? "globe" : ($containerPrivacy == 2 ? "user" : "lock"))."\"></i> · ".$systemCore->timeAgo($containerSubmitDate)."
												".($containerPostUser->mUserId == $systemHandler->loadHandler("USER")->mUserId ? "<a href=\"javascript:;\" class=\"remove-container\" container-id=\"".$containerId."\">
													<i class=\"glyphicon glyphicon-remove\"></i>
												</a>" : null)."
											</div>
										</div>
										<div class=\"panel-footer\" style=\"border-bottom-right-radius: 0px; border-bottom-left-radius: 0px\">
											<div class=\"row\">
												<div class=\"timelineInfoLeft\">
												
													<a container-id=\"".$containerId."\" class=\"bottomLine display-likes\" display-likes-type=\"container\" href=\"javascript:;\" style=\"margin-left: 15px\">
														<i style=\"font-size: 12px\" class=\"glyphicon glyphicon-thumbs-up\"></i>
														<strong id=\"container-like-count-".$containerId."\">
															".$containerLikeCount."
														</strong>
													</a>
													
													<a href=\"javascript:;\" class=\"bottomLine timeline-comment-event\" container-id=\"".$containerId."\">
														<i style=\"font-size: 12px; margin-left: 10px\" class=\"glyphicon glyphicon-comment\"></i>
														 <strong id=\"container-comment-count-".$containerId."\">".$containerCommentsCount."</strong>
													</a>
												
												</div>
												
												<div class=\"timelineInfoRight\">
															<a class=\"hidden-xs\" href=\"page.profile.php?profile-id=".$containerPostUser->mUserId."\"><img class=\"timeline-profilepicture\" src=\"".$containerPostUser->mProfilePicture."\" /> </a>
															<a href=\"page.profile.php?profile-id=".$containerPostUser->mUserId."\" class=\"profilePopOver\" profile-popover-id=\"1\">
																<div class=\"profilePopOverParent\">
																	<strong>".$containerPostUser->mFullname."</strong>
																</div>
																<div class=\"sendToProfileLink\">
																	<a href=\"page.profile.php?profile-id=".$containerPostUser->mUserId."\">
																		".$l->getValue("goprofile")." &raquo;
																	</a>
																</div>
															</a>
												</div>
											</div>
										</div>
										<div class=\"panel-footer timelineEventsContent\" style=\"border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; font-size: 14px\">
											<a href=\"javascript:;\" id=\"timeline-like-event-".$containerId."\" container-id=\"".$containerId."\" class=\"bottomLine timeline-like-event moveLeft\">
												".$containerLike."
											</a>
											
											<a href=\"javascript:;\" container-id=\"".$containerId."\" class=\"bottomLine timeline-comment-event moveLeft\">
												<i class=\"glyphicon glyphicon-comment\"></i> <span class=\"\">".$l->getValue("comment")."</span>
											</a>
											
											<a href=\"javascript:;\" container-id=\"".$containerId."\" class=\"bottomLine timeline-share-event moveLeft\">
												<i class=\"glyphicon glyphicon-share-alt\"></i> <span class=\"\">".$l->getValue("share")."</span>
											</a>
											
											<div class=\"containerIcons\">
												<i data-event=\"report\" data-type=\"container\" data-parameter=\"".$containerId."\" class=\"glyphicon glyphicon-bullhorn\"></i>
											</div>
										</div>
										<div class=\"commentsField\" id=\"commentsFields-".$containerId."\"></div>
										<div class=\"panel-footer\" id=\"panel-footer-".$containerId."\"><input type=\"text\" class=\"form-control submitCommentEvent\" container-id=\"".$containerId."\" /></div>
									</div>
								</li>".(strlen($systemHandler->loadHandler("CONFIG")->mAds[0]) > 0 ? ($mCounter % 2 == 0 ? "<li>".$systemHandler->loadHandler("CONFIG")->mAds[0]."</li>" : ($mCounter % 5 == 0 ? "<li class=\"timeline-inverted\">".$systemHandler->loadHandler("CONFIG")->mAds[0]."</li>" : null)) : null);
				$mCounter++;
			}
		}
	}
	
	foreach($mCollection as $mData)
		$a["containerHTML"] .= $mData;
	
	$a["containerHTML"] .= "<li class=\"clearfix\" style=\"float: none\"></li>";
	$a["containerCount"] = $mCounter;
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
	?>