Skip to content

Commit

Permalink
refactor to abstract component data from components so we can use dra…
Browse files Browse the repository at this point in the history
…fts/revisions without duplicating everything
  • Loading branch information
markhuot committed Oct 7, 2023
1 parent 02d9bc3 commit 3dde39f
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/actions/CompileTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Craft;
use craft\helpers\App;
use craft\web\View;
use markhuot\keystone\base\ComponentData;
use markhuot\keystone\base\ComponentType;
use markhuot\keystone\base\FieldDefinition;
use markhuot\keystone\models\Component;
use markhuot\keystone\models\ComponentData;
use PhpParser\Builder\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt;
Expand Down
62 changes: 0 additions & 62 deletions src/base/ComponentData.php

This file was deleted.

39 changes: 30 additions & 9 deletions src/controllers/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use markhuot\keystone\actions\AddComponent;
use markhuot\keystone\actions\GetComponentType;
use markhuot\keystone\models\Component;
use markhuot\keystone\models\ComponentData;
use markhuot\keystone\models\ComponentElement;

class ComponentsController extends Controller
{
Expand All @@ -33,25 +35,36 @@ public function actionAdd()

public function actionStore()
{
$componentData = new ComponentData;
$componentData->type = $this->request->getRequiredBodyParam('type');
$componentData->save();

$component = new Component;
$component->elementId = $elementId = $this->request->getRequiredBodyParam('elementId');
$component->fieldId = $fieldId = $this->request->getRequiredBodyParam('fieldId');
$component->dataId = $componentData->id;
$component->path = $path = $this->request->getBodyParam('path');
$slot = $this->request->getBodyParam('slot');
$slot = $slot === '' ? null : $slot;
$component->slot = $slot;
$component->slot = $this->request->getBodyParam('slot');
$component->type = $this->request->getRequiredBodyParam('type');
$component->sortOrder = ((Component::find()->where([
'elementId' => $elementId,
'fieldId' => $fieldId,
'path' => $path,
'slot' => $slot,
'path' => $component->path,
'slot' => $component->slot,
])->max('sortOrder')) ?? -1) + 1;
$component->save();

$element = Craft::$app->elements->getElementById($component->elementId);
$field = Craft::$app->fields->getFieldById($component->fieldId);

return $component->errors ?
$this->asFailure('Oh no') :
$this->asSuccess('Component added');
$this->asSuccess('Component added', [
'elementId' => $component->elementId,
'fieldId' => $component->fieldId,
'fieldHandle' => $field->handle,
'fieldHtml' => $field->getInputHtml(null, $element),
]);
}

public function actionEdit()
Expand All @@ -76,10 +89,18 @@ public function actionUpdate()
$data = $this->request->getBodyParam('fields', []);

$component = Component::findOne(['id' => $id]);
$component->data = array_merge($component->data->toArray(), $data);
$component->save();
$component->data->merge($data);
$component->data->save();

$element = Craft::$app->elements->getElementById($component->elementId);
$field = Craft::$app->fields->getFieldById($component->fieldId);

return $this->asSuccess('Component saved');
return $this->asSuccess('Component saved', [
'elementId' => $component->elementId,
'fieldId' => $component->fieldId,
'fieldHandle' => $field->handle,
'fieldHtml' => $field->getInputHtml(null, $element),
]);
}

public function actionGetEditModalHtml()
Expand Down
7 changes: 4 additions & 3 deletions src/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class ActiveRecord extends \craft\db\ActiveRecord
public function __set($name, $value): void
{
if (method_exists($this, $methodName= 'set' . ucfirst($name))) {
$value = $this->{$methodName}($value);
$this->{$methodName}($value);
}
else {
parent::__set($name, $value);
}

parent::__set($name, $value);
}

public function getRawAttributes(): array
Expand Down
2 changes: 1 addition & 1 deletion src/db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
class Table
{
const COMPONENTS = '{{%keystone_components}}';
const COMPONENTS_ELEMENTS = '{{%keystone_components_elements}}';
const COMPONENT_DATA = '{{%keystone_component_data}}';
}
6 changes: 1 addition & 5 deletions src/fields/Keystone.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
use craft\web\View;
use markhuot\keystone\actions\GetComponentType;
use markhuot\keystone\models\Component;
use markhuot\keystone\models\ComponentElement;
use Twig\Markup;

class Keystone extends Field
{
protected function getFragment(ElementInterface $element)
{
$component = new Component;
$component->elementId = $element->id;
$component->fieldId = $this->id;
$component->sortOrder = 0;
$component->level = 0;
$component->slot = null;
$component->type = 'keystone/fragment';
$component->setSlotted(Component::find()->where([
'elementId' => $element->id,
Expand Down
18 changes: 8 additions & 10 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ class Install extends Migration
{
public function safeUp()
{
$this->createTable(Table::COMPONENTS, [
$this->createTable(Table::COMPONENT_DATA, [
'id' => $this->primaryKey(),
'elementId' => $this->bigInteger()->unsigned(),
'fieldId' => $this->bigInteger()->unsigned(),
'type' => $this->string(256),
'sortOrder' => $this->integer(),
'path' => $this->string(1024),
'level' => $this->integer(),
'slot' => $this->string(256),
'data' => $this->json(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'uid' => $this->uid(),
]);

$this->createTable(Table::COMPONENTS_ELEMENTS, [
$this->createTable(Table::COMPONENTS, [
'id' => $this->primaryKey(),
'elementId' => $this->bigInteger()->unsigned(),
'fieldId' => $this->bigInteger()->unsigned(),
'componentId' => $this->bigInteger()->unsigned(),
'dataId' => $this->bigInteger()->unsigned(),
'sortOrder' => $this->integer(),
'path' => $this->string(1024),
'level' => $this->integer(),
'slot' => $this->string(256),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
'uid' => $this->uid(),
Expand All @@ -40,7 +38,7 @@ public function safeUp()
public function safeDown()
{
$this->dropTableIfExists(Table::COMPONENTS);
$this->dropTableIfExists(Table::COMPONENTS_ELEMENTS);
$this->dropTableIfExists(Table::COMPONENT_DATA);

return true;
}
Expand Down
Loading

0 comments on commit 3dde39f

Please sign in to comment.