<?php
class admin_photos_model extends tablemanager_model {
protected $table = "photos";
protected $order = array("photo_album_id", "title");
protected $elements = array(
"title" => array(
"label" => "Title",
"type" => "varchar",
"overview" => true,
"required" => true),
"photo_album_id" => array(
"label" => "Photo album",
"type" => "foreignkey",
"table" => "photo_albums",
"column" => "name",
"overview" => true,
"required" => true),
"image" => array(
"label" => "Image",
"type" => "blob",
"required" => true),
"thumbnail" => array(
"label" => "image",
"type" => "blob",
"readonly" => true),
"content_type" => array(
"label" => "Content type",
"type" => "varchar",
"overview" => false,
"readonly" => true),
"overview" => array(
"label" => "Overview",
"type" => "boolean",
"overview" => true,
"required" => true));
public function count_albums() {
$query = "select count(*) as count from photo_albums";
if (($result = $this->db->execute($query)) == false) {
return false;
}
return $result[0]["count"];
}
public function save_oke($item) {
$result = parent::save_oke($item);
$allowed_types = array("image/gif", "image/jpeg", "image/png");
if (isset($item["image"])) {
if (in_array($_FILES["image"]["type"], $allowed_types) == false) {
$this->output->add_message("Incorrect file type");
$result = false;
}
}
return $result;
}
private function limit_image_size(&$item) {
if (isset($item["image"]) == false) {
return true;
}
switch ($item["content_type"]) {
case "image/gif": $image = new gif_image(); break;
case "image/jpeg": $image = new jpeg_image(); break;
case "image/png": $image = new png_image(); break;
default: return false;
}
$image->from_string($item["image"]);
if ($image->width <= $this->settings->photo_max_image_width) {
unset($image);
return true;
}
$image->resize($this->settings->photo_max_image_width);
if (($tempname = tempnam("/tmp", "image_")) === false) {
return false;
}
$image->save($tempname);
unset($image);
$item["image"] = file_get_contents($tempname);
unlink($tempname);
return true;
}
private function create_thumbnail(&$item) {
if (isset($item["image"]) == false) {
return true;
}
switch ($item["content_type"]) {
case "image/gif": $image = new gif_image(); break;
case "image/jpeg": $image = new jpeg_image(); break;
case "image/png": $image = new png_image(); break;
default: return false;
}
$image->from_string($item["image"]);
$image->resize($this->settings->photo_thumbnail_width);
if (($tempname = tempnam("/tmp", "thumbnail_")) === false) {
return false;
}
$image->save($tempname);
unset($image);
$item["thumbnail"] = file_get_contents($tempname);
unlink($tempname);
return true;
}
private function set_content_type(&$item) {
if (isset($_FILES["image"]) == false) {
return false;
} else if ($_FILES["image"]["error"] != 0) {
return false;
}
$item["content_type"] = $_FILES["image"]["type"];
return true;
}
public function create_item($item) {
$this->set_content_type($item);
$this->limit_image_size($item);
$this->create_thumbnail($item);
parent::create_item($item);
}
public function update_item($item) {
$this->set_content_type($item);
$this->limit_image_size($item);
$this->create_thumbnail($item);
$this->elements["thumbnail"]["readonly"] = false;
parent::update_item($item);
}
}
?>