Skip to content
Open
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: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion resources/dist/build/assets/addon-e4Zwj-7O.js

This file was deleted.

1 change: 1 addition & 0 deletions resources/dist/build/assets/addon-l0sNRNKZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion resources/dist/build/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"resources/js/addon.js": {
"file": "assets/addon-e4Zwj-7O.js",
"file": "assets/addon-l0sNRNKZ.js",
"name": "addon",
"src": "resources/js/addon.js",
"isEntry": true
Expand Down
8 changes: 3 additions & 5 deletions resources/js/addon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Index from './pages/FormConfig/Index.vue';

Statamic.booting(() => {
Statamic.$inertia.register('statamic-mailerlite::FormConfig/Index', Index);
});
// No custom Control Panel components. MailerLite configuration lives on the
// native form configure page, injected via Form::appendConfigFields() in the
// addon service provider.
67 changes: 0 additions & 67 deletions resources/js/pages/FormConfig/Index.vue

This file was deleted.

13 changes: 0 additions & 13 deletions routes/cp.php

This file was deleted.

100 changes: 100 additions & 0 deletions src/Fieldtypes/MailerLiteFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Concept7\StatamicMailerLite\Fieldtypes;

use MailerLite\MailerLite;
use Statamic\CP\Column;
use Statamic\Facades\Addon;
use Statamic\Fieldtypes\Relationship;

class MailerLiteFields extends Relationship
{
protected $component = 'relationship';

protected $indexComponent = 'text';

protected $canCreate = false;

protected $canEdit = false;

protected $canSearch = false;

protected $statusIcons = false;

/** @var array<string, array<int, array{id: string, title: string}>> */
private static array $cache = [];

protected function toItemArray($id)
{
return collect($this->fields())->firstWhere('id', $id) ?? $this->invalidItemArray($id);
}

public function getIndexItems($request)
{
return collect($this->fields());
}

protected function getColumns()
{
return [
Column::make('title')->label(__('Name')),
];
}

public function preProcessIndex($data)
{
if (! $data) {
return [];
}

return collect($data)->map(function ($id) {
$item = $this->toItemArray($id);

return $item['title'] ?? $id;
})->join(', ');
}

/**
* The selectable MailerLite subscriber fields, always including the built-in
* email attribute. Values are MailerLite field keys (the subscriber payload
* is keyed by key, not id); email maps to the SDK's top-level email attribute.
*
* @return array<int, array{id: string, title: string}>
*/
private function fields(): array
{
$baseline = [
['id' => 'email', 'title' => __('Email (built-in)')],
];

$apiKey = Addon::get('concept7/statamic-mailerlite')->setting('api_key');

if (blank($apiKey)) {
return $baseline;
}

$cacheKey = hash('sha256', $apiKey);

if (isset(self::$cache[$cacheKey])) {
return self::$cache[$cacheKey];
}

$custom = rescue(function () use ($apiKey): array {
$response = (new MailerLite(['api_key' => $apiKey]))
->fields
->get([
'limit' => 100,
'sort' => 'name',
]);

return collect($response['body']['data'] ?? [])
->map(fn (array $field): array => [
'id' => $field['key'],
'title' => $field['name'],
])
->all();
}, [], report: false);

return self::$cache[$cacheKey] = [...$baseline, ...$custom];
}
}
Loading