2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* Asset.php
|
|
|
|
* Contains key class Asset.
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class Asset
|
|
|
|
{
|
|
|
|
protected $id_asset;
|
2016-09-02 21:41:01 +02:00
|
|
|
protected $id_user_uploaded;
|
2016-09-01 23:13:23 +02:00
|
|
|
protected $subdir;
|
|
|
|
protected $filename;
|
|
|
|
protected $title;
|
2022-12-25 13:50:33 +01:00
|
|
|
protected $slug;
|
2016-09-01 23:13:23 +02:00
|
|
|
protected $mimetype;
|
|
|
|
protected $image_width;
|
|
|
|
protected $image_height;
|
|
|
|
protected $date_captured;
|
|
|
|
protected $priority;
|
2017-12-20 14:51:23 +01:00
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
protected $meta;
|
|
|
|
protected $tags;
|
2017-12-20 14:51:23 +01:00
|
|
|
protected $thumbnails;
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
protected function __construct(array $data)
|
|
|
|
{
|
|
|
|
foreach ($data as $attribute => $value)
|
2023-03-11 21:39:20 +01:00
|
|
|
{
|
|
|
|
if (property_exists($this, $attribute))
|
|
|
|
$this->$attribute = $value;
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
if (!empty($data['date_captured']) && $data['date_captured'] !== 'NULL')
|
|
|
|
$this->date_captured = new DateTime($data['date_captured']);
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:45:48 +01:00
|
|
|
public static function cleanSlug($slug)
|
|
|
|
{
|
|
|
|
// Only alphanumerical chars, underscores and forward slashes are allowed
|
|
|
|
if (!preg_match_all('~([A-z0-9\/_]+)~', $slug, $allowedTokens, PREG_PATTERN_ORDER))
|
|
|
|
throw new UnexpectedValueException('Slug does not make sense.');
|
|
|
|
|
|
|
|
// Join valid substrings together with hyphens
|
|
|
|
return implode('-', $allowedTokens[1]);
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public static function fromId($id_asset, $return_format = 'object')
|
|
|
|
{
|
2016-09-03 21:32:55 +02:00
|
|
|
$row = Registry::get('db')->queryAssoc('
|
2016-09-01 23:13:23 +02:00
|
|
|
SELECT *
|
|
|
|
FROM assets
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $id_asset,
|
|
|
|
]);
|
|
|
|
|
2016-09-03 21:32:55 +02:00
|
|
|
return empty($row) ? false : self::byRow($row, $return_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromSlug($slug, $return_format = 'object')
|
|
|
|
{
|
|
|
|
$row = Registry::get('db')->queryAssoc('
|
|
|
|
SELECT *
|
|
|
|
FROM assets
|
|
|
|
WHERE slug = {string:slug}',
|
|
|
|
[
|
|
|
|
'slug' => $slug,
|
|
|
|
]);
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2016-09-03 21:32:55 +02:00
|
|
|
return empty($row) ? false : self::byRow($row, $return_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function byRow(array $row, $return_format = 'object')
|
|
|
|
{
|
2017-12-20 14:51:23 +01:00
|
|
|
$db = Registry::get('db');
|
|
|
|
|
2016-09-03 21:32:55 +02:00
|
|
|
// Supplement with metadata.
|
2017-12-20 14:51:23 +01:00
|
|
|
$row['meta'] = $db->queryPair('
|
2016-09-01 23:13:23 +02:00
|
|
|
SELECT variable, value
|
|
|
|
FROM assets_meta
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
2016-09-03 21:32:55 +02:00
|
|
|
'id_asset' => $row['id_asset'],
|
2016-09-01 23:13:23 +02:00
|
|
|
]);
|
|
|
|
|
2017-12-20 14:51:23 +01:00
|
|
|
// And thumbnails.
|
|
|
|
$row['thumbnails'] = $db->queryPair('
|
|
|
|
SELECT
|
|
|
|
CONCAT(
|
|
|
|
width,
|
|
|
|
{string:x},
|
|
|
|
height,
|
|
|
|
IF(mode != {string:empty}, CONCAT({string:_}, mode), {string:empty})
|
|
|
|
) AS selector, filename
|
|
|
|
FROM assets_thumbs
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $row['id_asset'],
|
|
|
|
'empty' => '',
|
|
|
|
'x' => 'x',
|
|
|
|
'_' => '_',
|
|
|
|
]);
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
return $return_format == 'object' ? new Asset($row) : $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromIds(array $id_assets, $return_format = 'array')
|
|
|
|
{
|
|
|
|
if (empty($id_assets))
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$db = Registry::get('db');
|
|
|
|
|
|
|
|
$res = $db->query('
|
|
|
|
SELECT *
|
|
|
|
FROM assets
|
|
|
|
WHERE id_asset IN ({array_int:id_assets})
|
|
|
|
ORDER BY id_asset',
|
|
|
|
[
|
|
|
|
'id_assets' => $id_assets,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$assets = [];
|
|
|
|
while ($asset = $db->fetch_assoc($res))
|
|
|
|
{
|
|
|
|
$assets[$asset['id_asset']] = $asset;
|
|
|
|
$assets[$asset['id_asset']]['meta'] = [];
|
2017-12-20 14:51:23 +01:00
|
|
|
$assets[$asset['id_asset']]['thumbnails'] = [];
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$metas = $db->queryRows('
|
|
|
|
SELECT id_asset, variable, value
|
|
|
|
FROM assets_meta
|
|
|
|
WHERE id_asset IN ({array_int:id_assets})
|
|
|
|
ORDER BY id_asset',
|
|
|
|
[
|
|
|
|
'id_assets' => $id_assets,
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($metas as $meta)
|
|
|
|
$assets[$meta[0]]['meta'][$meta[1]] = $meta[2];
|
|
|
|
|
2017-12-20 14:51:23 +01:00
|
|
|
$thumbnails = $db->queryRows('
|
|
|
|
SELECT id_asset,
|
|
|
|
CONCAT(
|
|
|
|
width,
|
|
|
|
{string:x},
|
|
|
|
height,
|
|
|
|
IF(mode != {string:empty}, CONCAT({string:_}, mode), {string:empty})
|
|
|
|
) AS selector, filename
|
|
|
|
FROM assets_thumbs
|
|
|
|
WHERE id_asset IN ({array_int:id_assets})
|
|
|
|
ORDER BY id_asset',
|
|
|
|
[
|
|
|
|
'id_assets' => $id_assets,
|
|
|
|
'empty' => '',
|
|
|
|
'x' => 'x',
|
|
|
|
'_' => '_',
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($thumbnails as $thumb)
|
|
|
|
$assets[$thumb[0]]['thumbnails'][$thumb[1]] = $thumb[2];
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
if ($return_format == 'array')
|
|
|
|
return $assets;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$objects = [];
|
|
|
|
foreach ($assets as $id => $asset)
|
|
|
|
$objects[$id] = new Asset($asset);
|
|
|
|
return $objects;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createNew(array $data, $return_format = 'object')
|
|
|
|
{
|
|
|
|
// Extract the data array.
|
|
|
|
extract($data);
|
|
|
|
|
|
|
|
// No filename? Abort!
|
|
|
|
if (!isset($filename_to_copy) || !is_file($filename_to_copy))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// No subdir? Use YYYY/MM
|
|
|
|
if (!isset($preferred_subdir))
|
|
|
|
$preferred_subdir = date('Y') . '/' . date('m');
|
|
|
|
|
|
|
|
// Does this dir exist yet? If not, create it.
|
|
|
|
if (!is_dir(ASSETSDIR . '/' . $preferred_subdir))
|
|
|
|
mkdir(ASSETSDIR . '/' . $preferred_subdir, 0755, true);
|
|
|
|
|
|
|
|
// Construct the destination filename. Make sure we don't accidentally overwrite anything.
|
|
|
|
if (!isset($preferred_filename))
|
|
|
|
$preferred_filename = basename($filename_to_copy);
|
|
|
|
|
|
|
|
$new_filename = $preferred_filename;
|
|
|
|
$destination = ASSETSDIR . '/' . $preferred_subdir . '/' . $preferred_filename;
|
2023-03-12 12:02:21 +01:00
|
|
|
for ($i = 1; file_exists($destination); $i++)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2023-03-12 12:02:21 +01:00
|
|
|
$suffix = $i;
|
|
|
|
$filename = pathinfo($preferred_filename, PATHINFO_FILENAME) . ' (' . $suffix . ')';
|
2016-09-01 23:13:23 +02:00
|
|
|
$extension = pathinfo($preferred_filename, PATHINFO_EXTENSION);
|
|
|
|
$new_filename = $filename . '.' . $extension;
|
|
|
|
$destination = dirname($destination) . '/' . $new_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can we write to the target directory? Then copy the file.
|
|
|
|
if (is_writable(ASSETSDIR . '/' . $preferred_subdir))
|
|
|
|
copy($filename_to_copy, $destination);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Figure out the mime type for the file.
|
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
|
|
$mimetype = finfo_file($finfo, $destination);
|
|
|
|
finfo_close($finfo);
|
|
|
|
|
2023-03-12 12:02:21 +01:00
|
|
|
// We're going to need the base name a few times...
|
|
|
|
$basename = pathinfo($new_filename, PATHINFO_FILENAME);
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
// Do we have a title yet? Otherwise, use the filename.
|
2023-03-12 12:02:21 +01:00
|
|
|
$title = $data['title'] ?? $basename;
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
// Same with the slug.
|
2023-11-20 22:45:48 +01:00
|
|
|
$slug = $data['slug'] ?? self::cleanSlug(sprintf('%s/%s', $preferred_subdir, $basename));
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
// Detected an image?
|
|
|
|
if (substr($mimetype, 0, 5) == 'image')
|
|
|
|
{
|
|
|
|
$image = new Imagick($destination);
|
|
|
|
$d = $image->getImageGeometry();
|
|
|
|
|
|
|
|
// Get image dimensions, bearing orientation in mind.
|
|
|
|
switch ($image->getImageOrientation())
|
|
|
|
{
|
|
|
|
case Imagick::ORIENTATION_LEFTBOTTOM:
|
|
|
|
case Imagick::ORIENTATION_RIGHTTOP:
|
|
|
|
$image_width = $d['height'];
|
|
|
|
$image_height = $d['width'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$image_width = $d['width'];
|
|
|
|
$image_height = $d['height'];
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($image);
|
|
|
|
|
|
|
|
$exif = EXIF::fromFile($destination);
|
2016-09-04 14:15:38 +02:00
|
|
|
$date_captured = $exif->created_timestamp > 0 ? $exif->created_timestamp : time();
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$db = Registry::get('db');
|
|
|
|
$res = $db->query('
|
|
|
|
INSERT INTO assets
|
2016-09-02 21:41:01 +02:00
|
|
|
(id_user_uploaded, subdir, filename, title, slug, mimetype, image_width, image_height, date_captured, priority)
|
2016-09-01 23:13:23 +02:00
|
|
|
VALUES
|
2016-09-02 21:41:01 +02:00
|
|
|
({int:id_user_uploaded}, {string:subdir}, {string:filename}, {string:title}, {string:slug}, {string:mimetype},
|
2016-09-01 23:13:23 +02:00
|
|
|
{int:image_width}, {int:image_height},
|
|
|
|
IF({int:date_captured} > 0, FROM_UNIXTIME({int:date_captured}), NULL),
|
|
|
|
{int:priority})',
|
|
|
|
[
|
2016-09-02 21:41:01 +02:00
|
|
|
'id_user_uploaded' => isset($id_user) ? $id_user : Registry::get('user')->getUserId(),
|
2016-09-01 23:13:23 +02:00
|
|
|
'subdir' => $preferred_subdir,
|
|
|
|
'filename' => $new_filename,
|
|
|
|
'title' => $title,
|
|
|
|
'slug' => $slug,
|
|
|
|
'mimetype' => $mimetype,
|
|
|
|
'image_width' => isset($image_width) ? $image_width : 'NULL',
|
|
|
|
'image_height' => isset($image_height) ? $image_height : 'NULL',
|
|
|
|
'date_captured' => isset($date_captured) ? $date_captured : 'NULL',
|
|
|
|
'priority' => isset($priority) ? (int) $priority : 0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!$res)
|
|
|
|
{
|
|
|
|
unlink($destination);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['id_asset'] = $db->insert_id();
|
2022-12-25 13:44:54 +01:00
|
|
|
return $return_format === 'object' ? new self($data) : $data;
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->id_asset;
|
|
|
|
}
|
|
|
|
|
2016-09-04 21:34:08 +02:00
|
|
|
public function getAuthor()
|
|
|
|
{
|
|
|
|
return Member::fromId($this->id_user_uploaded);
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function getDateCaptured()
|
|
|
|
{
|
|
|
|
return $this->date_captured;
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:29:32 +01:00
|
|
|
public function getDeleteUrl()
|
|
|
|
{
|
|
|
|
return BASEURL . '/editasset/?id=' . $this->id_asset . '&delete';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEditUrl()
|
|
|
|
{
|
|
|
|
return BASEURL . '/editasset/?id=' . $this->id_asset;
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function getFilename()
|
|
|
|
{
|
|
|
|
return $this->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinkedPosts()
|
|
|
|
{
|
|
|
|
$posts = Registry::get('db')->queryValues('
|
|
|
|
SELECT id_post
|
|
|
|
FROM posts_assets
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
['id_asset' => $this->id_asset]);
|
|
|
|
|
|
|
|
// TODO: fix empty post iterator.
|
|
|
|
if (empty($posts))
|
|
|
|
return [];
|
|
|
|
|
|
|
|
return PostIterator::getByOptions([
|
|
|
|
'ids' => $posts,
|
|
|
|
'type' => '',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMeta()
|
|
|
|
{
|
|
|
|
return $this->meta;
|
|
|
|
}
|
|
|
|
|
2016-09-03 21:32:55 +02:00
|
|
|
public function getFullPath()
|
|
|
|
{
|
|
|
|
return ASSETSDIR . '/' . $this->subdir . '/' . $this->filename;
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:07:06 +01:00
|
|
|
public function getSlug()
|
|
|
|
{
|
|
|
|
return $this->slug;
|
|
|
|
}
|
|
|
|
|
2017-12-20 14:51:23 +01:00
|
|
|
public function getSubdir()
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
return $this->subdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPriority()
|
|
|
|
{
|
|
|
|
return $this->priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
if (!isset($this->tags))
|
|
|
|
$this->tags = Tag::byAssetId($this->id_asset);
|
|
|
|
|
|
|
|
return $this->tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return BASEURL . '/assets/' . $this->subdir . '/' . $this->filename;
|
|
|
|
}
|
|
|
|
|
2016-09-03 21:32:55 +02:00
|
|
|
public function getPageUrl()
|
|
|
|
{
|
|
|
|
return BASEURL . '/' . $this->slug . '/';
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
return substr($this->mimetype, 0, strpos($this->mimetype, '/'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDimensions($as_type = 'string')
|
|
|
|
{
|
|
|
|
return $as_type === 'string' ? $this->image_width . 'x' . $this->image_height : [$this->image_width, $this->image_height];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isImage()
|
|
|
|
{
|
2022-12-25 13:50:33 +01:00
|
|
|
return isset($this->mimetype) && substr($this->mimetype, 0, 5) === 'image';
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getImage()
|
|
|
|
{
|
|
|
|
if (!$this->isImage())
|
|
|
|
throw new Exception('Trying to upgrade an Asset to an Image while the Asset is not an image!');
|
|
|
|
|
|
|
|
return new Image(get_object_vars($this));
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:14:57 +01:00
|
|
|
public function isOwnedBy(User $user)
|
|
|
|
{
|
|
|
|
return $this->id_user_uploaded == $user->getUserId();
|
|
|
|
}
|
|
|
|
|
2023-11-12 17:26:03 +01:00
|
|
|
public function moveToSubDir($destSubDir)
|
|
|
|
{
|
|
|
|
// Verify the original exists
|
|
|
|
$source = ASSETSDIR . '/' . $this->subdir . '/' . $this->filename;
|
|
|
|
if (!file_exists($source))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Ensure the intended target file doesn't exist yet
|
|
|
|
$destDir = ASSETSDIR . '/' . $destSubDir;
|
|
|
|
$destFile = $destDir . '/' . $this->filename;
|
|
|
|
|
|
|
|
if (file_exists($destFile))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
// Can we write to the target directory?
|
|
|
|
if (!is_writable($destDir))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
// Perform move
|
|
|
|
if (rename($source, $destFile))
|
|
|
|
{
|
|
|
|
$this->subdir = $destSubDir;
|
|
|
|
$this->slug = $this->subdir . '/' . $this->title;
|
|
|
|
Registry::get('db')->query('
|
|
|
|
UPDATE assets
|
|
|
|
SET subdir = {string:subdir},
|
|
|
|
slug = {string:slug}
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'subdir' => $this->subdir,
|
|
|
|
'slug' => $this->slug,
|
|
|
|
]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function replaceFile($filename)
|
|
|
|
{
|
|
|
|
// No filename? Abort!
|
|
|
|
if (!isset($filename) || !is_readable($filename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Can we write to the target file?
|
|
|
|
$destination = ASSETSDIR . '/' . $this->subdir . '/' . $this->filename;
|
|
|
|
if (!is_writable($destination))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
copy($filename, $destination);
|
|
|
|
|
|
|
|
// Figure out the mime type for the file.
|
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
|
|
$this->mimetype = finfo_file($finfo, $destination);
|
|
|
|
finfo_close($finfo);
|
|
|
|
|
|
|
|
// Detected an image?
|
2022-12-25 13:44:54 +01:00
|
|
|
if (substr($this->mimetype, 0, 5) === 'image')
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
$image = new Imagick($destination);
|
|
|
|
$d = $image->getImageGeometry();
|
|
|
|
$this->image_width = $d['width'];
|
|
|
|
$this->image_height = $d['height'];
|
|
|
|
unset($image);
|
|
|
|
|
|
|
|
$exif = EXIF::fromFile($destination);
|
|
|
|
if (!empty($exif->created_timestamp))
|
|
|
|
$this->date_captured = new DateTime(date('r', $exif->created_timestamp));
|
|
|
|
else
|
|
|
|
$this->date_captured = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->image_width = null;
|
|
|
|
$this->image_height = null;
|
|
|
|
$this->date_captured = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Registry::get('db')->query('
|
|
|
|
UPDATE assets
|
|
|
|
SET
|
|
|
|
mimetype = {string:mimetype},
|
|
|
|
image_width = {int:image_width},
|
|
|
|
image_height = {int:image_height},
|
|
|
|
date_captured = {datetime:date_captured},
|
|
|
|
priority = {int:priority}
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'mimetype' => $this->mimetype,
|
|
|
|
'image_width' => isset($this->image_width) ? $this->image_width : 'NULL',
|
|
|
|
'image_height' => isset($this->image_height) ? $this->image_height : 'NULL',
|
|
|
|
'date_captured' => isset($this->date_captured) ? $this->date_captured : 'NULL',
|
|
|
|
'priority' => $this->priority,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function saveMetaData()
|
|
|
|
{
|
|
|
|
$this->setMetaData($this->meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMetaData(array $new_meta, $mode = 'replace')
|
|
|
|
{
|
|
|
|
$db = Registry::get('db');
|
|
|
|
|
|
|
|
// If we're replacing, delete current data first.
|
|
|
|
if ($mode === 'replace')
|
|
|
|
{
|
|
|
|
$to_remove = array_diff_key($this->meta, $new_meta);
|
|
|
|
if (!empty($to_remove))
|
|
|
|
$db->query('
|
|
|
|
DELETE FROM assets_meta
|
|
|
|
WHERE id_asset = {int:id_asset} AND
|
|
|
|
variable IN({array_string:variables})',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'variables' => array_keys($to_remove),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build rows
|
|
|
|
$to_insert = [];
|
|
|
|
foreach ($new_meta as $key => $value)
|
|
|
|
$to_insert[] = [
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'variable' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Do the insertion
|
|
|
|
$res = Registry::get('db')->insert('replace', 'assets_meta', [
|
|
|
|
'id_asset' => 'int',
|
|
|
|
'variable' => 'string',
|
|
|
|
'value' => 'string',
|
|
|
|
], $to_insert, ['id_asset', 'variable']);
|
|
|
|
|
|
|
|
if ($res)
|
|
|
|
$this->meta = $new_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
2017-11-05 16:27:12 +01:00
|
|
|
$db = Registry::get('db');
|
|
|
|
|
2023-03-12 12:21:43 +01:00
|
|
|
// First: delete associated metadata
|
2018-07-07 14:21:12 +02:00
|
|
|
$db->query('
|
2018-07-08 10:19:37 +02:00
|
|
|
DELETE FROM assets_meta
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
|
|
|
|
2023-03-12 12:21:43 +01:00
|
|
|
// Second: figure out what tags to recount cardinality for
|
2019-09-29 14:20:43 +02:00
|
|
|
$recount_tags = $db->queryValues('
|
2018-07-08 10:19:37 +02:00
|
|
|
SELECT id_tag
|
|
|
|
FROM assets_tags
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
2018-07-07 14:21:12 +02:00
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
|
|
|
|
2017-11-05 16:27:12 +01:00
|
|
|
$db->query('
|
2018-07-08 10:19:37 +02:00
|
|
|
DELETE FROM assets_tags
|
2017-11-05 16:27:12 +01:00
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
|
|
|
|
2018-07-08 10:19:37 +02:00
|
|
|
Tag::recount($recount_tags);
|
|
|
|
|
2023-03-12 12:21:43 +01:00
|
|
|
// Third: figure out what associated thumbs to delete
|
|
|
|
$thumbs_to_delete = $db->queryValues('
|
|
|
|
SELECT filename
|
|
|
|
FROM assets_thumbs
|
2016-09-01 23:13:23 +02:00
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
2018-07-08 10:19:37 +02:00
|
|
|
|
2023-03-12 12:21:43 +01:00
|
|
|
foreach ($thumbs_to_delete as $filename)
|
|
|
|
{
|
|
|
|
$thumb_path = THUMBSDIR . '/' . $this->subdir . '/' . $filename;
|
|
|
|
if (is_file($thumb_path))
|
|
|
|
unlink($thumb_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->query('
|
|
|
|
DELETE FROM assets_thumbs
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Reset asset ID for tags that use this asset for their thumbnail
|
2018-07-08 10:19:37 +02:00
|
|
|
$rows = $db->query('
|
|
|
|
SELECT id_tag
|
|
|
|
FROM tags
|
2019-09-29 14:20:43 +02:00
|
|
|
WHERE id_asset_thumb = {int:id_asset}',
|
2018-07-08 10:19:37 +02:00
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!empty($rows))
|
|
|
|
{
|
|
|
|
foreach ($rows as $row)
|
|
|
|
{
|
|
|
|
$tag = Tag::fromId($row['id_tag']);
|
|
|
|
$tag->resetIdAsset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 12:21:43 +01:00
|
|
|
// Finally, delete the actual asset
|
|
|
|
if (!unlink(ASSETSDIR . '/' . $this->subdir . '/' . $this->filename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$return = $db->query('
|
|
|
|
DELETE FROM assets
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
]);
|
|
|
|
|
2018-07-08 10:19:37 +02:00
|
|
|
return $return;
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function linkTags(array $id_tags)
|
|
|
|
{
|
|
|
|
if (empty($id_tags))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
$pairs = [];
|
|
|
|
foreach ($id_tags as $id_tag)
|
|
|
|
$pairs[] = ['id_asset' => $this->id_asset, 'id_tag' => $id_tag];
|
|
|
|
|
|
|
|
Registry::get('db')->insert('ignore', 'assets_tags', [
|
|
|
|
'id_asset' => 'int',
|
|
|
|
'id_tag' => 'int',
|
|
|
|
], $pairs, ['id_asset', 'id_tag']);
|
|
|
|
|
|
|
|
Tag::recount($id_tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unlinkTags(array $id_tags)
|
|
|
|
{
|
|
|
|
if (empty($id_tags))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Registry::get('db')->query('
|
|
|
|
DELETE FROM assets_tags
|
|
|
|
WHERE id_asset = {int:id_asset} AND id_tag IN ({array_int:id_tags})',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'id_tags' => $id_tags,
|
|
|
|
]);
|
|
|
|
|
|
|
|
Tag::recount($id_tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getCount()
|
|
|
|
{
|
2017-11-05 16:27:12 +01:00
|
|
|
return Registry::get('db')->queryValue('
|
2016-09-01 23:13:23 +02:00
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM assets');
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:07:06 +01:00
|
|
|
public function setKeyData($title, $slug, DateTime $date_captured = null, $priority)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
$params = [
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'title' => $title,
|
2018-02-22 20:07:06 +01:00
|
|
|
'slug' => $slug,
|
2016-09-01 23:13:23 +02:00
|
|
|
'priority' => $priority,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (isset($date_captured))
|
|
|
|
$params['date_captured'] = $date_captured->format('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
return Registry::get('db')->query('
|
|
|
|
UPDATE assets
|
2018-02-22 20:07:06 +01:00
|
|
|
SET title = {string:title},
|
|
|
|
slug = {string:slug},' . (isset($date_captured) ? '
|
2016-09-01 23:13:23 +02:00
|
|
|
date_captured = {datetime:date_captured},' : '') . '
|
|
|
|
priority = {int:priority}
|
|
|
|
WHERE id_asset = {int:id_asset}',
|
|
|
|
$params);
|
|
|
|
}
|
2016-09-04 16:00:39 +02:00
|
|
|
|
|
|
|
public function getUrlForPreviousInSet($id_tag = null)
|
|
|
|
{
|
|
|
|
$row = Registry::get('db')->queryAssoc('
|
|
|
|
SELECT a.*
|
|
|
|
' . (isset($id_tag) ? '
|
|
|
|
FROM assets_tags AS t
|
|
|
|
INNER JOIN assets AS a ON a.id_asset = t.id_asset
|
|
|
|
WHERE t.id_tag = {int:id_tag} AND
|
2022-11-22 11:41:54 +01:00
|
|
|
(a.date_captured, a.id_asset) < ({datetime:date_captured}, {int:id_asset})
|
|
|
|
ORDER BY a.date_captured DESC, a.id_asset DESC'
|
2016-09-04 16:00:39 +02:00
|
|
|
: '
|
|
|
|
FROM assets AS a
|
2022-11-22 11:41:54 +01:00
|
|
|
WHERE (a.date_captured, a.id_asset) > ({datetime:date_captured}, {int:id_asset})
|
|
|
|
ORDER BY date_captured ASC, a.id_asset ASC')
|
2016-09-04 16:00:39 +02:00
|
|
|
. '
|
|
|
|
LIMIT 1',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'id_tag' => $id_tag,
|
|
|
|
'date_captured' => $this->date_captured,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($row)
|
|
|
|
{
|
|
|
|
$obj = self::byRow($row, 'object');
|
|
|
|
return $obj->getPageUrl() . ($id_tag ? '?in=' . $id_tag : '');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrlForNextInSet($id_tag = null)
|
|
|
|
{
|
|
|
|
$row = Registry::get('db')->queryAssoc('
|
|
|
|
SELECT a.*
|
|
|
|
' . (isset($id_tag) ? '
|
|
|
|
FROM assets_tags AS t
|
|
|
|
INNER JOIN assets AS a ON a.id_asset = t.id_asset
|
|
|
|
WHERE t.id_tag = {int:id_tag} AND
|
2022-11-22 11:41:54 +01:00
|
|
|
(a.date_captured, a.id_asset) > ({datetime:date_captured}, {int:id_asset})
|
|
|
|
ORDER BY a.date_captured ASC, a.id_asset ASC'
|
2016-09-04 16:00:39 +02:00
|
|
|
: '
|
|
|
|
FROM assets AS a
|
2022-11-22 11:41:54 +01:00
|
|
|
WHERE (a.date_captured, a.id_asset) < ({datetime:date_captured}, {int:id_asset})
|
|
|
|
ORDER BY date_captured DESC, a.id_asset DESC')
|
2016-09-04 16:00:39 +02:00
|
|
|
. '
|
|
|
|
LIMIT 1',
|
|
|
|
[
|
|
|
|
'id_asset' => $this->id_asset,
|
|
|
|
'id_tag' => $id_tag,
|
|
|
|
'date_captured' => $this->date_captured,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($row)
|
|
|
|
{
|
|
|
|
$obj = self::byRow($row, 'object');
|
|
|
|
return $obj->getPageUrl() . ($id_tag ? '?in=' . $id_tag : '');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|