diff --git a/app/Http/Controllers/Api/Publications/PublicationsController.php b/app/Http/Controllers/Api/Publications/PublicationsController.php index 2118bc4..26968b8 100644 --- a/app/Http/Controllers/Api/Publications/PublicationsController.php +++ b/app/Http/Controllers/Api/Publications/PublicationsController.php @@ -23,17 +23,10 @@ class PublicationsController extends Controller { } public function index(Request $request): JsonResponse { - $where = ['is_published' => true]; - if (Auth::check()) { - if ($request->has('edit_mode')) { - if ($request->get('edit_mode')==='yes') { - unset($where['is_published']); - } - } - } $query = $this->model->query()->orderBy('id', 'desc'); - if ($page = Page::byUuid($request->get('page'))->first()) $where[] = ['page_id' => $page->id]; - $query->where($where); + $user = Auth::user(); + if (!($user->isAdmin ?? null)) $query->where(['is_published' => true]); + if ($page = Page::byUuid($request->get('page'))->first()) $query->where(['page_id' => $page->id]); $paginator = $query->paginate(config('app.pagination_limit')); return fractal($paginator, new PublicationTransformer())->respond(); } @@ -51,7 +44,7 @@ class PublicationsController extends Controller { public function update(Request $request, $uuid): void { } - public function destroy(Request $request, $uuid) { + public function destroy(Request $request, $uuid): JsonResponse { $model = $this->model->byUuid($uuid)->firstOrFail(); $model->delete(); return response()->json(null, 204); diff --git a/app/Models/Pages/Page.php b/app/Models/Pages/Page.php index 29fc752..1809557 100644 --- a/app/Models/Pages/Page.php +++ b/app/Models/Pages/Page.php @@ -96,22 +96,9 @@ class Page extends Model { public static function byUrl($url) { if ($url = trim($url, '/ ')) { $query = self::query(); - $reduceIndex = 0; - collect(explode('/', $url))->reverse()->values()->each(function($slug, $index) use ($query, &$reduceIndex) { - $exist = self::query()->where(['slug' => $slug])->exists(); - if ($exist) { - if ($slug !== '') { - $currentIndex = $index - $reduceIndex; - $currentIndex ? $query->nthParentSlug($currentIndex, $slug) : $query->bySlug($slug); - } - } else { - $reduceIndex++; - $exist = Publication::query()->where(['slug' => $slug])->exists(); - if ($exist) { - $query->whereHas('publications', function($query) use($slug) { - $query->where(['slug' => $slug]); - }); - } + collect(explode('/', $url))->reverse()->values()->each(function($slug, $index) use ($query) { + if ($slug !== '') { + $index ? $query->nthParentSlug($index, $slug) : $query->bySlug($slug); } }); return $query->first(); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e5a0505..381a45a 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -11,6 +11,7 @@ use App\Models\Objects\NirObject; use App\Models\Objects\ObjectType; use App\Models\Pages\Page; use App\Models\Permission; +use App\Models\Publications\Publication; use App\Models\Role; use App\Models\SocialProvider; use App\Models\User; @@ -55,7 +56,8 @@ class AppServiceProvider extends ServiceProvider 'object' => NirObject::class, 'object-type' => ObjectType::class, - 'page' => Page::class + 'page' => Page::class, + 'publication' => Publication::class, ]); } } diff --git a/app/Services/Forms/Publications/PublicationForms.php b/app/Services/Forms/Publications/PublicationForms.php index 453bbb6..ae3d02d 100644 --- a/app/Services/Forms/Publications/PublicationForms.php +++ b/app/Services/Forms/Publications/PublicationForms.php @@ -41,12 +41,6 @@ class PublicationForms extends FormsService { 'required' => true, 'value' => $model->excerpt ?? null ], - [ - 'name' => 'description', - 'title' => 'Описание', - 'type' => FieldType::HTML, - 'value' => $model->description ?? null - ], [ 'name' => 'poster_id', 'title' => 'Фотография профиля', @@ -60,8 +54,8 @@ class PublicationForms extends FormsService { public function store(array $data): ?JsonResponse { - if (!empty($data['attach']['page_slug'])) { - $page = Page::query()->where(['slug' => $data['attach']['page_slug']])->first(); + if (!empty($data['attach']['page_id'])) { + $page = Page::byUuid($data['attach']['page_id'])->first(); $data['page_id'] = $page->id; } $data['user_id'] = Auth::user()->id; diff --git a/database/seeders/Pages/PagesTableSeeder.php b/database/seeders/Pages/PagesTableSeeder.php index 63a581b..822bf98 100644 --- a/database/seeders/Pages/PagesTableSeeder.php +++ b/database/seeders/Pages/PagesTableSeeder.php @@ -123,8 +123,8 @@ class PagesTableSeeder extends Seeder 'children' => [ 'Новости' => ['type' => PageType::PUBLICATIONS], 'СМИ о нас' => ['type' => PageType::REGISTRY], - 'Фотогалерея' => [], - 'Видеоархив' => [], + 'Фотогалерея' => ['type' => PageType::PUBLICATIONS], + 'Видеоархив' => ['type' => PageType::PUBLICATIONS], 'Контакты для СМИ' => [], ] ],