100 lines
3.2 KiB
PHP
100 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Template;
|
|
|
|
class TemplateApiController extends Controller
|
|
{
|
|
// Ambil semua template
|
|
public function index()
|
|
{
|
|
$templates = Template::with('kategori')->get();
|
|
|
|
$transformedTemplates = $templates->map(function ($template) {
|
|
return [
|
|
'id' => $template->id,
|
|
'nama_template' => $template->nama_template,
|
|
'slug' => $template->slug,
|
|
'harga' => $template->harga,
|
|
'paket' => $template->paket,
|
|
'kategori' => $template->kategori ? $template->kategori->nama : null,
|
|
'foto' => $template->foto ? asset('storage/' . $template->foto) : null,
|
|
];
|
|
});
|
|
|
|
return response()->json($transformedTemplates);
|
|
}
|
|
|
|
// Ambil detail template tertentu
|
|
public function show(Template $template)
|
|
{
|
|
// Jika ada query ?with_dummy=true, tambahkan dummy value
|
|
$formData = $this->addDummyValues($template->form ?? []);
|
|
|
|
return response()->json([
|
|
'id' => $template->id,
|
|
'nama_template' => $template->nama_template,
|
|
'slug' => $template->slug,
|
|
'harga' => $template->harga,
|
|
'paket' => $template->paket,
|
|
'kategori' => $template->kategori ? $template->kategori->nama : null,
|
|
'form' => $formData,
|
|
'foto' => $template->foto ? asset('storage/' . $template->foto) : null,
|
|
]);
|
|
}
|
|
|
|
private function addDummyValues(array $form)
|
|
{
|
|
if (empty($form['fields'])) {
|
|
return $form;
|
|
}
|
|
|
|
// Aturan dummy value berdasarkan tipe
|
|
$dummyValues = [
|
|
'text' => 'XXXXX XXXX',
|
|
'email' => 'example@domain.com',
|
|
'textarea' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
|
'date' => '2025-12-01',
|
|
'datetime-local' => '2025-12-01T09:00',
|
|
'number' => 17,
|
|
'file' => null,
|
|
'url' => 'https://example.com',
|
|
];
|
|
|
|
// Map field untuk menambahkan dummy value
|
|
$form['fields'] = array_map(function ($field) use ($dummyValues) {
|
|
$field['value'] = $dummyValues[$field['type']] ?? 'Unknown Type';
|
|
return $field;
|
|
}, $form['fields']);
|
|
|
|
return $form;
|
|
}
|
|
|
|
public function getByCategory($id)
|
|
{
|
|
$templates = Template::with('kategori')
|
|
->where('kategori_id', $id)
|
|
->get();
|
|
|
|
|
|
$transformed = $templates->map(function ($template) {
|
|
return [
|
|
'id' => $template->id,
|
|
'nama_template' => $template->nama_template,
|
|
'harga' => $template->harga,
|
|
'paket' => $template->paket,
|
|
'kategori' => $template->kategori ? [
|
|
'id' => $template->kategori->id,
|
|
'nama' => $template->kategori->nama
|
|
] : null,
|
|
'foto' => $template->foto ? asset('storage/' . $template->foto) : null,
|
|
'fiturs' => $template->fiturs ?? [],
|
|
];
|
|
});
|
|
|
|
return response()->json($transformed);
|
|
}
|
|
}
|