<?php
require_once 'mipiace.php';
include('commenti.php');
if (isset($_POST['action'])) {
$userid = $_SESSION['user'];
$photo_id = $_POST['photo_id'];
$action = $_POST['action'];
if ($action == 'like') {
add_like($userid, $photo_id);
}
elseif ($action == 'dislike') {
remove_like($userid, $photo_id);
}
}
if(isset($_POST['photo_id'])){
$userid = $_SESSION['user'];
$photo_id = $_POST['photo_id'];
$commento = $_POST['commenta'];
echo "Eseguo la query";
commenta($user_id, $photo_id, $commento);
}
function _ago($tm,$rcs = 0) {
$cur_tm = time();
$dif = $cur_tm-$tm;
$pds = array('secondo','minuto','ora','giorn','settimana','mese','anno');
$pdsp = array('secondi', 'minuti', 'ore', 'giorni', 'settimane', 'mesi', 'anni');
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
$no = floor($no);
if($no <> 1)
$pds[$v] = $pdsp[$v];
$x = sprintf("%d %s ",$no,$pds[$v]);
if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0))
$x .= time_ago($_tm);
$x .= " fa";
return $x;
}
function get_photos($followed) {
$photos = array();
if (empty($followed)) {
return $photos;
}
$clauses = array();
foreach ($followed as $user_id) {
$clauses[] = "photo.userid=" . $user_id;
}
$query = "SELECT * FROM photo INNER JOIN users ON photo.userid = users.userId WHERE ";
$query .= implode(" OR ", $clauses);
$query .= " ORDER BY timestamp DESC LIMIT 0,30";
$result = mysql_query($query);
if (!$result) {
echo mysql_error() . " while executing: \"$query\"";
exit();
}
while ($row = mysql_fetch_assoc($result)) {
$photos[] = $row;
}
return $photos;
}
array_push($seguiti, $userid);
$photos = get_photos($seguiti);
?>
<center>
<div class="page-header">
<h2>SocialEdge</h2>
</div>
</center>
<center>
<div class="home-wrapper">
<?php
foreach ($photos as $photo):
$photo_id = $photo['id'];
$photo_userid = $photo['userid'];
$photo_username = $photo['userName'];
$photo_timestamp = $photo['timestamp'];
$timeago = _ago($photo_timestamp, 0);
$users_who_liked = photo_get_like_information($photo_id);
?>
<div class="home-foto-profilofoto-wrapper">
<a href="<?php echo $baseurl . "/" . $photo_username ?>"><img src="./core/getimg.php?profiloimg=<?php echo $photo_userid ?>" class="home-foto-profilofoto" /></a>
</div>
<div class="home-foto-username">
<a href="<?php echo $baseurl . "/" . $photo_username; ?>"><?php echo $photo_username; ?></a>
</div>
<div class="home-foto-timeago">
<?php echo $timeago ?>
</div>
<div class="home-foto-wrapper">
<img src="./core/getimg.php?img=<?php echo $photo_id ?>" class="home-foto" />
<br>
<?php if (user_likes_photo($userid, $photo_id)): ?>
<form method="POST" align="left" >
<button type="submit" class="btn btn-lg"><i class="glyphicon glyphicon-thumbs-down"></i> Non mi piace</button>
<input type="hidden" name="action" value="dislike">
<input type="hidden" name="photo_id" value="<?php echo $photo_id; ?>">
</form>
<?php else: ?>
<form method="POST" align="left">
<button type="submit" class="btn btn-lg"><i class="glyphicon glyphicon-thumbs-up"></i> Mi piace</button>
<input type="hidden" name="action" value="like">
<input type="hidden" name="photo_id" value="<?php echo $photo_id; ?>">
</form>
<?php endif; ?>
<span>La foto piace a <?php echo count($users_who_liked); ?> utenti</span>
<form action="#" method="POST">
Commento:<br />
<textarea name="commenta" cols="30" rows="10"></textarea><br />
<input name="photo_id" type="hidden" value="<?php echo $photo_id;?>">
<input name="user_id" type="hidden" value="<?php echo $user_id;?>">
<input name="reply" type="submit" value="Invia">
</form>
</div>
<?php endforeach; ?>
<?php
if (empty($photos)):
echo "<h3>Nessuna immagine da visualizzare</h3>";
endif;
?>
</div>
</center>