Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions ProcessMaker/Console/Commands/ReviewCategoryNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace ProcessMaker\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Models\Screen;

class ReviewCategoryNull extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'category:review-category-assign';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check the category_id column for null values.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$table = 'screens';
$field = 'screen_category_id';
$class = Screen::class;
$default = DB::table('screen_categories')->where('name', 'Uncategorized')->pluck('id')->first();

DB::table($table)->select('id', 'title')
->whereNull([$field, 'key'])
->orderBy('id', 'DESC')->chunkById(100, function (Collection $items) use ($table, $field, $class, $default) {
foreach ($items as $item) {
$category = DB::table('category_assignments')
->select('category_id')
->where([
['assignable_type', '=', $class],
['assignable_id', '=', $item->id],
])->first();

$categoryId = $default;
if ($category) {
$categoryId = $category->category_id;
}

DB::table($table)
->where('id', $item->id)
->update([$field => $categoryId]);

$this->info('- ' . $item->title . ' ==> Category: ' . $categoryId);

}
});
}
}
28 changes: 28 additions & 0 deletions upgrades/2023_06_16_131342_review_categories_null.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use ProcessMaker\Upgrades\UpgradeMigration as Upgrade;

class ReviewCategoriesNull extends Upgrade
{
public $to = '4.6.2';

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Artisan::call('category:review-category-assign');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}