The following code has one of the most confusing lines I've ever wrote. I can imagine ten other way to write it but I do not know which else could be any better. So I'm here to ask you fellows how would you change this code. I feel like the context is self explaining itself here; if you need of any clarification write a comment below.
Notice:
Db()
is very similar to PDO()
, it just extends it adding few features I'm not using here.
Post::addExtra() add abstract datas elaborating database data. For example he created a $data[13] = $data['from db1'] .' with '. $data['from db2']
. These because they are going to be passed to the template.
$db = new Db();
$s = new Session();
# Default statement and parameters
$stmt =
"SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(*)
FROM Flags as f
JOIN Posts as p1
ON p1.PostPID = f.FlagPID
WHERE p1.PostPID = p.PostPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30";
$par = array();
# We change the statement if the tab is selected
if ($tab = get('tab')) {
switch ($tab) {
case 'admin':
$stmt =
"SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(*)
FROM Flags as f
JOIN Posts as p1
ON p1.PostPID = f.FlagPID
WHERE p1.PostPID = p.PostPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
WHERE p.PostUID = 1
ORDER BY PostTime DESC
LIMIT 0, 30";
break;
case 'trusted':
if ($s->isLogged()) {
$stmt =
"SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(*)
FROM Flags as f
JOIN Posts as p1
ON p1.PostPID = f.FlagPID
WHERE p1.PostPID = p.PostPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
WHERE p.PostUID IN (
SELECT TrustedUID
FROM Trust
WHERE TrusterUID = :uid
)
ORDER BY PostTime DESC
LIMIT 0, 30";
$par = array('uid' => $s->getUID());
} else {
$stmt = '';
}
break;
case 'favorite':
if ($s->isLogged()) {
$stmt =
"SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(*)
FROM Flags as f
JOIN Posts as p1
ON p1.PostPID = f.FlagPID
WHERE p1.PostPID = p.PostPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
WHERE p.PostPID IN (
SELECT FavoritePID
FROM Favorites
WHERE FavoriteUID = :uid
)
ORDER BY PostTime DESC
LIMIT 0, 30";
$par = array('uid' => $s->getUID());
} else {
$stmt = '';
}
break;
case 'top':
$weekAgo = time() - week;
$monthAgo = time() - month;
$stmt =
"SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(*)
FROM Flags as f
JOIN Posts as p1
ON p1.PostPID = f.FlagPID
WHERE p1.PostPID = p.PostPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
WHERE p.PostTime > $monthAgo
LIMIT 0, 3
UNION
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(*)
FROM Flags as f
JOIN Posts as p1
ON p1.PostPID = f.FlagPID
WHERE p1.PostPID = p.PostPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
WHERE p.PostTime > $weekAgo
ORDER BY PostFlags DESC
LIMIT 0, 30";
break;
case 'recent':
default:
break;
}
}
# Loading posts
try {
$sql = $db->prepare($stmt);
$sql->execute($par);
$posts['Data'] = $sql->fetchAll();
} catch (PDOException $e) {
throw new MyEx($e->getMessage());
}
if (count($posts['Data']) > 0) {
foreach ($posts['Data'] as &$post) {
$post = Post::addExtra($post);
}
}