115 lines
3.3 KiB
PHP
115 lines
3.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Routine: build_field_347_from_x_columns
|
|
*
|
|
* Purpose:
|
|
* For each imported XLS row:
|
|
* - check columns P to AT
|
|
* - when a cell contains "x", take the related column title from row 6
|
|
* - append the free text value from column AU
|
|
* - save the final comma-separated text into column P
|
|
*
|
|
* Target:
|
|
* Column P must be mapped to field_id 347.
|
|
*/
|
|
|
|
function applyRoutine(&$excelData, $routineData = [])
|
|
{
|
|
/*
|
|
* Excel column indexes are zero-based:
|
|
*
|
|
* P = 15
|
|
* AT = 45
|
|
* AU = 46
|
|
*/
|
|
$targetColumnIndex = 15; // P
|
|
$startColumnIndex = 15; // P
|
|
$endColumnIndex = 45; // AT
|
|
$extraColumnIndex = 46; // AU
|
|
|
|
/*
|
|
* Headers must come from XLS row 6.
|
|
* Usually they are passed inside $routineData['xls_headers'].
|
|
*/
|
|
$headers = $routineData['xls_headers'] ?? [];
|
|
|
|
if (empty($headers) || !is_array($headers)) {
|
|
error_log("Routine field_id 347: missing XLS headers from row 6.");
|
|
return;
|
|
}
|
|
|
|
foreach ($excelData as $rowIndex => &$row) {
|
|
if (!isset($row['data']) || !is_array($row['data'])) {
|
|
error_log("Routine field_id 347: invalid row structure at index {$rowIndex}.");
|
|
continue;
|
|
}
|
|
|
|
$selectedValues = [];
|
|
|
|
/*
|
|
* Check columns from P to AT.
|
|
* If the cell contains x, take the related column header.
|
|
*/
|
|
for ($columnIndex = $startColumnIndex; $columnIndex <= $endColumnIndex; $columnIndex++) {
|
|
$cellValue = strtolower(trim((string)($row['data'][$columnIndex] ?? '')));
|
|
|
|
if ($cellValue === 'x') {
|
|
$headerTitle = trim((string)($headers[$columnIndex] ?? ''));
|
|
|
|
if ($headerTitle !== '') {
|
|
$selectedValues[] = $headerTitle;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Add free text from column AU.
|
|
*/
|
|
$extraText = '';
|
|
|
|
if (isset($row['data'][$extraColumnIndex])) {
|
|
$extraText = trim((string)$row['data'][$extraColumnIndex]);
|
|
} elseif (isset($row['data']['AU'])) {
|
|
$extraText = trim((string)$row['data']['AU']);
|
|
}
|
|
|
|
error_log(
|
|
"Routine field_id 347: row " .
|
|
($row['excelrow'] ?? $rowIndex) .
|
|
" AU index {$extraColumnIndex} value: " .
|
|
print_r($row['data'][$extraColumnIndex] ?? null, true) .
|
|
" | AU key value: " .
|
|
print_r($row['data']['AU'] ?? null, true)
|
|
);
|
|
|
|
if ($extraText !== '') {
|
|
$selectedValues[] = $extraText;
|
|
}
|
|
|
|
/*
|
|
* Remove empty and duplicate values.
|
|
*/
|
|
$selectedValues = array_values(array_unique(array_filter($selectedValues, function ($value) {
|
|
return trim((string)$value) !== '';
|
|
})));
|
|
|
|
/*
|
|
* Save final value into column P.
|
|
* Column P must be mapped to field_id 347 in the template mapping.
|
|
*/
|
|
$row['data'][$targetColumnIndex] = implode(', ', $selectedValues);
|
|
|
|
error_log(
|
|
"Routine field_id 347: row " .
|
|
($row['excelrow'] ?? $rowIndex) .
|
|
" generated value: " .
|
|
$row['data'][$targetColumnIndex]
|
|
);
|
|
}
|
|
|
|
unset($row);
|
|
|
|
error_log("Routine field_id 347 completed.");
|
|
}
|