mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Added relationship logic for flex
This commit is contained in:
parent
6ba1cff114
commit
2957077935
|
|
@ -275,6 +275,7 @@ class FlexDirectoryForm implements FlexDirectoryFormInterface, JsonSerializable
|
|||
'unique_id' => $this->getUniqueId(),
|
||||
'form_name' => $this->getName(),
|
||||
'folder' => $this->getFlashFolder(),
|
||||
'id' => $this->getFlashId(),
|
||||
'directory' => $this->getDirectory()
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -326,6 +326,7 @@ class FlexForm implements FlexObjectFormInterface, JsonSerializable
|
|||
'unique_id' => $this->getUniqueId(),
|
||||
'form_name' => $this->getName(),
|
||||
'folder' => $this->getFlashFolder(),
|
||||
'id' => $this->getFlashId(),
|
||||
'object' => $this->getObject()
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ namespace Grav\Framework\Flex\Traits;
|
|||
use Grav\Common\Config\Config;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaObjectInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaUploadInterface;
|
||||
use Grav\Common\Media\Traits\MediaTrait;
|
||||
use Grav\Common\Page\Media;
|
||||
|
|
@ -22,6 +23,8 @@ use Grav\Framework\Cache\CacheInterface;
|
|||
use Grav\Framework\Filesystem\Filesystem;
|
||||
use Grav\Framework\Flex\FlexDirectory;
|
||||
use Grav\Framework\Form\FormFlashFile;
|
||||
use Grav\Framework\Media\MediaObject;
|
||||
use Grav\Framework\Media\UploadedMediaObject;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
use RuntimeException;
|
||||
|
|
@ -30,7 +33,6 @@ use function in_array;
|
|||
use function is_array;
|
||||
use function is_callable;
|
||||
use function is_int;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
use function strpos;
|
||||
|
||||
|
|
@ -297,6 +299,54 @@ trait FlexMediaTrait
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $field
|
||||
* @param string $filename
|
||||
* @param MediaObjectInterface|null $image
|
||||
* @return MediaObject|UploadedMediaObject
|
||||
*/
|
||||
protected function buildMediaObject(?string $field, string $filename, MediaObjectInterface $image = null)
|
||||
{
|
||||
if (!$image) {
|
||||
$media = $field ? $this->getMediaField($field) : null;
|
||||
if ($media) {
|
||||
$image = $media[$filename];
|
||||
}
|
||||
}
|
||||
|
||||
return new MediaObject($field, $filename, $image, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $field
|
||||
* @return array
|
||||
*/
|
||||
protected function buildMediaList(?string $field): array
|
||||
{
|
||||
$names = $field ? (array)$this->getNestedProperty($field) : [];
|
||||
$media = $field ? $this->getMediaField($field) : null;
|
||||
if (null === $media) {
|
||||
$media = $this->getMedia();
|
||||
}
|
||||
|
||||
$list = [];
|
||||
foreach ($names as $key => $val) {
|
||||
$name = is_string($val) ? $val : $key;
|
||||
$medium = $media[$name];
|
||||
if ($medium) {
|
||||
if ($medium->uploaded_file) {
|
||||
$upload = $medium->uploaded_file;
|
||||
|
||||
$list[] = new UploadedMediaObject($upload->getId(), $field, $name, $upload);
|
||||
} else {
|
||||
$list[] = $this->buildMediaObject($field, $name, $medium);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $files
|
||||
* @return void
|
||||
|
|
@ -388,6 +438,7 @@ trait FlexMediaTrait
|
|||
$updated = true;
|
||||
if ($medium) {
|
||||
$medium->uploaded = true;
|
||||
$medium->uploaded_file = $upload;
|
||||
$media->add($filename, $medium);
|
||||
} elseif (is_callable([$media, 'hide'])) {
|
||||
$media->hide($filename);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ use Grav\Framework\Contracts\Relationships\RelationshipsInterface;
|
|||
use Grav\Framework\Flex\FlexIdentifier;
|
||||
use Grav\Framework\Relationships\Relationships;
|
||||
|
||||
/**
|
||||
* Trait FlexRelationshipsTrait
|
||||
*/
|
||||
trait FlexRelationshipsTrait
|
||||
{
|
||||
private ?RelationshipsInterface $_relationships = null;
|
||||
|
|
@ -40,4 +43,18 @@ trait FlexRelationshipsTrait
|
|||
{
|
||||
$this->_relationships = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable $collection
|
||||
* @return array
|
||||
*/
|
||||
protected function buildFlexIdentifierList(iterable $collection): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($collection as $object) {
|
||||
$list[] = FlexIdentifier::createFromObject($object);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ class FormFlash implements FormFlashInterface
|
|||
/** @var bool */
|
||||
protected $exists;
|
||||
/** @var string */
|
||||
protected $id;
|
||||
/** @var string */
|
||||
protected $sessionId;
|
||||
/** @var string */
|
||||
protected $uniqueId;
|
||||
|
|
@ -75,9 +77,12 @@ class FormFlash implements FormFlashInterface
|
|||
});
|
||||
}
|
||||
|
||||
$this->sessionId = $config['session_id'] ?? 'no-session';
|
||||
$this->id = $config['id'] ?? '';
|
||||
$this->sessionId = $config['session_id'] ?? '';
|
||||
$this->uniqueId = $config['unique_id'] ?? '';
|
||||
|
||||
$this->setUser($config['user'] ?? null);
|
||||
|
||||
$folder = $config['folder'] ?? ($this->sessionId ? 'tmp://forms/' . $this->sessionId : '');
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
|
|
@ -133,6 +138,14 @@ class FormFlash implements FormFlashInterface
|
|||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id && $this->uniqueId ? $this->id . '/' . $this->uniqueId : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
|
@ -390,8 +403,8 @@ class FormFlash implements FormFlashInterface
|
|||
*/
|
||||
public function clearFiles()
|
||||
{
|
||||
foreach ($this->files as $field => $files) {
|
||||
foreach ($files as $name => $upload) {
|
||||
foreach ($this->files as $files) {
|
||||
foreach ($files as $upload) {
|
||||
$this->removeTmpFile($upload['tmp_name'] ?? '');
|
||||
}
|
||||
}
|
||||
|
|
@ -406,6 +419,7 @@ class FormFlash implements FormFlashInterface
|
|||
{
|
||||
return [
|
||||
'form' => $this->formName,
|
||||
'id' => $this->getId(),
|
||||
'unique_id' => $this->uniqueId,
|
||||
'url' => $this->url,
|
||||
'user' => $this->user,
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ use function sprintf;
|
|||
*/
|
||||
class FormFlashFile implements UploadedFileInterface, JsonSerializable
|
||||
{
|
||||
/** @var string */
|
||||
private $id;
|
||||
/** @var string */
|
||||
private $field;
|
||||
/** @var bool */
|
||||
|
|
@ -45,6 +47,7 @@ class FormFlashFile implements UploadedFileInterface, JsonSerializable
|
|||
*/
|
||||
public function __construct(string $field, array $upload, FormFlash $flash)
|
||||
{
|
||||
$this->id = $flash->getId() ?: $flash->getUniqueId();
|
||||
$this->field = $field;
|
||||
$this->upload = $upload;
|
||||
$this->flash = $flash;
|
||||
|
|
@ -107,6 +110,11 @@ class FormFlashFile implements UploadedFileInterface, JsonSerializable
|
|||
}
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
|
@ -222,6 +230,7 @@ class FormFlashFile implements UploadedFileInterface, JsonSerializable
|
|||
public function __debugInfo()
|
||||
{
|
||||
return [
|
||||
'id:private' => $this->id,
|
||||
'field:private' => $this->field,
|
||||
'moved:private' => $this->moved,
|
||||
'upload:private' => $this->upload,
|
||||
|
|
|
|||
|
|
@ -22,6 +22,13 @@ interface FormFlashInterface extends \JsonSerializable
|
|||
*/
|
||||
public function __construct($config);
|
||||
|
||||
/**
|
||||
* Get unique form flash id if set.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string;
|
||||
|
||||
/**
|
||||
* Get session Id associated to this form instance.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -453,10 +453,10 @@ trait FormTrait
|
|||
'session_id' => $this->getSessionId(),
|
||||
'unique_id' => $this->getUniqueId(),
|
||||
'form_name' => $this->getName(),
|
||||
'folder' => $this->getFlashFolder()
|
||||
'folder' => $this->getFlashFolder(),
|
||||
'id' => $this->getFlashId()
|
||||
];
|
||||
|
||||
|
||||
$this->flash = new FormFlash($config);
|
||||
$this->flash->setUrl($grav['uri']->url)->setUser($grav['user'] ?? null);
|
||||
}
|
||||
|
|
@ -486,7 +486,8 @@ trait FormTrait
|
|||
'session_id' => $this->getSessionId(),
|
||||
'unique_id' => $uniqueId,
|
||||
'form_name' => $name,
|
||||
'folder' => $this->getFlashFolder()
|
||||
'folder' => $this->getFlashFolder(),
|
||||
'id' => $this->getFlashId()
|
||||
];
|
||||
$flash = new FormFlash($config);
|
||||
if ($flash->exists() && $flash->getFormName() === $name) {
|
||||
|
|
@ -614,6 +615,28 @@ trait FormTrait
|
|||
return strpos($path, '!!') === false ? rtrim($path, '/') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getFlashId(): ?string
|
||||
{
|
||||
// Fill template token keys/value pairs.
|
||||
$dataMap = [
|
||||
'[FORM_NAME]' => $this->getName(),
|
||||
'[SESSIONID]' => 'session',
|
||||
'[USERNAME]' => '!!',
|
||||
'[USERNAME_OR_SESSIONID]' => '!!',
|
||||
'[ACCOUNT]' => 'account'
|
||||
];
|
||||
|
||||
$flashLookupFolder = $this->getFlashLookupFolder();
|
||||
|
||||
$path = str_replace(array_keys($dataMap), array_values($dataMap), $flashLookupFolder);
|
||||
|
||||
// Make sure we only return valid paths.
|
||||
return strpos($path, '!!') === false ? rtrim($path, '/') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ class MediaIdentifier extends Identifier
|
|||
if ($flash->exists()) {
|
||||
$uploadedFile = $flash->getFilesByField($field)[$filename] ?? null;
|
||||
|
||||
$this->object = new UploadedMediaObject($field, $filename, $flash, $uploadedFile);
|
||||
$this->object = UploadedMediaObject::createFromFlash($flash, $field, $filename, $uploadedFile);
|
||||
}
|
||||
} else {
|
||||
$type = array_shift($parts);
|
||||
|
|
|
|||
|
|
@ -14,8 +14,7 @@ use Throwable;
|
|||
*/
|
||||
class MediaObject implements MediaObjectInterface
|
||||
{
|
||||
// FIXME:
|
||||
static public string $placeholderImage = 'theme://img/revkit-temp.svg';
|
||||
static public string $placeholderImage = 'image://media/thumb.png';
|
||||
|
||||
public FlexObjectInterface $object;
|
||||
public ?GravMediaObjectInterface $media;
|
||||
|
|
|
|||
|
|
@ -13,28 +13,41 @@ use Psr\Http\Message\UploadedFileInterface;
|
|||
*/
|
||||
class UploadedMediaObject implements MediaObjectInterface
|
||||
{
|
||||
// FIXME:
|
||||
static public string $placeholderImage = 'theme://img/revkit-temp.svg';
|
||||
static public string $placeholderImage = 'image://media/thumb.png';
|
||||
|
||||
public FormFlashInterface $object;
|
||||
|
||||
private string $id;
|
||||
private ?string $field;
|
||||
private string $filename;
|
||||
private array $meta;
|
||||
private ?UploadedFileInterface $uploadedFile;
|
||||
|
||||
/**
|
||||
* UploadedMediaObject constructor.
|
||||
* @param FlexFormFlash $flash
|
||||
* @param string|null $field
|
||||
* @param string $filename
|
||||
* @param UploadedFileInterface|null $uploadedFile
|
||||
* @return static
|
||||
*/
|
||||
public static function createFromFlash(FlexFormFlash $flash, ?string $field, string $filename, ?UploadedFileInterface $uploadedFile = null)
|
||||
{
|
||||
$id = $flash->getId();
|
||||
|
||||
return new static($id, $field, $filename, $uploadedFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string|null $field
|
||||
* @param string $filename
|
||||
* @param FormFlashInterface $object
|
||||
* @param UploadedFileInterface|null $uploadedFile
|
||||
*/
|
||||
public function __construct(?string $field, string $filename, FormFlashInterface $object, ?UploadedFileInterface $uploadedFile = null)
|
||||
public function __construct(string $id, ?string $field, string $filename, ?UploadedFileInterface $uploadedFile = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->field = $field;
|
||||
$this->filename = $filename;
|
||||
$this->object = $object;
|
||||
$this->uploadedFile = $uploadedFile;
|
||||
if ($uploadedFile) {
|
||||
$this->meta = [
|
||||
|
|
@ -60,15 +73,8 @@ class UploadedMediaObject implements MediaObjectInterface
|
|||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
$id = $this->id;
|
||||
$field = $this->field;
|
||||
$object = $this->object;
|
||||
if ($object instanceof FlexFormFlash) {
|
||||
$type = $object->getObject()->getFlexType();
|
||||
} else {
|
||||
$type = 'undefined';
|
||||
}
|
||||
|
||||
$id = $type . '/' . $object->getUniqueId();
|
||||
$path = $field ? "/{$field}/" : '';
|
||||
|
||||
return 'uploads/' . $id . $path . basename($this->filename);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user