Merge pull request #364 from DakuTree/patch

Pools ext tweaks/additions.
This commit is contained in:
Shish 2013-12-30 02:49:06 -08:00
commit 2d40fcc92d
4 changed files with 134 additions and 26 deletions

View File

@ -476,6 +476,18 @@ class Image {
$this->delete_tags_from_image();
// insert each new tags
foreach($tags as $tag) {
if(preg_match("/^source=(.*)$/i", $tag, $matches)) {
$this->set_source($matches[1]);
continue;
}
if(preg_match("/^pool=(.*)$/i", $tag, $matches)) {
if(class_exists("Pools")) {
$pls = new Pools();
$pls->add_post_from_tag($matches[1], $this->id);
}
continue;
}
$id = $database->get_one(
$database->scoreql_to_sql(
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"

View File

@ -71,6 +71,13 @@ class Pools extends Extension {
log_info("pools", "extension installed");
}
if ($config->get_int("ext_pools_version") < 2){
$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;");
$config->set_int("ext_pools_version", 2);
}
}
// Add a block to the Board Config / Setup
@ -111,7 +118,7 @@ class Pools extends Extension {
$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);
$this->theme->display_error(401, "Error", $errMessage);
}
break;
@ -122,7 +129,7 @@ class Pools extends Extension {
$page->set_redirect(make_link("pool/view/".$newPoolID));
}
catch(PoolCreationException $e) {
$this->theme->display_error($e->error);
$this->theme->display_error(400, "Error", $e->error);
}
break;
@ -168,7 +175,7 @@ class Pools extends Extension {
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/view/".$pool_id));
} else {
$this->theme->display_error("Permssion denied.");
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
}
}
break;
@ -177,7 +184,7 @@ class Pools extends Extension {
if ($this->have_permission($user, $pool)) {
$this->import_posts($pool_id);
} else {
$this->theme->display_error("Permssion denied.");
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
}
break;
@ -187,7 +194,7 @@ class Pools extends Extension {
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/view/".$pool_id));
} else {
$this->theme->display_error("Permssion denied.");
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
}
break;
@ -197,7 +204,18 @@ class Pools extends Extension {
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/view/".$pool_id));
} else {
$this->theme->display_error("Permssion denied.");
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
}
break;
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;
@ -210,7 +228,7 @@ class Pools extends Extension {
$page->set_mode("redirect");
$page->set_redirect(make_link("pool/list"));
} else {
$this->theme->display_error("Permssion denied.");
$this->theme->display_error(403, "Permission Denied", "You do not have permission to access this page");
}
break;
@ -275,6 +293,23 @@ class Pools extends Extension {
}
}
public function add_post_from_tag(/*str*/ $poolTag, /*int*/ $imageID){
$poolTag = str_replace("_", " ", $poolTag);
//First check if pool tag is a title
if(ctype_digit($poolTag)){
//If string only contains numeric characters, assume it is $poolID
if($this->get_single_pool($poolTag)){ //Make sure pool exists
$this->add_post($poolTag, $imageID);
}
}else{
//If string doesn't contain only numeric characters, check to see if tag is title.
$pool = $this->get_single_pool_from_title($poolTag);
if($pool){
$this->add_post($pool['id'], $imageID);
}
}
}
/* ------------------------------------------------- */
/* -------------- Private Functions -------------- */
/* ------------------------------------------------- */
@ -309,13 +344,26 @@ class Pools extends Extension {
$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";
}
$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
$order_by
LIMIT :l OFFSET :o
", array("l"=>$poolsPerPage, "o"=>$pageNumber * $poolsPerPage)
);
@ -336,7 +384,10 @@ class Pools extends Extension {
throw new PoolCreationException("You must be registered and logged in to add a image.");
}
if(empty($_POST["title"])) {
throw new PoolCreationException("Pool needs a 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.");
}
$public = $_POST["public"] == "Y" ? "Y" : "N";
@ -361,7 +412,7 @@ class Pools extends Extension {
global $database;
return $database->get_all("SELECT * FROM pools WHERE id=:id", array("id"=>$poolID));
}
/**
* Retrieve information about a pool given a pool ID.
* @param $poolID Integer
@ -372,6 +423,16 @@ class Pools extends Extension {
return $database->get_row("SELECT * FROM pools WHERE id=:id", array("id"=>$poolID));
}
/**
* Retrieve information about a pool given a pool title.
* @param $poolTitle Integer
* @retval 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));
}
/**
* Get all of the pool IDs that an image is in, given an image ID.
* @param $imageID Integer
@ -476,6 +537,17 @@ class Pools extends Extension {
return $poolID;
}
/*
* 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;
}
/**
* This function checks if a given image is contained within a given pool.

10
ext/pools/script.js Normal file
View File

@ -0,0 +1,10 @@
$(function() {
var order_pool = $.cookie("shm_ui-order-pool") || "created";
$("#order_pool option[value="+order_pool+"]").attr("selected", true);
$('#order_pool').change(function(){
var val = $("#order_pool option:selected").val();
$.cookie("shm_ui-order-pool", val, {path: '/', expires: 365}); //FIXME: This won't play nice if COOKIE_PREFIX is not "shm_".
window.location.href = '';
});
});

View File

@ -67,11 +67,21 @@ class PoolsTheme extends Themelet {
<br><a href="'.make_link("pool/updated").'">Pool Changes</a>
';
$order_html = '
<select id="order_pool">
<option value="created">Recently created</option>
<option value="updated">Last updated</option>
<option value="name">Name</option>
<option value="count">Post count</option>
</select>
';
$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));
$page->add_block(new Block("Order By", $order_html, "left", 15));
$this->display_paginator($page, "pool/list", null, $pageNumber, $totalPages);
}
@ -156,6 +166,13 @@ class PoolsTheme extends Themelet {
$pool_images .= "\n".$thumb_html."\n";
}
$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>
';
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
$page->add_block(new Block("Viewing Posts", $pool_images, "main", 30));
$this->display_paginator($page, "pool/view/".$pools[0]['id'], null, $pageNumber, $totalPages);
}
@ -312,8 +329,17 @@ class PoolsTheme extends Themelet {
public function edit_pool(Page $page, /*array*/ $pools, /*array*/ $images) {
global $user;
$this->display_top($pools, "Editing Pool", true);
/* EDIT POOL DESCRIPTION */
$desc_html = "
".make_form(make_link("pool/edit_description"))."
<textarea name='description'>".$pools[0]['description']."</textarea><br />
<input type='hidden' name='pool_id' value='".$pools[0]['id']."'>
<input type='submit' value='Change Description' />
</form>
";
/* REMOVE POOLS */
$pool_images = "\n<form action='".make_link("pool/remove_posts")."' method='POST' name='checks'>";
foreach($images as $pair) {
@ -331,6 +357,9 @@ class PoolsTheme extends Themelet {
"<input type='hidden' name='pool_id' value='".$pools[0]['id']."'>".
"</form>";
$pools[0]['description'] = ""; //This is a rough fix to avoid showing the description twice.
$this->display_top($pools, "Editing Pool", true);
$page->add_block(new Block("Editing Description", $desc_html, "main", 28));
$page->add_block(new Block("Editing Posts", $pool_images, "main", 30));
}
@ -389,20 +418,5 @@ class PoolsTheme extends Themelet {
$this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages);
}
/**
* Display an error message to the user.
*/
public function display_error(/*int*/ $code, /*string*/ $title, /*string*/ $message) {
global $page;
// Quick n' Dirty fix
$message = $code;
$page->set_title("Error");
$page->set_heading("Error");
$page->add_block(new Block("Error", $errMessage, "main", 10));
}
}
?>