Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#WN-272# BULK #213

Merged
merged 2 commits into from
Mar 11, 2025
Merged
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
99 changes: 84 additions & 15 deletions app/Classes/RcnApi/Importer/BulkUploadTemplateExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class BulkUploadTemplateExport implements FromArray, ShouldAutoSize, WithEvents
{
/**
Expand All @@ -37,6 +40,7 @@ public function __construct(string $nationalSociety, string $region,array $data,
}, $eventTypes)) . '"';
$this->region = $region;
$this->data = $data;
if($maxSupportingMessages <= 0) $maxSupportingMessages = 3;
for($i = 0; $i< $maxSupportingMessages; $i++){
$this->headings[] = 'Supporting Message ' . ($i + 1);
}
Expand Down Expand Up @@ -67,26 +71,75 @@ public function registerEvents(): array
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
$sheet->setTitle("Bulk Upload Template");

$guideSheet = $sheet->getParent()->createSheet();
$guideSheet->setTitle("How template works");
$drawing = new Drawing();
$drawing->setName('How Template Works');
$drawing->setDescription('How Template Works');
$drawing->setPath(public_path('images\how_the_template_works.png'));
$drawing->setHeight(1080);
$drawing->setCoordinates('A1');
$drawing->setWorksheet($guideSheet);

$hiddenSheet = $sheet->getParent()->createSheet();
$hiddenSheet->setTitle('DropdownHazardData');


$protection = $sheet->getProtection();
$protection->setSheet(false);

$urgencyLevelsArray = array_map(function ($item) {
return str_replace('"', '', trim($item));
}, explode(',', $this->urgencyLevels));

$eventTypesArray = array_map(function ($item) {
return str_replace('"', '', trim($item));
}, explode(',', $this->eventTypesDropdown));

$validationUrgency = $sheet->getCell('E4')->getDataValidation();
if (empty($urgencyLevelsArray) || empty($eventTypesArray)) {
throw new \Exception('Dropdown lists cannot be empty.');
}

$row = 1;
foreach ($eventTypesArray as $type) {
$hiddenSheet->setCellValue("A{$row}", $type);
$row++;
}
$eventRange = "DropdownHazardData!A1:A" . ($row - 1);
$urgencyFormula = '"' . implode(',', $urgencyLevelsArray) . '"';

$validationUrgency = new DataValidation();
$validationUrgency->setType(DataValidation::TYPE_LIST);
$validationUrgency->setErrorStyle(DataValidation::STYLE_STOP);
$validationUrgency->setAllowBlank(false);
$validationUrgency->setAllowBlank(true);
$validationUrgency->setShowInputMessage(true);
$validationUrgency->setShowErrorMessage(true);
$validationUrgency->setShowDropDown(true);
$validationUrgency->setFormula1($this->urgencyLevels);
$sheet->getCell('E4')->setDataValidation(clone $validationUrgency);
$validationUrgency->setErrorTitle('Select a valid urgency level');
$validationUrgency->setError('Value is not in list.');
$validationUrgency->setPromptTitle('Pick from list');
$validationUrgency->setPrompt('Please pick a Urgency Level from the drop-down list.');
$validationUrgency->setFormula1($urgencyFormula);

$validationEvent = $sheet->getCell('D4')->getDataValidation();
$validationEvent = new DataValidation();
$validationEvent->setType(DataValidation::TYPE_LIST);
$validationEvent->setErrorStyle(DataValidation::STYLE_STOP);
$validationEvent->setAllowBlank(false);
$validationEvent->setAllowBlank(true);
$validationEvent->setShowInputMessage(true);
$validationEvent->setShowErrorMessage(true);
$validationEvent->setShowDropDown(true);
$validationEvent->setFormula1($this->eventTypesDropdown);
$sheet->getCell('D4')->setDataValidation(clone $validationEvent);
$validationEvent->setErrorTitle('Select a valid hazard level');
$validationEvent->setError('Value is not in list.');
$validationEvent->setPromptTitle('Pick from list');
$validationEvent->setPrompt('Please pick a Hazard from the drop-down list.');
$validationEvent->setFormula1($eventRange);

for ($i = 4; $i <= 100; $i++) {
$event->sheet->getCell("E{$i}")->setDataValidation(clone $validationUrgency);
$event->sheet->getCell("D{$i}")->setDataValidation(clone $validationEvent);
}


$maxColumnRow1 = Coordinate::stringFromColumnIndex(2);
Expand All @@ -110,20 +163,36 @@ public function registerEvents(): array
],
];


$sheet->getStyle($row1Range)->applyFromArray($headerStyle);
$sheet->getStyle($row3Range)->applyFromArray($headerStyle);

$protection = $sheet->getProtection();
$protection->setSheet(false);
$sheet->getStyle('A1:Z100')->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);

$sheet->getStyle('A1')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('B1')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('A2')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('B2')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('A3')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('B3')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('C3')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('D3')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getStyle('E3')->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getProtection()->setSort(true);
$sheet->getProtection()->setInsertRows(true);
$sheet->getProtection()->setInsertColumns(true);
$sheet->getProtection()->setDeleteRows(true);
$sheet->getProtection()->setDeleteColumns(true);
$sheet->getProtection()->setFormatColumns(false);
$sheet->getProtection()->setFormatRows(false);
$sheet->getProtection()->setSheet(true);
$base = new Base64Encoder();
$protection->setPassword($base->encode('fsjsD-1FfJTZvs2X'));

$sheet->getStyle('A1:Z100')->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
$hiddenSheet->setSheetState(Worksheet::SHEETSTATE_HIDDEN);



$cellsToLock = ['A1', 'B1', 'A2', 'B2', 'A3:F3'];
foreach ($cellsToLock as $cell) {
$sheet->getStyle($cell)->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
}
}
];
}
Expand Down
6 changes: 6 additions & 0 deletions app/Classes/RcnApi/Importer/BulkUploadTemplateImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use App\Classes\RcnApi\Importer\Exceptions\RcnImportException;
class BulkUploadTemplateImport implements ToCollection
{

private $data = [];
private $dataPageProcessed = false;

/**
* Process the imported data.
Expand All @@ -18,6 +20,8 @@ class BulkUploadTemplateImport implements ToCollection
*/
public function collection(Collection $rows): array
{
if($this->dataPageProcessed) return [];

$processedData = [
'nationalSociety' => null,
'region' => null,
Expand All @@ -27,6 +31,7 @@ public function collection(Collection $rows): array
$currentHazard = null;
$currentUrgencyLevel = null;
$firstRow = false;

foreach ($rows as $index => $row) {
if (!$firstRow || $index === 2) {
$firstRow = true;
Expand All @@ -36,6 +41,7 @@ public function collection(Collection $rows): array
if(!$row[0]){
throw new RcnImportException("Missing national society");
}
$this->dataPageProcessed = true;
$processedData['nationalSociety'] = $row[0];
$processedData['region'] = $row[1] ?? 'National';
continue;
Expand Down
Binary file modified resources/assets/pdf/what-now-csv-guide.pdf
Binary file not shown.
Loading