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

Date Ranges #78

Merged
merged 2 commits into from
Jan 7, 2025
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
2 changes: 2 additions & 0 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'assets_folder_instructions' => 'By default, downloaded assets will use same folder structure as the original URL. You can specify a different folder here.',
'assets_related_field_instructions' => 'Which field does the data reference?',
'assets_process_downloaded_images_instructions' => 'Should downloaded images be processed using the asset container\'s source preset?',
'date_start_date_instructions' => 'Which field should be used for the start date?',
'date_end_date_instructions' => 'Which field should be used for the end date?',
'entries_create_when_missing_instructions' => 'Create the entry if it doesn\'t exist.',
'entries_related_field_instructions' => 'Which field does the data reference?',
'terms_create_when_missing_instructions' => 'Create the term if it doesn\'t exist.',
Expand Down
53 changes: 52 additions & 1 deletion src/Transformers/DateTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@

use Illuminate\Support\Carbon;
use Statamic\Fieldtypes\Date as DateFieldtype;
use Statamic\Importer\Sources\Csv;
use Statamic\Importer\Sources\Xml;
use Statamic\Support\Arr;
use Statamic\Support\Str;

class DateTransformer extends AbstractTransformer
{
public function transform(string $value): int|string
public function transform(string $value): array|int|string
{
if ($this->field->get('mode') === 'range') {
return [
'start' => $this->transformDate($value),
'end' => $this->transformDate(Arr::get($this->values, $this->config('end'))),
];
}

return $this->transformDate($value);
}

private function transformDate(string $value): int|string
{
$date = Carbon::parse($value);

Expand All @@ -32,4 +48,39 @@ private function defaultFormat()

return DateFieldtype::DEFAULT_DATE_FORMAT;
}

public function fieldItems(): array
{
if ($this->field->get('mode') === 'range') {
$row = match ($this->import?->get('type')) {
'csv' => (new Csv($this->import))->getItems($this->import->get('path'))->first(),
'xml' => (new Xml($this->import))->getItems($this->import->get('path'))->first(),
};

// To prevent the field mapping from being filtered out, the Start Date is the "normal" key,
// and the End Date is a config option.
return [
'key' => [
'type' => 'select',
'display' => __('Start'),
'instructions' => __('importer::messages.date_start_date_instructions'),
'options' => collect($row)->map(fn ($value, $key) => [
'key' => $key,
'value' => "<{$key}>: ".Str::truncate($value, 200),
])->values()->all(),
],
'end' => [
'type' => 'select',
'display' => __('End'),
'instructions' => __('importer::messages.date_end_date_instructions'),
'options' => collect($row)->map(fn ($value, $key) => [
'key' => $key,
'value' => "<{$key}>: ".Str::truncate($value, 200),
])->values()->all(),
],
];
}

return [];
}
}
19 changes: 19 additions & 0 deletions tests/Transformers/DateTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,23 @@ public function it_transforms_dates_with_custom_format()

$this->assertEquals('31st October 2024', $transformer->transform('2024-10-31'));
}

#[Test]
public function it_transforms_date_range()
{
$this->blueprint->ensureField('the_date', ['type' => 'date', 'mode' => 'range', 'time_enabled' => false]);

$transformer = new DateTransformer(
import: $this->import,
blueprint: $this->blueprint,
field: $this->blueprint->field('the_date'),
config: ['end' => 'The End Date'],
values: ['The End Date' => '2023-12-20']
);

$this->assertEquals([
'start' => '2023-10-02',
'end' => '2023-12-20',
], $transformer->transform('2023-10-02'));
}
}
Loading