File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/Console/Commands/IndexSearch.php
<?php
namespace App\Console\Commands;
use App\KommaApp\Companies\Models\Company;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\Events\Models\EventTranslation;
use App\KommaApp\MagazineArticles\Models\MagazineArticle;
use App\KommaApp\MagazineArticles\Models\MagazineArticleTranslation;
use App\KommaApp\Magazines\Models\Magazine;
use App\KommaApp\Magazines\Models\MagazineTranslation;
use App\KommaApp\NewsArticles\Models\NewsArticle;
use App\KommaApp\NewsArticles\Models\NewsArticleTranslation;
use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Pages\Models\PageTranslation;
use App\KommaApp\PastEvents\Models\PastEvent;
use App\KommaApp\PastEvents\Models\PastEventTranslation;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class IndexSearch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'search:index';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Index all the non shop models';
protected $searchModels = [
Company::class,
Event::class,
EventTranslation::class,
MagazineArticleTranslation::class,
MagazineTranslation::class,
NewsArticleTranslation::class,
PageTranslation::class,
PastEventTranslation::class
];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->flush();
$this->info(' ');
$this->index();
$this->info(' ');
$this->removeInactiveChainedModels();
Log::info('Search database updated');
}
private function flush()
{
$this->info('Flushing models for the search index. Please wait...');
foreach ($this->searchModels as $model) $this->call('scout:flush', ['model' => $model]);
$this->info('Flushing complete');
}
private function index()
{
$this->info('Importing models for the search index. Please wait...');
foreach ($this->searchModels as $model) $this->call('scout:import', ['model' => $model]);
$this->info('Importing complete');
}
private function removeInactiveChainedModels()
{
$inactiveMagazines = Magazine::where('active', 0)->with('translations', 'magazineArticles')->get();
foreach ($inactiveMagazines as $inactiveMagazine){
$this->info('Remove inactive magazine and belonging articles from search database: '. $inactiveMagazine->id);
// Remove the magazine translation from the search database
$inactiveMagazine->translations()->unsearchable();
// Remove the belonging articles translations from search database
foreach ($inactiveMagazine->magazineArticles as $magazineArticle)
{
$magazineArticle->translations()->unsearchable();
}
}
$this->removeInactiveModelAndTranslations(MagazineArticle::class);
$this->removeInactiveModelAndTranslations(Event::class, false);
$this->removeInactiveModelAndTranslations(PastEvent::class);
$this->removeInactiveModelAndTranslations(NewsArticle::class);
$this->removeInactiveModelAndTranslations(Page::class);
Company::where('active', '0')->get()->unsearchable();
}
private function removeInactiveModelAndTranslations($className, $translationOnly = true)
{
$inactiveModels = $className::where('active', 0)->with('translations')->get();
if(!$translationOnly) $inactiveModels->unsearchable();
foreach ($inactiveModels as $inactiveModel){
$this->info('Remove belonging inactive ' .$className. ' translations search database: '. $inactiveModel->id);
// Remove the magazine translation from the search database
$inactiveModel->translations()->unsearchable();
}
}
}