Skip to content

Commit a90dc6a

Browse files
authored
Merge pull request #3080 from codeeu/dev
Dev
2 parents a4dbc3e + 4077d55 commit a90dc6a

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace App\Console\Commands\Onetime;
4+
5+
use App\ResourceItem;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Log;
8+
use Illuminate\Support\Facades\Storage;
9+
use Maatwebsite\Excel\Facades\Excel;
10+
11+
/**
12+
* Update resource descriptions from an Excel file.
13+
*
14+
* Expected Excel structure:
15+
* - Column A: "Name of the resource"
16+
* - Column B: "Description of the resource"
17+
*
18+
* Usage:
19+
* php artisan resources:update-descriptions
20+
* php artisan resources:update-descriptions --dry-run
21+
*/
22+
class UpdateResourceDescriptions extends Command
23+
{
24+
protected $signature = 'resources:update-descriptions {--dry-run : Simulate without saving changes}';
25+
26+
protected $description = 'Update the description field for resources from Excel file based on name match.';
27+
28+
private string $filePath = 'Uploaded resources.xlsx';
29+
30+
public function handle(): int
31+
{
32+
$dryRun = $this->option('dry-run');
33+
34+
if (!Storage::disk('excel')->exists($this->filePath)) {
35+
$this->error("Excel file not found: {$this->filePath}");
36+
return self::FAILURE;
37+
}
38+
39+
$fullPath = Storage::disk('excel')->path($this->filePath);
40+
$this->info("Reading: {$this->filePath}");
41+
42+
$rows = Excel::toArray([], $fullPath);
43+
44+
if (empty($rows) || empty($rows[0])) {
45+
$this->error('No data found in the Excel file.');
46+
return self::FAILURE;
47+
}
48+
49+
// Skip Row 1 is header
50+
$data = collect($rows[0])->skip(1);
51+
$results = [];
52+
$updated = 0;
53+
54+
$bar = $this->output->createProgressBar($data->count());
55+
$bar->start();
56+
57+
foreach ($data as $row) {
58+
$name = trim((string)($row['Name of the resource'] ?? $row[0] ?? ''));
59+
$desc = trim((string)($row['Description of the resource'] ?? $row[1] ?? ''));
60+
61+
// skip blank lines
62+
if ($name === '' || $desc === '') {
63+
$bar->advance();
64+
continue;
65+
}
66+
67+
$resource = ResourceItem::whereRaw('TRIM(name) = ?', [$name])->first();
68+
69+
if (!$resource) {
70+
$results[] = [
71+
'name' => $name,
72+
'status' => 'NOT FOUND',
73+
'description' => mb_substr($desc, 0, 80) . (strlen($desc) > 80 ? '...' : ''),
74+
];
75+
$bar->advance();
76+
continue;
77+
}
78+
79+
if ($dryRun) {
80+
$results[] = [
81+
'name' => $name,
82+
'status' => 'WOULD UPDATE',
83+
'description' => mb_substr($desc, 0, 80) . (strlen($desc) > 80 ? '...' : ''),
84+
];
85+
} else {
86+
$resource->description = $desc;
87+
$resource->save();
88+
89+
$results[] = [
90+
'name' => $name,
91+
'status' => 'UPDATED',
92+
'description' => mb_substr($desc, 0, 80) . (strlen($desc) > 80 ? '...' : ''),
93+
];
94+
}
95+
96+
$updated++;
97+
$bar->advance();
98+
}
99+
100+
$bar->finish();
101+
$this->newLine(2);
102+
103+
// summary
104+
$this->info(sprintf(
105+
'Processed: %d | Updated: %d | Not Found: %d',
106+
$data->count(),
107+
$updated,
108+
count(array_filter($results, fn($r) => $r['status'] === 'NOT FOUND'))
109+
));
110+
111+
// print table of results
112+
$this->newLine();
113+
$this->table(['Resource Name', 'Status', 'Description (excerpt)'], $results);
114+
115+
// log summary
116+
Log::info('Resource descriptions update completed', [
117+
'dry_run' => $dryRun,
118+
'total_rows' => $data->count(),
119+
'updated' => $updated,
120+
'not_found' => array_filter($results, fn($r) => $r['status'] === 'NOT FOUND'),
121+
]);
122+
123+
if ($dryRun) {
124+
$this->warn('Dry run only — no data was modified.');
125+
}
126+
127+
return self::SUCCESS;
128+
}
129+
}
59.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)