-->
julis

Sharetronix Extension - Like Dislike - Part 2

Last post i was write about database schema to handle the Like/Dislike data.
In this post i will tell you modification that i made for the class Post to handle Like and Dislike.
Please note this is the first version, so maybe you will find the bug, if you find any bug please report to me. 
thanks.

This code change is for Sharetronix 1.4.2

So Let's begin

Open Class_Post.php

Add member field 

public $like_type;
public $post_likesnum;                
public $post_dislikesnum; 

In Constructor Method add this

$this->post_likesnum = $this->db2->fetch_field('SELECT COUNT(id) FROM post_likes WHERE post_id="'.$obj->id.'" AND like_type="like"');
$this->post_dislikesnum = $this->db2->fetch_field('SELECT COUNT(id) FROM post_likes WHERE post_id="'.$obj->id.'" AND like_type="dislike"');

Add these functions 

public function is_post_liked()
{

if( isset($this->tmp->is_post_liked) ) {
return $this->tmp->is_post_liked;
}

if( $this->error ) {
$this->tmp->is_post_liked = FALSE;
return FALSE;
}

if( $this->is_system_post ) {
$this->tmp->is_post_liked = FALSE;
return FALSE;
}

if( ! $this->user->is_logged ) {
$this->tmp->is_post_liked = FALSE;
return FALSE;
}

if( ! $likes = $this->get_post_likes() ) {
$this->tmp->is_post_liked = FALSE;
return FALSE;
}

$this->tmp->is_post_liked = in_array(intval($this->user->id), $likes);

return $this->tmp->is_post_liked;
}
public function is_post_disliked()
{

if( isset($this->tmp->is_post_disliked) ) {
return $this->tmp->is_post_disliked;
}

if( $this->error ) {
$this->tmp->is_post_disliked = FALSE;
return FALSE;
}

if( $this->is_system_post ) {
$this->tmp->is_post_disliked = FALSE;
return FALSE;
}

if( ! $this->user->is_logged ) {
$this->tmp->is_post_disliked = FALSE;
return FALSE;
}

if( ! $likes = $this->get_post_dislikes() ) {
$this->tmp->is_post_disliked = FALSE;
return FALSE;
}

$this->tmp->is_post_disliked = in_array(intval($this->user->id), $likes);

return $this->tmp->is_post_disliked;
}

public function get_post_likes($force_refresh=FALSE)
{

if( $this->error ) {
return FALSE;
}

if( $this->is_system_post ) {
return FALSE;
}

$cachekey = 'n:'.$this->network->id.',post_likes:'.$this->post_type.':'.$this->post_id;

$data = $this->cache->get($cachekey);

if( FALSE!==$data && TRUE!=$force_refresh ) {
return $data;
}

$data = array();

$r = $this->db2->query('SELECT user_id FROM post_likes WHERE like_type="like" AND post_type="'.$this->post_type.'" AND post_id="'.$this->post_id.'" ', FALSE);

while($o = $this->db2->fetch_object($r)) {
$data[] = intval($o->user_id);
}

$this->cache->set($cachekey, $data, $GLOBALS['C']->CACHE_EXPIRE);

return $data;
}

public function get_post_dislikes($force_refresh=FALSE)
{

if( $this->error ) {
return FALSE;
}

if( $this->is_system_post ) {
return FALSE;
}

$cachekey = 'n:'.$this->network->id.',post_dislikes:'.$this->post_type.':'.$this->post_id;

$data = $this->cache->get($cachekey);

if( FALSE!==$data && TRUE!=$force_refresh ) {
return $data;
}

$data = array();

$r = $this->db2->query('SELECT user_id FROM post_likes WHERE like_type="dislike" AND post_type="'.$this->post_type.'" AND post_id="'.$this->post_id.'" ', FALSE);

while($o = $this->db2->fetch_object($r)) {
$data[] = intval($o->user_id);
}

$this->cache->set($cachekey, $data, $GLOBALS['C']->CACHE_EXPIRE);

return $data;
}
public function like_post()
{

if( $this->error ) {
return FALSE;
}
if( $this->is_system_post ) {
return FALSE;
}

if( ! $this->user->is_logged ) {
return FALSE;
}

if( $this->is_post_liked()){
return TRUE;
}

if( $this->is_post_disliked()){
$u = intval($this->user->id);
$this->db2->query('UPDATE post_likes SET like_type="like", date="'.time().'" WHERE user_id="'.$u.'" AND post_type="'.$this->post_type.'" AND post_id="'.$this->post_id.'"', FALSE);
$this->get_post_dislikes(TRUE);
$this->get_post_likes(TRUE);
return TRUE;
}
$u = intval($this->user->id);
$this->db2->query('INSERT INTO post_likes SET user_id="'.$u.'", post_type="'.$this->post_type.'", post_id="'.$this->post_id.'", like_type="like", date="'.time().'" ', FALSE);

$this->get_post_likes(TRUE);
return TRUE;
}

public function dislike_post()
{

if( $this->error ) {
return FALSE;
}
if( $this->is_system_post ) {
return FALSE;
}

if( ! $this->user->is_logged ) {
return FALSE;
}

if( $this->is_post_disliked()){
return TRUE;
}

if( $this->is_post_liked()){
$u = intval($this->user->id);
$this->db2->query('UPDATE post_likes SET like_type="dislike", date="'.time().'" WHERE user_id="'.$u.'"  AND post_type="'.$this->post_type.'" AND post_id="'.$this->post_id.'"', FALSE);
$this->get_post_dislikes(TRUE);
$this->get_post_likes(TRUE);
return TRUE;
}
$u = intval($this->user->id);
$this->db2->query('INSERT INTO post_likes SET user_id="'.$u.'", post_type="'.$this->post_type.'", post_id="'.$this->post_id.'", like_type="dislike", date="'.time().'" ', FALSE);

$this->get_post_dislikes(TRUE);
return TRUE;
}

Next, i will post how to change in controller part, please comment if you like it, or not.
thanks
julis
julis
Load comments