AppsController.php
3 weeks ago
CollaborationCommentController.php
3 weeks ago
CollectionController.php
3 weeks ago
CollectionItemController.php
3 weeks ago
EditorController.php
3 weeks ago
GlobalDataController.php
4 days ago
MediaController.php
3 weeks ago
PageController.php
4 days ago
PageSettingsController.php
3 weeks ago
UserController.php
3 weeks ago
CollectionController.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Http\Controllers\Api; |
| 4 | |
| 5 | use Kirki\App\DTO\ListFilterDTO; |
| 6 | use Kirki\App\DTO\Collection\UpsertCollectionDTO; |
| 7 | use Kirki\App\Http\Requests\Collection\CollectionStoreRequest; |
| 8 | use Kirki\App\Resources\Collection\CollectionResource; |
| 9 | use Kirki\App\Services\CollectionService; |
| 10 | use Kirki\App\Supports\ContentManager; |
| 11 | use Kirki\Framework\Http\Request; |
| 12 | |
| 13 | use function Kirki\Framework\response; |
| 14 | |
| 15 | class CollectionController |
| 16 | { |
| 17 | /** |
| 18 | * @var CollectionService |
| 19 | */ |
| 20 | protected $service; |
| 21 | |
| 22 | public function __construct(CollectionService $service) |
| 23 | { |
| 24 | $this->service = $service; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Paginated listing of collections. |
| 29 | */ |
| 30 | public function index(Request $request) |
| 31 | { |
| 32 | $filters = ListFilterDTO::from_array([ |
| 33 | 'page' => $request->int('page', 1), |
| 34 | 'limit' => $request->int('limit', 20), |
| 35 | ]); |
| 36 | |
| 37 | $collections = $this->service->paginated($filters); |
| 38 | |
| 39 | return response()->json([ |
| 40 | 'data' => CollectionResource::paginated($collections), |
| 41 | 'message' => __('Collections retrieved successfully.', 'kirki'), |
| 42 | ]); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Single collection |
| 47 | */ |
| 48 | public function show(Request $request) |
| 49 | { |
| 50 | $collection = $this->service->get_single($request->int('collection_id')); |
| 51 | |
| 52 | return response()->json([ |
| 53 | 'data' => CollectionResource::make($collection), |
| 54 | 'message' => __('Collection retrieved successfully.', 'kirki'), |
| 55 | ]); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Create or update a collection. |
| 60 | */ |
| 61 | public function store(CollectionStoreRequest $request) |
| 62 | { |
| 63 | $payload = UpsertCollectionDTO::from_array($request->all()); |
| 64 | $collection = $this->service->create_or_update($payload); |
| 65 | |
| 66 | return response()->json([ |
| 67 | 'data' => CollectionResource::make($collection), |
| 68 | 'message' => __('Collection saved successfully.', 'kirki'), |
| 69 | ]); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Delete a collection. |
| 74 | */ |
| 75 | public function destroy(Request $request) |
| 76 | { |
| 77 | return response()->json([ |
| 78 | 'data' => $this->service->delete($request->int('collection_id')), |
| 79 | 'message' => __('Collection deleted successfully.', 'kirki'), |
| 80 | ]); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Duplicate a collection (parent only). |
| 85 | */ |
| 86 | public function duplicate(Request $request) |
| 87 | { |
| 88 | $collection = $this->service->duplicate($request->int('collection_id')); |
| 89 | |
| 90 | return response()->json([ |
| 91 | 'data' => CollectionResource::make($collection), |
| 92 | 'message' => __('Collection duplicated successfully.', 'kirki'), |
| 93 | ]); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Validate a post slug. |
| 98 | */ |
| 99 | public function validate_slug(Request $request) |
| 100 | { |
| 101 | $is_valid = ContentManager::validate_slug( |
| 102 | $request->int('post_id', 0), |
| 103 | $request->string('post_type'), |
| 104 | $request->string('post_name') |
| 105 | ); |
| 106 | |
| 107 | return response()->json([ |
| 108 | 'data' => $is_valid, |
| 109 | 'message' => __('Slug validated successfully.', 'kirki'), |
| 110 | ]); |
| 111 | } |
| 112 | } |
| 113 |