diff --git a/app/Console/Commands/Import/Command.php b/app/Console/Commands/Import/Command.php index 05128312..a85c2f04 100644 --- a/app/Console/Commands/Import/Command.php +++ b/app/Console/Commands/Import/Command.php @@ -139,48 +139,48 @@ public function parseDate(?string $input): ?Carbon public function mapCounty(int $oldCountyId): int { $countiesMap = [ - 1 => 1,//Alba - 2 => 2,//Arad - 3 => 3,//Arges - 4 => 4,//Bacau - 5 => 5,//Bihor - 6 => 6,//Bistrita-Nasaud - 7 => 7,//Botosani - 8 => 9,//Braila - 9 => 8,//Brasov - 10 => 40,//Bucuresti - 11 => 10,//Buzau - 12 => 51,//Calarasi - 13 => 11,//Caras-Severin - 14 => 12,//Cluj - 15 => 13,//Constanta - 16 => 14,//Covasna - 17 => 15,//Dambovita - 18 => 16,//Dolj - 19 => 17,//Galati - 20 => 52,//Giurgiu - 21 => 18,//Gorj - 22 => 19,//Harghita - 23 => 20,//Hunedoara - 24 => 21,//Ialomita - 25 => 22,//Iasi - 26 => 23,//Ilfov - 27 => 24,//Maramures - 28 => 25,//Mehedinti - 29 => 26,//Mures - 30 => 27,//Neamt - 31 => 28,//Olt - 32 => 29,//Prahova - 33 => 31,//Salaj - 34 => 30,//Satu Mare - 35 => 32,//Sibiu - 36 => 33,//Suceava - 37 => 34,//Teleorman - 38 => 35,//Timis - 39 => 36,//Tulcea - 40 => 38,//Valcea - 41 => 37,//Vaslui - 42 => 39,//Vrancea + 1 => 1, //Alba + 2 => 2, //Arad + 3 => 3, //Arges + 4 => 4, //Bacau + 5 => 5, //Bihor + 6 => 6, //Bistrita-Nasaud + 7 => 7, //Botosani + 8 => 9, //Braila + 9 => 8, //Brasov + 10 => 40, //Bucuresti + 11 => 10, //Buzau + 12 => 51, //Calarasi + 13 => 11, //Caras-Severin + 14 => 12, //Cluj + 15 => 13, //Constanta + 16 => 14, //Covasna + 17 => 15, //Dambovita + 18 => 16, //Dolj + 19 => 17, //Galati + 20 => 52, //Giurgiu + 21 => 18, //Gorj + 22 => 19, //Harghita + 23 => 20, //Hunedoara + 24 => 21, //Ialomita + 25 => 22, //Iasi + 26 => 23, //Ilfov + 27 => 24, //Maramures + 28 => 25, //Mehedinti + 29 => 26, //Mures + 30 => 27, //Neamt + 31 => 28, //Olt + 32 => 29, //Prahova + 33 => 31, //Salaj + 34 => 30, //Satu Mare + 35 => 32, //Sibiu + 36 => 33, //Suceava + 37 => 34, //Teleorman + 38 => 35, //Timis + 39 => 36, //Tulcea + 40 => 38, //Valcea + 41 => 37, //Vaslui + 42 => 39, //Vrancea ]; diff --git a/app/Console/Commands/Import/ImportProjectsCommand.php b/app/Console/Commands/Import/ImportProjectsCommand.php index aa7c8ed7..72bf1c78 100644 --- a/app/Console/Commands/Import/ImportProjectsCommand.php +++ b/app/Console/Commands/Import/ImportProjectsCommand.php @@ -5,10 +5,12 @@ namespace App\Console\Commands\Import; use App\Enums\ProjectStatus; +use App\Models\BCRProject; use App\Models\Project; use App\Models\ProjectCategory; use App\Services\Sanitize; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Throwable; @@ -89,7 +91,7 @@ protected function importProjects(): void ->selectRaw("STRING_AGG(CountyId,',')") ->whereColumn('dbo.ProjectCounties.ProjectId', 'dbo.ONGProjects.Id'), ]) - ->join('dbo.Projects', 'dbo.Projects.Id', 'dbo.ONGProjects.Id') + ->rightJoin('dbo.Projects', 'dbo.Projects.Id', 'dbo.ONGProjects.Id') ->orderBy('dbo.ONGProjects.Id'); $this->createProgressBar( @@ -105,41 +107,9 @@ protected function importProjects(): void ->each(function (object $row) { $created_at = Carbon::parse($row->CreationDate); try { - $project = Project::forceCreate([ - 'id' => (int) $row->Id, - 'organization_id' => (int) $row->ONGId, - 'name' => Sanitize::text($row->Name), - 'slug' => Sanitize::text($row->DynamicUrl), - 'description' => Sanitize::text($row->Description), - 'target_budget' => (int) $row->TargetAmmount, - 'start' => $this->parseDate($row->StartDate), - 'end' => $this->parseDate($row->EndDate), - 'accepting_volunteers' => (bool) $row->HasVolunteering, - 'accepting_comments' => (bool) $row->AcceptComments, - 'reason_to_donate' => Sanitize::text($row->WhyDonate), - 'scope' => Sanitize::text($row->ProjectPurpose), - 'beneficiaries' => Sanitize::text($row->ToWhomDoIDonate), - - 'created_at' => $created_at, - 'updated_at' => $created_at, - 'archived_at' => $row->ProjectStatusTypeId === 3 ? $created_at : null, - - 'status' => match ($row->ProjectStatusTypeId) { - 1 => ProjectStatus::pending, - 2 => ProjectStatus::approved, - 3 => ProjectStatus::approved,//set arhivat - 4 => ProjectStatus::approved, - default => ProjectStatus::draft, - }, - ]); - - $project->categories()->attach($row->ProjectCategoryId); - - $mappedCounties = collect(explode(',', $row->Counties ?? '')) - ->filter(fn (string $county) => $county !== '') - ->map(fn (int $county) => $this->mapCounty($county)) - ->all(); - $project->counties()->attach($mappedCounties); + $project = empty($row->ONGId) ? + $this->createBcrProject($row, $created_at) : + $this->createProject($row, $created_at); if (! $this->option('skip-files')) { // Add main image @@ -164,4 +134,80 @@ protected function importProjects(): void : 'Imported projects' ); } + + private function createProject(object $row, Carbon $created_at): Model|Project + { + $project = Project::forceCreate([ + 'id' => (int) $row->Id, + 'organization_id' => (int) $row->ONGId, + 'name' => Sanitize::text($row->Name), + 'slug' => Sanitize::text($row->DynamicUrl), + 'description' => Sanitize::text($row->Description), + 'target_budget' => (int) $row->TargetAmmount, + 'start' => $this->parseDate($row->StartDate), + 'end' => $this->parseDate($row->EndDate), + 'accepting_volunteers' => (bool) $row->HasVolunteering, + 'accepting_comments' => (bool) $row->AcceptComments, + 'reason_to_donate' => Sanitize::text($row->WhyDonate), + 'scope' => Sanitize::text($row->ProjectPurpose), + 'beneficiaries' => Sanitize::text($row->ToWhomDoIDonate), + + 'created_at' => $created_at, + 'updated_at' => $created_at, + 'archived_at' => $row->ProjectStatusTypeId === 3 ? $created_at : null, + + 'status' => match ($row->ProjectStatusTypeId) { + 1 => ProjectStatus::pending, + 2 => ProjectStatus::approved, + 3 => ProjectStatus::approved,//set arhivat + 4 => ProjectStatus::approved, + default => ProjectStatus::draft, + }, + ]); + + $project->categories()->attach($row->ProjectCategoryId); + + $mappedCounties = collect(explode(',', $row->Counties ?? '')) + ->filter(fn (string $county) => $county !== '') + ->map(fn (int $county) => $this->mapCounty($county)) + ->all(); + $project->counties()->attach($mappedCounties); + + return $project; + } + + private function createBcrProject(object $row, Carbon $created_at): Model|BCRProject + { + $mappedCounties = collect(explode(',', $row->Counties ?? '')) + ->filter(fn (string $county) => $county !== '') + ->map(fn (int $county) => $this->mapCounty($county)); + + $countyID = $mappedCounties?->first() ?? null; + + + return BCRProject::forceCreate( + [ + 'id' => (int) $row->Id, + 'name' => Sanitize::text($row->Name), + 'slug' => Sanitize::text($row->DynamicUrl), + 'description' => Sanitize::text($row->Description), + 'project_category_id' => $row->ProjectCategoryId, + 'county_id' => $countyID, + 'start_date' => $this->parseDate($row->StartDate), + 'end_date' => $this->parseDate($row->EndDate), + 'is_national' => (bool) $row->AllCounties, + 'facebook_link' => $row->FacebookPageLink, + 'created_at' => $created_at, + 'updated_at' => $created_at, + + 'status' => match ($row->ProjectStatusTypeId) { + 1 => ProjectStatus::pending, + 2 => ProjectStatus::approved, + 3 => ProjectStatus::approved,//set arhivat + 4 => ProjectStatus::approved, + default => ProjectStatus::draft, + }, + ] + ); + } } diff --git a/app/Filament/Resources/BCRProjectResource.php b/app/Filament/Resources/BCRProjectResource.php index 187733d3..41ec134b 100644 --- a/app/Filament/Resources/BCRProjectResource.php +++ b/app/Filament/Resources/BCRProjectResource.php @@ -4,12 +4,11 @@ namespace App\Filament\Resources; +use App\Enums\ProjectStatus; use App\Filament\Resources\BCRProjectResource\Pages; -use App\Models\Article; use App\Models\BCRProject; use Camya\Filament\Forms\Components\TitleWithSlugInput; use Filament\Forms\Components\DatePicker; -use Filament\Forms\Components\Grid; use Filament\Forms\Components\Repeater; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; @@ -78,6 +77,11 @@ public static function form(Form $form): Form ->inlineLabel() ->columnSpanFull() ->required(), + Select::make('status') + ->label('bcr-project.labels.status') + ->inlineLabel() + ->options(ProjectStatus::options()) + ->required(), Textarea::make('description') ->label(__('bcr-project.labels.description')) diff --git a/app/Http/Resources/BcrProjectResource.php b/app/Http/Resources/BcrProjectResource.php index a37ecfe2..dfc16910 100644 --- a/app/Http/Resources/BcrProjectResource.php +++ b/app/Http/Resources/BcrProjectResource.php @@ -13,11 +13,12 @@ class BcrProjectResource extends JsonResource public function toArray(Request $request): array { + return [ 'id' => $this->id, 'name' => $this->name, 'slug' => $this->slug, - 'county' => $this->county->name, + 'county' => $this?->county?->name, 'image' => $this->getFirstMediaUrl('preview'), 'gallery' => $this->getMedia('gallery')->map(function ($media) { return [ @@ -26,7 +27,7 @@ public function toArray(Request $request): array ]; })->toArray(), 'is_national' => \boolval($this->is_national), - 'area' => $this->is_national ? __('bcr-project.labels.national') : $this->county->name, + 'area' => $this->is_national ? __('bcr-project.labels.national') : $this?->county?->name, 'start' => $this->start_date?->format('d.m.Y'), 'end' => $this->end_date?->format('d.m.Y'), 'description' => $this->description, diff --git a/app/Models/BCRProject.php b/app/Models/BCRProject.php index 7ff55815..113ac7d8 100644 --- a/app/Models/BCRProject.php +++ b/app/Models/BCRProject.php @@ -23,6 +23,8 @@ class BCRProject extends Model implements HasMedia use InteractsWithMedia; use HasSlug; + protected $table='bcr_projects'; + protected $fillable = [ 'name', 'description', diff --git a/database/factories/BCRProjectFactory.php b/database/factories/BCRProjectFactory.php index 84b3f371..f4b510e2 100644 --- a/database/factories/BCRProjectFactory.php +++ b/database/factories/BCRProjectFactory.php @@ -4,6 +4,7 @@ namespace Database\Factories; +use App\Enums\ProjectStatus; use App\Models\City; use App\Models\ProjectCategory; use Illuminate\Database\Eloquent\Factories\Factory; @@ -37,6 +38,7 @@ public function definition(): array 'description' => $this->faker->paragraph(), 'start_date' => $this->faker->dateTimeBetween('-1 day', 'now'), 'end_date' => $this->faker->dateTimeBetween('now', '+1 day'), + 'status' => $this->faker->randomElement(ProjectStatus::options()), 'external_links' => [ [ 'title' => $this->faker->sentence(), diff --git a/database/migrations/2023_10_23_122957_create_b_c_r_projects_table.php b/database/migrations/2023_10_23_122957_create_bcr_projects_table.php similarity index 93% rename from database/migrations/2023_10_23_122957_create_b_c_r_projects_table.php rename to database/migrations/2023_10_23_122957_create_bcr_projects_table.php index 77302c24..2c58e60e 100644 --- a/database/migrations/2023_10_23_122957_create_b_c_r_projects_table.php +++ b/database/migrations/2023_10_23_122957_create_bcr_projects_table.php @@ -16,7 +16,7 @@ */ public function up(): void { - Schema::create('b_c_r_projects', function (Blueprint $table) { + Schema::create('bcr_projects', function (Blueprint $table) { $table->id(); $table->foreignIdFor(ProjectCategory::class) ->nullable() @@ -32,6 +32,7 @@ public function up(): void ->cascadeOnDelete(); $table->string('name'); $table->string('slug')->unique(); + $table->string('status'); $table->text('description'); $table->date('start_date'); $table->date('end_date');