Skip to content

Commit

Permalink
Implement summary on home page
Browse files Browse the repository at this point in the history
  • Loading branch information
vvye committed Aug 13, 2018
1 parent 659d977 commit 34324bf
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 23 deletions.
4 changes: 2 additions & 2 deletions css/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ button.full-size,
margin-top: 2rem;
}

.register form {
.home-panel form {
margin: 2rem 0;
}

.register table {
.home-panel table {
margin: auto;
}

Expand Down
21 changes: 18 additions & 3 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ p {
width: 50%;
}

.column:first-child .content {
.column:first-child .column-content {
padding-right: 1rem;
}

.column:last-child .content {
.column:last-child .column-content {
padding-left: 1rem;
}

Expand Down Expand Up @@ -138,10 +138,25 @@ p {
color: #4a2;
}

.register h3 {
.home-panel h3 {
font-size: 1.8rem;
text-align: center;
font-weight: normal;
margin-bottom: 1rem;
}

.home-panel .content {
padding-left: 6rem;
}

.home-panel ul {
margin-left: 1rem;
}

.home-panel .avatar.centered {
float: none;
display: block;
margin: 1rem auto 0;
}

footer {
Expand Down
60 changes: 58 additions & 2 deletions inc/functions/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ function getNumMessages()
{
global $database;

return $database->count('messages', 'id', [
return (int)($database->count('messages', 'id', [
'AND' => [
'parent' => null,
'deleted' => 0
]
]);
]));
}


Expand Down Expand Up @@ -174,4 +174,60 @@ function deleteMessage($id)
])->rowCount();

return $rowCount === 1;
}


function getNumNewMessages()
{
global $database;

return (int)($database->count('messages', 'id', [
'AND' => [
'parent' => null,
'post_time[>]' => $_SESSION['lastActivityTime']
]
]));
}


function getNumNewReplies()
{
global $database;

return (int)($database->count('messages', [
'[>]messages(parents)' => ['parent' => 'id']
], 'messages.id', [
'AND' => [
'parents.post_time[>]' => $_SESSION['lastActivityTime'],
'messages.post_time[>]' => $_SESSION['lastActivityTime']
]
]));
}


function getNumNewAddressings()
{
global $database;

return (int)($database->count('messages', 'id', [
'AND' => [
'addressee' => $_SESSION['userId'],
'post_time[>]' => $_SESSION['lastActivityTime']
]
]));
}


function getNumNewRepliesToUser()
{
global $database;

return (int)($database->count('messages', [
'[>]messages(parents)' => ['parent' => 'id']
], 'messages.id', [
'AND' => [
'parents.user' => $_SESSION['userId'],
'messages.post_time[>]' => $_SESSION['lastActivityTime']
]
]));
}
10 changes: 8 additions & 2 deletions inc/functions/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function doLogin()
'id',
'first_name',
'last_name',
'password'
'password',
'last_activity_time'
], [
'email' => $givenEmail
]);
Expand All @@ -30,6 +31,7 @@ function doLogin()
$_SESSION['firstName'] = $user['first_name'];
$_SESSION['lastName'] = $user['last_name'];
$_SESSION['loggedIn'] = true;
$_SESSION['lastActivityTime'] = $user['last_activity_time'];

$_SESSION['csrfToken'] = renewCsrfToken();

Expand Down Expand Up @@ -122,8 +124,12 @@ function updateLastActivityTime()
return;
}

$now = time();

$_SESSION['lastActivityTime'] = $now;

$database->update('users', [
'last_activity_time' => time()
'last_activity_time' => $now
], [
'id' => $_SESSION['userId']
]);
Expand Down
19 changes: 17 additions & 2 deletions inc/pages/home.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

require_once __DIR__ . '/../functions/form.php';
require_once __DIR__ . '/../functions/messages.php';
require_once __DIR__ . '/../functions/register.php';


if (isset($_POST['register']))
if (isset($_POST['home-panel']))
{
$registrationErrorMessages = validateRegistrationForm();

Expand All @@ -19,14 +20,28 @@
}
}

if (isLoggedIn())
{
$numNewMessages = getNumNewMessages();
$numNewReplies = getNumNewReplies();
$numNewAddressings = getNumNewAddressings();
$numNewRepliesToUser = getNumNewRepliesToUser();
}

