forked from chyrp/chyrp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More work to completing the Like Module functionality.
Changed name from Love Post to Like Post.
- Loading branch information
1 parent
65c5cec
commit b70437a
Showing
6 changed files
with
111 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.