diff --git a/app/Http/Controllers/Api/Publications/PublicationsController.php b/app/Http/Controllers/Api/Publications/PublicationsController.php index b0fb4fe..5391949 100644 --- a/app/Http/Controllers/Api/Publications/PublicationsController.php +++ b/app/Http/Controllers/Api/Publications/PublicationsController.php @@ -25,7 +25,7 @@ class PublicationsController extends Controller { } public function index(Request $request): JsonResponse { - $query = $this->model->query()->orderBy('id', 'desc'); + $query = $this->model->query()->orderBy('published_at', 'desc'); $user = Auth::user(); if (!($user->isAdmin ?? null)) $query->where(['is_published' => true]); if ($page = Page::byUuid($request->get('page_id'))->first()) $query->where(['page_id' => $page->id]); diff --git a/app/Models/Publications/Publication.php b/app/Models/Publications/Publication.php index 4fca6f0..bd53110 100644 --- a/app/Models/Publications/Publication.php +++ b/app/Models/Publications/Publication.php @@ -18,6 +18,7 @@ class Publication extends Model { use UuidScopeTrait, SoftDeletes, HasObjectsTrait, RelationValuesTrait; protected $dates = [ + 'published_at' ]; protected $fillable = [ @@ -30,7 +31,8 @@ class Publication extends Model { 'name', 'excerpt', 'params', - 'is_published' + 'published_at', + 'is_published', ]; protected $hidden = [ diff --git a/app/Services/Forms/Publications/PublicationNewsForms.php b/app/Services/Forms/Publications/PublicationNewsForms.php index 04b43de..9e3e0ea 100644 --- a/app/Services/Forms/Publications/PublicationNewsForms.php +++ b/app/Services/Forms/Publications/PublicationNewsForms.php @@ -27,11 +27,20 @@ class PublicationNewsForms extends FormsService { public function commonGroupFields(?Publication $model): array { $fields = [ + + [ + 'name' => 'published_at', + 'title' => 'Дата публикации', + 'type' => FieldType::DATE, + 'value' => $model->published_at ?? null, + 'required' => true, + ], [ 'name' => 'name', 'title' => 'Название', 'type' => FieldType::STRING, 'required' => true, + 'max_length' => 127, 'value' => $model->name ?? null ], [ @@ -52,7 +61,6 @@ class PublicationNewsForms extends FormsService { } - public function store(array $data): ?JsonResponse { if (!empty($data['attach']['page_id'])) { $page = Page::byUuid($data['attach']['page_id'])->first(); diff --git a/app/Services/Forms/Publications/PublicationPhotosForms.php b/app/Services/Forms/Publications/PublicationPhotosForms.php index d8b1b25..d946c34 100644 --- a/app/Services/Forms/Publications/PublicationPhotosForms.php +++ b/app/Services/Forms/Publications/PublicationPhotosForms.php @@ -38,11 +38,19 @@ class PublicationPhotosForms } $fields = [ + [ + 'name' => 'published_at', + 'title' => 'Дата публикации', + 'type' => FieldType::DATE, + 'value' => $model->published_at ?? null, + 'required' => true, + ], [ 'name' => 'name', 'title' => 'Название', 'type' => FieldType::STRING, 'required' => true, + 'max_length' => 127, 'value' => $model->name ?? null ], [ @@ -66,6 +74,7 @@ class PublicationPhotosForms $pub['author_id'] = Auth::user()->id; $pub['type'] = PublicationType::PHOTOS; $pub['name'] = $data['name']; + $pub['published_at'] = $data['published_at']; if (!empty($data['assets'])) { $pub['params'] = json_encode([ 'assets' => $data['assets'] @@ -87,6 +96,7 @@ class PublicationPhotosForms 'assets' => $data['assets'] ]); } + $pub['published_at'] = $data['published_at']; $model->update($pub); return fractal($model, new PublicationTransformer())->respond(); diff --git a/app/Services/Forms/Publications/PublicationSmiForms.php b/app/Services/Forms/Publications/PublicationSmiForms.php index 5e66020..1311f13 100644 --- a/app/Services/Forms/Publications/PublicationSmiForms.php +++ b/app/Services/Forms/Publications/PublicationSmiForms.php @@ -31,11 +31,19 @@ class PublicationSmiForms $params = $model->parsedParams ?? null; $fields = [ + [ + 'name' => 'published_at', + 'title' => 'Дата публикации', + 'type' => FieldType::DATE, + 'value' => $model->published_at ?? null, + 'required' => true, + ], [ 'name' => 'name', 'title' => 'Название', 'type' => FieldType::STRING, 'required' => true, + 'max_length' => 127, 'value' => $model->name ?? null ], [ @@ -80,6 +88,7 @@ class PublicationSmiForms $pub['type'] = PublicationType::SMI; $pub['name'] = $data['name']; $pub['excerpt'] = $data['excerpt']; + $pub['published_at'] = $data['published_at']; if (empty($data['poster_id'])) { $pub['poster_id'] = null; } else { @@ -129,6 +138,7 @@ class PublicationSmiForms if (count($params) > 0) { $pub['params'] = json_encode($params); } + $pub['published_at'] = $data['published_at']; $model->update($pub); return fractal($model->fresh(), new PublicationTransformer())->respond(); } diff --git a/app/Services/Forms/Publications/PublicationVideoForms.php b/app/Services/Forms/Publications/PublicationVideoForms.php index 1673779..a1687b9 100644 --- a/app/Services/Forms/Publications/PublicationVideoForms.php +++ b/app/Services/Forms/Publications/PublicationVideoForms.php @@ -13,6 +13,7 @@ use App\Transformers\Publications\PublicationTransformer; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; +use stringEncode\Encode; class PublicationVideoForms extends FormsService { @@ -31,6 +32,13 @@ class PublicationVideoForms $params = $model->parsedParams ?? null; $fields = [ + [ + 'name' => 'published_at', + 'title' => 'Дата публикации', + 'type' => FieldType::DATE, + 'value' => $model->published_at ?? null, + 'required' => true, + ], [ 'name' => 'url', 'title' => 'Ссылка на видео', @@ -58,9 +66,10 @@ class PublicationVideoForms $pub['excerpt'] = $data['excerpt']; $pub['author_id'] = Auth::user()->id; $pub['type'] = PublicationType::VIDEO; + $pub['published_at'] = $data['published_at']; if (!empty($data['url'])) { $pub['params'] = json_encode([ - 'url' => $data['url'] + 'url' => $this->formatUrl($data['url']) ]); } $model = Publication::create($pub, true); @@ -74,11 +83,30 @@ class PublicationVideoForms } if (!empty($data['url'])) { $pub['params'] = json_encode([ - 'url' => $data['url'] + 'url' => $this->formatUrl($data['url']) ]); } + $pub['published_at'] = $data['published_at']; $model = Publication::byUuid($id)->firstOrFail(); $model->update($pub); return fractal($model->fresh(), new PublicationTransformer())->respond(); } + + public function formatUrl($url): string { + $videoId = ''; + $videoUrl = ''; + switch (true) { + case str_contains($url, $mask = 'https://youtu.be/'): + case str_contains($url, $mask = 'https://www.youtube.com/watch?v='): + $videoId = str_replace($mask, '', $url); + break; + default: + $videoUrl = $url; + break; + } + if ($videoId != '') { + $videoUrl = "https://www.youtube.com/embed/$videoId"; + } + return $videoUrl; + } } diff --git a/app/Transformers/Publications/PublicationTransformer.php b/app/Transformers/Publications/PublicationTransformer.php index 1185abc..942f22d 100644 --- a/app/Transformers/Publications/PublicationTransformer.php +++ b/app/Transformers/Publications/PublicationTransformer.php @@ -38,6 +38,7 @@ class PublicationTransformer extends TransformerAbstract { 'name' => $model->name, 'excerpt' => $model->excerpt, 'is_published' => boolval($model->is_published), + 'published_at' => $model->published_at ? $model->published_at->toIso8601String() : null, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null, 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null ]; diff --git a/database/migrations/2023_06_07_153956_create_publications_table.php b/database/migrations/2023_06_07_153956_create_publications_table.php index 760a352..82fcd4e 100644 --- a/database/migrations/2023_06_07_153956_create_publications_table.php +++ b/database/migrations/2023_06_07_153956_create_publications_table.php @@ -25,6 +25,7 @@ class CreatePublicationsTable extends Migration $table->text('excerpt')->nullable(); $table->text('params')->nullable(); $table->boolean('is_published')->index()->default(0); + $table->timestamp('published_at')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/seeders/Objects/FieldsTableSeeder.php b/database/seeders/Objects/FieldsTableSeeder.php index bdc6b38..144491a 100644 --- a/database/seeders/Objects/FieldsTableSeeder.php +++ b/database/seeders/Objects/FieldsTableSeeder.php @@ -219,7 +219,13 @@ class FieldsTableSeeder extends Seeder { 'developer' => [ 'title' => 'Разработчик', 'type' => FieldType::STRING - ] + ], + + 'maps-url' => [ + 'title' => 'Адрес карты', + 'type' => FieldType::STRING, + 'required' => true, + ], ]; public function run() { diff --git a/database/seeders/Objects/ObjectTypeFieldsTableSeeder.php b/database/seeders/Objects/ObjectTypeFieldsTableSeeder.php index 14155f3..b3e1647 100644 --- a/database/seeders/Objects/ObjectTypeFieldsTableSeeder.php +++ b/database/seeders/Objects/ObjectTypeFieldsTableSeeder.php @@ -65,6 +65,11 @@ class ObjectTypeFieldsTableSeeder extends Seeder { 'fields' => ['feedback-support-email', 'feedback-form-type'] ] ], + 'page-section-maps' => [ + 'common' => [ + 'fields' => ['maps-url'] + ] + ], 'feedback-form-support' => [ 'common' => [ 'fields' => ['feedback-email', 'feedback-name', 'feedback-type', 'feedback-message'] diff --git a/database/seeders/Objects/ObjectTypesTableSeeder.php b/database/seeders/Objects/ObjectTypesTableSeeder.php index 54eff82..cbafd89 100644 --- a/database/seeders/Objects/ObjectTypesTableSeeder.php +++ b/database/seeders/Objects/ObjectTypesTableSeeder.php @@ -43,6 +43,9 @@ class ObjectTypesTableSeeder extends Seeder { ], 'page-section-feedback' => [ 'title' => 'Форма обратной связи' + ], + 'page-section-maps' => [ + 'title' => 'Карта' ] ] ], diff --git a/routes/api.php b/routes/api.php index f7b0369..e2044a7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -75,4 +75,4 @@ Route::group(['middleware' => ['auth:api']], function() { Route::put('publications/published/{id}', 'Api\Publications\PublicationsController@published'); Route::delete('publications/{id}', 'Api\Publications\PublicationsController@destroy'); -}); +}); \ No newline at end of file