diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 1ca256d..ffd44af 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 4.1.0 - 2024-04-23 +### Added +- Added an option to specify the filename as which the export will be generated ([#6](https://github.com/studioespresso/craft-exporter/issues/6)) +- Added support for Formie's Name field ([#13](https://github.com/studioespresso/craft-exporter/issues/13)) +- Exports can now be created for empty sections ([#12](https://github.com/studioespresso/craft-exporter/issues/12)) + +### Fixed +- Fixed an error that would occur when editing exports for element that had new fields ([#11](https://github.com/studioespresso/craft-exporter/issues/11)) + + ## 4.0.0 - 2024-03-12 - Inital release 🚀 diff --git a/composer.json b/composer.json index 77ae7c1..e4f2c94 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Export all things", "type": "craft-plugin", "license": "proprietary", - "version": "4.0.0", + "version": "4.1.0", "authors": [ { "name": "Studio Espresso", diff --git a/src/Exporter.php b/src/Exporter.php index 65f0835..fa89d55 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -22,6 +22,7 @@ use studioespresso\exporter\events\RegisterExportableElementTypes; use studioespresso\exporter\events\RegisterExportableFieldTypes; use studioespresso\exporter\fields\DateTimeParser; +use studioespresso\exporter\fields\FormieNameParser; use studioespresso\exporter\fields\MultiOptionsFieldParser; use studioespresso\exporter\fields\OptionsFieldParser; use studioespresso\exporter\fields\PlainTextParser; @@ -309,6 +310,10 @@ function(RegisterExportableFieldTypes $event) { \verbb\formie\fields\formfields\Entries::class, // @phpstan-ignore-line \verbb\formie\fields\formfields\Categories::class, // @phpstan-ignore-line ]); + + $event->fieldTypes = array_merge($event->fieldTypes, [FormieNameParser::class => [ + \verbb\formie\fields\formfields\Name::class, // @phpstan-ignore-line + ]]); }); } } diff --git a/src/elements/ExportElement.php b/src/elements/ExportElement.php index e61edfe..95948ed 100644 --- a/src/elements/ExportElement.php +++ b/src/elements/ExportElement.php @@ -114,9 +114,17 @@ public static function isLocalized(): bool public function canView(User $user): bool { $element = $this->getElementQuery()->one(); - if ($element->canView(Craft::$app->getUser()->getIdentity())) { + if ($element && $element->canView(Craft::$app->getUser()->getIdentity())) { return true; } + + if (!$element) { + $element = $this->mockElement(); + if ($element && $element->canView(Craft::$app->getUser()->getIdentity())) { + return true; + } + } + return false; } @@ -270,6 +278,11 @@ public function getElementQuery(): ElementQuery return Exporter::$plugin->query->buildQuery($this); } + public function mockElement(): Element|null + { + return Exporter::$plugin->query->mockElement($this); + } + public function getExportableAttributes(): array { $helper = new ElementTypeHelper(); diff --git a/src/fields/BaseFieldParser.php b/src/fields/BaseFieldParser.php index f9b342d..018ea58 100644 --- a/src/fields/BaseFieldParser.php +++ b/src/fields/BaseFieldParser.php @@ -7,7 +7,7 @@ abstract class BaseFieldParser extends Component { - abstract protected function getValue(Element $element, array $handle); + abstract protected function getValue(Element $element, array $field); abstract protected function getOptionType(): string|bool; diff --git a/src/fields/FormieNameParser.php b/src/fields/FormieNameParser.php new file mode 100644 index 0000000..66c7d82 --- /dev/null +++ b/src/fields/FormieNameParser.php @@ -0,0 +1,52 @@ +getFieldValue($field['handle']); + /** @phpstan-ignore-next-line */ + if (is_object($value) && get_class($value) === verbb\formie\models\Name::class) { + $string = ''; + if ($value->prefix) { + $string = $string . $value->prefix . " "; + } + if ($value->firstName) { + $string = $string . $value->firstName . " "; + } + if ($value->lastName) { + $string = $string . $value->lastName; + } + return $string; + } + return $value; + } + + public function getOptions(): array + { + return []; + } + + public function getOptionType(): string|bool + { + return false; + } + + protected function getOptionLabel(): string|bool + { + return false; + } + + protected function getOptionDescription(): string|bool + { + return false; + } +} diff --git a/src/helpers/FieldTypeHelper.php b/src/helpers/FieldTypeHelper.php index 6ad9dba..e9d570c 100644 --- a/src/helpers/FieldTypeHelper.php +++ b/src/helpers/FieldTypeHelper.php @@ -107,7 +107,6 @@ public function getAvailableFieldTypes(): array $event->fieldTypes, self::SUPPORTED_FIELD_TYPES, ); - return self::$_supportedFieldTypes; } diff --git a/src/services/ExportQueryService.php b/src/services/ExportQueryService.php index b03c454..2d7e49b 100644 --- a/src/services/ExportQueryService.php +++ b/src/services/ExportQueryService.php @@ -57,6 +57,24 @@ public function buildQuery(ExportElement $export): ElementQuery return $query; } + public function mockElement(ExportElement $export): Element|null + { + try { + $elementType = $export->elementType; + $settings = $export->getSettings(); + $elementOptions = Exporter::getInstance()->elements->getElementTypeSettings($elementType); + + $element = Craft::createObject($elementType); + if (isset($elementOptions['group'])) { + $group = $elementOptions['group']['parameter']; + $element->$group = $settings['group']; + } + return $element; + } catch (\Throwable $e) { + return null; + } + } + public function getFields(ExportElement $export, Element $element): array { $data = []; diff --git a/src/services/MailService.php b/src/services/MailService.php index 1966bb5..6ccd324 100644 --- a/src/services/MailService.php +++ b/src/services/MailService.php @@ -33,7 +33,8 @@ public function send(ExportElement $export, $path): bool $message = new Message(); - $fileName = "Export.{$exportSettings['fileType']}"; + $name = $exportSettings['fileName'] ?? "Export"; + $fileName = "{$name}.{$exportSettings['fileType']}"; $message->attach($path, ['fileName' => $fileName, 'contentType' => "application/{$exportSettings['fileType']}"]); $message->setSubject("Your export"); $message->setTo($exportSettings['email']); diff --git a/src/templates/sprig/element/step_2.twig b/src/templates/sprig/element/step_2.twig index f7b77f5..26b1204 100644 --- a/src/templates/sprig/element/step_2.twig +++ b/src/templates/sprig/element/step_2.twig @@ -14,7 +14,7 @@ {% set attributes = export.getAttributes() %} {% set fields = export.getFields() ?? [] %} - {% set element = export.getElementQuery().one() %} + {% set element = export.mockElement() %} {% if not element %} {{ "No elements can be found for your selection. Please create an element first or check your settings in step 1."|t('exporter') }} @@ -69,7 +69,7 @@ {% else %} {# We have a parser, so the field is supported #} {% set fieldSettings = false %} - {% if fields|length and attribute(fields, field.handle) %} + {% if fields|length and attribute(fields, field.handle) is defined %} {% set fieldSettings = attribute(fields, field.handle) %} {% endif %}