renderTemplate('home', [
'loggedIn' => isLoggedIn(),
'displayName' => $_SESSION['firstName'] ?? '',
'hasAvatar' => hasAvatar($_SESSION['userId']),
'userId' => $_SESSION['userId'],
'loginError' => isset($_GET['login-error']),
'firstName' => getFieldValue('first-name'),
'lastName' => getFieldValue('last-name'),
'email' => getFieldValue('email'),
'password' => getFieldValue('password'),
'registrationAttempted' => isset($registrationErrorMessages),
'registrationErrorMessages' => $registrationErrorMessages ?? []
'registrationErrorMessages' => $registrationErrorMessages ?? [],
'numNewMessages' => $numNewMessages ?? '',
'numNewReplies' => $numNewReplies ?? '',
'numNewAddressings' => $numNewAddressings ?? '',
'numNewRepliesToUser' => $numNewRepliesToUser ?? ''
]);
57 changes: 53 additions & 4 deletions inc/templates/home.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="grid">

<div class="column">
<div class="content">
<div class="column-content">
<h1>simpson</h1>
<p>a <em>simp</em>le <em>soc</em>ial <em>n</em>etwork</p>
<p>Hello World! <em>simpson</em> is a minimalist social network that exists for no reason in particular.</p>
Expand All @@ -21,11 +21,58 @@
</div>

<div class="column">
<div class="content">
<div class="panel register">
<div class="column-content">
<div class="panel home-panel">

<?php if ($loggedIn): ?>
<h3>Hi <?= $displayName ?>!</h3>

<h3>Hi <a href="<?= BASE_PATH ?>/?p=profile"><?= $displayName ?></a>!</h3>

<?php if ($numNewMessages > 0 || $numNewReplies > 0 || $numNewAddressings > 0
|| $numNewRepliesToUser > 0): ?>

<?php if ($hasAvatar): ?>
<img class="avatar" src="<?= BASE_PATH ?>/img/avatars/<?= $userId ?>.png" />
<?php else: ?>
<img class="avatar" src="<?= BASE_PATH ?>/img/avatars/default.png" />
<?php endif ?>

<p>Here's what happened since your last visit:</p>
<div class="content">
<ul>
<?php if ($numNewMessages !== 0): ?>
<li>
<strong><?= $numNewMessages ?></strong> new
message<?= $numNewMessages === 1 ? '' : 's' ?>
<?php if ($numNewReplies !== 0): ?>
with <strong><?= $numNewReplies ?></strong> new
<?= $numNewReplies === 1 ? 'reply' : 'replies' ?> in total
<?php endif ?>
</li>
<?php endif ?>
<?php if ($numNewAddressings !== 0): ?>
<li><strong><?= $numNewAddressings ?></strong> of them are for you!</li>
<?php endif ?>
<?php if ($numNewRepliesToUser !== 0): ?>
<li><strong><?= $numNewRepliesToUser ?></strong> new replies to your messages</li>
<?php endif ?>
</ul>
</div>

<?php else: ?>

<?php if ($hasAvatar): ?>
<img class="avatar centered" src="<?= BASE_PATH ?>/img/avatars/<?= $userId ?>.png" />
<?php else: ?>
<img class="avatar centered" src="<?= BASE_PATH ?>/img/avatars/default.png" />
<?php endif ?>

<?php endif ?>

<div class="clearfix"></div>

<?php else: ?>

<h3>Join the fun!</h3>
<form action="<?= BASE_PATH ?>/?p=home" method="post">
<table>
Expand Down Expand Up @@ -78,7 +125,9 @@
<?php endif ?>
</table>
</form>

<?php endif ?>

</div>
</div>
</div>
Expand Down
8 changes: 2 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

$database = getDatabase();

updateLastActivityTime();

?><!doctype html>
<html lang="en">
<head>
Expand All @@ -37,17 +35,14 @@

<header>
<?php

renderMenu();

?>
</header>

<div class="main">
<?php

renderCurrentPage();

updateLastActivityTime();
?>
</div>

Expand All @@ -60,4 +55,5 @@
<script type="text/javascript" src="<?= BASE_PATH ?>/js/main.js"></script>

</body>

</html>
4 changes: 2 additions & 2 deletions js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const replyToggleButtons = document.querySelectorAll('.reply-toggle');
writeMessageButton.onclick = function () {
if (messageForm.style.display === 'none') {
messageForm.style.display = 'block';
this.innerHTML = 'Hide message-form';
this.innerHTML = 'Hide editor';
this.classList.remove('primary');
postMessageButton.classList.add('primary');
successAlert.parentNode.removeChild(successAlert);
Expand Down Expand Up @@ -39,7 +39,7 @@ for (let button of replyButtons) {
}
if (replyForm.style.display === 'none') {
replyForm.style.display = 'block';
this.innerHTML = 'Hide message-form';
this.innerHTML = 'Hide editor';
} else {
replyForm.style.display = 'none';
this.innerHTML = 'Reply';
Expand Down

0 comments on commit 34324bf

Please sign in to comment.