335 lines
9.0 KiB
PHP
335 lines
9.0 KiB
PHP
<?php
|
||
|
||
namespace App\Repositories;
|
||
|
||
use App\TranslatableModel;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use App\ModelTranslation;
|
||
use Cache;
|
||
|
||
class ModelTranslationRepository extends BaseRepository
|
||
{
|
||
/**
|
||
* @var Model $translatedModel
|
||
*/
|
||
private $translatedModel = '';
|
||
|
||
public function __construct(ModelTranslation $modelTranslation)
|
||
{
|
||
$this->setModel($modelTranslation);
|
||
}
|
||
|
||
/**
|
||
* 產生Cache key
|
||
*
|
||
* @param $model
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @param string $locale
|
||
* @return string
|
||
*/
|
||
private function getCacheKey($model, $modelId, $attribute, $locale = '')
|
||
{
|
||
return "model_translation_{$model}_{$modelId}_{$attribute}_{$locale}";
|
||
}
|
||
|
||
/**
|
||
* 設定當前的Model
|
||
*
|
||
* @param $model
|
||
*/
|
||
public function setTranslatedModel($model)
|
||
{
|
||
if(is_string($model)) {
|
||
$this->translatedModel = $model;
|
||
} elseif($model instanceof TranslatableModel) {
|
||
$this->translatedModel = get_class($model);
|
||
}
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 更新一項翻譯,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @param $locale
|
||
* @param $content
|
||
*/
|
||
public function updateTranslation($model, $modelId, $attribute, $locale, $content)
|
||
{
|
||
$updated = $this->model->updateOrInsert([
|
||
'model_type' => $model,
|
||
'model_id' => $modelId,
|
||
'attribute' => $attribute,
|
||
'locale' => $locale,
|
||
], [
|
||
'model_type' => $model,
|
||
'model_id' => $modelId,
|
||
'attribute' => $attribute,
|
||
'locale' => $locale,
|
||
'content' => $content
|
||
]);
|
||
Cache::forget($this->getCacheKey($model, $modelId, $attribute, $locale));
|
||
}
|
||
|
||
/**
|
||
* 更新一項翻譯
|
||
*
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @param $locale
|
||
* @param $content
|
||
*/
|
||
public function updateModelTranslation($modelId, $attribute, $locale, $content)
|
||
{
|
||
return $this->updateTranslation($this->translatedModel, $modelId, $attribute, $locale, $content);
|
||
}
|
||
|
||
/**
|
||
* 取得一項翻譯,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @param $locale
|
||
* @return string|null
|
||
*/
|
||
public function getTranslation($model, $modelId, $attribute, $locale)
|
||
{
|
||
$cacheKey = $this->getCacheKey($model, $modelId, $attribute, $locale);
|
||
if(Cache::has($cacheKey)) {
|
||
return Cache::get($cacheKey);
|
||
}
|
||
$record = $this->model->where([
|
||
'model_type' => $model,
|
||
'model_id' => $modelId,
|
||
'attribute' => $attribute,
|
||
'locale' => $locale,
|
||
])->first();
|
||
if($record) {
|
||
Cache::put($cacheKey, $record->content);
|
||
return $record->content;
|
||
} else {
|
||
Cache::put($cacheKey, null);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 取得一項翻譯
|
||
*
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @param $locale
|
||
* @return string|null
|
||
*/
|
||
public function getModelTranslation($modelId, $attribute, $locale)
|
||
{
|
||
return $this->getTranslation($this->translatedModel, $modelId, $attribute, $locale);
|
||
}
|
||
|
||
/**
|
||
* 取得所有語系的翻譯內容,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @return array
|
||
*/
|
||
public function getTranslations($model, $modelId, $attribute)
|
||
{
|
||
$cacheKey = $this->getCacheKey($model, $modelId, $attribute);
|
||
if(Cache::has($cacheKey)) {
|
||
return Cache::get($cacheKey);
|
||
}
|
||
$record = $this->model->where([
|
||
'model_type' => $model,
|
||
'model_id' => $modelId,
|
||
'attribute' => $attribute,
|
||
])
|
||
->get()
|
||
->mapWithKeys(function($model){
|
||
/** @var \App\ModelTranslation $model */
|
||
return [$model->locale => $model->content];
|
||
})
|
||
->toArray();
|
||
Cache::put($cacheKey, $record);
|
||
return $record;
|
||
}
|
||
|
||
/**
|
||
* 取得所有語系的翻譯內容
|
||
*
|
||
* @param $modelId
|
||
* @param $attribute
|
||
* @return array
|
||
*/
|
||
public function getModelTranslations($modelId, $attribute)
|
||
{
|
||
return $this->getTranslations($this->translatedModel, $modelId, $attribute);
|
||
}
|
||
|
||
/**
|
||
* 取得所有屬性的所有語系翻譯,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $modeId
|
||
* @return array
|
||
*/
|
||
public function getAllTranslations($model, $modeId)
|
||
{
|
||
/** @var TranslatableModel $modelInstance */
|
||
$modelInstance = app($model);
|
||
$translatableAttributes = $modelInstance->getTranslatableAttributes();
|
||
|
||
$translations = [];
|
||
$modelTranslations = $this->model->where([
|
||
'model_type' => $model,
|
||
'model_id' => $modeId
|
||
])
|
||
->whereIn('attribute', $translatableAttributes)
|
||
->get();
|
||
|
||
foreach ($modelTranslations as $modelTranslation) {
|
||
$translations[$modelTranslation->attribute][$modelTranslation->locale] = $modelTranslation->content;
|
||
}
|
||
|
||
return $translations;
|
||
}
|
||
|
||
/**
|
||
* 取得所有屬性的所有語系翻譯
|
||
*
|
||
* @param $modelId
|
||
* @return array
|
||
*/
|
||
public function getModelAllTranslations($modelId)
|
||
{
|
||
return $this->getAllTranslations($this->translatedModel, $modelId);
|
||
}
|
||
|
||
/**
|
||
* 刪除所有語系的翻譯,可指定屬性,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $modelId
|
||
* @param null $attribute
|
||
* @return bool|null
|
||
* @throws \Exception
|
||
*/
|
||
public function deleteTranslations($model, $modelId, $attribute = null)
|
||
{
|
||
$cacheKey = $this->getCacheKey($model, $modelId, $attribute);
|
||
$whereArg = [
|
||
'model_type' => $model,
|
||
'model_id' => $modelId
|
||
];
|
||
|
||
if(!empty($attribute)) {
|
||
$whereArg['attribute'] = $attribute;
|
||
}
|
||
|
||
$translations = $this->model->where($whereArg)->delete();
|
||
Cache::forget($cacheKey);
|
||
return $translations;
|
||
}
|
||
|
||
/**
|
||
* 刪除所有語系的翻譯,可指定屬性
|
||
*
|
||
* @param $modelId
|
||
* @param null $attribute
|
||
* @throws \Exception
|
||
*/
|
||
public function deleteModelTranslations($modelId, $attribute = null)
|
||
{
|
||
$this->deleteTranslations($this->translatedModel, $modelId, $attribute);
|
||
}
|
||
|
||
/**
|
||
* 批次取得指定屬性群的指定語系翻譯,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $modelId
|
||
* @param $locale
|
||
* @param $attributes
|
||
* @return mixed
|
||
*/
|
||
public function getTranslationsWithAttributesInLocale($model, $modelId, $locale, $attributes)
|
||
{
|
||
$translations = $this->model->where([
|
||
'model_type' => $model,
|
||
'model_id' => $modelId,
|
||
'locale' => $locale,
|
||
])
|
||
->whereIn('attribute', $attributes)
|
||
->get();
|
||
|
||
return $translations;
|
||
}
|
||
|
||
/**
|
||
* 批次取得指定屬性群與指定ID群的指定語系翻譯,需傳入目標model
|
||
*
|
||
* @param $model
|
||
* @param $locale
|
||
* @param $ids
|
||
* @param $attributes
|
||
* @return mixed
|
||
*/
|
||
public function getTranslationsWithIdsAndAttributesInLocale($model, $locale, $ids, $attributes)
|
||
{
|
||
$translations = $this->model->select(['model_id', 'attribute', 'content'])
|
||
->where([
|
||
'model_type' => $model,
|
||
'locale' => $locale,
|
||
])
|
||
->whereIn('attribute', $attributes)
|
||
->whereIn('model_id', $ids)
|
||
->orderBy('model_id')
|
||
->get();
|
||
return $translations;
|
||
}
|
||
|
||
/**
|
||
* 搜尋指定屬性的所有語系翻譯,需傳入目標model,可指定語系
|
||
*
|
||
* @param $model
|
||
* @param $attribute
|
||
* @param $search
|
||
* @param string $compare
|
||
* @param null $locale
|
||
* @return mixed
|
||
*/
|
||
public function searchAttribute($model, $attribute, $search, $compare = '=', $locale = null)
|
||
{
|
||
$modelQuery = $this->model
|
||
->where('model_type', $model)
|
||
->where('attribute', $attribute)
|
||
->where('content', $compare, $search);
|
||
|
||
if($locale) {
|
||
$modelQuery->where('locale', $locale);
|
||
}
|
||
|
||
|
||
return $modelQuery->get();
|
||
}
|
||
|
||
/**
|
||
* 搜尋指定屬性的所有語系翻譯,可指定語系
|
||
*
|
||
* @param $attribute
|
||
* @param $search
|
||
* @param string $compare
|
||
* @param null $locale
|
||
* @return mixed
|
||
*/
|
||
public function searchModelAttribute($attribute, $search, $compare = '=', $locale = null)
|
||
{
|
||
return $this->searchAttribute($this->translatedModel, $attribute, $search, $compare, $locale);
|
||
}
|
||
}
|