Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: model select #104

Merged
merged 8 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Services/Assets/Components/ComponentModelSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace NotFound\Framework\Services\Assets\Components;

use NotFound\Layout\Elements\AbstractLayout;
use NotFound\Layout\Elements\Table\LayoutTableColumn;
use NotFound\Layout\Inputs\LayoutInputDropdown;

class ComponentModelSelect extends AbstractComponent
{
public function getAutoLayoutClass(): ?AbstractLayout
{
$inputDropdown = new LayoutInputDropdown($this->assetItem->internal, $this->assetItem->name);

$items = $this->getModels();

foreach ($items as $key => $value) {
$inputDropdown->addItem(
$key,
$value,
);
}

return $inputDropdown;
}

public function validate($newValue): bool
{
// TODO: Implement validate() method.
return true;
}

public function getTableOverviewContent(): LayoutTableColumn
{
$value = $this->properties()->selectedModel::{$this->properties()->methodName}($this->getCurrentValue(), true);

$display = ($value) ? $value : '-';

return new LayoutTableColumn($display, $this->type);
}

private function getModels()
{
return $this->properties()->selectedModel::{$this->properties()->methodName}($this->getCurrentValue());
}

/**
* Get the value used in the default storage mechanism.
* This is always a string. Use JSON or your own logic for other types of values.
*/
public function getValueForStorage(): ?string
{
return $this->newValue === '' ? null : $this->newValue;
}
}
37 changes: 37 additions & 0 deletions src/Services/Editor/Fields/ModelSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace NotFound\Framework\Services\Editor\Fields;

use NotFound\Framework\Services\Editor\Properties;

class ModelSelect extends Properties
{
public function description(): string
{
return 'Model dropdown';
}

public function properties(): void
{
$this->allOverviewOptions();

$this->addText('selectedModel', 'Custom model path', true, default: 'App\\Models\\');
}

public function serverProperties(): void
{
$this->addText('methodName', 'Method', true);
}

public function checkColumnType(?\Doctrine\DBAL\Types\Type $type): string
{
if ($type === null) {
return 'COLUMN MISSING';
}
if (! in_array($type->getName(), ['string', 'integer'])) {
return 'TYPE ERROR: '.$type->getName().' is not a valid type for a table select field';
}

return '';
}
}
2 changes: 1 addition & 1 deletion src/Services/Editor/FieldsProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($properties = null)

public function availableFields()
{
$fields = ['Text', 'Checkbox', 'ChildTable', 'DropDown', 'Tags', 'Description', 'TableSelect', 'Header', 'DatePicker', 'TimePicker', 'DateTimePicker', 'Image', 'File', 'ContentBlocks', 'Button', 'Slug', 'Number'];
$fields = ['Text', 'Checkbox', 'ChildTable', 'DropDown', 'Tags', 'Description', 'TableSelect', 'Header', 'DatePicker', 'TimePicker', 'DateTimePicker', 'Image', 'File', 'ContentBlocks', 'Button', 'Slug', 'Number', 'ModelSelect'];
sort($fields);

return $fields;
Expand Down
Loading