PDO pools

This commit is contained in:
Shish 2011-08-24 09:01:43 +01:00
parent 4f864c6a80
commit db142a2be8

View file

@ -249,7 +249,7 @@ class Pools extends SimpleExtension {
$pools = $database->get_all("SELECT * FROM pools"); $pools = $database->get_all("SELECT * FROM pools");
} }
else { else {
$pools = $database->get_all("SELECT * FROM pools WHERE user_id=?", array($user->id)); $pools = $database->get_all("SELECT * FROM pools WHERE user_id=:id", array("id"=>$user->id));
} }
if(count($pools) > 0) { if(count($pools) > 0) {
$event->add_part($this->theme->get_adder_html($event->image, $pools)); $event->add_part($this->theme->get_adder_html($event->image, $pools));
@ -280,8 +280,8 @@ class Pools extends SimpleExtension {
INNER JOIN users AS u INNER JOIN users AS u
ON p.user_id = u.id ON p.user_id = u.id
ORDER BY p.date DESC ORDER BY p.date DESC
LIMIT ? OFFSET ? LIMIT :l OFFSET :o
", array($poolsPerPage, $pageNumber * $poolsPerPage) ", array("l"=>$poolsPerPage, "o"=>$pageNumber * $poolsPerPage)
); );
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pools") / $poolsPerPage); $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pools") / $poolsPerPage);
@ -306,8 +306,8 @@ class Pools extends SimpleExtension {
$public = $_POST["public"] == "Y" ? "Y" : "N"; $public = $_POST["public"] == "Y" ? "Y" : "N";
$database->execute(" $database->execute("
INSERT INTO pools (user_id, public, title, description, date) INSERT INTO pools (user_id, public, title, description, date)
VALUES (?, ?, ?, ?, now())", VALUES (:uid, :public, :title, :desc, now())",
array($user->id, $public, $_POST["title"], $_POST["description"])); array("uid"=>$user->id, "public"=>$public, "title"=>$_POST["title"], "desc"=>$_POST["description"]));
$result = $database->get_row("SELECT LAST_INSERT_ID() AS poolID"); # FIXME database specific? $result = $database->get_row("SELECT LAST_INSERT_ID() AS poolID"); # FIXME database specific?
@ -318,12 +318,12 @@ class Pools extends SimpleExtension {
private function get_pool($poolID) { private function get_pool($poolID) {
global $database; global $database;
return $database->get_all("SELECT * FROM pools WHERE id=?", array($poolID)); return $database->get_all("SELECT * FROM pools WHERE id=:id", array("id"=>$poolID));
} }
private function get_single_pool($poolID) { private function get_single_pool($poolID) {
global $database; global $database;
return $database->get_row("SELECT * FROM pools WHERE id=?", array($poolID)); return $database->get_row("SELECT * FROM pools WHERE id=:id", array("id"=>$poolID));
} }
/* /*
@ -331,7 +331,7 @@ class Pools extends SimpleExtension {
*/ */
private function get_pool_id($imageID) { private function get_pool_id($imageID) {
global $database; global $database;
return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id=?", array($imageID)); return $database->get_all("SELECT pool_id FROM pool_images WHERE image_id=:iid", array("iid"=>"iid"=>$imageID));
} }
@ -363,8 +363,8 @@ class Pools extends SimpleExtension {
if(!$this->check_post($poolID, $imageID)){ if(!$this->check_post($poolID, $imageID)){
$database->execute(" $database->execute("
INSERT INTO pool_images (pool_id, image_id) INSERT INTO pool_images (pool_id, image_id)
VALUES (?, ?)", VALUES (:pid, :iid)",
array($poolID, $imageID)); array("pid"=>$poolID, "iid"=>$imageID));
$images .= " ".$imageID; $images .= " ".$imageID;
} }
@ -372,15 +372,15 @@ class Pools extends SimpleExtension {
} }
if(!strlen($images) == 0) { if(!strlen($images) == 0) {
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); $count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, 1, $images, $count); $this->add_history($poolID, 1, $images, $count);
} }
$database->Execute(" $database->Execute("
UPDATE pools UPDATE pools
SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid)
WHERE id=?", WHERE id=:pid",
array($poolID, $poolID) array("pid"=>$poolID)
); );
return $poolID; return $poolID;
} }
@ -394,9 +394,9 @@ class Pools extends SimpleExtension {
list($imageORDER, $imageID) = $data; list($imageORDER, $imageID) = $data;
$database->Execute(" $database->Execute("
UPDATE pool_images UPDATE pool_images
SET image_order = ? SET image_order = :ord
WHERE pool_id = ? AND image_id = ?", WHERE pool_id = :pid AND image_id = :iid",
array($imageORDER, $poolID, $imageID) array("ord"=>$imageORDER, "pid"=>$poolID, "iid"=>$imageID)
); );
} }
@ -414,11 +414,11 @@ class Pools extends SimpleExtension {
$images = ""; $images = "";
foreach($_POST['check'] as $imageID) { foreach($_POST['check'] as $imageID) {
$database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID)); $database->execute("DELETE FROM pool_images WHERE pool_id = :pid AND image_id = :iid", array("pid"=>$poolID, "iid"=>$imageID));
$images .= " ".$imageID; $images .= " ".$imageID;
} }
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); $count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, 0, $images, $count); $this->add_history($poolID, 0, $images, $count);
return $poolID; return $poolID;
} }
@ -430,7 +430,7 @@ class Pools extends SimpleExtension {
*/ */
private function check_post($poolID, $imageID) { private function check_post($poolID, $imageID) {
global $database; global $database;
$result = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=? AND image_id=?", array($poolID, $imageID)); $result = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid AND image_id=:iid", array("pid"=>$poolID, "iid"=>$imageID));
return ($result != 0); return ($result != 0);
} }
@ -462,27 +462,27 @@ class Pools extends SimpleExtension {
SELECT p.image_id SELECT p.image_id
FROM pool_images AS p FROM pool_images AS p
INNER JOIN images AS i ON i.id = p.image_id INNER JOIN images AS i ON i.id = p.image_id
WHERE p.pool_id = ? AND i.rating IN ($rating) WHERE p.pool_id = :pid AND i.rating IN ($rating)
ORDER BY p.image_order ASC ORDER BY p.image_order ASC
LIMIT ? OFFSET ?", LIMIT :l OFFSET :o",
array($poolID, $imagesPerPage, $pageNumber * $imagesPerPage)); array("pid"=>$poolID, "l"=>$imagesPerPage, "o"=>$pageNumber * $imagesPerPage));
$totalPages = ceil($database->get_one(" $totalPages = ceil($database->get_one("
SELECT COUNT(*) SELECT COUNT(*)
FROM pool_images AS p FROM pool_images AS p
INNER JOIN images AS i ON i.id = p.image_id INNER JOIN images AS i ON i.id = p.image_id
WHERE pool_id=? AND i.rating IN ($rating)", WHERE pool_id=:pid AND i.rating IN ($rating)",
array($poolID)) / $imagesPerPage); array("pid"=>$poolID)) / $imagesPerPage);
} }
else { else {
$result = $database->get_all(" $result = $database->get_all("
SELECT image_id SELECT image_id
FROM pool_images FROM pool_images
WHERE pool_id=? WHERE pool_id=:pid
ORDER BY image_order ASC ORDER BY image_order ASC
LIMIT ? OFFSET ?", LIMIT :l OFFSET :o",
array($poolID, $imagesPerPage, $pageNumber * $imagesPerPage)); array("pid"=>$poolID, "l"=>$imagesPerPage, "o"=>$pageNumber * $imagesPerPage));
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)) / $imagesPerPage); $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID)) / $imagesPerPage);
} }
$images = array(); $images = array();
@ -501,7 +501,7 @@ class Pools extends SimpleExtension {
private function edit_posts($poolID) { private function edit_posts($poolID) {
global $database; global $database;
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID)); $result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=:pid ORDER BY image_order ASC", array("pid"=>$poolID));
$images = array(); $images = array();
while(!$result->EOF) { while(!$result->EOF) {
@ -520,14 +520,14 @@ class Pools extends SimpleExtension {
private function edit_order($poolID) { private function edit_order($poolID) {
global $database; global $database;
$result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=? ORDER BY image_order ASC", array($poolID)); $result = $database->Execute("SELECT image_id FROM pool_images WHERE pool_id=:pid ORDER BY image_order ASC", array("pid"=>$poolID));
$images = array(); $images = array();
while(!$result->EOF) { while(!$result->EOF) {
$image = $database->get_row(" $image = $database->get_row("
SELECT * FROM images AS i SELECT * FROM images AS i
INNER JOIN pool_images AS p ON i.id = p.image_id INNER JOIN pool_images AS p ON i.id = p.image_id
WHERE pool_id=? AND i.id=?", WHERE pool_id=:pid AND i.id=:iid",
array($poolID, $result->fields["image_id"])); array("pid"=>$poolID, "iid"=>$result->fields["image_id"]));
$image = ($image ? new Image($image) : null); $image = ($image ? new Image($image) : null);
$images[] = array($image); $images[] = array($image);
$result->MoveNext(); $result->MoveNext();
@ -551,14 +551,14 @@ class Pools extends SimpleExtension {
global $user, $database; global $user, $database;
if($user->is_admin()) { if($user->is_admin()) {
$database->execute("DELETE FROM pool_history WHERE pool_id = ?", array($poolID)); $database->execute("DELETE FROM pool_history WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID)); $database->execute("DELETE FROM pool_images WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pools WHERE id = ?", array($poolID)); $database->execute("DELETE FROM pools WHERE id = :pid", array("pid"=>$poolID));
} elseif(!$user->is_anonymous()) { } elseif(!$user->is_anonymous()) {
// FIXME: WE CHECK IF THE USER IS THE OWNER OF THE POOL IF NOT HE CAN'T DO ANYTHING // 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_history WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pool_images WHERE pool_id = ?", array($poolID)); $database->execute("DELETE FROM pool_images WHERE pool_id = :pid", array("pid"=>$poolID));
$database->execute("DELETE FROM pools WHERE id = ? AND user_id = ?", array($poolID, $user->id)); $database->execute("DELETE FROM pools WHERE id = :pid AND user_id = :uid", array("pid"=>$poolID, "uid"=>$user->id));
} }
} }
@ -571,8 +571,8 @@ class Pools extends SimpleExtension {
global $user, $database; global $user, $database;
$database->execute(" $database->execute("
INSERT INTO pool_history (pool_id, user_id, action, images, count, date) INSERT INTO pool_history (pool_id, user_id, action, images, count, date)
VALUES (?, ?, ?, ?, ?, now())", VALUES (:pid, :uid, :act, :img, :count, now())",
array($poolID, $user->id, $action, $images, $count)); array("pid"=>$poolID, "uid"=>$user->id, "act"=>$action, "img"=>$images, "count"=>$count));
} }
@ -601,8 +601,8 @@ class Pools extends SimpleExtension {
INNER JOIN users AS u INNER JOIN users AS u
ON h.user_id = u.id ON h.user_id = u.id
ORDER BY h.date DESC ORDER BY h.date DESC
LIMIT ? OFFSET ? LIMIT :l OFFSET :o
", array($historiesPerPage, $pageNumber * $historiesPerPage)); ", array("l"=>$historiesPerPage, "o"=>$pageNumber * $historiesPerPage));
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pool_history") / $historiesPerPage); $totalPages = ceil($database->get_one("SELECT COUNT(*) FROM pool_history") / $historiesPerPage);
@ -616,7 +616,7 @@ class Pools extends SimpleExtension {
*/ */
private function revert_history($historyID) { private function revert_history($historyID) {
global $database; global $database;
$status = $database->get_all("SELECT * FROM pool_history WHERE id=?", array($historyID)); $status = $database->get_all("SELECT * FROM pool_history WHERE id=:hid", array("hid"=>$historyID));
foreach($status as $entry) { foreach($status as $entry) {
$images = trim($entry['images']); $images = trim($entry['images']);
@ -645,7 +645,7 @@ class Pools extends SimpleExtension {
} }
} }
$count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=?", array($poolID)); $count = $database->get_one("SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", array("pid"=>$poolID));
$this->add_history($poolID, $newAction, $imageArray, $count); $this->add_history($poolID, $newAction, $imageArray, $count);
} }
} }
@ -662,11 +662,11 @@ class Pools extends SimpleExtension {
if(!$this->check_post($poolID, $imageID)) { if(!$this->check_post($poolID, $imageID)) {
$database->execute(" $database->execute("
INSERT INTO pool_images (pool_id, image_id) INSERT INTO pool_images (pool_id, image_id)
VALUES (?, ?)", VALUES (:pid, :iid)",
array($poolID, $imageID)); array("pid"=>$poolID, "iid"=>$imageID));
} }
$database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=?) WHERE id=?", array($poolID, $poolID)); $database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid) WHERE id=:pid", array("pid"=>$poolID));
} }
@ -678,8 +678,8 @@ class Pools extends SimpleExtension {
private function delete_post($poolID, $imageID) { private function delete_post($poolID, $imageID) {
global $database; global $database;
$database->execute("DELETE FROM pool_images WHERE pool_id = ? AND image_id = ?", array($poolID, $imageID)); $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=?) WHERE id=?", array($poolID, $poolID)); $database->execute("UPDATE pools SET posts=(SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid) WHERE id=:pid", array("pid"=>$poolID));
} }
} }