Preview: ServiceCategoryRepository.php
Size: 4.97 KB
/home/byroehnu/easetack.com/app/Repositories/Admin/ServiceCategoryRepository.php
<?php
namespace App\Repositories\Admin;
use App\Http\Requests\Admin\ServiceCategoryStoreRequest;
use App\Http\Requests\Admin\ServiceCategoryUpdateRequest;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\Setting;
use App\Repositories\Traits\ModelRepositoryTraits;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
class ServiceCategoryRepository
{
use ModelRepositoryTraits;
/**
* Object model will be used to modify categories table
*/
protected ServiceCategory $model;
/**
* Constructor for Category repository
*/
public function __construct(ServiceCategory $category)
{
$this->model = $category;
}
/**
* Get search result with paginate
*/
public function paginateSearchResult($search, array $sort = []): LengthAwarePaginator
{
$query = $this->model->with('content')->newQuery();
// search category
if (isset($search)) {
$query->whereHas('contents', function ($q) use ($search) {
$q->where('language_code', app()->getLocale())
->where('title', 'like', '%' . $search . '%');
});
}
// sort category
if (isset($sort['column'])) {
$query->orderBy($sort['column'], $sort['order']);
}
return $query->paginate(30)
->appends(array_filter([
'search' => $search,
'sort' => $sort,
'lang' => app()->getLocale(),
]));
}
/**
* Store category
*/
public function store(ServiceCategoryStoreRequest $request): void
{
$category = $this->model->create($request->all());
$languages = json_decode(Setting::pull('languages'), true);
$content = array_map(function ($language) use ($request) {
$langCode = $language['code'];
return [
'language_code' => $langCode,
'title' => $request->input($langCode . '_title'),
];
}, $languages);
$category->contents()->createMany($content);
}
/**
* Get edited data
*/
public function getEditData(ServiceCategory $serviceCategory): array
{
$languages = json_decode(Setting::pull('languages'), true);
$data = [
'id' => $serviceCategory->id,
'created_at' => $serviceCategory->created_at,
];
foreach ($languages as $language) {
$langCode = $language['code'];
$data[$langCode . '_title'] = '';
}
foreach ($serviceCategory->contents as $content) {
$langCode = $content->language_code;
$data[$langCode . '_title'] = $content->title;
}
return $data;
}
/**
* Update category
*/
public function update(ServiceCategory $serviceCategory, ServiceCategoryUpdateRequest $request): void
{
if ($serviceCategory?->content?->title == 'Uncategorized') {
throw new \Exception('The category cannot be updated.');
}
$languages = json_decode(Setting::pull('languages'), true);
foreach ($languages as $language) {
$langCode = $language['code'];
$serviceCategory->contents()->updateOrCreate(
['language_code' => $langCode],
[
'title' => $request[$langCode . '_title'],
],
);
}
}
/**
* Bulk delete
*/
public function bulkDelete(Request $request): void
{
$uncategorizedCategoryId = ServiceCategory::where('title', 'Uncategorized')->first()->id;
if ($uncategorizedCategoryId) {
// Exclude the "uncategorized" category ID from the list
$categoriesToDelete = array_diff(explode(',', $request->ids), [$uncategorizedCategoryId]);
if (empty($categoriesToDelete)) {
// No categories to delete (excluding "uncategorized")
return;
}
// Update posts assigned to the categories to the "uncategorized" category
Service::whereIn('category_id', $categoriesToDelete)->update(['category_id' => $uncategorizedCategoryId]);
// Delete the categories (excluding "uncategorized")
ServiceCategory::whereIn('id', $categoriesToDelete)->delete();
}
}
/**
* Delete category
*
* @throws \Exception
*/
public function destroy(ServiceCategory $serviceCategory): void
{
if ($serviceCategory->title == 'Uncategorized') {
throw new \Exception('The category cannot be deleted.');
}
$uncategorizedCategoryId = ServiceCategory::where('title', 'Uncategorized')->first()?->id;
// Update posts assigned to the categories to the "uncategorized" category
Service::where('category_id', $serviceCategory->id)->update(['category_id' => $uncategorizedCategoryId]);
// Delete the categories
$serviceCategory->delete();
}
}
Directory Contents
Dirs: 0 × Files: 37