This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/contrib/pools/main.php

736 lines
21 KiB
PHP
Raw Normal View History

2009-10-09 12:10:58 +00:00
<?php
/**
* Name: [Beta] Pools System
* Author: Sein Kraft <mail@seinkraft.info>
* License: GPLv2
* Description: Simple pools extension
* Documentation:
*/
class Pools extends SimpleExtension {
public function onInitExt($event) {
2009-11-15 05:00:24 +00:00
global $config, $database;
if ($config->get_int("ext_pools_version") < 1) {
2009-10-09 12:10:58 +00:00
$database->create_table("pools", "
2009-11-15 05:00:24 +00:00
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)
");
2009-10-09 12:10:58 +00:00
$database->create_table("pool_images", "
2009-11-15 05:00:24 +00:00
pool_id INTEGER NOT NULL,
image_id INTEGER NOT NULL,
image_order INTEGER NOT NULL DEFAULT 0
");
2009-10-09 12:10:58 +00:00
$database->create_table("pool_history", "
2009-11-15 05:00:24 +00:00
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)
");
2009-10-09 12:10:58 +00:00
$config->set_int("ext_pools_version", 1);
2009-11-15 05:00:24 +00:00
$config->set_int("poolsMaxImportResults", 1000);
$config->set_int("poolsImagesPerPage", 20);
2009-10-09 12:10:58 +00:00
$config->set_int("poolsListsPerPage", 20);
$config->set_int("poolsUpdatedPerPage", 20);
$config->set_bool("poolsInfoOnViewImage", "N");
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
log_info("pools", "extension installed");
}
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
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: ");
$event->panel->add_block($sb);
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
public function onPageRequest($event) {
global $config, $page, $user;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
if($event->page_matches("pool")) {
switch($event->get_arg(0)) {
case "list": //index
{
$this->list_pools($page, $event);
break;
}
case "new": // Show form
{
2009-11-15 05:00:24 +00:00
if(!$user->is_anonymous()) {
2009-10-09 12:10:58 +00:00
$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
{
2009-11-15 05:00:24 +00:00
if(!$user->is_anonymous()) {
$newPoolID = $this->add_pool();
$page->set_mode("redirect");
2009-11-15 05:32:14 +00:00
$page->set_redirect(make_link("pool/view/".$newPoolID));
2009-10-09 12:10:58 +00:00
} else {
2009-11-15 05:00:24 +00:00
$this->theme->display_error("You must be registered and logged in to add a image.");
}
2009-10-09 12:10:58 +00:00
break;
}
case "view":
{
2009-11-15 05:32:14 +00:00
$poolID = int_escape($event->get_arg(1));
2009-10-09 12:10:58 +00:00
$this->get_posts($event, $poolID);
break;
}
case "updated":
{
$this->get_history($event);
break;
}
case "revert":
{
2009-11-15 05:00:24 +00:00
if(!$user->is_anonymous()) {
2009-11-15 05:32:14 +00:00
$historyID = int_escape($event->get_arg(1));
2009-11-15 05:00:24 +00:00
$this->revert_history($historyID);
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/updated"));
}
break;
2009-10-09 12:10:58 +00:00
}
case "edit":
{
2009-11-15 05:32:14 +00:00
$poolID = int_escape($event->get_arg(1));
2009-10-09 12:10:58 +00:00
$pools = $this->get_pool($poolID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
foreach($pools as $pool) {
2009-11-15 05:00:24 +00:00
//if the pool is public and user is logged OR if the user is admin OR the user is the owner
2009-10-09 12:10:58 +00:00
if(($pool['public'] == "Y" && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id']) {
2009-11-15 05:00:24 +00:00
$this->theme->edit_pool($page, $this->get_pool($poolID), $this->edit_posts($poolID));
} else {
2009-10-09 12:10:58 +00:00
$page->set_mode("redirect");
2009-11-15 05:32:14 +00:00
$page->set_redirect(make_link("pool/view/".$poolID));
2009-11-15 05:00:24 +00:00
}
2009-10-09 12:10:58 +00:00
}
break;
}
case "edit_pool":
{
$poolID = int_escape($_POST["pool_id"]);
$page->set_mode("redirect");
2009-11-15 05:32:14 +00:00
$page->set_redirect(make_link("pool/edit/".$poolID));
2009-10-09 12:10:58 +00:00
break;
}
case "order":
{
2009-11-15 05:32:14 +00:00
$poolID = int_escape($event->get_arg(1));
2009-10-09 12:10:58 +00:00
$pools = $this->get_pool($poolID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
foreach($pools as $pool) {
2009-11-15 05:00:24 +00:00
//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.""));
}
}
2009-10-09 12:10:58 +00:00
break;
}
case "edit_order":
{
$poolID = int_escape($_POST["pool_id"]);
$page->set_mode("redirect");
2009-11-15 05:00:24 +00:00
$page->set_redirect(make_link("pool/order/".$poolID.""));
2009-10-09 12:10:58 +00:00
break;
}
case "import":
{
2009-11-15 05:00:24 +00:00
$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.");
}
2009-10-09 12:10:58 +00:00
break;
}
case "add_posts":
{
2009-11-15 05:00:24 +00:00
$pool_id = int_escape($_POST["pool_id"]);
$pool = $this->get_single_pool($pool_id);
2009-10-09 12:10:58 +00:00
2009-11-15 05:00:24 +00:00
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;
2009-10-09 12:10:58 +00:00
}
case "order_posts":
{
2009-11-15 05:00:24 +00:00
$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.");
}
2009-10-09 12:10:58 +00:00
break;
}
case "remove_posts":
{
2009-11-15 05:00:24 +00:00
$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.");
}
2009-10-09 12:10:58 +00:00
break;
}
case "nuke":
{
2009-11-15 06:31:18 +00:00
$pool_id = int_escape($event->get_arg(1));
2009-11-15 05:00:24 +00:00
$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.");
}
2009-10-09 12:10:58 +00:00
break;
}
case "nuke_pool":
{
$poolID = int_escape($_POST["pool_id"]);
$page->set_mode("redirect");
2009-11-15 05:00:24 +00:00
$page->set_redirect(make_link("pool/nuke/".$poolID));
2009-10-09 12:10:58 +00:00
break;
}
default:
2009-11-15 05:00:24 +00:00
{
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/list"));
break;
}
}
}
}
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE GET THE POOLS WHERE THE IMAGE APPEARS WHEN THE IMAGE IS DISPLAYED
*/
2009-10-09 12:10:58 +00:00
public function onDisplayingImage($event) {
global $page;
2009-11-15 05:00:24 +00:00
$imageID = $event->image->id;
2009-10-09 12:10:58 +00:00
$poolsIDs = $this->get_pool_id($imageID);
$linksPools = "";
2009-11-15 05:00:24 +00:00
foreach ($poolsIDs as $poolID) {
$pools = $this->get_pool($poolID['pool_id']);
foreach ($pools as $pool) {
$linksPools .= "<a href='".make_link("pool/view/".$pool['id']."")."'>".$pool['title']."</a>, ";
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
}
2009-10-09 12:10:58 +00:00
$linksPools = substr($linksPools, 0, -2);
$linksPools = $linksPools." ";
$this->theme->pool_info($linksPools);
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE GET THE LIST OF POOLS
*/
private function list_pools(Page $page, $event) {
2009-10-09 12:10:58 +00:00
global $config, $database;
2009-11-15 05:00:24 +00:00
2009-11-15 06:31:18 +00:00
$pageNumber = int_escape($event->get_arg(1));
2009-11-15 05:00:24 +00:00
if(is_null($pageNumber) || !is_numeric($pageNumber))
$pageNumber = 0;
else if ($pageNumber <= 0)
$pageNumber = 0;
else
$pageNumber--;
$poolsPerPage = $config->get_int("poolsListsPerPage");
2009-10-09 12:10:58 +00:00
$pools = $database->get_all(
2009-11-15 05:00:24 +00:00
"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)
);
2009-10-09 12:10:58 +00:00
$totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pools") / $poolsPerPage);
2009-11-15 05:00:24 +00:00
$this->theme->list_pools($page, $pools, $pageNumber + 1, $totalPages);
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE CREATE A NEW POOL
*/
private function add_pool() {
2009-10-09 12:10:58 +00:00
global $user, $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$public = html_escape($_POST["public"]);
$title = html_escape($_POST["title"]);
$description = html_escape($_POST["description"]);
2009-11-15 05:00:24 +00:00
if($public == "") {
2009-10-09 12:10:58 +00:00
$public = "N";
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$database->execute("
2009-11-15 05:00:24 +00:00
INSERT INTO pools
2009-10-09 12:10:58 +00:00
(user_id, public, title, description, date)
2009-11-15 05:00:24 +00:00
VALUES
2009-10-09 12:10:58 +00:00
(?, ?, ?, ?, now())",
2009-11-15 05:00:24 +00:00
array($user->id, $public, $title, $description));
2009-11-15 05:32:14 +00:00
$result = $database->get_row("SELECT LAST_INSERT_ID() AS poolID");
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
log_info("pools", "Pool {$result["poolID"]} created by {$user->name}");
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
return $result["poolID"];
}
2009-11-15 05:00:24 +00:00
private function get_pool($poolID) {
2009-10-09 12:10:58 +00:00
global $database;
$poolID = int_escape($poolID);
return $database->get_all("SELECT * FROM pools WHERE id=?", array($poolID));
}
2009-11-15 05:00:24 +00:00
2009-11-15 05:32:14 +00:00
private function get_single_pool($poolID) {
2009-10-09 12:10:58 +00:00
global $database;
$poolID = int_escape($poolID);
return $database->get_row("SELECT * FROM pools WHERE id=?", array($poolID));
2009-11-15 05:00:24 +00:00
}
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE GET THE ID OF THE POOL FROM AN IMAGE
*/
private function get_pool_id($imageID) {
2009-10-09 12:10:58 +00:00
global $database;
$imageID = int_escape($imageID);
2009-11-15 05:32:14 +00:00
return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id=?", array($imageID));
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE GET THE IMAGES FROM THE TAG ON IMPORT
*/
private function import_posts() {
2009-10-09 12:10:58 +00:00
global $page, $config, $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$pool_id = int_escape($_POST["pool_id"]);
$pool_tag = html_escape($_POST["pool_tag"]);;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$poolsMaxResults = $config->get_int("poolsMaxImportResults", 1000);
2009-11-15 05:00:24 +00:00
2009-11-15 06:31:18 +00:00
$images = Image::find_images(0, $poolsMaxResults, Tag::explode($pool_tag));
2009-10-09 12:10:58 +00:00
$this->theme->pool_result($page, $images, $pool_id);
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE ADD CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY
*/
private function add_posts() { //ADD CHECKED POSTS
2009-10-09 12:10:58 +00:00
global $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$poolID = int_escape($_POST['pool_id']);
$images = "";
2009-11-15 05:00:24 +00:00
foreach ($_POST['check'] as $imageID) {
if(!$this->check_post($poolID, $imageID)) {
2009-10-09 12:10:58 +00:00
$database->execute("
2009-11-15 05:00:24 +00:00
INSERT INTO pool_images
2009-10-09 12:10:58 +00:00
(pool_id, image_id)
2009-11-15 05:00:24 +00:00
VALUES
2009-10-09 12:10:58 +00:00
(?, ?)",
2009-11-15 05:00:24 +00:00
array($poolID, $imageID));
$images .= " ".$imageID;
2009-10-09 12:10:58 +00:00
}
}
2009-11-15 05:00:24 +00:00
if(!strlen($images) == 0) {
2009-10-09 12:10:58 +00:00
$count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID));
$this->add_history($poolID, 1, $images, $count);
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$database->Execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID));
return $poolID;
}
2009-11-15 05:00:24 +00:00
private function order_posts() { //ORDER POSTS
2009-10-09 12:10:58 +00:00
global $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$poolID = int_escape($_POST['pool_id']);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
foreach ($_POST['imgs'] as $data) {
2009-11-15 05:00:24 +00:00
list ($imageORDER, $imageID) = $data;
$imageID = int_escape($imageID);
2009-11-15 05:32:14 +00:00
$database->Execute("UPDATE pool_images SET image_order=? WHERE pool_id=? AND image_id=?", array($imageORDER, $poolID, $imageID));
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
return $poolID;
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE REMOVE CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY
*/
private function remove_posts() {
2009-10-09 12:10:58 +00:00
global $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$poolID = int_escape($_POST['pool_id']);
$images = "";
2009-11-15 05:00:24 +00:00
foreach ($_POST['check'] as $imageID) {
2009-11-15 05:32:14 +00:00
$database->execute("DELETE FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID));
2009-10-09 12:10:58 +00:00
$images .= " ".$imageID;
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID));
$this->add_history($poolID, 0, $images, $count);
return $poolID;
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE CHECK IF THE POST IS ALREADY ON POOL
* USED IN add_posts()
*/
private function check_post($poolID, $imageID) {
2009-10-09 12:10:58 +00:00
global $database;
$result = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID));
if($result == 0) {
return FALSE;
} else {
return TRUE;
}
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE GET ALL IMAGES FOR THE POOL
*/
private function get_posts($event, $poolID) {
2009-10-09 12:10:58 +00:00
global $config, $user, $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$pageNumber = $event->get_arg(2);
2009-11-15 05:00:24 +00:00
if(is_null($pageNumber) || !is_numeric($pageNumber))
$pageNumber = 0;
else if ($pageNumber <= 0)
$pageNumber = 0;
else
$pageNumber--;
2009-10-09 12:10:58 +00:00
$poolID = int_escape($poolID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$imagesPerPage = $config->get_int("poolsImagesPerPage");
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
// WE CHECK IF THE EXTENSION RATING IS INSTALLED, WICH VERSION AND IF IT WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER
if($config->get_int("ext_ratings2_version") < 3) {
2009-11-15 05:32:14 +00:00
$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);
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
if($config->get_int("ext_ratings2_version") >= 3) {
if($user->is_anonymous()) {
2009-11-15 05:00:24 +00:00
$sqes = $config->get_string("ext_rating_anon_privs");
2009-10-09 12:10:58 +00:00
}
else if($user->is_admin()) {
$sqes = $config->get_string("ext_rating_admin_privs");
}
else {
$sqes = $config->get_string("ext_rating_user_privs");
}
$arr = array();
for($i=0; $i<strlen($sqes); $i++) {
$arr[] = "'" . $sqes[$i] . "'";
}
$rating = join(', ', $arr);
2009-11-15 05:00:24 +00:00
2009-11-15 05:32:14 +00:00
$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));
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$totalPages = ceil($database->db->GetOne("SELECT COUNT(*) ".
2009-11-15 05:00:24 +00:00
"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);
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$images = array();
2009-11-15 05:00:24 +00:00
foreach ($result as $singleResult) {
$image = Image::by_id($singleResult["image_id"]);
$images[] = array($image);
}
2009-10-09 12:10:58 +00:00
$pool = $this->get_pool($poolID);
$this->theme->view_pool($pool, $images, $pageNumber + 1, $totalPages);
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* WE GET THE ORDER OF THE IMAGES
*/
private function edit_posts($poolID) {
2009-10-09 12:10:58 +00:00
global $database;
$poolID = int_escape($poolID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID));
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$images = array();
while(!$result->EOF) {
$image = Image::by_id($result->fields["image_id"]);
$images[] = array($image);
$result->MoveNext();
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
return $images;
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* 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) {
2009-10-09 12:10:58 +00:00
global $database;
$poolID = int_escape($poolID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID));
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$images = array();
while(!$result->EOF) {
$image = $database->get_row("SELECT * FROM images AS i ".
2009-11-15 05:00:24 +00:00
"INNER JOIN pool_images AS p ".
"ON i.id = p.image_id ".
"WHERE pool_id=? AND i.id=?"
, array($poolID, $result->fields["image_id"]));
2009-10-09 12:10:58 +00:00
$image = ($image ? new Image($image) : null);
$images[] = array($image);
$result->MoveNext();
}
2009-11-15 05:00:24 +00:00
// Original code
//
// $images = array();
// while(!$result->EOF) {
// $image = Image::by_id($result->fields["image_id"]);
// $images[] = array($image);
// $result->MoveNext();
// }
2009-10-09 12:10:58 +00:00
return $images;
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE NUKE ENTIRE POOL. WE REMOVE POOLS AND POSTS FROM REMOVED POOL AND HISTORIES ENTRIES FROM REMOVED POOL
*/
private function nuke_pool($poolID) {
2009-10-09 12:10:58 +00:00
global $user, $database;
2009-11-15 05:00:24 +00:00
if($user->is_admin()) {
2009-10-09 12:10:58 +00:00
$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));
2009-11-15 05:00:24 +00:00
} elseif(!$user->is_anonymous()) {
2009-10-09 12:10:58 +00:00
// 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));
}
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE ADD A HISTORY ENTRY
* FOR $action 1 (one) MEANS ADDED, 0 (zero) MEANS REMOVED
*/
private function add_history($poolID, $action, $images, $count) {
2009-10-09 12:10:58 +00:00
global $user, $database;
$user_id = $user->id;
$database->execute("
2009-11-15 05:00:24 +00:00
INSERT INTO pool_history
(pool_id, user_id, action, images, count, date)
VALUES
(?, ?, ?, ?, ?, now())",
array($poolID, $user_id, $action, $images, $count));
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE WE GET THE HISTORY LIST
*/
private function get_history($event) {
2009-10-09 12:10:58 +00:00
global $config, $database;
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$pageNumber = $event->get_arg(1);
2009-11-15 05:00:24 +00:00
if(is_null($pageNumber) || !is_numeric($pageNumber))
$pageNumber = 0;
else if ($pageNumber <= 0)
$pageNumber = 0;
else
$pageNumber--;
2009-10-09 12:10:58 +00:00
$historiesPerPage = $config->get_int("poolsUpdatedPerPage");
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$history = $database->get_all(
2009-11-15 05:00:24 +00:00
"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));
2009-10-09 12:10:58 +00:00
$totalPages = ceil($database->db->GetOne("SELECT COUNT(*) FROM pool_history") / $historiesPerPage);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$this->theme->show_history($history, $pageNumber + 1, $totalPages);
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* HERE GO BACK IN HISTORY AND ADD OR REMOVE POSTS TO POOL
*/
private function revert_history($historyID) {
2009-10-09 12:10:58 +00:00
global $database;
$status = $database->get_all("SELECT * FROM pool_history WHERE id=?", array($historyID));
2009-11-15 05:00:24 +00:00
2009-11-15 06:31:18 +00:00
foreach ($status as $entry) {
2009-10-09 12:10:58 +00:00
$images = trim($entry['images']);
$images = explode(" ", $images);
$poolID = $entry['pool_id'];
$imageArray = "";
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
if ($entry['action'] == 0) {
// READD ENTRIES
foreach ($images as $image) {
$imageID = $image;
$this->add_post($poolID, $imageID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$imageArray .= " ".$imageID;
$newAction = 1;
}
} elseif ($entry['action'] == 1) {
// DELETE ENTRIES
foreach ($images as $image) {
$imageID = $image;
$this->delete_post($poolID, $imageID);
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$imageArray .= " ".$imageID;
$newAction = 0;
}
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
$count = $database->db->GetOne("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID));
$this->add_history($poolID, $newAction, $imageArray, $count);
}
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* 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));
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
/*
2009-11-15 05:00:24 +00:00
* 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));
2009-10-09 12:10:58 +00:00
}
2009-11-15 05:00:24 +00:00
2009-10-09 12:10:58 +00:00
}
?>