118 lines
3.7 KiB
PHP
118 lines
3.7 KiB
PHP
<?php
|
||
use PhpOffice\PhpWord\TemplateProcessor;
|
||
|
||
function replacePlaceholdersInDocx($templateFilePath, $outputFilePath, $replacements) {
|
||
// Load the DOCX template
|
||
//$storagePath = "storage/documents/$outputFilePath";
|
||
$storagePath = $outputFilePath;
|
||
$directoryPath = dirname($storagePath);
|
||
|
||
if (!is_dir($directoryPath)) {
|
||
mkdir($directoryPath, 0777, true);
|
||
}
|
||
|
||
$templateProcessor = new TemplateProcessor($templateFilePath);
|
||
|
||
$templateProcessor->setValue("text.ncr_no", "test");
|
||
|
||
// $templateProcessor->setValue("corrective_action_ru", "teasdasdast");
|
||
/*
|
||
$reflectionClass = new \ReflectionClass($templateProcessor);
|
||
|
||
// Accessing the protected/private property
|
||
$property = $reflectionClass->getProperty('tempDocumentMainPart');
|
||
$property->setAccessible(true);
|
||
|
||
// Get the value of the property
|
||
$xmlContent = $property->getValue($templateProcessor);
|
||
|
||
$xmlContent = str_replace("corrective_action_ru", "test", $xmlContent);
|
||
|
||
$property->setValue($templateProcessor, $xmlContent);
|
||
*/
|
||
|
||
|
||
/*
|
||
|
||
|
||
$formTypes = ['date', 'time', 'text', 'textarea', 'radio', 'checkbox', 'select'];
|
||
|
||
// Replace placeholders
|
||
|
||
foreach ($replacements as $search => $replace) {
|
||
|
||
foreach($formTypes AS $formType)
|
||
{
|
||
|
||
$isValidDate = false;
|
||
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $replace)) {
|
||
// YYYY-MM-DD formatında tarih
|
||
$isValidDate = true;
|
||
} elseif (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $replace)) {
|
||
// YYYY-MM-DD HH:MM:SS formatında tarih ve saat
|
||
$isValidDate = true;
|
||
} elseif (preg_match('/^\d{2}.\d{2}.\d{4}$/', $replace)) {
|
||
// DD.MM.YYYY formatında tarih
|
||
$isValidDate = true;
|
||
} elseif (preg_match('/^\d{2}.\d{2}.\d{4} \d{2}:\d{2}:\d{2}$/', $replace)) {
|
||
// DD.MM.YYYY HH:MM:SS formatında tarih ve saat
|
||
$isValidDate = true;
|
||
}
|
||
|
||
if($isValidDate)
|
||
{
|
||
if (strtotime($replace)) {
|
||
$dateTime = new DateTime($replace);
|
||
|
||
// Eğer $replace saati de içeriyorsa
|
||
if ($dateTime->format('H:i:s') !== '00:00:00') {
|
||
$replace = $dateTime->format('H:i');
|
||
} else {
|
||
$replace = $dateTime->format('d.m.Y');
|
||
}
|
||
}
|
||
}
|
||
|
||
if (in_array($formType, ['radio', 'checkbox'])) {
|
||
$replacePlaceHolder = $formType . '.' . $search . '.' . $replace;
|
||
$okValue = '✓';
|
||
$cellValue = $okValue;
|
||
|
||
$otherPattern = "/<w:t>\{$formType\.$search\.[^}]*\}<\/w:t>/";
|
||
|
||
//$xmlContent = preg_replace($otherPattern, "", /$xmlContent);
|
||
} else {
|
||
$replacePlaceHolder = $formType . '.' . $search; // Note the curly braces
|
||
$cellValue = $replace;
|
||
|
||
$pattern = '/\{' . preg_quote($formType, '/') . '\.<\/w:t><\/w:r><w:r[^>]*><w:rPr>.*?<\/w:rPr><w:t>' . preg_quote($search, '/') . '<\/w:t>/';
|
||
dump($pattern);
|
||
// $xmlContent = preg_replace($pattern, $cellValue, $xmlContent);
|
||
$property->setValue($templateProcessor, $xmlContent);
|
||
|
||
}
|
||
|
||
|
||
|
||
// dump($pattern);
|
||
// dump($cellValue);
|
||
|
||
// preg_replace ile değişim
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
}
|
||
*/
|
||
|
||
|
||
|
||
|
||
$templateProcessor->saveAs($storagePath);
|
||
}
|
||
|
||
|
||
?>
|