177 lines
5.0 KiB
PHP
177 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace plugin\admin\app\controller;
|
|
|
|
use support\Request;
|
|
use support\Response;
|
|
use plugin\admin\app\model\Location;
|
|
use plugin\admin\app\controller\Crud;
|
|
use support\exception\BusinessException;
|
|
use util\Util;
|
|
use support\Log;
|
|
|
|
/**
|
|
* 仓库/港口
|
|
*/
|
|
class LocationController extends Crud
|
|
{
|
|
|
|
/**
|
|
* @var Location
|
|
*/
|
|
protected $model = null;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->model = new Location;
|
|
}
|
|
|
|
/**
|
|
* 浏览
|
|
* @return Response
|
|
*/
|
|
public function index(): Response
|
|
{
|
|
// 获取当前语言
|
|
$lang = session('lang', 'zh-cn');
|
|
Log::info('Current language', ['lang' => $lang]);
|
|
|
|
// 加载语言文件
|
|
$location_translations = include __DIR__ . "/../lang/{$lang}/location.php";
|
|
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
|
|
|
// 合并翻译
|
|
$translations = array_merge($location_translations, $common_translations);
|
|
Log::info('Loaded translations', ['keys' => array_keys($translations)]);
|
|
|
|
return view('location/index', [
|
|
'lang' => $lang,
|
|
'translations' => $translations
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 插入
|
|
* @param Request $request
|
|
* @return Response
|
|
* @throws BusinessException
|
|
*/
|
|
public function insert(Request $request): Response
|
|
{
|
|
// 获取当前语言
|
|
$lang = session('lang', 'zh-cn');
|
|
|
|
// 加载语言文件
|
|
$location_translations = include __DIR__ . "/../lang/{$lang}/location.php";
|
|
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
|
|
|
// 合并翻译
|
|
$translations = array_merge($location_translations, $common_translations);
|
|
|
|
// 如果是 POST 请求,处理数据保存和翻译
|
|
if ($request->method() === 'POST') {
|
|
// 获取表单数据
|
|
$data = $request->all();
|
|
Log::info('location',$data);
|
|
|
|
// 保存主表数据
|
|
$location = new Location();
|
|
$location->fill($data);
|
|
$location->save();
|
|
|
|
// 处理多语言翻译
|
|
$languages = ['en', 'ar', 'ru'];
|
|
$translationModels = [
|
|
'en' => \app\model\LocationEn::class,
|
|
'ar' => \app\model\LocationAr::class,
|
|
'ru' => \app\model\LocationRu::class
|
|
];
|
|
|
|
// 创建翻译服务实例
|
|
$translatorService = new \app\service\TranslatorService();
|
|
|
|
foreach ($languages as $lang) {
|
|
$translationModel = $translationModels[$lang];
|
|
$translation = new $translationModel();
|
|
|
|
// 复制主表数据
|
|
$translation->fill($data);
|
|
$translation->id = $location->id;
|
|
|
|
// 使用翻译服务处理需要翻译的字段
|
|
$translatedData = $translatorService->location($data, $lang);
|
|
|
|
// 更新翻译后的字段
|
|
$translation->name = $translatedData['name'] ?? $data['name'];
|
|
// $translation->description = $translatedData['description'] ?? $data['description'];
|
|
|
|
Log::info('location:'.$lang.':translatedData',$translatedData);
|
|
$translation->save();
|
|
}
|
|
}
|
|
|
|
return view('location/insert', [
|
|
'lang' => $lang,
|
|
'translations' => $translations
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 更新
|
|
* @param Request $request
|
|
* @return Response
|
|
* @throws BusinessException
|
|
*/
|
|
public function update(Request $request): Response
|
|
{
|
|
if ($request->method() === 'POST') {
|
|
return parent::update($request);
|
|
}
|
|
// 获取当前语言
|
|
$lang = session('lang', 'zh-cn');
|
|
|
|
// 加载语言文件
|
|
$location_translations = include __DIR__ . "/../lang/{$lang}/location.php";
|
|
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
|
|
|
// 合并翻译
|
|
$translations = array_merge($location_translations, $common_translations);
|
|
|
|
return view('location/update', [
|
|
'lang' => $lang,
|
|
'translations' => $translations
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取下拉选项接口
|
|
*/
|
|
public function getAll()
|
|
{
|
|
$lang = session('lang', 'zh-cn');
|
|
switch ($lang) {
|
|
case 'zh-cn':
|
|
$hz = new \app\model\Location();
|
|
break;
|
|
|
|
case 'en':
|
|
$hz = new \app\model\LocationEn();
|
|
break;
|
|
|
|
case 'ru':
|
|
$hz = new \app\model\LocationRu();
|
|
break;
|
|
case 'ar':
|
|
$hz = new \app\model\LocationAr();
|
|
break;
|
|
}
|
|
$locations = $hz::get();
|
|
return Util::success($locations);
|
|
}
|
|
|
|
}
|