public function addOnPost($iPost_id,$iUser_id){
$this->execute('DELETE FROM Likes WHERE user_id=? AND post_id=?',$iUser_id,$iPost_id);
$this->execute('INSERT INTO Likes (user_id,post_id) VALUES (?,?)',$iUser_id,$iPost_id);
}
public function countByPost($iPost_id){
$oRow = $this->findOne('SELECT count(*) as total FROM '.$this->sTable.' WHERE post_id=?',$iPost_id );
return $oRow->total;
}
<form action="" method="POST">
<input type="hidden" name="post_id" value="<?php echo $this->iPost_id?>"/>
<p style="text-align:right"><a href="#" onclick="submit();return false;"><strong><?php echo $this->iCount?></strong> J'aime</a> </p>
</form>
public function _show($iPost_id){
//on verifie si le formulaire n'a pas ete soumis
$this->processAdd($iPost_id);
//on recupere le nombre de "j'aime"
$iCount=model_Likes::getInstance()->countByPost($iPost_id);
$oView=new _view('like::show');
$oView->iCount=$iCount;
$oView->iPost_id=$iPost_id;
return $oView;
}
private function processAdd($iPost_id){
//si le formulaire n'a pas ete soumis on ne fait rien
if(!_root::getRequest()->isPost()){
return null;
}
//cette ligne c'est bien ajouter un "j'aime" sur le post et pas tous les posts
if(_root::getParam('post_id')!=$iPost_id){
return null;
}
$iUser_id=_root::getauth()->getAccount()->id;
//ajout d'un "j'aime" sur le post
model_Likes::getInstance()->addOnPost($iPost_id,$iUser_id);
}
public function _list(){
$tPosts=model_Posts::getInstance()->findListByUser($this->user_id);
$oView=new _view('Posts::list');
$oView->tPosts=$tPosts;
$oView->tIndexdUsers=model_Users::getInstance()->getListIndexed();
//instanciation du module like
$oView->oModuleLike=new module_like;
return $oView;
}
<?php echo $this->oModuleLike->_show($oPosts->id)->show()?>
<p> </p>
<?php if($this->tPosts):?>
<?php foreach($this->tPosts as $oPosts):?>
<?php $oDateTime=new plugin_datetime($oPosts->date.' '.$oPosts->time);
$iDelta= time() - $oDateTime->getMkTime();
$sDelta=null;
$iDuration=(60*60*24);
$iDay= (int)($iDelta / $iDuration );
if( $iDay ){
$sDelta = $iDay.'j ';
$iDelta -= ($iDuration * $iDay);
}
$iDuration=(60*60);
$iHour= (int)($iDelta / $iDuration );
if( $iHour ){
$sDelta .= $iHour.'h ';
$iDelta -= ($iDuration * $iHour);
}
$iDuration=(60);
$iMinute= (int)($iDelta / $iDuration );
if( $iMinute ){
$sDelta .= $iMinute.'m ';
$iDelta -= ($iDuration * $iMinute);
}
if($iDelta > 0){
$sDelta .= $iDelta.'s ';
}
?>
<div>
<div style="float:right;padding:2px;border:4px solid #67807c;border-radius: 0px 0px;width:400px;margin-bottom:4px;">
<h1 style="text-align:right;margin-top:0px;border-radius: 0px 0px;"><?php echo $oPosts->title ?></h1>
<p style="font-style:italic;margin:0px;text-align:right">le <?php echo $oPosts->date ?> à <?php echo $oPosts->time ?> (il y a <?php echo $sDelta?>) </p>
<p><?php echo $oPosts->body ?></p>
<?php echo $this->oModuleLike->_show($oPosts->id)->show()?>
</div>
<div style="float:right;margin-right:10px;text-align:right">
<h2><a href="<?php echo _root::getLink('mainShare::profil',array('user_id'=>$oPosts->user_id))?>"><?php echo $this->tIndexdUsers[$oPosts->user_id]->firstname ?> <?php echo $this->tIndexdUsers[$oPosts->user_id]->lastname ?></a></h2>
<img style="width:50px" src="<?php echo $this->tIndexdUsers[$oPosts->user_id]->profilPicture ?>"/>
</div>
<div style="clear:both"></div>
</div>
<?php endforeach;?>
<?php endif;?>
Lire la suite : IV Ajoutons la possibilité de commenter