move image functions to the image class

git-svn-id: file:///home/shish/svn/shimmie2/trunk@1007 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-08-26 09:11:40 +00:00
parent 1eee417f0a
commit 432e0c4dc6
6 changed files with 41 additions and 33 deletions

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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");

View file

@ -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) == "") {

View file

@ -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());
?>