From 432e0c4dc69ccd0152f846217a45d26ebd88e650 Mon Sep 17 00:00:00 2001 From: shish Date: Tue, 26 Aug 2008 09:11:40 +0000 Subject: [PATCH] move image functions to the image class git-svn-id: file:///home/shish/svn/shimmie2/trunk@1007 7f39781d-f577-437e-ae19-be835c7a54ca --- contrib/featured/main.php | 2 +- contrib/report_image/main.php | 6 ++--- core/image.class.php | 44 +++++++++++++++++++++++------------ ext/admin/main.php | 3 ++- ext/comment/main.php | 5 ++-- ext/image/main.php | 14 +++-------- 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/contrib/featured/main.php b/contrib/featured/main.php index 795017cf..06bc15be 100644 --- a/contrib/featured/main.php +++ b/contrib/featured/main.php @@ -34,7 +34,7 @@ class Featured implements Extension { global $config, $database; $fid = $config->get_int("featured_id"); if($fid > 0) { - $image = $database->get_image($fid); + $image = Image::by_id($config, $database, $fid); if(!is_null($image)) { $this->theme->display_featured($event->page, $image); } diff --git a/contrib/report_image/main.php b/contrib/report_image/main.php index 1078fdda..811367a8 100755 --- a/contrib/report_image/main.php +++ b/contrib/report_image/main.php @@ -135,14 +135,14 @@ class ReportImage implements Extension { $reports = array(); foreach($all_reports as $report) { - global $database; + global $database, $config; $image_id = int_escape($report['image_id']); - $image = $database->get_image($image_id); + $image = Image::by_id($config, $database, $image_id); if(is_null($image)) { send_event(new RemoveReportedImageEvent($report['id'])); continue; } - $report['image'] = $database->get_image($image_id); + $report['image'] = $image; $reports[] = $report; } diff --git a/core/image.class.php b/core/image.class.php index 7206b347..ffe02466 100644 --- a/core/image.class.php +++ b/core/image.class.php @@ -5,6 +5,9 @@ * sound file, or any other supported upload type. */ class Image { + var $config; + var $database; + var $id = null; var $height, $width; var $hash, $filesize; @@ -14,6 +17,12 @@ class Image { var $source; public function Image($row=null) { + global $config; + global $database; + + $this->config = $config; + $this->database = $database; + if(!is_null($row)) { foreach($row as $name => $value) { // FIXME: some databases use table.name rather than name @@ -22,16 +31,28 @@ class Image { } } + public static function by_id(Config $config, Database $database, $id) { + assert(is_numeric($id)); + $image = null; + $row = $this->database->get_row("{$this->get_images} WHERE images.id=?", array($id)); + return ($row ? new Image($row) : null); + } + + public function delete() { + $this->database->execute("DELETE FROM images WHERE id=?", array($this->id)); + + unlink($this->get_image_filename()); + unlink($this->get_thumb_filename()); + } + public function get_owner() { - global $database; - return $database->get_user_by_id($this->owner_id); + return User::by_id($this->config, $this->database, $this->owner_id); } public function get_tag_array() { if(!isset($this->tag_array)) { - global $database; $this->tag_array = Array(); - $row = $database->Execute("SELECT tag FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=? ORDER BY tag", array($this->id)); + $row = $this->database->Execute("SELECT tag FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=? ORDER BY tag", array($this->id)); while(!$row->EOF) { $this->tag_array[] = $row->fields['tag']; $row->MoveNext(); @@ -45,18 +66,15 @@ class Image { } public function get_image_link() { - global $config; - return $this->parse_link_template($config->get_string('image_ilink')); + return $this->parse_link_template($this->config->get_string('image_ilink')); } public function get_short_link() { - global $config; - return $this->parse_link_template($config->get_string('image_slink')); + return $this->parse_link_template($this->config->get_string('image_slink')); } public function get_thumb_link() { - global $config; - return $this->parse_link_template($config->get_string('image_tlink')); + return $this->parse_link_template($this->config->get_string('image_tlink')); } public function get_tooltip() { @@ -65,7 +83,6 @@ class Image { } public function get_image_filename() { - global $config; $hash = $this->hash; $ab = substr($hash, 0, 2); $ext = $this->ext; @@ -73,7 +90,6 @@ class Image { } public function get_thumb_filename() { - global $config; $hash = $this->hash; $ab = substr($hash, 0, 2); return "thumbs/$ab/$hash"; @@ -96,8 +112,6 @@ class Image { } public function parse_link_template($tmpl, $_escape="url_escape") { - global $config; - // don't bother hitting the database if it won't be used... $safe_tags = ""; if(strpos($tmpl, '$tags') !== false) { // * stabs dynamically typed languages with a rusty spoon * @@ -106,7 +120,7 @@ class Image { "", $this->get_tag_list()); } - $base_href = $config->get_string('base_href'); + $base_href = $this->config->get_string('base_href'); $fname = $this->get_filename(); $base_fname = strpos($fname, '.') ? substr($fname, 0, strrpos($fname, '.')) : $fname; diff --git a/ext/admin/main.php b/ext/admin/main.php index d664bd54..382513ad 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -25,8 +25,9 @@ class AdminPage implements Extension { if($event->get_arg(0) == "delete_image") { // FIXME: missing lots of else {complain} if(isset($_POST['image_id'])) { + global $config; global $database; - $image = $database->get_image($_POST['image_id']); + $image = Image::by_id($config, $database, $_POST['image_id']); if($image) { send_event(new ImageDeletionEvent($image)); $event->page->set_mode("redirect"); diff --git a/ext/comment/main.php b/ext/comment/main.php index d2805ae7..3a85d0dd 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -189,6 +189,7 @@ class CommentList implements Extension { // page building {{{ private function build_page($current_page) { global $page; + global $config; global $database; if(is_null($current_page) || $current_page <= 0) { @@ -214,7 +215,7 @@ class CommentList implements Extension { $n = 10; while(!$result->EOF) { - $image = $database->get_image($result->fields["image_id"]); + $image = Image::by_id($config, $database, $result->fields["image_id"]); $comments = $this->get_comments($image->id); $this->theme->add_comment_list($page, $image, $comments, $n, $this->can_comment()); $n += 1; @@ -331,7 +332,7 @@ class CommentList implements Extension { if(!$config->get_bool('comment_anon') && $user->is_anonymous()) { $event->veto("Anonymous posting has been disabled"); } - else if(is_null($database->get_image($image_id))) { + else if(is_null(Image::by_id($config, $database, $image_id))) { $event->veto("The image does not exist"); } else if(trim($comment) == "") { diff --git a/ext/image/main.php b/ext/image/main.php index 742637d6..9746b9f2 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -40,7 +40,7 @@ class ImageIO implements Extension { } if($event instanceof ImageDeletionEvent) { - $this->remove_image($event->image); + $event->image->delete(); } if($event instanceof SetupBuildingEvent) { @@ -128,8 +128,9 @@ class ImageIO implements Extension { // }}} // fetch image {{{ private function send_file($image_id, $type) { + global $config; global $database; - $image = $database->get_image($image_id); + $image = Image::by_id($config, $database, $image_id); global $page; if(!is_null($image)) { @@ -171,15 +172,6 @@ class ImageIO implements Extension { } } // }}} -// delete image {{{ - private function remove_image($image) { - global $database; - $database->remove_image($image->id); - - unlink($image->get_image_filename()); - unlink($image->get_thumb_filename()); - } -// }}} } add_event_listener(new ImageIO()); ?>