Skip to content

Commit

Permalink
Field support
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Dec 17, 2023
1 parent 510e30e commit 3bc15f9
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/console/controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function actionDebug($id): bool
$fields = $export->getHeadings();
$data[] = array_merge($attributes, $fields);

foreach ($query->limit(1)->all() as $element) {
foreach ($query->id(14263)->limit(1)->all() as $element) {
d($element->id);
$values = $element->toArray(array_keys($export->getAttributes()));
// Convert values to strings
Expand All @@ -54,7 +54,7 @@ public function actionDebug($id): bool

// Fetch the custom field content, already prepped
$fieldValues = $export->parseFieldValues($element);
d($fieldValues);
dd($fieldValues);
$data[] = array_merge($row, $fieldValues);
}

Expand Down
12 changes: 11 additions & 1 deletion src/controllers/ElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@ public function actionGetDownload()
$fileName = $this->request->getRequiredBodyParam('fileName');

$file = file_get_contents(Craft::$app->getPath()->getTempPath() . "/{$fileName}.{$settings['fileType']}");
return $file;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}

public function actionWatch($elementId = null)
Expand Down
22 changes: 22 additions & 0 deletions src/fields/MultiOptionsFieldParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace studioespresso\exporter\fields;

use Craft;


class MultiOptionsFieldParser extends OptionsFieldParser
{
public function getValue($element, $field)
{
$selected = [];
$property = $field['property'] ?? 'value';

foreach($element->getFieldValue($field['handle'])->getOptions() as $option) {
if($option->selected) {
$selected[] = $option->$property;
}
}
return $selected;
}
}
39 changes: 39 additions & 0 deletions src/fields/OptionsFieldParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace studioespresso\exporter\fields;

use Craft;


class OptionsFieldParser extends BaseFieldParser
{
public function getValue($element, $field)
{
$property = $field['property'] ?? 'value';
return $element->getFieldValue($field['handle'])->$property;
}

public function getOptions(): array
{
return [
'label' => 'Label',
'value' => 'Value'
];
}

public function getOptionType(): string|bool
{
return "select";
}

public function getOptionLabel(): string|bool
{
return Craft::t('exporter', 'Select field property');

}

public function getOptionDescription(): string|bool
{
return Craft::t('exporter', 'Select which property you want to export.');
}
}
1 change: 1 addition & 0 deletions src/fields/RelationFieldParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getOptions(): array
return [
'title' => Craft::t('app', 'Title'),
'id' => Craft::t('app', 'ID'),
'url' => Craft::t('app', 'URL'),
];
}

Expand Down
39 changes: 39 additions & 0 deletions src/fields/TimeParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace studioespresso\exporter\fields;

class TimeParser extends BaseFieldParser
{
public function getValue($element, array $field)
{
$value = $element->getFieldValue($field['handle']);
if ($value) {
return $value->format($field['property']);
}
}

public function getOptionType(): string
{
return "select";
}


public function getOptionLabel(): string|bool
{
return false;
}


public function getOptionDescription(): string|bool
{
return false;
}

public function getOptions(): array
{
return [
'H:i:s' => 'H:i:s',
'H:i' => 'H:i',
];
}
}
38 changes: 29 additions & 9 deletions src/helpers/FieldTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@
use craft\base\FieldInterface;
use craft\fields\Assets;
use craft\fields\Categories;
use craft\fields\Checkboxes;
use craft\fields\Color;
use craft\fields\Date;
use craft\fields\Dropdown;
use craft\fields\Email;
use craft\fields\Entries;
use craft\fields\Lightswitch;
use craft\fields\Money;
use craft\fields\MultiSelect;
use craft\fields\Number;
use craft\fields\PlainText;
use craft\fields\RadioButtons;
use craft\fields\Tags;
use craft\fields\Time;
use craft\fields\Url;
use studioespresso\exporter\events\RegisterExportableFieldTypes;
use studioespresso\exporter\fields\BaseFieldParser;
use studioespresso\exporter\fields\DateTimeParser;
use studioespresso\exporter\fields\LightswitchParser;
use studioespresso\exporter\fields\MultiOptionsFieldParser;
use studioespresso\exporter\fields\OptionsFieldParser;
use studioespresso\exporter\fields\PlainTextParser;
use studioespresso\exporter\fields\RelationFieldParser;
use studioespresso\exporter\fields\TimeParser;

