SimpleModelSite/class_album.php

352 lines
12 KiB
PHP

<?php
class Album implements JsonSerializable {
private $id = 0;
private $foldername = "";
private $title = "";
private $thumbnail = "";
private $thumbnailid = 0;
private $description = "";
private $manufacturer = "";
private $scale = "";
private $createtime = "";
public function getID() {
return intval($this->id);
}
public function getFolderName($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->foldername);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->foldername, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->foldername);
break;
default:
return $this->foldername;
break;
}
}
public function getTitle($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->title);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->title, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->title);
break;
default:
return $this->title;
break;
}
}
public function getThumbnail($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->thumbnail);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->thumbnail, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->thumbnail);
break;
case URLSAFE:
if ( $this->thumbnail == "" ) {
return "graphics/no-image-available_thm.gif";
} else {
return htmlspecialchars(basename(THUMBNAILFOLDER) . "/" . $this->foldername . $this->thumbnail, ENT_QUOTES);
}
break;
default:
return $this->thumbnail;
break;
}
}
public function getThumbnailID() {
return intval($this->thumbnailid);
}
public function getDescription($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->description);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->description, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->description);
break;
default:
return $this->description;
break;
}
}
public function getManufacturer($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->manufacturer);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->manufacturer, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->manufacturer);
break;
default:
return $this->manufacturer;
break;
}
}
public function getScale($flag = 0) {
switch ($flag) {
case HTMLSAFE:
return htmlspecialchars($this->scale);
break;
case HTMLFORMSAFE:
return htmlspecialchars($this->scale, ENT_QUOTES);
break;
case CSVSAFE:
return str_replace('"', '""', $this->scale);
break;
default:
return $this->scale;
break;
}
}
public function getCreateTime($flag = 0) {
switch ($flag) {
case TIMESTAMP:
return strtotime($this->createtime);
break;
case PRETTY:
return date("F j Y H:i:s", strtotime($this->createtime));
break;
default:
return $this->createtime;
break;
}
}
public function setID($id = null) {
if (is_null($id)) return false;
$id = abs(intval($id));
if ($id == 0) return false;
$this->id = $id;
return true;
}
public function setFolderName($foldername = null) {
if (is_null($foldername) || ($foldername == "")) return false;
settype($foldername, "string");
$this->foldername = $foldername;
return true;
}
public function setTitle($title = null) {
if (is_null($title) || ($title == "")) return false;
settype($title, "string");
$this->title = $title;
return true;
}
public function setThumbnail($thumbnail = null) {
if (is_null($thumbnail) || ($thumbnail == "")) return false;
settype($thumbnail, "string");
$this->thumbnail = $thumbnail;
return true;
}
public function setDescription($description = null) {
if (is_null($description) || ($description == "")) return false;
settype($description, "string");
$this->description = $description;
return true;
}
public function setManufacturer($manufacturer = null) {
if (is_null($manufacturer) || ($manufacturer == "")) return false;
settype($manufacturer, "string");
$this->manufacturer = $manufacturer;
return true;
}
public function setScale($scale = null) {
if (is_null($scale) || ($scale == "")) return false;
settype($scale, "string");
$this->scale = $scale;
return true;
}
public function refreshImages() {
global $globaldbh;
// Get the images from this album's folder
$imagefiles = glob(ALBUMFOLDER . $this->getFolderName() . "*.[jJpPgG][pPeEiI][gGeEfF]*");
// Remove existing images from the DB if file no longer exists
$images = Image::getImagesForAlbum($this->getID());
foreach ( $images as $index => $image ) {
$albumfile = ALBUMFOLDER . $this->getFolderName() . $image->getFileName();
if ( !in_array($albumfile, $imagefiles) ) {
if ( $image->getFileName() == $this->getThumbnail ) {
$this->setThumbnail("");
$this->save();
}
$image->delete();
} else {
// If this image already exists,
// delete it from the imagefiles list so we only
// create newly identified images in the next step
array_splice($imagefiles, array_search($albumfile, $imagefiles), 1);
}
}
// Add new images
foreach ( $imagefiles as $imagefile ) {
$imagefile = basename($imagefile);
$image = new Image();
$image->setAlbumID($this->getID());
$image->setAlbumFolder($this->getFolderName());
$image->setFileName($imagefile);
$image->setTitle(substr($imagefile, 0, strrpos($imagefile, ".")));
$image->save();
}
}
public static function refreshAlbums() {
global $globaldbh;
// Get the folders inside ALBUMFOLDER
$folders = glob(ALBUMFOLDER . "*", GLOB_ONLYDIR|GLOB_MARK);
// Remove existing albums from DB if folder doesn't exist
$albums = Album::getList();
foreach ( $albums as $index => $album ) {
$albumfolder = ALBUMFOLDER . $album->getFolderName();
if ( !in_array($albumfolder, $folders) ) {
$album->delete();
} else {
// If this album has a folder that exists,
// delete it from the folder list so we only
// create newly identified folders in the next step
array_splice($folders, array_search(ALBUMFOLDER . $album->getFolderName(), $folders), 1);
}
}
// Add albums for new folders
foreach ( $folders as $folder ) {
$folder = substr($folder, strlen(ALBUMFOLDER));
$album = new Album();
$album->setFolderName($folder);
$album->setTitle(substr($folder, 0, -1));
$album->save();
}
// Refresh images for all albums
$albums = Album::getList();
foreach ( $albums as $album ) {
$album->refreshImages();
}
}
public static function getList() {
global $globaldbh;
$query = "SELECT id FROM " . AppDB::TABLE_ALBUMS . " ORDER BY title";
$sth = $globaldbh->prepare($query);
$sth->execute();
$thelist = array();
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$thelist[] = new Album($row['id']);
}
return $thelist;
}
public function jsonSerialize() {
return [
'id' => $this->getID(),
'foldername' => $this->getFolderName(),
'thumbnail' => $this->getThumbnail(),
'thumburl' => ($this->getThumbnail() == "") ? "graphics/no-image-available_thm.gif" : THUMBNAILFOLDER . $this->getFolderName() . $this->getThumbnail(),
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'manufacturer' => $this->getManufacturer(),
'scale' => $this->getScale(),
'createtime' => $this->getCreateTime(),
'images' => Image::getImagesForAlbum($this->getID())
];
}
public function delete() {
global $globaldbh;
$query = "DELETE FROM " . AppDB::TABLE_ALBUMS . " WHERE id=:id";
$sth = $globaldbh->prepare($query);
$sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT);
$sth->execute();
$this->setID(0);
$thumbnailfolder = THUMBNAILFOLDER . $this->getFolderName();
if ( is_dir($thumbnailfolder) ) {
unlink($thumbnailfolder . "*");
rmdir($thumbnailfolder);
}
}
public function save() {
global $globaldbh;
$query = "INSERT INTO " . AppDB::TABLE_ALBUMS . " ";
$query .= "(id, foldername, title, thumbnail, description, manufacturer, scale, createtime) ";
$query .= "VALUES(NULL, :foldername, :title, :thumbnail, :description, :manufacturer, :scale, NOW()) ";
$query .= "ON DUPLICATE KEY UPDATE ";
$query .= "foldername=:foldername, title=:title, thumbnail=:thumbnail, description=:description, manufacturer=:manufacturer, scale=:scale";
$sth = $globaldbh->prepare($query);
$sth->bindValue(":id", (int) $this->getID(), PDO::PARAM_INT);
$sth->bindValue(":foldername", $this->getFolderName(), PDO::PARAM_STR);
$sth->bindValue(":title", $this->getTitle(), PDO::PARAM_STR);
$sth->bindValue(":thumbnail", $this->getThumbnail(), PDO::PARAM_STR);
$sth->bindValue(":description", $this->getDescription(), PDO::PARAM_STR);
$sth->bindValue(":manufacturer", $this->getManufacturer(), PDO::PARAM_STR);
$sth->bindValue(":scale", $this->getScale(), PDO::PARAM_STR);
$sth->execute();
if ( $this->getID() == 0 ) {
$this->setID($globaldbh->lastInsertId());
mkdir(THUMBNAILFOLDER . $this->getFolderName());
}
}
function __construct($reqid = 0) {
global $globaldbh;
$reqid = intval($reqid);
$query = "SELECT a.id, a.foldername, a.title, a.thumbnail, a.description, a.manufacturer, a.scale, a.createtime, i.id AS thumbnailid " .
"FROM " . AppDB::TABLE_ALBUMS . " AS a LEFT JOIN " . AppDB::TABLE_IMAGES . " AS i ON i.filename=a.thumbnail WHERE a.id=:id";
$sth = $globaldbh->prepare($query);
$sth->bindValue(":id", $reqid, PDO::PARAM_INT);
$sth->execute();
if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
$this->setID($row['id']);
$this->setFolderName($row['foldername']);
$this->setTitle($row['title']);
$this->setThumbnail($row['thumbnail']);
$this->thumbnailid = $row['thumbnailid'];
$this->setDescription($row['description']);
$this->setManufacturer($row['manufacturer']);
$this->setScale($row['scale']);
$this->createtime = $row['createtime'];
}
}
}
// vim: set ts=4:sw=4