generated from liutk/owl-admin-base
96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\ProjectArticle;
|
|
use App\Models\Filters\ProjectArticleFilter;
|
|
use App\Traits\UploadTrait;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @method ProjectArticle getModel()
|
|
* @method ProjectArticle|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class ProjectArticleService extends BaseService
|
|
{
|
|
use UploadTrait;
|
|
|
|
protected string $modelName = ProjectArticle::class;
|
|
|
|
protected string $modelFilterName = ProjectArticleFilter::class;
|
|
|
|
public function listQuery()
|
|
{
|
|
$currentUrl = url()->current();
|
|
$query= parent::listQuery();
|
|
if(Str::contains($currentUrl, 'project_articles')){
|
|
$query->where('type', ProjectArticle::TYPE_ARTICLE);
|
|
}elseif(Str::contains($currentUrl, 'project_flows')){
|
|
$query->where('type', ProjectArticle::TYPE_FLOW);
|
|
}elseif(Str::contains($currentUrl, 'project_photos')){
|
|
$query->where('type', ProjectArticle::TYPE_PHOTO);
|
|
}elseif(Str::contains($currentUrl, 'project_advances')){
|
|
$query->where('type', ProjectArticle::TYPE_ADVANCE);
|
|
}elseif(Str::contains($currentUrl, 'case_studies')){
|
|
$query->where('type', ProjectArticle::TYPE_CASE_STUDY);
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function store($data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->getModel();
|
|
|
|
$isEnable = Arr::get($data, 'is_enabled');
|
|
$publishedAt = Arr::get($data, 'published_at');
|
|
if ($isEnable && empty($publishedAt)) {
|
|
$data['published_at'] = now();
|
|
}
|
|
|
|
$data['cover'] = $this->saveImage('cover', 'project_articles/cover')[0] ?? '';
|
|
$data['appendixes'] = $this->saveFile('appendixes', 'project_articles/appendixes');
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->query()->whereKey($primaryKey)->first();
|
|
|
|
$isEnable = Arr::get($data, 'is_enable');
|
|
$publishedAt = Arr::get($data, 'published_at');
|
|
|
|
if ($isEnable && empty($publishedAt) && empty($model->published_at)) {
|
|
$data['published_at'] = now();
|
|
}
|
|
|
|
if(isset($data['cover'])){
|
|
$data['cover'] = $this->saveImage('cover', 'project_articles/covers')[0] ?? '';
|
|
}
|
|
|
|
if(isset($data['appendixes'])){
|
|
$data['appendixes'] = $this->saveFile('appendixes', 'project_articles/appendixes');
|
|
}
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
} |