julienmonnerie/kirby/src/Cms/HasFiles.php

190 lines
3.5 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
namespace Kirby\Cms;
2022-12-19 14:56:05 +01:00
use Kirby\Uuid\Uuid;
2022-06-17 17:51:59 +02:00
/**
* HasFiles
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
trait HasFiles
{
2022-08-31 15:02:43 +02:00
/**
* The Files collection
*/
2025-04-21 18:57:21 +02:00
protected Files|array|null $files = null;
2022-08-31 15:02:43 +02:00
/**
* Filters the Files collection by type audio
*/
2025-04-21 18:57:21 +02:00
public function audio(): Files
2022-08-31 15:02:43 +02:00
{
return $this->files()->filter('type', '==', 'audio');
}
/**
* Filters the Files collection by type code
*/
2025-04-21 18:57:21 +02:00
public function code(): Files
2022-08-31 15:02:43 +02:00
{
return $this->files()->filter('type', '==', 'code');
}
/**
* Creates a new file
*
2023-04-14 16:34:06 +02:00
* @param bool $move If set to `true`, the source will be deleted
2022-08-31 15:02:43 +02:00
*/
2025-04-21 18:57:21 +02:00
public function createFile(array $props, bool $move = false): File
2022-08-31 15:02:43 +02:00
{
$props = array_merge($props, [
'parent' => $this,
'url' => null
]);
2023-04-14 16:34:06 +02:00
return File::create($props, $move);
2022-08-31 15:02:43 +02:00
}
/**
* Filters the Files collection by type documents
*/
2025-04-21 18:57:21 +02:00
public function documents(): Files
2022-08-31 15:02:43 +02:00
{
return $this->files()->filter('type', '==', 'document');
}
/**
* Returns a specific file by filename or the first one
*/
2025-04-21 18:57:21 +02:00
public function file(
string|null $filename = null,
string $in = 'files'
): File|null {
2022-08-31 15:02:43 +02:00
if ($filename === null) {
return $this->$in()->first();
}
2022-12-19 14:56:05 +01:00
// find by global UUID
if (Uuid::is($filename, 'file') === true) {
2024-12-20 12:37:52 +01:00
return Uuid::for($filename, $this->$in())->model();
2022-12-19 14:56:05 +01:00
}
2022-08-31 15:02:43 +02:00
if (strpos($filename, '/') !== false) {
$path = dirname($filename);
$filename = basename($filename);
if ($page = $this->find($path)) {
return $page->$in()->find($filename);
}
return null;
}
return $this->$in()->find($filename);
}
/**
* Returns the Files collection
*/
2025-04-21 18:57:21 +02:00
public function files(): Files
2022-08-31 15:02:43 +02:00
{
2022-12-19 14:56:05 +01:00
if ($this->files instanceof Files) {
2022-08-31 15:02:43 +02:00
return $this->files;
}
return $this->files = Files::factory($this->inventory()['files'], $this);
}
/**
* Checks if the Files collection has any audio files
*/
public function hasAudio(): bool
{
return $this->audio()->count() > 0;
}
/**
* Checks if the Files collection has any code files
*/
public function hasCode(): bool
{
return $this->code()->count() > 0;
}
/**
* Checks if the Files collection has any document files
*/
public function hasDocuments(): bool
{
return $this->documents()->count() > 0;
}
/**
* Checks if the Files collection has any files
*/
public function hasFiles(): bool
{
return $this->files()->count() > 0;
}
/**
* Checks if the Files collection has any images
*/
public function hasImages(): bool
{
return $this->images()->count() > 0;
}
/**
* Checks if the Files collection has any videos
*/
public function hasVideos(): bool
{
return $this->videos()->count() > 0;
}
/**
* Returns a specific image by filename or the first one
*/
2025-04-21 18:57:21 +02:00
public function image(string|null $filename = null): File|null
2022-08-31 15:02:43 +02:00
{
return $this->file($filename, 'images');
}
/**
* Filters the Files collection by type image
*/
2025-04-21 18:57:21 +02:00
public function images(): Files
2022-08-31 15:02:43 +02:00
{
return $this->files()->filter('type', '==', 'image');
}
/**
* Sets the Files collection
*
* @return $this
*/
2025-04-21 18:57:21 +02:00
protected function setFiles(array|null $files = null): static
2022-08-31 15:02:43 +02:00
{
if ($files !== null) {
$this->files = Files::factory($files, $this);
}
return $this;
}
/**
* Filters the Files collection by type videos
*/
2025-04-21 18:57:21 +02:00
public function videos(): Files
2022-08-31 15:02:43 +02:00
{
return $this->files()->filter('type', '==', 'video');
}
2022-06-17 17:51:59 +02:00
}