Navigation Menu

Skip to content

Commit

Permalink
More work to completing the Like Module functionality.
Browse files Browse the repository at this point in the history
Changed name from Love Post to Like Post.
  • Loading branch information
xhezairbey committed Nov 23, 2011
1 parent 65c5cec commit b70437a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 84 deletions.
8 changes: 8 additions & 0 deletions modules/likes/info.yaml
@@ -0,0 +1,8 @@
name: Likes
url: http://chyrp.net/
version: 1.0
description: Allow users to like/unlike a post.
author:
name: the Chyrp team
url: http://chyrp.net/
confirm: Are you sure you want to remove all Post Likes from the database?
26 changes: 26 additions & 0 deletions modules/likes/likes.php
@@ -0,0 +1,26 @@
<?php
require_once "model.Like.php";

class Likes extends Modules {
static function __install() {
Like::install();
}

static function __uninstall($confirm) {
if ($confirm)
Like::uninstall();
}

static function route_add_like() {
$post = new Post($_POST['post_id'], array("drafts" => true));
Like::add($post);
}

static function route_remove_like() {
Like::delete();
}

public function post($post) {
$post->has_many[] = "likes";
}
}
77 changes: 77 additions & 0 deletions modules/likes/model.Like.php
@@ -0,0 +1,77 @@
<?php
/**
* Class: Love
* The model for the love SQL table.
*
* See Also:
* <Model>
*/
class Like extends Model {

/**
* Function: add
* Adds a like to the database.
*
* Parameters:
* $post - The <Post> they're liking
*/
static function add($post) {
$sql = SQL::current();
$visitor = Visitor::current();
$likes = $sql->select("likes",
array("user_id", "likes"),
array("post_id" => $post->id));

$user_ids = unserialize($likes->user_id);
if (!in_array($visitor->id, $user_ids)) {
array_push($user_ids, $visitor->id);
$users = serialize($user_ids);
$sql->update("likes", array(null, "post_id" => $post,
"user_id" => $user_ids,
"likes" => $likes->likes + 1));
} else
self::unlike($post);
}

static function unlike($post) {
$sql = SQL::current();
$visitor = Visitor::current();
$likes = $sql->select("likes",
array("user_id", "likes"),
array("post_id" => $post->id));

$loves = unserialize($loves);
$unliked = false;

foreach ($loves as $key => $value) {
if ($unliked == false)
if ($value == $visitor->id) {
unset($loves[$key]);
$unliked = true;
}
}
$loves = serialize($loves);
$sql->update("posts",
array("id" => $post->id),
array("love_user_id" => $loves));
}

static function install() {
SQL::current()->query("CREATE TABLE IF NOT EXISTS __likes (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
post_id INTEGER DEFAULT 0,
user_id VARCHAR(400) DEFAULT 0,
total_likes INTEGER DEFAULT 0
) DEFAULT CHARSET=utf8");

Group::add_permission("like_posts", "Like Posts");
Group::add_permission("unlike_posts", "Unlike Posts");
}

static function uninstall() {
SQL::current()->query("DROP TABLE __likes");
Group::remove_permission("like_posts");
Group::remove_permission("unlike_posts");
}

}
7 changes: 0 additions & 7 deletions modules/love/info.yaml

This file was deleted.

22 changes: 0 additions & 22 deletions modules/love/love.php

This file was deleted.

55 changes: 0 additions & 55 deletions modules/love/model.love.php

This file was deleted.

0 comments on commit b70437a

Please sign in to comment.