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/ext/pools/main.php

885 lines
27 KiB
PHP
Raw Normal View History

2009-12-24 07:36:09 +00:00
<?php
/**
2009-12-30 08:09:31 +00:00
* Name: Pools System
2012-02-08 04:15:23 +00:00
* Author: Sein Kraft <mail@seinkraft.info>, jgen <jgen.tech@gmail.com>
2009-12-24 07:36:09 +00:00
* License: GPLv2
2012-02-08 04:15:23 +00:00
* Description: Allow users to create groups of images and order them.
* Documentation: This extension allows users to created named groups of
* images, and order the images within the group.
* Useful for related images like in a comic, etc.
2009-12-24 07:36:09 +00:00
*/
2012-02-08 04:15:23 +00:00
/**
* This class is just a wrapper around SCoreException.
*/
2010-05-04 19:10:37 +00:00
class PoolCreationException extends SCoreException {
2012-02-08 04:15:23 +00:00
var $error;
public function __construct($error) {
$this->error = $error;
}
2010-05-04 19:10:37 +00:00
}
class Pools extends Extension {
2012-02-08 04:15:23 +00:00
public function onInitExt(InitExtEvent $event) {
2009-12-25 23:05:57 +00:00
global $config, $database;
// Create the database tables
2009-12-25 23:05:57 +00:00
if ($config->get_int("ext_pools_version") < 1){
2009-12-24 07:36:09 +00:00
$database->create_table("pools", "
2009-12-25 23:05:57 +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,
2014-03-02 18:50:46 +00:00
date SCORE_DATETIME NOT NULL,
2009-12-25 23:05:57 +00:00
posts INTEGER NOT NULL DEFAULT 0,
2012-03-11 02:13:25 +00:00
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE
2009-12-25 23:05:57 +00:00
");
2009-12-24 07:36:09 +00:00
$database->create_table("pool_images", "
2009-12-25 23:05:57 +00:00
pool_id INTEGER NOT NULL,
image_id INTEGER NOT NULL,
2012-03-11 02:13:25 +00:00
image_order INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (pool_id) REFERENCES pools(id) ON UPDATE CASCADE ON DELETE CASCADE,
2012-03-11 02:17:16 +00:00
FOREIGN KEY (image_id) REFERENCES images(id) ON UPDATE CASCADE ON DELETE CASCADE
2009-12-25 23:05:57 +00:00
");
2009-12-24 07:36:09 +00:00
$database->create_table("pool_history", "
2009-12-25 23:05:57 +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,
2014-03-02 18:50:46 +00:00
date SCORE_DATETIME NOT NULL,
2012-03-11 02:13:25 +00:00
FOREIGN KEY (pool_id) REFERENCES pools(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE
2009-12-25 23:05:57 +00:00
");
2012-02-08 16:12:33 +00:00
// Set the defaults for the pools extension
2009-12-24 07:36:09 +00:00
$config->set_int("ext_pools_version", 1);
2009-12-25 23:05:57 +00:00
$config->set_int("poolsMaxImportResults", 1000);
$config->set_int("poolsImagesPerPage", 20);
2009-12-24 07:36:09 +00:00
$config->set_int("poolsListsPerPage", 20);
$config->set_int("poolsUpdatedPerPage", 20);
$config->set_bool("poolsInfoOnViewImage", "N");
2009-12-30 07:07:10 +00:00
$config->set_bool("poolsAdderOnViewImage", "N");
2012-02-08 21:42:29 +00:00
$config->set_bool("poolsShowNextLink","N");
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
log_info("pools", "extension installed");
}
2013-12-29 20:46:37 +00:00
if ($config->get_int("ext_pools_version") < 2){
2013-12-30 10:36:32 +00:00
$database->Execute("ALTER TABLE pools ADD UNIQUE INDEX (title);");
$database->Execute("ALTER TABLE pools ADD lastupdated TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;");
2013-12-29 20:46:37 +00:00
$config->set_int("ext_pools_version", 2);
}
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2012-02-08 16:12:33 +00:00
// Add a block to the Board Config / Setup
2009-12-24 07:36:09 +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: ");
2012-02-08 21:42:29 +00:00
$sb->add_bool_option("poolsShowNextLink", "<br>Show 'Next' link when viewing pool images: ");
2009-12-30 08:11:24 +00:00
//$sb->add_bool_option("poolsAdderOnViewImage", "<br>Show pool adder on image: ");
2009-12-24 07:36:09 +00:00
$event->panel->add_block($sb);
}
2009-12-25 23:05:57 +00:00
public function onPageRequest(PageRequestEvent $event) {
2009-12-24 07:36:09 +00:00
global $config, $page, $user;
2012-02-08 04:15:23 +00:00
if ($event->page_matches("pool")) {
$pool_id = 0;
$pool = array();
2012-02-08 04:15:23 +00:00
// Check if we have pool id, since this is most often the case.
if (isset($_POST["pool_id"])) {
$pool_id = int_escape($_POST["pool_id"]);
$pool = $this->get_single_pool($pool_id);
}
// What action are we trying to perform?
2009-12-24 07:36:09 +00:00
switch($event->get_arg(0)) {
case "list": //index
2009-12-26 00:56:53 +00:00
$this->list_pools($page, int_escape($event->get_arg(1)));
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
case "new": // Show form for new pools
2009-12-24 07:36:09 +00:00
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(401, "Error", $errMessage);
2009-12-24 07:36:09 +00:00
}
break;
2009-12-26 00:56:53 +00:00
2009-12-24 07:36:09 +00:00
case "create": // ADD _POST
2010-05-04 19:10:37 +00:00
try {
2009-12-25 23:05:57 +00:00
$newPoolID = $this->add_pool();
$page->set_mode("redirect");
2009-12-26 00:56:53 +00:00
$page->set_redirect(make_link("pool/view/".$newPoolID));
2010-05-04 19:10:37 +00:00
}
2012-02-08 04:15:23 +00:00
catch(PoolCreationException $e) {
$this->theme->display_error(400, "Error", $e->error);
2009-12-25 23:05:57 +00:00
}
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
2009-12-24 07:36:09 +00:00
case "view":
2009-12-26 00:31:02 +00:00
$poolID = int_escape($event->get_arg(1));
2009-12-24 07:36:09 +00:00
$this->get_posts($event, $poolID);
break;
2009-12-26 00:56:53 +00:00
2009-12-24 07:36:09 +00:00
case "updated":
2009-12-26 00:56:53 +00:00
$this->get_history(int_escape($event->get_arg(1)));
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
2009-12-24 07:36:09 +00:00
case "revert":
2009-12-25 23:05:57 +00:00
if(!$user->is_anonymous()) {
2009-12-26 00:31:02 +00:00
$historyID = int_escape($event->get_arg(1));
2009-12-25 23:05:57 +00:00
$this->revert_history($historyID);
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/updated"));
}
break;
2009-12-26 00:56:53 +00:00
case "edit": // Edit the pool (remove images)
if ($this->have_permission($user, $pool)) {
$this->theme->edit_pool($page, $this->get_pool($pool_id), $this->edit_posts($pool_id));
} else {
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/view/".$pool_id));
}
break;
2009-12-25 23:05:57 +00:00
case "order": // Order the pool (view and change the order of images within the pool)
if (isset($_POST["order_view"])) {
if ($this->have_permission($user, $pool)) {
$this->theme->edit_order($page, $this->get_pool($pool_id), $this->edit_order($pool_id));
2009-12-25 23:05:57 +00:00
} else {
2009-12-24 07:36:09 +00:00
$page->set_mode("redirect");
2012-02-08 19:14:11 +00:00
$page->set_redirect(make_link("pool/view/".$pool_id));
2009-12-25 23:05:57 +00:00
}
2009-12-30 07:07:10 +00:00
}
else {
if ($this->have_permission($user, $pool)) {
2009-12-30 07:07:10 +00:00
$this->order_posts();
2009-12-25 23:05:57 +00:00
$page->set_mode("redirect");
2009-12-30 07:07:10 +00:00
$page->set_redirect(make_link("pool/view/".$pool_id));
} else {
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
2009-12-25 23:05:57 +00:00
}
}
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
case "import":
if ($this->have_permission($user, $pool)) {
$this->import_posts($pool_id);
2009-12-25 23:05:57 +00:00
} else {
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
2009-12-25 23:05:57 +00:00
}
break;
2009-12-26 00:56:53 +00:00
case "add_posts":
if ($this->have_permission($user, $pool)) {
2009-12-26 00:56:53 +00:00
$this->add_posts();
2009-12-25 23:05:57 +00:00
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/view/".$pool_id));
} else {
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
2009-12-25 23:05:57 +00:00
}
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
2009-12-24 07:36:09 +00:00
case "remove_posts":
if ($this->have_permission($user, $pool)) {
2009-12-25 23:05:57 +00:00
$this->remove_posts();
$page->set_mode("redirect");
2009-12-26 00:31:02 +00:00
$page->set_redirect(make_link("pool/view/".$pool_id));
2009-12-25 23:05:57 +00:00
} else {
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
2009-12-25 23:05:57 +00:00
}
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
case "edit_description":
if ($this->have_permission($user, $pool)) {
$this->edit_description();
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/view/".$pool_id));
} else {
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
}
break;
2009-12-24 07:36:09 +00:00
case "nuke":
// Completely remove the given pool.
// -> Only admins and owners may do this
2012-02-08 04:15:23 +00:00
if($user->is_admin() || $user->id == $pool['user_id']) {
2009-12-25 23:05:57 +00:00
$this->nuke_pool($pool_id);
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/list"));
} else {
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
2009-12-25 23:05:57 +00:00
}
2009-12-24 07:36:09 +00:00
break;
2009-12-26 00:56:53 +00:00
2009-12-24 07:36:09 +00:00
default:
2009-12-25 23:05:57 +00:00
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/list"));
break;
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
2009-12-30 07:07:10 +00:00
$event->add_link("Pools", make_link("pool/list"));
}
2009-12-25 23:05:57 +00:00
2012-02-08 16:12:33 +00:00
/**
* When displaying an image, optionally list all the pools that the
2012-02-08 21:42:29 +00:00
* image is currently a member of on a side panel, as well as a link
* to the Next image in the pool.
2009-12-25 23:05:57 +00:00
*/
public function onDisplayingImage(DisplayingImageEvent $event) {
2009-12-30 07:07:10 +00:00
global $config, $database, $page;
2009-12-26 00:56:53 +00:00
if($config->get_bool("poolsInfoOnViewImage")) {
$imageID = $event->image->id;
$poolsIDs = $this->get_pool_id($imageID);
2012-02-08 21:42:29 +00:00
$show_next = $config->get_bool("poolsShowNextLink", false);
2009-12-26 00:56:53 +00:00
$linksPools = array();
foreach($poolsIDs as $poolID) {
$pools = $this->get_pool($poolID['pool_id']);
foreach ($pools as $pool){
2009-12-30 07:07:10 +00:00
$linksPools[] = "<a href='".make_link("pool/view/".$pool['id'])."'>".html_escape($pool['title'])."</a>";
2012-02-08 21:42:29 +00:00
// Optionally show a link the Next image in the Pool.
if ($show_next) {
$next_image_in_pool = $this->get_next_post($pool, $imageID);
2012-02-08 21:42:29 +00:00
if (!empty($next_image_in_pool)) {
$linksPools[] = '<a href="'.make_link('post/view/'.$next_image_in_pool).'" class="pools_next_img">Next</a>';
}
}
2009-12-26 00:56:53 +00:00
}
}
2009-12-30 07:07:10 +00:00
$this->theme->pool_info($linksPools);
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) {
2009-12-30 07:07:10 +00:00
global $config, $database, $user;
if($config->get_bool("poolsAdderOnViewImage") && !$user->is_anonymous()) {
if($user->is_admin()) {
$pools = $database->get_all("SELECT * FROM pools");
}
else {
2011-08-24 08:01:43 +00:00
$pools = $database->get_all("SELECT * FROM pools WHERE user_id=:id", array("id"=>$user->id));
2009-12-30 07:07:10 +00:00
}
if(count($pools) > 0) {
$event->add_part($this->theme->get_adder_html($event->image, $pools));
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
}
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array();
if(preg_match("/^pool[=|:]([0-9]+|any|none)$/i", $event->term, $matches)) {
$poolID = $matches[1];
if(preg_match("/^(any|none)$/", $poolID)){
$not = ($poolID == "none" ? "NOT" : "");
$event->add_querylet(new Querylet("images.id $not IN (SELECT DISTINCT image_id FROM pool_images)"));
}else{
$event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = $poolID)"));
}
}
else if(preg_match("/^pool_by_name[=|:](.*)$/i", $event->term, $matches)) {
$poolTitle = str_replace("_", " ", $matches[1]);
$pool = $this->get_single_pool_from_title($poolTitle);
$poolID = 0;
if ($pool){ $poolID = $pool['id']; }
$event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = $poolID)"));
}
}
public function onTagTermParse(TagTermParseEvent $event) {
$matches = array();
if(preg_match("/^pool[=|:](.*)$/i", $event->term, $matches)) {
2014-02-03 15:35:42 +00:00
global $user;
$poolTag = (string) str_replace("_", " ", $matches[1]);
2014-02-03 15:35:42 +00:00
$pool = null;
if(ctype_digit($poolTag)){ //If only digits, assume PoolID
$pool = $this->get_single_pool($poolTag);
}else{ //assume PoolTitle
$pool = $this->get_single_pool_from_title($poolTag);
}
2014-02-03 15:35:42 +00:00
if($pool ? $this->have_permission($user, $pool) : FALSE){
$this->add_post($pool['id'], $event->id, true);
}
}
if(!empty($matches)) $event->metatag = true;
}
2012-02-08 04:15:23 +00:00
/* ------------------------------------------------- */
/* -------------- Private Functions -------------- */
/* ------------------------------------------------- */
/**
* Check if the given user has permission to edit/change the pool.
2012-02-09 04:32:22 +00:00
* TODO: Should the user variable be global?
2014-04-19 05:36:18 +00:00
* @return bool
2012-02-08 04:15:23 +00:00
*/
private function have_permission($user, $pool) {
// If the pool is public and user is logged OR if the user is admin OR if the pool is owned by the user.
if ( (($pool['public'] == "Y" || $pool['public'] == "y") && !$user->is_anonymous()) || $user->is_admin() || $user->id == $pool['user_id'])
{
return true;
} else {
return false;
}
}
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE GET THE LIST OF POOLS
*/
private function list_pools(Page $page, /*int*/ $pageNumber) {
2009-12-24 07:36:09 +00:00
global $config, $database;
2009-12-25 23:05:57 +00:00
if(is_null($pageNumber) || !is_numeric($pageNumber))
$pageNumber = 0;
else if ($pageNumber <= 0)
$pageNumber = 0;
else
$pageNumber--;
$poolsPerPage = $config->get_int("poolsListsPerPage");
$order_by = "";
$order = get_prefixed_cookie("ui-order-pool");
if($order == "created" || is_null($order)){
$order_by = "ORDER BY p.date DESC";
}elseif($order == "updated"){
$order_by = "ORDER BY p.lastupdated DESC";
}elseif($order == "name"){
$order_by = "ORDER BY p.title ASC";
}elseif($order == "count"){
$order_by = "ORDER BY p.posts DESC";
}
2010-05-04 19:10:37 +00:00
$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
2011-08-24 08:01:43 +00:00
LIMIT :l OFFSET :o
", array("l"=>$poolsPerPage, "o"=>$pageNumber * $poolsPerPage)
2009-12-25 23:05:57 +00:00
);
2011-01-01 16:28:04 +00:00
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pools") / $poolsPerPage);
2009-12-25 23:05:57 +00:00
$this->theme->list_pools($page, $pools, $pageNumber + 1, $totalPages);
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE CREATE A NEW POOL
*/
2009-12-26 00:31:02 +00:00
private function add_pool() {
2009-12-24 07:36:09 +00:00
global $user, $database;
2010-05-04 19:10:37 +00:00
if($user->is_anonymous()) {
throw new PoolCreationException("You must be registered and logged in to add a image.");
}
if(empty($_POST["title"])) {
throw new PoolCreationException("Pool title is empty.");
}
if($this->get_single_pool_from_title($_POST["title"])) {
throw new PoolCreationException("A pool using this title already exists.");
2010-05-04 19:10:37 +00:00
}
2014-04-19 05:36:18 +00:00
$public = $_POST["public"] === "Y" ? "Y" : "N";
2009-12-24 07:36:09 +00:00
$database->execute("
2009-12-26 00:31:02 +00:00
INSERT INTO pools (user_id, public, title, description, date)
2011-08-24 08:01:43 +00:00
VALUES (:uid, :public, :title, :desc, now())",
array("uid"=>$user->id, "public"=>$public, "title"=>$_POST["title"], "desc"=>$_POST["description"]));
2014-04-19 05:36:18 +00:00
$result = array();
$result['poolID'] = $database->get_last_insert_id('pools_id_seq');
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
log_info("pools", "Pool {$result["poolID"]} created by {$user->name}");
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
return $result["poolID"];
}
2009-12-25 23:05:57 +00:00
2012-02-08 16:12:33 +00:00
/**
2014-04-19 05:36:18 +00:00
* Retrieve information about pools given multiple pool IDs.
2012-02-08 16:12:33 +00:00
* @param $poolID Array of integers
2014-04-19 05:36:18 +00:00
* @return 2D Array
2012-02-08 16:12:33 +00:00
*/
private function get_pool(/*int*/ $poolID) {
2009-12-24 07:36:09 +00:00
global $database;
2011-08-24 08:01:43 +00:00
return $database->get_all("SELECT * FROM pools WHERE id=:id", array("id"=>$poolID));
2009-12-24 07:36:09 +00:00
}
2012-02-08 16:12:33 +00:00
/**
* Retrieve information about a pool given a pool ID.
* @param $poolID Integer
2014-04-19 05:36:18 +00:00
* @return 2D array (with only 1 element in the one dimension)
2012-02-08 16:12:33 +00:00
*/
private function get_single_pool(/*int*/ $poolID) {
2009-12-24 07:36:09 +00:00
global $database;
2011-08-24 08:01:43 +00:00
return $database->get_row("SELECT * FROM pools WHERE id=:id", array("id"=>$poolID));
2009-12-25 23:05:57 +00:00
}
/**
* Retrieve information about a pool given a pool title.
* @param $poolTitle Integer
2014-04-19 05:36:18 +00:00
* @return 2D array (with only 1 element in the one dimension)
*/
private function get_single_pool_from_title(/*string*/ $poolTitle) {
global $database;
return $database->get_row("SELECT * FROM pools WHERE title=:title", array("title"=>$poolTitle));
}
2012-02-08 16:12:33 +00:00
/**
* Get all of the pool IDs that an image is in, given an image ID.
* @param $imageID Integer
2014-04-19 05:36:18 +00:00
* @return 2D array
2009-12-25 23:05:57 +00:00
*/
private function get_pool_id(/*int*/ $imageID) {
2009-12-24 07:36:09 +00:00
global $database;
2011-08-24 09:40:32 +00:00
return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id=:iid", array("iid"=>$imageID));
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE GET THE IMAGES FROM THE TAG ON IMPORT
*/
private function import_posts(/*int*/ $pool_id) {
2009-12-24 07:36:09 +00:00
global $page, $config, $database;
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$poolsMaxResults = $config->get_int("poolsMaxImportResults", 1000);
2009-12-26 00:31:02 +00:00
$images = $images = Image::find_images(0, $poolsMaxResults, Tag::explode($_POST["pool_tag"]));
2009-12-24 07:36:09 +00:00
$this->theme->pool_result($page, $images, $pool_id);
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE ADD CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY
*
* TODO: Fix this so that the pool ID and images are passed as Arguments to the function.
2009-12-25 23:05:57 +00:00
*/
2009-12-26 00:31:02 +00:00
private function add_posts() {
2009-12-24 07:36:09 +00:00
global $database;
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$poolID = int_escape($_POST['pool_id']);
$images = "";
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
foreach ($_POST['check'] as $imageID){
if(!$this->check_post($poolID, $imageID)){
$database->execute("
2009-12-26 00:31:02 +00:00
INSERT INTO pool_images (pool_id, image_id)
2011-08-24 08:01:43 +00:00
VALUES (:pid, :iid)",
array("pid"=>$poolID, "iid"=>$imageID));
2009-12-25 23:05:57 +00:00
$images .= " ".$imageID;
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
if(!strlen($images) == 0) {
2011-08-24 08:01:43 +00:00
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
2009-12-24 07:36:09 +00:00
$this->add_history($poolID, 1, $images, $count);
}
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
$database->Execute("
UPDATE pools
2011-08-24 08:01:43 +00:00
SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid)
WHERE id=:pid",
array("pid"=>$poolID)
2009-12-26 00:31:02 +00:00
);
2009-12-24 07:36:09 +00:00
return $poolID;
}
2009-12-25 23:05:57 +00:00
/*
* TODO: Fix this so that the pool ID and images are passed as Arguments to the function.
*/
2009-12-26 00:31:02 +00:00
private function order_posts() {
2009-12-24 07:36:09 +00:00
global $database;
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$poolID = int_escape($_POST['pool_id']);
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
foreach($_POST['imgs'] as $data) {
list($imageORDER, $imageID) = $data;
$database->Execute("
UPDATE pool_images
2011-08-24 08:01:43 +00:00
SET image_order = :ord
WHERE pool_id = :pid AND image_id = :iid",
array("ord"=>$imageORDER, "pid"=>$poolID, "iid"=>$imageID)
2009-12-26 00:31:02 +00:00
);
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
return $poolID;
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE REMOVE CHECKED IMAGES FROM POOL AND UPDATE THE HISTORY
*
* TODO: Fix this so that the pool ID and images are passed as Arguments to the function.
2009-12-25 23:05:57 +00:00
*/
2009-12-26 00:31:02 +00:00
private function remove_posts() {
2009-12-24 07:36:09 +00:00
global $database;
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$poolID = int_escape($_POST['pool_id']);
$images = "";
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
foreach($_POST['check'] as $imageID) {
2011-08-24 08:01:43 +00:00
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid AND image_id = :iid", array("pid"=>$poolID, "iid"=>$imageID));
2009-12-24 07:36:09 +00:00
$images .= " ".$imageID;
}
2009-12-25 23:05:57 +00:00
2011-08-24 08:01:43 +00:00
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
2009-12-24 07:36:09 +00:00
$this->add_history($poolID, 0, $images, $count);
2012-02-08 04:15:23 +00:00
return $poolID;
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
/*
* Allows editing of pool description.
*/
private function edit_description() {
global $database;
$poolID = int_escape($_POST['pool_id']);
$database->execute("UPDATE pools SET description=:dsc WHERE id=:pid", array("dsc"=>$_POST['description'], "pid"=>$poolID));
return $poolID;
}
2009-12-25 23:05:57 +00:00
/**
* This function checks if a given image is contained within a given pool.
* Used by add_posts()
*
* @see add_posts()
* @param $poolID integer
* @param $imageID integer
2014-04-19 05:36:18 +00:00
* @return bool
2009-12-25 23:05:57 +00:00
*/
private function check_post(/*int*/ $poolID, /*int*/ $imageID) {
2009-12-24 07:36:09 +00:00
global $database;
2011-08-24 08:01:43 +00:00
$result = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid AND image_id=:iid", array("pid"=>$poolID, "iid"=>$imageID));
2009-12-26 00:31:02 +00:00
return ($result != 0);
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
/**
* Gets the next successive image from a pool, given a pool ID and an image ID.
*
* @param $pool Array for the given pool
* @param $imageID Integer
2014-04-19 05:36:18 +00:00
* @return Integer which is the next Image ID or NULL if none.
*/
private function get_next_post(/*array*/ $pool, /*int*/ $imageID) {
global $database;
if (empty($pool) || empty($imageID))
return null;
$result = $database->get_one("
SELECT image_id
FROM pool_images
WHERE pool_id=:pid
AND image_order > (SELECT image_order FROM pool_images WHERE pool_id=:pid AND image_id =:iid LIMIT 1 )
ORDER BY image_order ASC LIMIT 1",
array("pid"=>$pool['id'], "iid"=>$imageID) );
if (empty($result)) {
// assume that we are at the end of the pool
return null;
} else {
return $result;
}
}
2009-12-25 23:05:57 +00:00
2012-02-08 16:12:33 +00:00
/**
* Retrieve all the images in a pool, given a pool ID.
2009-12-25 23:05:57 +00:00
*/
private function get_posts($event, /*int*/ $poolID) {
2009-12-24 07:36:09 +00:00
global $config, $user, $database;
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
$pageNumber = int_escape($event->get_arg(2));
2009-12-25 23:05:57 +00:00
if(is_null($pageNumber) || !is_numeric($pageNumber))
$pageNumber = 0;
else if ($pageNumber <= 0)
$pageNumber = 0;
else
$pageNumber--;
2009-12-24 07:36:09 +00:00
$poolID = int_escape($poolID);
2012-02-08 16:12:33 +00:00
$pool = $this->get_pool($poolID);
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$imagesPerPage = $config->get_int("poolsImagesPerPage");
2009-12-25 23:05:57 +00:00
2009-12-30 07:07:10 +00:00
// 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")) {
2009-12-26 00:31:02 +00:00
$rating = Ratings::privs_to_sql(Ratings::get_user_privs($user));
2012-02-08 16:12:33 +00:00
}
if (isset($rating) && !empty($rating)) {
2009-12-26 00:31:02 +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
2011-08-24 08:01:43 +00:00
WHERE p.pool_id = :pid AND i.rating IN ($rating)
2009-12-26 00:31:02 +00:00
ORDER BY p.image_order ASC
2011-08-24 08:01:43 +00:00
LIMIT :l OFFSET :o",
array("pid"=>$poolID, "l"=>$imagesPerPage, "o"=>$pageNumber * $imagesPerPage));
2009-12-26 00:31:02 +00:00
2011-01-01 16:28:04 +00:00
$totalPages = ceil($database->get_one("
2009-12-26 00:31:02 +00:00
SELECT COUNT(*)
FROM pool_images AS p
INNER JOIN images AS i ON i.id = p.image_id
2011-08-24 08:01:43 +00:00
WHERE pool_id=:pid AND i.rating IN ($rating)",
array("pid"=>$poolID)) / $imagesPerPage);
2012-02-08 16:12:33 +00:00
} else {
2009-12-30 07:07:10 +00:00
$result = $database->get_all("
SELECT image_id
FROM pool_images
2011-08-24 08:01:43 +00:00
WHERE pool_id=:pid
2009-12-30 07:07:10 +00:00
ORDER BY image_order ASC
2011-08-24 08:01:43 +00:00
LIMIT :l OFFSET :o",
array("pid"=>$poolID, "l"=>$imagesPerPage, "o"=>$pageNumber * $imagesPerPage));
2012-02-08 16:12:33 +00:00
2011-08-24 08:01:43 +00:00
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID)) / $imagesPerPage);
2009-12-30 07:07:10 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$images = array();
2009-12-26 00:31:02 +00:00
foreach($result as $singleResult) {
2009-12-30 07:07:10 +00:00
$images[] = Image::by_id($singleResult["image_id"]);
2009-12-25 23:05:57 +00:00
}
2009-12-24 07:36:09 +00:00
$this->theme->view_pool($pool, $images, $pageNumber + 1, $totalPages);
}
2009-12-25 23:05:57 +00:00
/**
* This function gets the current order of images from a given pool.
* @param $poolID integer
2014-04-19 05:36:18 +00:00
* @return Array of image objects.
2009-12-25 23:05:57 +00:00
*/
private function edit_posts(/*int*/ $poolID) {
2009-12-24 07:36:09 +00:00
global $database;
2009-12-25 23:05:57 +00:00
2011-08-24 08:01:43 +00:00
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=:pid ORDER BY image_order ASC", array("pid"=>$poolID));
2009-12-24 07:36:09 +00:00
$images = array();
while($row = $result->fetch()) {
$image = Image::by_id($row["image_id"]);
2009-12-24 07:36:09 +00:00
$images[] = array($image);
}
2009-12-24 07:36:09 +00:00
return $images;
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +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(/*int*/ $poolID) {
2009-12-24 07:36:09 +00:00
global $database;
2009-12-25 23:05:57 +00:00
2011-08-24 08:01:43 +00:00
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=:pid ORDER BY image_order ASC", array("pid"=>$poolID));
2009-12-24 07:36:09 +00:00
$images = array();
while($row = $result->fetch())
{
$image = $database->get_row("
SELECT * FROM images AS i
INNER JOIN pool_images AS p ON i.id = p.image_id
WHERE pool_id=:pid AND i.id=:iid",
array("pid"=>$poolID, "iid"=>$row['image_id']));
$image = ($image ? new Image($image) : null);
$images[] = array($image);
}
2009-12-24 07:36:09 +00:00
return $images;
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +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(/*int*/ $poolID) {
2009-12-24 07:36:09 +00:00
global $user, $database;
2009-12-25 23:05:57 +00:00
$p_id = $database->get_one("SELECT user_id FROM pools WHERE id = :pid", array("pid"=>$poolID));
2009-12-26 00:31:02 +00:00
if($user->is_admin()) {
2011-08-24 08:01:43 +00:00
$database->execute("DELETE FROM pool_history WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pools WHERE id = :pid", array("pid"=>$poolID));
} elseif($user->id == $p_id) {
2011-08-24 08:01:43 +00:00
$database->execute("DELETE FROM pool_history WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pools WHERE id = :pid AND user_id = :uid", array("pid"=>$poolID, "uid"=>$user->id));
2009-12-24 07:36:09 +00:00
}
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE ADD A HISTORY ENTRY
* FOR $action 1 (one) MEANS ADDED, 0 (zero) MEANS REMOVED
*/
private function add_history(/*int*/ $poolID, $action, $images, $count) {
2009-12-24 07:36:09 +00:00
global $user, $database;
2014-04-19 05:36:18 +00:00
2009-12-24 07:36:09 +00:00
$database->execute("
2009-12-26 00:31:02 +00:00
INSERT INTO pool_history (pool_id, user_id, action, images, count, date)
2011-08-24 08:01:43 +00:00
VALUES (:pid, :uid, :act, :img, :count, now())",
array("pid"=>$poolID, "uid"=>$user->id, "act"=>$action, "img"=>$images, "count"=>$count));
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE GET THE HISTORY LIST
*/
private function get_history(/*int*/ $pageNumber) {
2009-12-24 07:36:09 +00:00
global $config, $database;
2009-12-25 23:05:57 +00:00
if(is_null($pageNumber) || !is_numeric($pageNumber))
$pageNumber = 0;
else if ($pageNumber <= 0)
$pageNumber = 0;
else
$pageNumber--;
2009-12-24 07:36:09 +00:00
$historiesPerPage = $config->get_int("poolsUpdatedPerPage");
2009-12-25 23:05:57 +00:00
2010-05-04 19:10:37 +00:00
$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
2011-08-24 08:01:43 +00:00
LIMIT :l OFFSET :o
", array("l"=>$historiesPerPage, "o"=>$pageNumber * $historiesPerPage));
2009-12-25 23:05:57 +00:00
2011-01-01 16:28:04 +00:00
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pool_history") / $historiesPerPage);
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$this->theme->show_history($history, $pageNumber + 1, $totalPages);
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE GO BACK IN HISTORY AND ADD OR REMOVE POSTS TO POOL
*/
private function revert_history(/*int*/ $historyID) {
2009-12-24 07:36:09 +00:00
global $database;
2011-08-24 08:01:43 +00:00
$status = $database->get_all("SELECT * FROM pool_history WHERE id=:hid", array("hid"=>$historyID));
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
foreach($status as $entry) {
2009-12-24 07:36:09 +00:00
$images = trim($entry['images']);
$images = explode(" ", $images);
$poolID = $entry['pool_id'];
$imageArray = "";
2014-04-19 06:33:34 +00:00
$newAction = -1;
2009-12-25 23:05:57 +00:00
2009-12-26 00:31:02 +00:00
if($entry['action'] == 0) {
// READ ENTRIES
foreach($images as $image) {
2009-12-24 07:36:09 +00:00
$imageID = $image;
$this->add_post($poolID, $imageID);
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$imageArray .= " ".$imageID;
$newAction = 1;
}
2009-12-26 00:31:02 +00:00
}
else if($entry['action'] == 1) {
2009-12-24 07:36:09 +00:00
// DELETE ENTRIES
2009-12-26 00:31:02 +00:00
foreach($images as $image) {
2009-12-24 07:36:09 +00:00
$imageID = $image;
$this->delete_post($poolID, $imageID);
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
$imageArray .= " ".$imageID;
$newAction = 0;
}
2014-04-19 06:33:34 +00:00
} else {
// FIXME: should this throw an exception instead?
log_error("pools", "Invalid history action.");
continue; // go on to the next one.
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2011-08-24 08:01:43 +00:00
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
2009-12-24 07:36:09 +00:00
$this->add_history($poolID, $newAction, $imageArray, $count);
}
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE ADD A SIMPLE POST FROM POOL
* USED WITH FOREACH IN revert_history() & onTagTermParse()
2009-12-25 23:05:57 +00:00
*/
private function add_post(/*int*/ $poolID, /*int*/ $imageID, $history=false) {
2009-12-25 23:05:57 +00:00
global $database;
2009-12-26 00:31:02 +00:00
if(!$this->check_post($poolID, $imageID)) {
2009-12-25 23:05:57 +00:00
$database->execute("
2009-12-26 00:31:02 +00:00
INSERT INTO pool_images (pool_id, image_id)
2011-08-24 08:01:43 +00:00
VALUES (:pid, :iid)",
array("pid"=>$poolID, "iid"=>$imageID));
2009-12-25 23:05:57 +00:00
}
2011-08-24 08:01:43 +00:00
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid) WHERE id=:pid", array("pid"=>$poolID));
if($history){
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, 1, $imageID, $count);
}
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
/*
2009-12-25 23:05:57 +00:00
* HERE WE REMOVE A SIMPLE POST FROM POOL
* USED WITH FOREACH IN revert_history() & onTagTermParse()
2009-12-25 23:05:57 +00:00
*/
private function delete_post(/*int*/ $poolID, /*int*/ $imageID, $history=false) {
2009-12-25 23:05:57 +00:00
global $database;
2011-08-24 08:01:43 +00:00
$database->execute("DELETE FROM pool_images WHERE pool_id = :pid AND image_id = :iid", array("pid"=>$poolID, "iid"=>$imageID));
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid) WHERE id=:pid", array("pid"=>$poolID));
if($history){
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, 0, $imageID, $count);
}
2009-12-24 07:36:09 +00:00
}
2009-12-25 23:05:57 +00:00
2009-12-24 07:36:09 +00:00
}
?>