2019-07-11 11:45:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2022-04-20 15:23:31 +02:00
|
|
|
use App\Models\Media;
|
|
|
|
|
use App\Models\Sujet;
|
2019-07-11 11:45:47 +02:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
|
|
class DeleteMediaArchive extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2022-06-20 14:57:58 +02:00
|
|
|
protected $signature = 'media:deletearchive';
|
2019-07-11 11:45:47 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
2022-04-20 15:23:31 +02:00
|
|
|
* @phpcs:disable Generic.Files.LineLength.TooLong
|
2019-07-11 11:45:47 +02:00
|
|
|
*/
|
|
|
|
|
protected $description = 'Suppression définitive de tous les médias (et leurs fichiers) qui ont été supprimés (en soft delete)';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2022-04-20 15:23:31 +02:00
|
|
|
//phpcs:ignore Generic.Files.LineLength.TooLong
|
2022-04-19 16:07:27 +02:00
|
|
|
$this->info('Vous allez supprimer définitivement tous les médias (et leurs fichiers) qui ont été supprimés (en soft delete).');
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$this->confirm('Etes-vous sûr de vouloir lancer ce traitement ?')) {
|
|
|
|
|
return 0;
|
2022-04-19 16:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Sujet> */
|
|
|
|
|
$subjects = Sujet::where('sujet_dam', true)->get();
|
2022-04-19 16:07:27 +02:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
$this->newLine();
|
|
|
|
|
$this->line('Liste des sujets disponibles');
|
|
|
|
|
$this->line('----------------------------');
|
|
|
|
|
$this->line('[0] Tous les sujets');
|
|
|
|
|
foreach ($subjects as $subject) {
|
|
|
|
|
$this->line("[{$subject->codeSujet}] {$subject->libelle}");
|
2022-04-19 16:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
$sujetId = $this->askWithCompletion(
|
|
|
|
|
'Pour quel sujet de média souhaitez vous executer le traitement ?',
|
|
|
|
|
$subjects->pluck('codeSujet')->toArray(),
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->newLine();
|
2022-04-19 16:07:27 +02:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
if ($sujetId != 0 and !$subjects->pluck('codeSujet')->contains(\intval($sujetId))) {
|
2022-04-19 16:07:27 +02:00
|
|
|
$this->error('Id de sujet incorrect');
|
2022-06-20 14:57:58 +02:00
|
|
|
return -1;
|
2022-04-19 16:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
$sujetId = intval($sujetId);
|
|
|
|
|
|
|
|
|
|
$mediaCollection = Media::onlyTrashed()->with('sujet.formats');
|
|
|
|
|
if ($sujetId !== 0) {
|
|
|
|
|
$mediaCollection->where('sujet_id', $sujetId);
|
2022-04-19 16:07:27 +02:00
|
|
|
}
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Media> */
|
|
|
|
|
$mediaCollection = $mediaCollection->get();
|
2022-04-19 16:07:27 +02:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
if (!$mediaCollection->count()) {
|
2022-04-19 16:07:27 +02:00
|
|
|
$this->error('Aucun média a supprimer');
|
2022-06-20 14:57:58 +02:00
|
|
|
return -1;
|
2022-04-19 16:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
$this->info($mediaCollection->count() . ' média(s) trouvé(s) - Début du traitement');
|
|
|
|
|
$bar = $this->output->createProgressBar($mediaCollection->count());
|
2022-04-19 16:07:27 +02:00
|
|
|
$bar->start();
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
foreach ($mediaCollection as $media) {
|
|
|
|
|
$media->prune();
|
2022-04-19 16:07:27 +02:00
|
|
|
$bar->advance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bar->finish();
|
2022-06-20 14:57:58 +02:00
|
|
|
$this->newLine();
|
2022-04-19 16:07:27 +02:00
|
|
|
$this->info('Fin du traitement');
|
2019-07-11 11:45:47 +02:00
|
|
|
}
|
|
|
|
|
}
|