Last active
December 18, 2018 10:13
-
-
Save flydev-fr/ff83e530da95d660b3f0d2223002587a to your computer and use it in GitHub Desktop.
Module / POC - Like system for ProcessWire Pages - ProcessWire
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
<?php namespace ProcessWire; | |
$likesmod = $modules->get('LikeSystem'); | |
// render a 'like' button | |
$content = $likesmod->render(); | |
// total like of the page | |
$content .= "Number of likes for this page: " . $likesmod->getTotal($page->id); | |
// most liked page | |
$limit = 1 | |
$mostliked = $likesmod->getMostLikedPage($limit); | |
$content .= "<br>Most liked page: " . $pages->get($mostliked[0]['pageId'])->title . "(" . $mostliked[0]['pageId'] . ") " . " (N likes: ". $mostliked[0]['likesCount'] . ")"; |
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
<?php namespace ProcessWire; | |
class LikeSystem extends WireData implements Module { | |
const minVersion = '3.0.0'; | |
protected $tableName = 'likes_system'; | |
public static function getModuleInfo() | |
{ | |
return [ | |
'title' => 'LikeSystem', | |
'version' => "0.0.1", | |
'summary' => 'Like System for ProcessWire.', | |
'author' => 'flydev', | |
'href' => 'https://processwire.com', | |
'singular' => false, | |
'autoload' => false, | |
'icon' => 'thumbs-up', | |
'requires' => [ | |
'ProcessWire>=3.0.0' | |
] | |
]; | |
} | |
protected function createTable() { | |
$sql = "CREATE TABLE IF NOT EXISTS " . $this->tableName . " ( " . | |
"id int unsigned NOT NULL AUTO_INCREMENT, " . | |
"userId int unsigned NOT NULL, " . | |
"pageId int unsigned NOT NULL, " . | |
"likes boolean NOT NULL," . | |
"ts timestamp NULL DEFAULT CURRENT_TIMESTAMP, " . | |
"PRIMARY KEY (id) " . | |
")"; | |
$this->database->exec($sql); | |
$sql = "CREATE UNIQUE INDEX userId_pageId ON " . $this->tableName . " ('userId', 'pageId');"; | |
$this->database->exec($sql); | |
} | |
public function ___install() { | |
if (version_compare($this->config->version, self::minVersion, '<')) | |
throw new WireException("This module requires ProcessWire " . self::minVersion . " or newer."); | |
$this->createTable(); | |
} | |
public function ___uninstall() { | |
$sql = "DROP TABLE IF EXISTS " . $this->tableName; | |
$this->database->exec($sql); | |
} | |
public function init() { | |
// check if the button 'like' as been clicked | |
if(wire('input')->post->likethispage) { | |
$status = (wire('input')->post->likethispage === 'like') ? 1 : 0; | |
$this->likePage($status); | |
} | |
} | |
// Like or unlike a page | |
protected function likePage($status) { | |
$userId = wire('user')->id; | |
$sql = "INSERT INTO " . $this->tableName . "(userId, pageId, likes) VALUES ('".$userId."', '".wire('page')->id. "', '" . $status . "') | |
ON DUPLICATE KEY UPDATE | |
likes = $status, | |
ts = CURRENT_TIMESTAMP;"; | |
$this->database->exec($sql); | |
} | |
// get the total of like of $pageId or current page | |
public function getTotal($pageId = '') { | |
$pageId = empty($pageId) ? wire('page')->id : $pageId; | |
$sql = "SELECT pageId FROM " . $this->tableName . " WHERE pageId = '" . $pageId . "' and likes = '1';"; | |
$res = $this->database->query($sql); | |
$row = $res->fetchAll(); | |
return count($row); | |
} | |
// get the most liked page, if $limit is defined, it return the N most liked page | |
public function getMostLikedPage($limit = 5) { | |
$sql = "SELECT pageId, count(pageId) as likesCount FROM " . $this->tableName . | |
" WHERE likes='1' GROUP BY pageId ORDER BY likesCount DESC LIMIT $limit;"; | |
$res = $this->database->query($sql); | |
$row = $res->fetchAll(); | |
return $row; | |
} | |
public function render() { | |
$modules = wire('modules'); | |
$sql = "SELECT pageId, likes FROM " . $this->tableName . " WHERE pageId = '" . wire('page')->id . "' and userId = '". wire('user')->id ."';"; | |
$res = $this->database->query($sql); | |
$row = $res->fetch(); | |
$label = 'like'; | |
if(count($row)) { | |
if($row['likes'] == 1) $label = 'unlike'; | |
} | |
$form = $modules->get("InputfieldForm"); | |
$form->action = './'; | |
$form->method = 'post'; | |
$form->attr('id+name', 'likeSystemForm'); | |
$field = $modules->get("InputfieldSubmit"); | |
$field->value = $label; | |
$field->icon = "thumbs-up"; | |
$field->attr('id+name', 'likethispage'); | |
$form->append($field); | |
return $form->render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment