pools extension
This commit is contained in:
parent
ddd91196d9
commit
eeee22863d
673
contrib/pools/main.php
Normal file
673
contrib/pools/main.php
Normal file
@ -0,0 +1,673 @@
|
||||
<?php
|
||||
/**
|
||||
* Name: Pools System
|
||||
* Author: Sein Kraft <mail@seinkraft.info>
|
||||
* License: GPLv2
|
||||
* Description: Allow users to create groups of images
|
||||
* Documentation:
|
||||
*/
|
||||
|
||||
class Pools extends SimpleExtension {
|
||||
public function onInitExt($event) {
|
||||
global $config, $database;
|
||||
|
||||
if ($config->get_int("ext_pools_version") < 1){
|
||||
$database->create_table("pools", "
|
||||
id SCORE_AIPK,
|
||||
user_id INTEGER NOT NULL,
|
||||
public SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
date DATETIME NOT NULL,
|
||||
posts INTEGER NOT NULL DEFAULT 0,
|
||||
INDEX (id)
|
||||
");
|
||||
$database->create_table("pool_images", "
|
||||
pool_id INTEGER NOT NULL,
|
||||
image_id INTEGER NOT NULL,
|
||||
image_order INTEGER NOT NULL DEFAULT 0
|
||||
");
|
||||
$database->create_table("pool_history", "
|
||||
id SCORE_AIPK,
|
||||
pool_id INTEGER NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
action INTEGER NOT NULL,
|
||||
images TEXT,
|
||||
count INTEGER NOT NULL DEFAULT 0,
|
||||
date DATETIME NOT NULL,
|
||||
INDEX (id)
|
||||
");
|
||||
|
||||
$config->set_int("ext_pools_version", 1);
|
||||
|
||||
$config->set_int("poolsMaxImportResults", 1000);
|
||||
$config->set_int("poolsImagesPerPage", 20);
|
||||
$config->set_int("poolsListsPerPage", 20);
|
||||
$config->set_int("poolsUpdatedPerPage", 20);
|
||||
$config->set_bool("poolsInfoOnViewImage", "N");
|
||||
$config->set_bool("poolsAdderOnViewImage", "N");
|
||||
|
||||
log_info("pools", "extension installed");
|
||||
}
|
||||
}
|
||||
|
||||
public function onSetupBuilding(SetupBuildingEvent $event) {
|
||||
$sb = new SetupBlock("Pools");
|
||||
$sb->add_int_option("poolsMaxImportResults", "Max results on import: ");
|
||||
$sb->add_int_option("poolsImagesPerPage", "<br>Images per page: ");
|
||||
$sb->add_int_option("poolsListsPerPage", "<br>Index list items per page: ");
|
||||
$sb->add_int_option("poolsUpdatedPerPage", "<br>Updated list items per page: ");
|
||||
$sb->add_bool_option("poolsInfoOnViewImage", "<br>Show pool info on image: ");
|
||||
//$sb->add_bool_option("poolsAdderOnViewImage", "<br>Show pool adder on image: ");
|
||||
$event->panel->add_block($sb);
|
||||
}
|
||||
|
||||
public function onPageRequest($event) {
|
||||
global $config, $page, $user;
|
||||
|
||||
if($event->page_matches("pool")) {
|
||||
switch($event->get_arg(0)) {
|
||||
case "list": //index
|
||||
$this->list_pools($page, int_escape($event->get_arg(1)));
|
||||
break;
|
||||
|
||||
case "new": // Show form
|
||||
if(!$user->is_anonymous()){
|
||||
$this->theme->new_pool_composer($page);
|
||||
} else {
|
||||
$errMessage = "You must be registered and logged in to create a new pool.";
|
||||
$this->theme->display_error($errMessage);
|
||||
}
|
||||
break;
|
||||
|
||||
case "create": // ADD _POST
|
||||
if(!$user->is_anonymous()) {
|
||||
$newPoolID = $this->add_pool();
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/view/".$newPoolID));
|
||||
} else {
|
||||
$this->theme->display_error("You must be registered and logged in to add a image.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "view":
|
||||
$poolID = int_escape($event->get_arg(1));
|
||||
$this->get_posts($event, $poolID);
|
||||
break;
|
||||
|
||||
case "updated":
|
||||
$this->get_history(int_escape($event->get_arg(1)));
|
||||
break;
|
||||
|
||||
case "revert":
|
||||
if(!$user->is_anonymous()) {
|
||||
$historyID = int_escape($event->get_arg(1));
|
||||
$this->revert_history($historyID);
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/updated"));
|
||||
}
|
||||
break;
|
||||
|
||||
case "edit":
|
||||
$poolID = int_escape($event->get_arg(1));
|
||||
$pools = $this->get_pool($poolID);
|
||||
|
||||
foreach($pools as $pool) {
|
||||
// if the pool is public and user is logged OR if the user is admin OR the user is the owner
|
||||
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->theme->edit_pool($page, $this->get_pool($poolID), $this->edit_posts($poolID));
|
||||
} else {
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/view/".$poolID));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "order":
|
||||
if($_SERVER["REQUEST_METHOD"] == "GET") {
|
||||
$poolID = int_escape($event->get_arg(1));
|
||||
$pools = $this->get_pool($poolID);
|
||||
|
||||
foreach($pools as $pool) {
|
||||
//if the pool is public and user is logged OR if the user is admin
|
||||
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->theme->edit_order($page, $this->get_pool($poolID), $this->edit_order($poolID));
|
||||
} else {
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/view/".$poolID));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
$pool_id = int_escape($_POST["pool_id"]);
|
||||
$pool = $this->get_single_pool($pool_id);
|
||||
|
||||
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->order_posts();
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/view/".$pool_id));
|
||||
} else {
|
||||
$this->theme->display_error("Permssion denied.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "import":
|
||||
$pool_id = int_escape($_POST["pool_id"]);
|
||||
$pool = $this->get_single_pool($pool_id);
|
||||
|
||||
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->import_posts();
|
||||
} else {
|
||||
$this->theme->display_error("Permssion denied.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "add_posts":
|
||||
$pool_id = int_escape($_POST["pool_id"]);
|
||||
$pool = $this->get_single_pool($pool_id);
|
||||
|
||||
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->add_posts();
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/view/".$pool_id));
|
||||
} else {
|
||||
$this->theme->display_error("Permssion denied.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "remove_posts":
|
||||
$pool_id = int_escape($_POST["pool_id"]);
|
||||
$pool = $this->get_single_pool($pool_id);
|
||||
|
||||
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->remove_posts();
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/view/".$pool_id));
|
||||
} else {
|
||||
$this->theme->display_error("Permssion denied.");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "nuke":
|
||||
$pool_id = int_escape($_POST['pool_id']);
|
||||
$pool = $this->get_single_pool($pool_id);
|
||||
|
||||
// only admins and owners may do this
|
||||
if($user->is_admin() || $user->id == $pool['user_id']) {
|
||||
$this->nuke_pool($pool_id);
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/list"));
|
||||
} else {
|
||||
$this->theme->display_error("Permssion denied.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("pool/list"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function onUserBlockBuilding($event) {
|
||||
$event->add_link("Pools", make_link("pool/list"));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE GET THE POOLS WHERE THE IMAGE APPEARS WHEN THE IMAGE IS DISPLAYED
|
||||
*/
|
||||
public function onDisplayingImage($event) {
|
||||
global $config, $database, $page;
|
||||
|
||||
if($config->get_bool("poolsInfoOnViewImage")) {
|
||||
$imageID = $event->image->id;
|
||||
$poolsIDs = $this->get_pool_id($imageID);
|
||||
|
||||
$linksPools = array();
|
||||
foreach($poolsIDs as $poolID) {
|
||||
$pools = $this->get_pool($poolID['pool_id']);
|
||||
foreach ($pools as $pool){
|
||||
$linksPools[] = "<a href='".make_link("pool/view/".$pool['id'])."'>".html_escape($pool['title'])."</a>";
|
||||
}
|
||||
}
|
||||
$this->theme->pool_info($linksPools);
|
||||
}
|
||||
}
|
||||
|
||||
public function onImageAdminBlockBuilding($event) {
|
||||
global $config, $database, $user;
|
||||
if($config->get_bool("poolsAdderOnViewImage") && !$user->is_anonymous()) {
|
||||
if($user->is_admin()) {
|
||||
$pools = $database->get_all("SELECT * FROM pools");
|
||||
}
|
||||
else {
|
||||
$pools = $database->get_all("SELECT * FROM pools WHERE user_id=?", array($user->id));
|
||||
}
|
||||
if(count($pools) > 0) {
|
||||
$event->add_part($this->theme->get_adder_html($event->image, $pools));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE GET THE LIST OF POOLS
|
||||
*/
|
||||
private function list_pools(Page $page, $pageNumber) {
|
||||
global $config, $database;
|
||||
|
||||
if(is_null($pageNumber) || !is_numeric($pageNumber))
|
||||
$pageNumber = 0;
|
||||
else if ($pageNumber <= 0)
|
||||
$pageNumber = 0;
|
||||
else
|
||||
$pageNumber--;
|
||||
|
||||
$poolsPerPage = $config->get_int("poolsListsPerPage");
|
||||
|
||||
$pools = $database->get_all(
|
||||
"SELECT p.id, p.user_id, p.public, p.title, p.description, p.posts, u.name as user_name ".
|
||||
"FROM pools AS p ".
|
||||
"INNER JOIN users AS u ".
|
||||
"ON p.user_id = u.id ".
|
||||
"ORDER BY p.date DESC ".
|
||||
"LIMIT ?, ?"
|
||||
, array($pageNumber * $poolsPerPage, $poolsPerPage)
|
||||
);
|
||||
|
||||
$totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pools") / $poolsPerPage);
|
||||
|
||||
$this->theme->list_pools($page, $pools, $pageNumber + 1, $totalPages);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE CREATE A NEW POOL
|
||||
*/
|
||||
private function add_pool() {
|
||||
global $user, $database;
|
||||
|
||||
$public = $_POST["public"] == "Y" ? "Y" : "N";
|
||||
$database->execute("
|
||||
INSERT INTO pools (user_id, public, title, description, date)
|
||||
VALUES (?, ?, ?, ?, now())",
|
||||
array($user->id, $public, $_POST["title"], $_POST["description"]));
|
||||
|
||||
$result = $database->get_row("SELECT LAST_INSERT_ID() AS poolID"); # FIXME database specific?
|
||||
|
||||
log_info("pools", "Pool {$result["poolID"]} created by {$user->name}");
|
||||
|
||||
return $result["poolID"];
|
||||
}
|
||||
|
||||
private function get_pool($poolID) {
|
||||
global $database;
|
||||
return $database->get_all("SELECT * FROM pools WHERE id=?", array($poolID));
|
||||
}
|
||||
|
||||
private function get_single_pool($poolID) {
|
||||
global $database;
|
||||
return $database->get_row("SELECT * FROM pools WHERE id=?", array($poolID));
|
||||
}
|
||||
|
||||
/*
|
||||
* HERE WE GET THE ID OF THE POOL FROM AN IMAGE
|
||||
*/
|
||||
private function get_pool_id($imageID) {
|
||||
global $database;
|
||||
return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id=?", array($imageID));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE GET THE IMAGES FROM THE TAG ON IMPORT
|
||||
*/
|
||||
private function import_posts() {
|
||||
global $page, $config, $database;
|
||||
|
||||
$pool_id = int_escape($_POST["pool_id"]);
|
||||
|
||||
$poolsMaxResults = $config->get_int("poolsMaxImportResults", 1000);
|
||||
|
||||
$images = $images = Image::find_images(0, $poolsMaxResults, Tag::explode($_POST["pool_tag"]));
|
||||
$this->theme->pool_result($page, $images, $pool_id);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE ADD CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY
|
||||
*/
|
||||
private function add_posts() {
|
||||
global $database;
|
||||
|
||||
$poolID = int_escape($_POST['pool_id']);
|
||||
$images = "";
|
||||
|
||||
foreach ($_POST['check'] as $imageID){
|
||||
if(!$this->check_post($poolID, $imageID)){
|
||||
$database->execute("
|
||||
INSERT INTO pool_images (pool_id, image_id)
|
||||
VALUES (?, ?)",
|
||||
array($poolID, $imageID));
|
||||
|
||||
$images .= " ".$imageID;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!strlen($images) == 0) {
|
||||
$count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID));
|
||||
$this->add_history($poolID, 1, $images, $count);
|
||||
}
|
||||
|
||||
$database->Execute("
|
||||
UPDATE pools
|
||||
SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?)
|
||||
WHERE id=?",
|
||||
array($poolID, $poolID)
|
||||
);
|
||||
return $poolID;
|
||||
}
|
||||
|
||||
private function order_posts() {
|
||||
global $database;
|
||||
|
||||
$poolID = int_escape($_POST['pool_id']);
|
||||
|
||||
foreach($_POST['imgs'] as $data) {
|
||||
list($imageORDER, $imageID) = $data;
|
||||
$database->Execute("
|
||||
UPDATE pool_images
|
||||
SET image_order = ?
|
||||
WHERE pool_id = ? AND image_id = ?",
|
||||
array($imageORDER, $poolID, $imageID)
|
||||
);
|
||||
}
|
||||
|
||||
return $poolID;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE REMOVE CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY
|
||||
*/
|
||||
private function remove_posts() {
|
||||
global $database;
|
||||
|
||||
$poolID = int_escape($_POST['pool_id']);
|
||||
$images = "";
|
||||
|
||||
foreach($_POST['check'] as $imageID) {
|
||||
$database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID));
|
||||
$images .= " ".$imageID;
|
||||
}
|
||||
|
||||
$count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID));
|
||||
$this->add_history($poolID, 0, $images, $count);
|
||||
return $poolID;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE CHECK IF THE POST IS ALREADY ON POOL
|
||||
* USED IN add_posts()
|
||||
*/
|
||||
private function check_post($poolID, $imageID) {
|
||||
global $database;
|
||||
$result = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID));
|
||||
return ($result != 0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE GET ALL IMAGES FOR THE POOL
|
||||
*/
|
||||
private function get_posts($event, $poolID) {
|
||||
global $config, $user, $database;
|
||||
|
||||
$pageNumber = int_escape($event->get_arg(2));
|
||||
if(is_null($pageNumber) || !is_numeric($pageNumber))
|
||||
$pageNumber = 0;
|
||||
else if ($pageNumber <= 0)
|
||||
$pageNumber = 0;
|
||||
else
|
||||
$pageNumber--;
|
||||
|
||||
$poolID = int_escape($poolID);
|
||||
|
||||
$imagesPerPage = $config->get_int("poolsImagesPerPage");
|
||||
|
||||
// WE CHECK IF THE EXTENSION RATING IS INSTALLED, WHICH VERSION AND IF IT
|
||||
// WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER
|
||||
if(class_exists("Ratings")) {
|
||||
$rating = Ratings::privs_to_sql(Ratings::get_user_privs($user));
|
||||
|
||||
$result = $database->get_all("
|
||||
SELECT p.image_id
|
||||
FROM pool_images AS p
|
||||
INNER JOIN images AS i ON i.id = p.image_id
|
||||
WHERE p.pool_id = ? AND i.rating IN ($rating)
|
||||
ORDER BY p.image_order ASC
|
||||
LIMIT ?, ?",
|
||||
array($poolID, $pageNumber * $imagesPerPage, $imagesPerPage));
|
||||
|
||||
$totalPages = ceil($database->db->GetOne("
|
||||
SELECT COUNT(*)
|
||||
FROM pool_images AS p
|
||||
INNER JOIN images AS i ON i.id = p.image_id
|
||||
WHERE pool_id=? AND i.rating IN ($rating)",
|
||||
array($poolID)) / $imagesPerPage);
|
||||
}
|
||||
else {
|
||||
$result = $database->get_all("
|
||||
SELECT image_id
|
||||
FROM pool_images
|
||||
WHERE pool_id=?
|
||||
ORDER BY image_order ASC
|
||||
LIMIT ?, ?",
|
||||
array($poolID, $pageNumber * $imagesPerPage, $imagesPerPage));
|
||||
$totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)) / $imagesPerPage);
|
||||
}
|
||||
|
||||
$images = array();
|
||||
foreach($result as $singleResult) {
|
||||
$images[] = Image::by_id($singleResult["image_id"]);
|
||||
}
|
||||
|
||||
$pool = $this->get_pool($poolID);
|
||||
$this->theme->view_pool($pool, $images, $pageNumber + 1, $totalPages);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* WE GET THE ORDER OF THE IMAGES
|
||||
*/
|
||||
private function edit_posts($poolID) {
|
||||
global $database;
|
||||
|
||||
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID));
|
||||
|
||||
$images = array();
|
||||
while(!$result->EOF) {
|
||||
$image = Image::by_id($result->fields["image_id"]);
|
||||
$images[] = array($image);
|
||||
$result->MoveNext();
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* WE GET THE ORDER OF THE IMAGES BUT HERE WE SEND KEYS ADDED IN ARRAY TO GET THE ORDER IN THE INPUT VALUE
|
||||
*/
|
||||
private function edit_order($poolID) {
|
||||
global $database;
|
||||
|
||||
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID));
|
||||
$images = array();
|
||||
while(!$result->EOF) {
|
||||
$image = $database->get_row("
|
||||
SELECT * FROM images AS i
|
||||
INNER JOIN pool_images AS p ON i.id = p.image_id
|
||||
WHERE pool_id=? AND i.id=?",
|
||||
array($poolID, $result->fields["image_id"]));
|
||||
$image = ($image ? new Image($image) : null);
|
||||
$images[] = array($image);
|
||||
$result->MoveNext();
|
||||
}
|
||||
// Original code
|
||||
//
|
||||
// $images = array();
|
||||
// while(!$result->EOF) {
|
||||
// $image = Image::by_id($result->fields["image_id"]);
|
||||
// $images[] = array($image);
|
||||
// $result->MoveNext();
|
||||
// }
|
||||
return $images;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE NUKE ENTIRE POOL. WE REMOVE POOLS AND POSTS FROM REMOVED POOL AND HISTORIES ENTRIES FROM REMOVED POOL
|
||||
*/
|
||||
private function nuke_pool($poolID) {
|
||||
global $user, $database;
|
||||
|
||||
if($user->is_admin()) {
|
||||
$database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID));
|
||||
$database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID));
|
||||
$database->execute("DELETE FROM pools WHERE id = ?", array($poolID));
|
||||
} elseif(!$user->is_anonymous()) {
|
||||
// FIXME: WE CHECK IF THE USER IS THE OWNER OF THE POOL IF NOT HE CAN'T DO ANYTHING
|
||||
$database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID));
|
||||
$database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID));
|
||||
$database->execute("DELETE FROM pools WHERE id = ? AND user_id = ?", array($poolID, $user->id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE ADD A HISTORY ENTRY
|
||||
* FOR $action 1 (one) MEANS ADDED, 0 (zero) MEANS REMOVED
|
||||
*/
|
||||
private function add_history($poolID, $action, $images, $count) {
|
||||
global $user, $database;
|
||||
$database->execute("
|
||||
INSERT INTO pool_history (pool_id, user_id, action, images, count, date)
|
||||
VALUES (?, ?, ?, ?, ?, now())",
|
||||
array($poolID, $user->id, $action, $images, $count));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE GET THE HISTORY LIST
|
||||
*/
|
||||
private function get_history($pageNumber) {
|
||||
global $config, $database;
|
||||
|
||||
if(is_null($pageNumber) || !is_numeric($pageNumber))
|
||||
$pageNumber = 0;
|
||||
else if ($pageNumber <= 0)
|
||||
$pageNumber = 0;
|
||||
else
|
||||
$pageNumber--;
|
||||
|
||||
|
||||
$historiesPerPage = $config->get_int("poolsUpdatedPerPage");
|
||||
|
||||
$history = $database->get_all(
|
||||
"SELECT h.id, h.pool_id, h.user_id, h.action, h.images, h.count, h.date, u.name as user_name, p.title as title ".
|
||||
"FROM pool_history AS h ".
|
||||
"INNER JOIN pools AS p ".
|
||||
"ON p.id = h.pool_id ".
|
||||
"INNER JOIN users AS u ".
|
||||
"ON h.user_id = u.id ".
|
||||
"ORDER BY h.date DESC ".
|
||||
"LIMIT ?, ?"
|
||||
, array($pageNumber * $historiesPerPage, $historiesPerPage));
|
||||
|
||||
$totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_history") / $historiesPerPage);
|
||||
|
||||
$this->theme->show_history($history, $pageNumber + 1, $totalPages);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* HERE GO BACK IN HISTORY AND ADD OR REMOVE POSTS TO POOL
|
||||
*/
|
||||
private function revert_history($historyID) {
|
||||
global $database;
|
||||
$status = $database->get_all("SELECT * FROM pool_history WHERE id=?", array($historyID));
|
||||
|
||||
foreach($status as $entry) {
|
||||
$images = trim($entry['images']);
|
||||
$images = explode(" ", $images);
|
||||
$poolID = $entry['pool_id'];
|
||||
$imageArray = "";
|
||||
|
||||
if($entry['action'] == 0) {
|
||||
// READ ENTRIES
|
||||
foreach($images as $image) {
|
||||
$imageID = $image;
|
||||
$this->add_post($poolID, $imageID);
|
||||
|
||||
$imageArray .= " ".$imageID;
|
||||
$newAction = 1;
|
||||
}
|
||||
}
|
||||
else if($entry['action'] == 1) {
|
||||
// DELETE ENTRIES
|
||||
foreach($images as $image) {
|
||||
$imageID = $image;
|
||||
$this->delete_post($poolID, $imageID);
|
||||
|
||||
$imageArray .= " ".$imageID;
|
||||
$newAction = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID));
|
||||
$this->add_history($poolID, $newAction, $imageArray, $count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE ADD A SIMPLE POST FROM POOL
|
||||
* USED WITH FOREACH IN revert_history()
|
||||
*/
|
||||
private function add_post($poolID, $imageID) {
|
||||
global $database;
|
||||
|
||||
if(!$this->check_post($poolID, $imageID)) {
|
||||
$database->execute("
|
||||
INSERT INTO pool_images (pool_id, image_id)
|
||||
VALUES (?, ?)",
|
||||
array($poolID, $imageID));
|
||||
}
|
||||
|
||||
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE REMOVE A SIMPLE POST FROM POOL
|
||||
* USED WITH FOREACH IN revert_history()
|
||||
*/
|
||||
private function delete_post($poolID, $imageID) {
|
||||
global $database;
|
||||
|
||||
$database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID));
|
||||
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID));
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
41
contrib/pools/test.php
Normal file
41
contrib/pools/test.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
class PoolsTest extends ShimmieWebTestCase {
|
||||
function testPools() {
|
||||
$this->get_page('pool/list');
|
||||
$this->assert_title("Pools");
|
||||
|
||||
$this->get_page('pool/new');
|
||||
$this->assert_title("Error");
|
||||
|
||||
|
||||
$this->log_in_as_user();
|
||||
|
||||
$this->get_page('pool/list');
|
||||
$this->click("Create New");
|
||||
$this->assert_title("Create Pool");
|
||||
$this->click("Create");
|
||||
$this->assert_title("Error");
|
||||
|
||||
$this->get_page('pool/new');
|
||||
$this->assert_title("Create Pool");
|
||||
$this->set_field("title", "Test Pool Title");
|
||||
$this->set_field("description", "Test pool description");
|
||||
$this->click("Create");
|
||||
$this->assert_title("Pool: Test Pool Title");
|
||||
|
||||
$this->log_out();
|
||||
|
||||
|
||||
$this->log_in_as_admin();
|
||||
|
||||
$this->get_page('pool/list');
|
||||
$this->click("Test Pool Title");
|
||||
$this->assert_title("Pool: Test Pool Title");
|
||||
$this->click("Delete");
|
||||
$this->assert_title("Pools");
|
||||
$this->assert_no_text("Test Pool Title");
|
||||
|
||||
$this->log_out();
|
||||
}
|
||||
}
|
||||
?>
|
403
contrib/pools/theme.php
Normal file
403
contrib/pools/theme.php
Normal file
@ -0,0 +1,403 @@
|
||||
<?php
|
||||
class PoolsTheme extends Themelet {
|
||||
/*
|
||||
* HERE WE ADD THE POOL INFO ON IMAGE
|
||||
*/
|
||||
public function pool_info($linksPools) {
|
||||
global $page;
|
||||
if(count($linksPools) > 0) {
|
||||
$page->add_block(new Block("Pools", implode("<br>", $linksPools), "left"));
|
||||
}
|
||||
}
|
||||
|
||||
public function get_adder_html(Image $image, $pools) {
|
||||
$editor = "";
|
||||
$h = "";
|
||||
foreach($pools as $pool) {
|
||||
$h .= "<option value='".$pool['id']."'>".html_escape($pool['title'])."</option>";
|
||||
}
|
||||
$editor = "
|
||||
<form method='POST' action='".make_link("pool/add_post")."'>
|
||||
<select name='pool_id'>
|
||||
$h
|
||||
</select>
|
||||
<input type='hidden' name='image_id' value='{$image->id}'>
|
||||
<input type='submit' value='Add Image to Pool'>
|
||||
</form>
|
||||
";
|
||||
return $editor;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE SHOWS THE LIST OF POOLS
|
||||
*/
|
||||
public function list_pools(Page $page, $pools, $pageNumber, $totalPages) {
|
||||
global $user;
|
||||
$html = '<table id="poolsList" class="zebra">'.
|
||||
"<thead><tr>".
|
||||
"<th>Name</th>".
|
||||
"<th>Creator</th>".
|
||||
"<th>Posts</th>".
|
||||
"<th>Public</th>".
|
||||
"</tr></thead>";
|
||||
|
||||
$n = 0;
|
||||
foreach($pools as $pool) {
|
||||
$oe = ($n++ % 2 == 0) ? "even" : "odd";
|
||||
|
||||
$pool_link = '<a href="'.make_link("pool/view/".$pool['id']).'">'.html_escape($pool['title'])."</a>";
|
||||
$user_link = '<a href="'.make_link("user/".url_escape($pool['user_name'])).'">'.html_escape($pool['user_name'])."</a>";
|
||||
$public = ($pool['public'] == "Y" ? "Yes" : "No");
|
||||
|
||||
$html .= "<tr class='$oe'>".
|
||||
"<td class='left'>".$pool_link."</td>".
|
||||
"<td>".$user_link."</td>".
|
||||
"<td>".$pool['posts']."</td>".
|
||||
"<td>".$public."</td>".
|
||||
"</tr>";
|
||||
}
|
||||
|
||||
$html .= "</tbody></table>";
|
||||
|
||||
|
||||
$nav_html = "
|
||||
<a href=".make_link().">Index</a>
|
||||
<br><a href=".make_link("pool/new").">Create Pool</a>
|
||||
<br><a href=".make_link("pool/updated").">Pool Changes</a>
|
||||
";
|
||||
|
||||
$blockTitle = "Pools";
|
||||
$page->set_title(html_escape($blockTitle));
|
||||
$page->set_heading(html_escape($blockTitle));
|
||||
$page->add_block(new Block($blockTitle, $html, "main", 10));
|
||||
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
|
||||
|
||||
$this->display_paginator($page, "pool/list", null, $pageNumber, $totalPages);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE NEW POOL COMPOSER
|
||||
*/
|
||||
public function new_pool_composer(Page $page) {
|
||||
$create_html = "<form action=".make_link("pool/create")." method='POST'>
|
||||
<table>
|
||||
<tr><td>Title:</td><td><input type='text' name='title'></td></tr>
|
||||
<tr><td>Public?</td><td><input name='public' type='checkbox' value='Y' checked='checked'/></td></tr>
|
||||
<tr><td>Description:</td><td><textarea name='description'></textarea></td></tr>
|
||||
<tr><td colspan='2'><input type='submit' value='Submit' /></td></tr>
|
||||
</table>
|
||||
";
|
||||
|
||||
$blockTitle = "Create Pool";
|
||||
$page->set_title(html_escape($blockTitle));
|
||||
$page->set_heading(html_escape($blockTitle));
|
||||
$page->add_block(new Block("Create Pool", $create_html, "main", 20));
|
||||
}
|
||||
|
||||
|
||||
private function display_top($pools, $heading, $check_all=false) {
|
||||
global $page, $user;
|
||||
|
||||
$page->set_title($heading);
|
||||
$page->set_heading($heading);
|
||||
if(count($pools) == 1) {
|
||||
$pool = $pools[0];
|
||||
if($pool['public'] == "Y" || $user->is_admin()) {// IF THE POOL IS PUBLIC OR IS ADMIN SHOW EDIT PANEL
|
||||
if(!$user->is_anonymous()) {// IF THE USER IS REGISTERED AND LOGGED IN SHOW EDIT PANEL
|
||||
$this->sidebar_options($page, $pool, $check_all);
|
||||
}
|
||||
}
|
||||
$page->add_block(new Block(html_escape($pool['title']), html_escape($pool['description']), "main", 10));
|
||||
}
|
||||
else {
|
||||
$pool_info = "<table id='poolsList' class='zebra'>".
|
||||
"<thead><tr>".
|
||||
"<th class='left'>Title</th>".
|
||||
"<th class='left'>Description</th>".
|
||||
"</tr></thead>";
|
||||
|
||||
$n = 0;
|
||||
foreach($pools as $pool) {
|
||||
$oe = ($n++ % 2 == 0) ? "even" : "odd";
|
||||
|
||||
$pool_info .= "<tr class='$oe'>".
|
||||
"<td class='left'>".html_escape($pool['title'])."</td>".
|
||||
"<td class='left'>".html_escape($pool['description'])."</td>".
|
||||
"</tr>";
|
||||
|
||||
// this will make disasters if more than one pool comes in the parameter
|
||||
if($pool['public'] == "Y" || $user->is_admin()) {// IF THE POOL IS PUBLIC OR IS ADMIN SHOW EDIT PANEL
|
||||
if(!$user->is_anonymous()) {// IF THE USER IS REGISTERED AND LOGGED IN SHOW EDIT PANEL
|
||||
$this->sidebar_options($page, $pool, $check_all);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pool_info .= "</tbody></table>";
|
||||
$page->add_block(new Block($heading, $pool_info, "main", 10));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE POOL WITH TITLE DESCRIPTION AND IMAGES WITH PAGINATION
|
||||
*/
|
||||
public function view_pool($pools, $images, $pageNumber, $totalPages) {
|
||||
global $user, $page;
|
||||
|
||||
$this->display_top($pools, "Viewing Pool");
|
||||
|
||||
$pool_images = '';
|
||||
foreach($images as $image) {
|
||||
$thumb_html = $this->build_thumb_html($image);
|
||||
$pool_images .= '<span class="thumb">'.
|
||||
'<a href="$image_link">'.$thumb_html.'</a>'.
|
||||
'</span>';
|
||||
}
|
||||
|
||||
$page->add_block(new Block("Viewing Posts", $pool_images, "main", 30));
|
||||
$this->display_paginator($page, "pool/view/".$pools[0]['id'], null, $pageNumber, $totalPages);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE POOL OPTIONS ON SIDEBAR BUT WE HIDE REMOVE OPTION IF THE USER IS NOT THE OWNER OR ADMIN
|
||||
*/
|
||||
public function sidebar_options(Page $page, $pool, $check_all) {
|
||||
global $user;
|
||||
|
||||
$editor = " <form action='".make_link("pool/import")."' method='POST'>
|
||||
<input type='text' name='pool_tag' id='edit' value='Please enter a tag' onclick='this.value=\"\";'/>
|
||||
<input type='submit' name='edit' id='edit' value='Import'/>
|
||||
<input type='hidden' name='pool_id' value='".$pool['id']."'>
|
||||
</form>
|
||||
|
||||
<form method='GET' action='".make_link("pool/edit/".$pool['id'])."'>
|
||||
<input type='submit' name='edit' id='edit' value='Edit Pool'/>
|
||||
</form>
|
||||
|
||||
<form method='GET' action='".make_link("pool/order/".$pool['id'])."'>
|
||||
<input type='submit' name='edit' id='edit' value='Order Pool'/>
|
||||
</form>
|
||||
";
|
||||
|
||||
if($user->id == $pool['user_id'] || $user->is_admin()){
|
||||
$editor .= "
|
||||
<script type='text/javascript'>
|
||||
function confirm_action() {
|
||||
return confirm('Are you sure that you want to delete this pool?');
|
||||
}
|
||||
</script>
|
||||
|
||||
<form action='".make_link("pool/nuke")."' method='POST'>
|
||||
<input type='submit' name='delete' id='delete' value='Delete Pool' onclick='return confirm_action()' />
|
||||
<input type='hidden' name='pool_id' value='".$pool['id']."'>
|
||||
</form>
|
||||
";
|
||||
}
|
||||
|
||||
if($check_all) {
|
||||
$editor .= "
|
||||
<script language='JavaScript' type='text/javascript'>
|
||||
function setAll(value) {
|
||||
var a=new Array();
|
||||
a=document.getElementsByName('check[]');
|
||||
var p=0;
|
||||
for(i=0;i<a.length;i++){
|
||||
a[i].checked = value;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<br><input type='button' name='CheckAll' value='Check All' onClick='setAll(true)'>
|
||||
<input type='button' name='UnCheckAll' value='Uncheck All' onClick='setAll(false)'>
|
||||
";
|
||||
}
|
||||
$page->add_block(new Block("Manage Pool", $editor, "left", 10));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE RESULT OF THE SEARCH ON IMPORT
|
||||
*/
|
||||
public function pool_result(Page $page, $images, $pool_id) {
|
||||
$pool_images = "
|
||||
<script language='JavaScript' type='text/javascript'>
|
||||
function setAll(value) {
|
||||
var a=new Array();
|
||||
a=document.getElementsByName('check[]');
|
||||
var p=0;
|
||||
for(i=0;i<a.length;i++) {
|
||||
a[i].checked = value;
|
||||
}
|
||||
}
|
||||
|
||||
function confirm_action() {
|
||||
return confirm('Are you sure you want to add selected posts to this pool?');
|
||||
}
|
||||
</script>
|
||||
";
|
||||
|
||||
$pool_images .= "<form action='".make_link("pool/add_posts")."' method='POST' name='checks'>";
|
||||
|
||||
foreach($images as $image) {
|
||||
$thumb_html = $this->build_thumb_html($image);
|
||||
|
||||
$pool_images .= '<span class="thumb">'.
|
||||
'<a href="$image_link">'.$thumb_html.'</a>'.
|
||||
'<br>'.
|
||||
'<input name="check[]" type="checkbox" value="'.$image->id.'" />'.
|
||||
'</span>';
|
||||
}
|
||||
$pool_images .= "<br>".
|
||||
"<input type='submit' name='edit' id='edit' value='Add Selected' onclick='return confirm_action()'/>".
|
||||
"<input type='hidden' name='pool_id' value='".$pool_id."'>".
|
||||
"</form>";
|
||||
|
||||
$page->add_block(new Block("Import", $pool_images, "main", 10));
|
||||
|
||||
$editor = "
|
||||
<input type='button' name='CheckAll' value='Check All' onClick='setAll(true)'>
|
||||
<input type='button' name='UnCheckAll' value='Uncheck All' onClick='setAll(false)'>
|
||||
";
|
||||
|
||||
$page->add_block(new Block("Manage Pool", $editor, "left", 10));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE POOL ORDERER
|
||||
* WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH A TEXT INPUT TO SET A NUMBER AND CHANGE THE ORDER
|
||||
*/
|
||||
public function edit_order(Page $page, $pools, $images) {
|
||||
global $user;
|
||||
|
||||
$this->display_top($pools, "Sorting Pool");
|
||||
|
||||
$pool_images = "<form action='".make_link("pool/order")."' method='POST' name='checks'>";
|
||||
$n = 0;
|
||||
foreach($images as $pair) {
|
||||
$image = $pair[0];
|
||||
$thumb_html = $this->build_thumb_html($image);
|
||||
$pool_images .= '<span class="thumb">'.
|
||||
'<a href="$image_link">'.$thumb_html.'</a>'.
|
||||
'<br><input name="imgs['.$n.'][]" type="text" style="max-width:50px;" value="'.$image->image_order.'" />'.
|
||||
'<input name="imgs['.$n.'][]" type="hidden" value="'.$image->id.'" />'.
|
||||
'</span>';
|
||||
$n++;
|
||||
}
|
||||
|
||||
$pool_images .= "<br>".
|
||||
"<input type='submit' name='edit' id='edit' value='Order'/>".
|
||||
"<input type='hidden' name='pool_id' value='".$pools[0]['id']."'>".
|
||||
"</form>";
|
||||
|
||||
$page->add_block(new Block("Sorting Posts", $pool_images, "main", 30));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE POOL EDITOR
|
||||
* WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH
|
||||
* A CHECKBOX TO SELECT WHICH IMAGE WE WANT TO REMOVE
|
||||
*/
|
||||
public function edit_pool(Page $page, $pools, $images) {
|
||||
global $user;
|
||||
|
||||
$this->display_top($pools, "Editing Pool", true);
|
||||
|
||||
$pool_images = "
|
||||
";
|
||||
|
||||
$pool_images = "<form action='".make_link("pool/remove_posts")."' method='POST' name='checks'>";
|
||||
|
||||
foreach($images as $pair) {
|
||||
$image = $pair[0];
|
||||
|
||||
$thumb_html = $this->build_thumb_html($image);
|
||||
|
||||
$pool_images .= '<span class="thumb">'.
|
||||
'<a href="$image_link">'.$thumb_html.'</a>'.
|
||||
'<br><input name="check[]" type="checkbox" value="'.$image->id.'" />'.
|
||||
'</span>';
|
||||
}
|
||||
|
||||
$pool_images .= "<br>".
|
||||
"<input type='submit' name='edit' id='edit' value='Remove Selected'/>".
|
||||
"<input type='hidden' name='pool_id' value='".$pools[0]['id']."'>".
|
||||
"</form>";
|
||||
|
||||
$page->add_block(new Block("Editing Posts", $pool_images, "main", 30));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE HISTORY LIST
|
||||
*/
|
||||
public function show_history($histories, $pageNumber, $totalPages) {
|
||||
global $page;
|
||||
$html = "<table id='poolsList' class='zebra'>".
|
||||
"<thead><tr>".
|
||||
"<th>Pool</th>".
|
||||
"<th>Post Count</th>".
|
||||
"<th>Changes</th>".
|
||||
"<th>Updater</th>".
|
||||
"<th>Date</th>".
|
||||
"<th>Action</th>".
|
||||
"</tr></thead>";
|
||||
|
||||
$n = 0;
|
||||
foreach($histories as $history) {
|
||||
$oe = ($n++ % 2 == 0) ? "even" : "odd";
|
||||
|
||||
$pool_link = "<a href='".make_link("pool/view/".$history['pool_id'])."'>".html_escape($history['title'])."</a>";
|
||||
$user_link = "<a href='".make_link("user/".url_escape($history['user_name']))."'>".html_escape($history['user_name'])."</a>";
|
||||
$revert_link = "<a href='".make_link("pool/revert/".$history['id'])."'>Revert</a>";
|
||||
|
||||
if ($history['action'] == 1) {
|
||||
$prefix = "+";
|
||||
} elseif ($history['action'] == 0) {
|
||||
$prefix = "-";
|
||||
}
|
||||
|
||||
$images = trim($history['images']);
|
||||
$images = explode(" ", $images);
|
||||
|
||||
$image_link = "";
|
||||
foreach ($images as $image) {
|
||||
$image_link .= "<a href='".make_link("post/view/".$image)."'>".$prefix.$image." </a>";
|
||||
}
|
||||
|
||||
$html .= "<tr class='$oe'>".
|
||||
"<td class='left'>".$pool_link."</td>".
|
||||
"<td>".$history['count']."</td>".
|
||||
"<td>".$image_link."</td>".
|
||||
"<td>".$user_link."</td>".
|
||||
"<td>".$history['date']."</td>".
|
||||
"<td>".$revert_link."</td>".
|
||||
"</tr>";
|
||||
}
|
||||
|
||||
$html .= "</tbody></table>";
|
||||
|
||||
$page->set_title("Recent Changes");
|
||||
$page->set_heading("Recent Changes");
|
||||
$page->add_block(new Block("Recent Changes", $html, "main", 10));
|
||||
|
||||
$this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* HERE WE DISPLAY THE ERROR
|
||||
*/
|
||||
public function display_error($errMessage) {
|
||||
global $page;
|
||||
|
||||
$page->set_title("Error");
|
||||
$page->set_heading("Error");
|
||||
$page->add_block(new Block("Error", $errMessage, "main", 10));
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user