Files
citrus-cms/app/Services/TpCreator/DocumentProcessors/AbstractTpDocumentProcessor.php
T
2026-04-28 21:14:25 +03:00

85 lines
2.8 KiB
PHP

<?php
namespace App\Services\TpCreator\DocumentProcessors;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
abstract class AbstractTpDocumentProcessor
{
protected array $settings;
/**
* Process the document and add rows to Excel
*
* @param array $lineData Test Package data
* @param array $document Document configuration
* @param Worksheet $sheet Excel sheet
* @param int $currentRow Current row index in Excel
* @param int $rowNo Current row number (counter)
* @param string $fullFolder Full path to destination folder
* @param string $testPackageNo Test Package Number
* @param bool $override Override existing files
* @return int New row number (counter)
*/
abstract public function process(
array $lineData,
array $document,
Worksheet $sheet,
int $currentRow,
int $rowNo,
string $fullFolder,
string $testPackageNo,
bool $override
): int;
protected function alternativeGlob($pattern)
{
return glob($pattern);
}
protected function extractFileName($fullPath, $basePath)
{
if(empty($fullPath)) return '';
// Remove storage/documents prefix if present to get relative path
$name = str_replace("storage/documents/{$basePath}/", "", $fullPath);
// Also try removing just basePath if fullPath is relative
$name = str_replace("{$basePath}/", "", $name);
return str_replace(".pdf", "", $name);
}
protected function addRow(array $search, array $doc, string $folder, string $line, string $date, int &$rowNo, Worksheet $sheet, int &$currentRow, bool $override): void
{
// Check if file exists
if(empty($search)) {
$logPath = $folder . 'log.txt';
$msg = "❌ NOT FOUND: " . ($doc['title2'] ?? 'Unknown') . " (" . ($doc['path'] ?? 'Unknown Path') . ")\n";
file_put_contents($logPath, $msg, FILE_APPEND);
return;
}
$file = $search[0];
$fileName = basename($file);
$destPath = $folder . $fileName;
// Copy file
if(!file_exists($destPath) || $override) {
copy($file, $destPath);
}
// Insert new row
$sheet->insertNewRowBefore($currentRow + 1, 1);
// Set values (Columns A, B, C, D as per TpCreatorService logic)
$sheet->setCellValue("A" . $currentRow, $rowNo);
$sheet->setCellValue("B" . $currentRow, $doc['title2'] ?? '');
$sheet->setCellValue("C" . $currentRow, $doc['title3'] ?? ''); // Title 3 is usually extracted filename or specific detail
$sheet->setCellValue("D" . $currentRow, $date);
// Increment
$currentRow++;
$rowNo++;
}
}