{{ "Export delivery"|t('exporter') }}

+ + {{ forms.textField({ + first: true, + label: "Filename"|t('exporter'), + instructions: "The filename that should be used for the export."|t('exporter'), + id: 'email', + name: 'settings[fileName]', + value: settings.fileName ?? export.name|lower|replace(' ', '-'), + errors: (errors is defined and errors.email ) ? errors.email : null, + required: true + }) }} + + {{ hiddenInput('settings[deliveryType]', 'email') }} {{ forms.textField({ first: true, diff --git a/yarn.lock b/yarn.lock index ddfd2c4..55ca380 100644 --- a/yarn.lock +++ b/yarn.lock @@ -686,9 +686,9 @@ tabbable@^6.2.0: integrity sha512-Cat63mxsVJlzYvN51JmVXIgNoUokrIaT2zLclCXjRd8boZ0004U4KCs/sToJ75C6sdlByWxpYnb5Boif1VSFew== vite@^4.5.0: - version "4.5.2" - resolved "https://registry.yarnpkg.com/vite/-/vite-4.5.2.tgz#d6ea8610e099851dad8c7371599969e0f8b97e82" - integrity sha512-tBCZBNSBbHQkaGyhGCDUGqeo2ph8Fstyp6FMSvTtsXeZSPpSMGlviAOav2hxVTqFcx8Hj/twtWKsMJXNY0xI8w== + version "4.5.3" + resolved "https://registry.yarnpkg.com/vite/-/vite-4.5.3.tgz#d88a4529ea58bae97294c7e2e6f0eab39a50fb1a" + integrity sha512-kQL23kMeX92v3ph7IauVkXkikdDRsYMGTVl5KY2E9OY4ONLvkHf04MDTbnfo6NKxZiDLWzVpP5oTa8hQD8U3dg== dependencies: esbuild "^0.18.10" postcss "^8.4.27"