Skip to content

Commit

Permalink
Move element Settings to a specific model
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Dec 1, 2023
1 parent f6016e4 commit 5549578
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 45 deletions.
59 changes: 21 additions & 38 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\base\Model;
use craft\base\Plugin;
use craft\db\Table;
use craft\elements\Category;
use craft\elements\Entry;
use craft\events\DefineBehaviorsEvent;
use craft\events\RegisterComponentTypesEvent;
Expand All @@ -24,6 +25,10 @@
use studioespresso\exporter\fields\PlainTextParser;
use studioespresso\exporter\helpers\ElementTypeHelper;
use studioespresso\exporter\helpers\FieldTypeHelper;
use studioespresso\exporter\models\ExportableCategoryModel;
use studioespresso\exporter\models\ExportableElementTypeModel;
use studioespresso\exporter\models\ExportableEntryModel;
use studioespresso\exporter\models\ExportableFormieSubmissionModel;
use studioespresso\exporter\models\Settings;
use studioespresso\exporter\records\ExportRecord;
use studioespresso\exporter\services\ExportQueryService;
Expand Down Expand Up @@ -78,7 +83,7 @@ public function init(): void
}

// Defer most setup tasks until Craft is fully initialized
Craft::$app->onInit(function() {
Craft::$app->onInit(function () {
Sprig::bootstrap();
$this->registerElementTypes();
$this->attachEventHandlers();
Expand Down Expand Up @@ -139,7 +144,7 @@ public function getCpNavItem(): array
private function registerElementTypes(): void
{
Event::on(Elements::class, Elements::EVENT_REGISTER_ELEMENT_TYPES,
function(RegisterComponentTypesEvent $event) {
function (RegisterComponentTypesEvent $event) {
$event->types[] = ExportElement::class;
});
}
Expand All @@ -149,7 +154,7 @@ private function registerCpRoutes(): void
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function(RegisterUrlRulesEvent $event) {
function (RegisterUrlRulesEvent $event) {
$event->rules['exporter'] = ['template' => 'exporter/_index'];
$event->rules['exporter/create'] = 'exporter/element/edit';
$event->rules['exporter/<elementId:\\d+>/<step:\\d+>'] = 'exporter/element/edit';
Expand All @@ -164,24 +169,12 @@ private function registerSupportedElementTypes()
Event::on(
ElementTypeHelper::class,
ElementTypeHelper::EVENT_REGISTER_EXPORTABLE_ELEMENT_TYPES,
function(RegisterExportableElementTypes $event) {
function (RegisterExportableElementTypes $event) {
$entryModel = new ExportableEntryModel();
$categoryModel = new ExportableCategoryModel();
$event->elementTypes = array_merge($event->elementTypes, [
Entry::class => [
"label" => "Entries",
"group" => [
"parameter" => "sectionId",
"label" => "Section",
"instructions" => "Choose a group from which you want to start your export",
"items" => Craft::$app->getSections()->getEditableSections(),
],
"subGroup" => [
'label' => "Entry type",
"instructions" => "Choose which entrytype you want to export",
'parameter' => 'id',
'class' => Sections::class,
'function' => 'getEntryTypesBySectionId',
],
],
Entry::class => $entryModel,
Category::class => $categoryModel,
]);
});
}
Expand All @@ -191,7 +184,7 @@ private function registerFieldParsers()
Event::on(
FieldTypeHelper::class,
FieldTypeHelper::EVENT_REGISTER_EXPORTABLE_FIELD_TYPES,
function(RegisterExportableFieldTypes $event) {
function (RegisterExportableFieldTypes $event) {
if (Craft::$app->getPlugins()->isPluginEnabled('ckeditor')) {
$event->fieldTypes[PlainTextParser::class][] = \craft\ckeditor\Field::class; // @phpstan-ignore-line
}
Expand Down Expand Up @@ -219,20 +212,10 @@ private function registerFormie()
Event::on(
ElementTypeHelper::class,
ElementTypeHelper::EVENT_REGISTER_EXPORTABLE_ELEMENT_TYPES,
function(RegisterExportableElementTypes $event) {
function (RegisterExportableElementTypes $event) {
$model = new ExportableFormieSubmissionModel();
$event->elementTypes = array_merge($event->elementTypes, [
// TODO Move all this to a model?
/** @phpstan-ignore-next-line */
Submission::class => [
"label" => "Formie submissions",
// TODO: Add setting for localization or not
"group" => [
"label" => "Form",
"parameter" => "formId",
"items" => Formie::getInstance()->getForms()->getAllForms(), // @phpstan-ignore-line
"nameProperty" => "title",
],
],
Submission::class => $model
]);
});
}
Expand All @@ -243,7 +226,7 @@ private function attachEventHandlers(): void
Event::on(
Gc::class,
Gc::EVENT_RUN,
function(Event $event) {
function (Event $event) {
// Delete `elements` table rows without peers in our custom products table
Craft::$app->getGc()->deletePartialElements(
ExportElement::class,
Expand All @@ -263,7 +246,7 @@ function(Event $event) {
Event::on(
CraftVariable::class,
CraftVariable::EVENT_DEFINE_BEHAVIORS,
function(DefineBehaviorsEvent $e) {
function (DefineBehaviorsEvent $e) {
$e->sender->attachBehaviors([
CraftVariableBehavior::class,
]);
Expand All @@ -273,7 +256,7 @@ function(DefineBehaviorsEvent $e) {
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function(Event $event) {
function (Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('exporter', ExporterVariable::class);
Expand All @@ -283,7 +266,7 @@ function(Event $event) {

private function registerUserPermissions()
{
Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, function(RegisterUserPermissionsEvent $event) {
Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, function (RegisterUserPermissionsEvent $event) {
$event->permissions[] = [
'heading' => Craft::t('exporter', 'Exporter'),
'permissions' => [
Expand Down
16 changes: 13 additions & 3 deletions src/helpers/ElementTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

use craft\base\Event;
use studioespresso\exporter\events\RegisterExportableElementTypes;
use studioespresso\exporter\Exporter;
use studioespresso\exporter\models\ExportableElementTypeModel;

class ElementTypeHelper
{
public const EVENT_REGISTER_EXPORTABLE_ELEMENT_TYPES = 'registerExportableElementTypes';


/** @var ExportableElementTypeModel[] SUPPORTED_ELEMENT_TYPES */
public const SUPPORTED_ELEMENT_TYPES = [];


private static ?array $_supportedElementTypes = null;

/**
* @return ExportableElementTypeModel[] array
*/
public function getAvailableElementTypes(): array
{
if (self::$_supportedElementTypes !== null) {
Expand Down Expand Up @@ -42,8 +47,13 @@ public function getElementTypesOnly(): array
}

$types = [];
foreach ($this->getAvailableElementTypes() as $class => $data) {
$types[$class] = $data['label'];
foreach ($this->getAvailableElementTypes() as $model) {
if(!$model->validate()) {
\Craft::error("ExportableElementTypeModel is not configured correctly", Exporter::class);
continue;
}

$types[$model->elementType] = $model->elementLabel;
}

return $types;
Expand Down
33 changes: 33 additions & 0 deletions src/models/ExportableCategoryModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace studioespresso\exporter\models;

use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Model;
use craft\elements\Category;
use craft\elements\Entry;
use craft\services\Sections;

class ExportableCategoryModel extends ExportableElementTypeModel
{
public $elementType = Category::class;

public string $elementLabel = "Categories";

public function getGroup(): array
{
return [
"parameter" => "groupId",
"label" => "Group",
"instructions" => "Choose a group from which you want to start your export",
"items" => \Craft::$app->getCategories()->getEditableGroups(),
];
}

public function getSubGroup(): bool|array
{
return false;
}

}
19 changes: 19 additions & 0 deletions src/models/ExportableElementTypeModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace studioespresso\exporter\models;

use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Model;

abstract class ExportableElementTypeModel extends Model
{
public $elementType;

public string $elementLabel = "";

abstract public function getGroup(): bool|array;

abstract public function getSubGroup(): bool|array;

}
38 changes: 38 additions & 0 deletions src/models/ExportableEntryModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace studioespresso\exporter\models;

use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Model;
use craft\elements\Entry;
use craft\services\Sections;

class ExportableEntryModel extends ExportableElementTypeModel
{
public $elementType = Entry::class;

public string $elementLabel = "Entries";

public function getGroup(): array
{
return [
"parameter" => "sectionId",
"label" => "Section",
"instructions" => "Choose a group from which you want to start your export",
"items" => \Craft::$app->getSections()->getEditableSections(),
];
}

public function getSubGroup(): array
{
return [
'label' => "Entry type",
"instructions" => "Choose which entry-type you want to export",
'parameter' => 'id',
'class' => Sections::class,
'function' => 'getEntryTypesBySectionId',
];
}

}
35 changes: 35 additions & 0 deletions src/models/ExportableFormieSubmissionModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace studioespresso\exporter\models;

use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Model;
use craft\elements\Category;
use craft\elements\Entry;
use craft\services\Sections;
use verbb\formie\elements\Submission;
use verbb\formie\Formie;

class ExportableFormieSubmissionModel extends ExportableElementTypeModel
{
public $elementType = Submission::class;

public string $elementLabel = "Formie Submissions";

public function getGroup(): array
{
return [
"label" => "Form",
"parameter" => "formId",
"items" => Formie::getInstance()->getForms()->getAllForms(), // @phpstan-ignore-line
"nameProperty" => "title",
];
}

public function getSubGroup(): bool|array
{
return false;
}

}
6 changes: 3 additions & 3 deletions src/templates/sprig/element/step_1.twig
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@

{% if elementType is defined %}
{% set elementOptions = craft.exporter.getElementTypeSettings(elementType) %}
{% set subGroup = elementOptions.subGroup %}
{% set subGroup = elementOptions.getSubGroup() %}
{% if elementOptions.group is defined %}
{% set groupOptions = elementOptions.group %}
{% set groupOptions = elementOptions.getGroup() %}
<div class="flex-fields">
<div class="field width-50" id="elementType-entry-subfields">
<div class="heading">
Expand Down Expand Up @@ -106,7 +106,7 @@
{% endif %}
</div>
{% if elementOptions.subGroup is defined and subGroup|length and (settings and settings.group is defined) %}
{% set subGroupSettings = elementOptions.subGroup %}
{% set subGroupSettings = elementOptions.getSubGroup() %}
<div class="field width-50" id="elementType-entry-subfields">
<div class="heading">
<label id="section-label" for="section">{{ subGroupSettings.label|t('exporter') }}<span
Expand Down
3 changes: 2 additions & 1 deletion src/variables/ExporterVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use studioespresso\exporter\elements\ExportElement;
use studioespresso\exporter\Exporter;
use studioespresso\exporter\jobs\ExportBatchJob;
use studioespresso\exporter\models\ExportableElementTypeModel;

/**
* The class name isn't important, but we've used something that describes
Expand All @@ -30,7 +31,7 @@ public function listenForJob(ExportElement $export)
return $query->one();
}

public function getElementTypeSettings($element): array
public function getElementTypeSettings($element): ExportableElementTypeModel
{
return Exporter::getInstance()->elements->getElementTypeSettings($element);
}
Expand Down

0 comments on commit 5549578

Please sign in to comment.