<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.notifications.global");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	$a["containerHTML"] = null;
	$mQuery = $systemExecution->runQuery("SELECT * FROM web_notifications WHERE user_id = '%s' ORDER BY id DESC LIMIT 10", $systemHandler->loadHandler("USER")->mUserId);
	$mCount = 0;
	while($mRow = $mQuery->fetch_assoc()) {
		
		switch($mRow["notification_type"]) {
			case 0: // CONTAINER LIKED
				$mSenderUser = new systemUSER($mRow["sender_user"]);
				$a["containerHTML"] .= '<li>
								<a href="javascript:;">
									<div class="row black">
										<div class="col-md-2 col-xs-2">
											<img src="'.$mSenderUser->mProfilePicture.'" class="notificationPicture thumbnail" />
										</div>
										<div class="col-md-10 col-xs-10 textWrap">
											<b>'.$mSenderUser->mFullname.'</b> '.$l->getValue("likedyourentry").'
												<br />
											<b style="font-size: 11px"><i class="glyphicon glyphicon-thumbs-up"></i> '.$systemCore->timeAgo($mRow["submit_date"]).'</b>
										</div>
									</div>
								</a>
							</li>';
				$mCount++;
			break;
			
			case 1: // COMMENT LIKED
				$mSenderUser = new systemUSER($mRow["sender_user"]);
				$mCommentId = $mRow["comment_id"];
				$mCommentText = $systemExecution->runQuery("SELECT comment_text FROM web_comments WHERE id = '%s'", $mCommentId)->fetch_object()->comment_text;
				$mCommentBeginning = strlen($mCommentText) > 35 ? substr($mCommentText, 0, 35).'..' : $mCommentText;
				$a["containerHTML"] .= '<li>
								<a href="javascript:;">
									<div class="row black">
										<div class="col-md-2 col-xs-2">
											<img src="'.$mSenderUser->mProfilePicture.'" class="notificationPicture thumbnail" />
										</div>
										<div class="col-md-10 col-xs-10 textWrap">
											<b>'.$mSenderUser->mFullname.'</b> '.$l->getValue("likedcomment").' „<b>'.htmlspecialchars($mCommentBeginning).'</b>“
												<br />
											<b style="font-size: 11px"><i class="glyphicon glyphicon-thumbs-up"></i> '.$systemCore->timeAgo($mRow["submit_date"]).'</b>
										</div>
									</div>
								</a>
							</li>';
				$mCount++;
			break;
							
			case 2: // PICTURE LIKED
				$mSenderUser = new systemUSER($mRow["sender_user"]);
				$mImageHash = "system.uploads/".$systemExecution->runQuery("SELECT image_hash FROM web_images WHERE id = '%s'", $mRow["picture_id"])->fetch_object()->image_hash;
				$mAlbumName = $systemExecution->runQuery("SELECT album_name FROM web_albums WHERE id = '%s'", $systemExecution->runQuery("SELECT image_album_id FROM web_images WHERE id = '%s'", $mRow["picture_id"])->fetch_object()->image_album_id)->fetch_object()->album_name;
				$a["containerHTML"] .= '<li>
								<a href="javascript:;">
									<div class="row black">
										<div class="col-md-2 col-xs-2">
											<img src="'.$mImageHash.'" class="notificationPicture thumbnail" />
										</div>
										<div class="col-md-10 col-xs-10 textWrap">
											<b>'.$mSenderUser->mFullname.'</b> '.$l->getValue("likedpicture").' <b>'.htmlspecialchars($mAlbumName).'</b>.
												<br />
											<b style="font-size: 11px"><i class="glyphicon glyphicon-thumbs-up"></i> '.$systemCore->timeAgo($mRow["submit_date"]).'</b>
										</div>
									</div>
								</a>
							</li>';
				$mCount++;
			break;
			
			case 3: // NEW COMMENT
				$mSenderUser = new systemUSER($mRow["sender_user"]);
				$mContainerType = "Beitrag"; // TODO: Check container
				$mCommentId = $mRow["comment_id"];
				$mCommentText = $systemExecution->runQuery("SELECT comment_text FROM web_comments WHERE id = '%s'", $mCommentId)->fetch_object()->comment_text;
				$mCommentBeginning = strlen($mCommentText) > 35 ? substr($mCommentText, 0, 35).'..' : $mCommentText;
				$a["containerHTML"] .= '<li>
								<a href="javascript:;">
									<div class="row black">
										<div class="col-md-2 col-xs-2">
											<img src="'.$mSenderUser->mProfilePicture.'" class="notificationPicture thumbnail" />
										</div>
										<div class="col-md-10 col-xs-10 textWrap">
											<b>'.$mSenderUser->mFullname.'</b> '.$l->getValue("hascommentedyour").' '.$mContainerType.' <b>'.htmlspecialchars($mCommentBeginning).'</b>“</b>.
												<br />
											<b style="font-size: 11px"><i class="glyphicon glyphicon-comment"></i> '.$systemCore->timeAgo($mRow["submit_date"]).'</b>
										</div>
									</div>
								</a>
							</li>';
				$mCount++;
			break;
		}
	}
	
	if($mCount == 0)
		$a["containerHTML"] .= "<li><center>".$l->getValue("nonotifications")."</center></li>";
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>