Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('custom_fields', function (Blueprint $table) {
$table->boolean('is_searchable')->default(true);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('custom_fields', function (Blueprint $table) {
$table->dropColumn('is_searchable');
});
}
};
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
Route::match(['put', 'patch'], 'remote/{remote_type}', [RemoteCustomFieldController::class, 'update'])->name('remote.update');
Route::get('remote/{remote_type}/resolve', [RemoteCustomFieldController::class, 'resolve'])->name('remote.resolve');
Route::get('remote/{remote_type}/resolve/{identifier_value}', [RemoteCustomFieldController::class, 'resolveByIdentifierValue'])->name('remote.resolveByIdentifierValue');
Route::get('remote/{remote_type}/search/{q?}', [RemoteCustomFieldController::class, 'search'])->name('remote.search');

Route::get('selection', [SelectionCustomFieldController::class, 'index'])->name('selection.index');
Route::post('selection/{plain_type}', [SelectionCustomFieldController::class, 'store'])->name('selection.store');
Expand Down
19 changes: 18 additions & 1 deletion src/App/Http/Controllers/RemoteCustomFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Asseco\CustomFields\App\Http\Requests\RemoteTypeRequest;
use Asseco\CustomFields\App\Traits\TransformsOutput;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -120,7 +121,7 @@ public function resolve(RemoteType $remoteType): JsonResponse
*/
public function resolveByIdentifierValue(RemoteType $remoteType, string $identifierValue): JsonResponse
{
$data = $remoteType->getRemoteData();
$data = $remoteType->getRemoteData($identifierValue);

$data = $remoteType->data_path ? Arr::get($data, $remoteType->data_path) : $data;

Expand All @@ -130,4 +131,20 @@ public function resolveByIdentifierValue(RemoteType $remoteType, string $identif

return response()->json($transformed);
}

public function search(Request $request, RemoteType $remoteType, string $q = ''): JsonResponse
{
// check query parameter
$q = $q ?: $request->input('q');
if (!empty($q)) {
$data = $remoteType->searchRemoteData($q);
} else {
$data = $remoteType->getRemoteData();
}

$data = $remoteType->data_path ? Arr::get($data, $remoteType->data_path) : $data;
$transformed = $this->transform($data, $remoteType->mappings);

return response()->json($transformed);
}
}
12 changes: 12 additions & 0 deletions src/App/Models/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
* @method static Builder remote()
* @method static Builder selection()
*
* @property string $id
* @property string $name
* @property string $label
* @property string $placeholder
* @property string $model
* @property bool $required
* @property bool $hidden
* @property bool $is_searchable
* @property string $group
* @property int $order
* @property string $renderer
*
* Class CustomField
*/
class CustomField extends Model implements CustomFieldContract
Expand Down
47 changes: 41 additions & 6 deletions src/App/Models/RemoteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class RemoteType extends ParentType implements \Asseco\CustomFields\App\Contract
'mappings' => 'array',
];

const DEFAULT_IDENTIFIER_PROPERTY = 'id';
const DEFAULT_SEARCH_QUERY_PARAMETER = 'q';

protected static function newFactory()
{
return RemoteTypeFactory::new();
Expand All @@ -44,23 +47,55 @@ public function getNameAttribute()
return 'remote';
}

public function getRemoteData()
private function fetchData(?string $value = null, bool $search = false)
{
$cacheKey = 'remote_custom_field_' . $this->id;
$qParam = $this->identifier_property ?: self::DEFAULT_IDENTIFIER_PROPERTY;
if ($search) {
$qParam = self::DEFAULT_SEARCH_QUERY_PARAMETER;
}

$body = $this->body;
$url = $this->url;

if ($value) {
// get by ID
if ($this->method == 'POST') {
empty($body) ? ($body = [$qParam => $value]) : ($body[$qParam] = $value);
} else {
$parsed = parse_url($url);
parse_str($parsed['query'] ?? '', $params);
$params[$qParam] = $value;
$url = $parsed['scheme'] . '://' . $parsed['host'];
if (!empty($parsed['port'])) {
$url .= ':' . $parsed['port'];
}
$url .= $parsed['path'] . '?' . http_build_query($params);
}
}

return Http::withHeaders($this->getHeaders() ?: [])
->withBody($body, 'application/json')
->{$this->method}($url)->throw()->json();
}

public function getRemoteData(?string $identifierValue = null)
{
$cacheKey = 'remote_custom_field_' . $this->id;
if (config('asseco-custom-fields.should_cache_remote') && Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}

$response = Http::withHeaders($this->getHeaders() ?: [])
->withBody($this->body, 'application/json')
->{$this->method}($this->url)->throw()->json();

$response = $this->fetchData($identifierValue, false);
Cache::put($cacheKey, $response, config('asseco-custom-fields.remote_cache_ttl'));

return $response;
}

public function searchRemoteData(string $searchString)
{
return $this->fetchData($searchString, true);
}

protected function getHeaders()
{
return $this->headers;
Expand Down
1 change: 0 additions & 1 deletion src/App/Traits/TransformsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ protected function transform(array $response, ?array $mappings): array
protected function mapSingle(array $mappings, array $item): array
{
$data = [];

foreach ($mappings as $remoteKey => $localKey) {
if (!array_key_exists($remoteKey, $item)) {
continue;
Expand Down