class FieldTypeHelper
{
public const EVENT_REGISTER_EXPORTABLE_FIELD_TYPES = 'registerExportableFieldTypes';

public const SUPPORTED_FIELD_TYPES = [
PlainTextParser::class => [
PlainText::class,
Number::class,
Lightswitch::class,
Email::class,
Color::class,
Url::class,
RadioButtons::class,
],
RelationFieldParser::class => [
Entries::class,
Assets::class,
Expand All @@ -46,6 +46,26 @@ class FieldTypeHelper
DateTimeParser::class => [
Date::class,
],
TimeParser::class => [
Time::class,
],
OptionsFieldParser::class => [
Dropdown::class,
RadioButtons::class,
],
MultiOptionsFieldParser::class => [
MultiSelect::class,
Checkboxes::class,
],
PlainTextParser::class => [
PlainText::class,
Number::class,
Email::class,
Color::class,
Url::class,
Money::class,
Lightswitch::class,
],
];


Expand Down
1 change: 0 additions & 1 deletion src/models/ExportableFormieSubmissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ public function getSubGroup(): bool|array
{
return false;
}

}
7 changes: 2 additions & 5 deletions src/templates/sprig/element/step_1.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

{% if export.id %}
{% set settings = export.getSettings() %}
{% set elementType = export.elementType %}
{% set elementType = elementType ?? export.elementType %}
{% set showSiteOptions = true %}
{% else %}
{% if elementType is not defined %}
Expand Down Expand Up @@ -63,13 +63,10 @@
{% set group = '' %}
{% set subGroup = '' %}
{% endif %}

{% if elementType is defined %}
{% set elementOptions = craft.exporter.getElementTypeSettings(elementType) %}
{% set subGroup = elementOptions.getSubGroup() %}
{% if elementOptions.group is defined %}
{% set groupOptions = elementOptions.getGroup() %}
{{ d(groupOptions) }}
<div class="flex-fields">
<div class="field width-50" id="elementType-entry-subfields">
<div class="heading">
Expand Down Expand Up @@ -106,7 +103,7 @@
<div class="error">{{ "No groups found to export"|t('exporter') }}</div>
{% endif %}
</div>
{% if elementOptions.subGroup is defined and subGroup|length and (settings and settings.group is defined) %}
{% if elementOptions.subGroup is defined and (settings and settings.group is defined) %}
{% set subGroupSettings = elementOptions.getSubGroup() %}
<div class="field width-50" id="elementType-entry-subfields">
<div class="heading">
Expand Down
10 changes: 6 additions & 4 deletions src/templates/sprig/queue.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
{# Refreshes the component every 5 seconds #}
{% set timezone = timezone ?? 'Europe/Brussels' %}

<div sprig s-trigger="every 1s" class="pulse">
The time is {{ now|time('long', timezone=timezone) }}
<div sprig s-trigger="every 3s" class="pulse">
The time is {{ now|time('short', timezone=timezone) }}

{# Check if we have a job in the queue for this export and track its progress? #}
{% set isRunning = craft.exporter.listenForJob(export) %}
{% if isRunning %}
Your export running, stay tuned. Depending on the size of your export, this could take a while.
<p>
Your export running, stay tuned. Depending on the size of your export, this could take a while.
</p>
{% else %}
<div>
Done! Click the button below to download your export!
Expand All @@ -18,7 +20,7 @@
{{ actionInput('exporter/element/get-download') }}
{{ hiddenInput('exportId', export.id) }}
{{ hiddenInput('fileName', craft.app.request.getQueryParam('fileName')) }}
<button type="submit">{{ 'Download file' }}</button>
<button type="submit" class="btn submit">{{ 'Download file' }}</button>
</form>
</div>
{% endif %}
Expand Down
10 changes: 7 additions & 3 deletions src/variables/ExporterVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use craft\base\Field;
use craft\db\Query;
use ReflectionClass;
use studioespresso\exporter\elements\ExportElement;
use studioespresso\exporter\Exporter;
use studioespresso\exporter\jobs\ExportBatchJob;
Expand All @@ -21,14 +22,17 @@ class ExporterVariable
public function listenForJob(ExportElement $export)
{
$class = ExportBatchJob::class;
$reflect = new ReflectionClass($class);
$reflect->getShortName();


$query = new Query();
$query->from("{{%queue}}")
->select('*')
->where(['like', 'job', "%{$class}%", false])
->where(['like', 'job', "%{$reflect->getShortName()}%", false])
->andWhere(['like', 'job', "%{$export->id}%", false]);
return $query->one();

return $query->one() ?? false;
}

public function getElementTypeSettings($element): ExportableElementTypeModel
Expand Down

0 comments on commit 3bc15f9

Please sign in to comment.