113 lines
4.1 KiB
PHP
113 lines
4.1 KiB
PHP
<?php
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Category;
|
|
|
|
class ProductController extends AdminController
|
|
{
|
|
private function specsToJson($text): string
|
|
{
|
|
$out = [];
|
|
foreach (explode("\n", $text) as $line) {
|
|
$line = trim($line);
|
|
if (!$line) continue;
|
|
$p = strpos($line, '|');
|
|
if ($p === false) { $out[] = ['k' => $line, 'v' => '']; }
|
|
else { $out[] = ['k' => trim(substr($line, 0, $p)), 'v' => trim(substr($line, $p + 1))]; }
|
|
}
|
|
return json_encode($out, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$product = new Product();
|
|
return $this->view('admin/products', [
|
|
'products' => $product->all(),
|
|
'seg' => $this->seg(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return $this->view('admin/product_form', [
|
|
'p' => null,
|
|
'cats' => (new Category())->all(),
|
|
]);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
if (!csrf_check()) { $this->redirect('admin/products'); }
|
|
$product = new Product();
|
|
$cover = $this->uploadFile('cover') ?? $this->post('cover_url', '');
|
|
$id = $product->insert([
|
|
'category_id' => (int)$this->post('category_id'),
|
|
'name' => $this->post('name'),
|
|
'slug' => '',
|
|
'cover' => $cover,
|
|
'summary' => $this->post('summary'),
|
|
'description' => $this->post('description'),
|
|
'price' => (float)$this->post('price', 0),
|
|
'specs' => $this->specsToJson($this->post('specs', '')),
|
|
'gallery' => '[]',
|
|
'tags' => $this->post('tags', ''),
|
|
'sort_order' => (int)$this->post('sort_order', 0),
|
|
'status' => $this->post('status', 1) ? 1 : 0,
|
|
'created_at' => date('Y-m-d'),
|
|
'layout' => $this->post('layout', ''),
|
|
]);
|
|
// URL 标识留空时按记录序号顺序生成(短、稳定)
|
|
$slug = $this->post('slug') ? slugify($this->post('slug')) : (string)$id;
|
|
$product->update($id, ['slug' => $slug]);
|
|
$this->redirect('admin/products');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$product = new Product();
|
|
$p = $product->find($id);
|
|
if (!$p) { $this->redirect('admin/products'); }
|
|
$specs = $product->specsArray($p);
|
|
$specText = '';
|
|
foreach ($specs as $s) $specText .= ($s['k'] ?? '') . '|' . ($s['v'] ?? '') . "\n";
|
|
return $this->view('admin/product_form', [
|
|
'p' => $p,
|
|
'cats' => (new Category())->all(),
|
|
'specText'=> $specText,
|
|
]);
|
|
}
|
|
|
|
public function update($id)
|
|
{
|
|
if (!csrf_check()) { $this->redirect('admin/products'); }
|
|
$product = new Product();
|
|
$p = $product->find($id);
|
|
if (!$p) { $this->redirect('admin/products'); }
|
|
$cover = $this->uploadFile('cover');
|
|
if (!$cover && $this->post('cover_url')) $cover = $this->post('cover_url');
|
|
if (!$cover) $cover = $p['cover'] ?? '';
|
|
$product->update($id, [
|
|
'category_id' => (int)$this->post('category_id'),
|
|
'name' => $this->post('name'),
|
|
'slug' => $this->post('slug') ? slugify($this->post('slug')) : (string)$id,
|
|
'cover' => $cover,
|
|
'summary' => $this->post('summary'),
|
|
'description' => $this->post('description'),
|
|
'price' => (float)$this->post('price', 0),
|
|
'specs' => $this->specsToJson($this->post('specs', '')),
|
|
'tags' => $this->post('tags', ''),
|
|
'sort_order' => (int)$this->post('sort_order', 0),
|
|
'status' => $this->post('status', 1) ? 1 : 0,
|
|
'layout' => $this->post('layout', ''),
|
|
]);
|
|
$this->redirect('admin/products');
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
if (csrf_check()) { (new Product())->delete($id); }
|
|
$this->redirect('admin/products');
|
|
}
|
|
}
|