source_path, now()->addMinutes(2)); return response()->json([ 'id' => $template->id, 'name' => $template->name, 'description' => $template->description, 'file_url' => $urlFile, 'variables' => $template->variables, ]); } public function update(Request $request) { $data = $request->validate([ 'id' => 'required|numeric', 'file' => 'nullable|file|mimes:docx|max:10240', 'name' => 'nullable|string|max:255', 'description' => 'nullable|string', 'variables' => 'nullable|array', ]); $template = DocumentTemplate::findOrFail($data['id']); if ($request->hasFile('file')) { $dirName = pathinfo($template->source_path, PATHINFO_DIRNAME); $fileName = pathinfo($template->source_path, PATHINFO_BASENAME); $file = $request->file('file'); $file->move("$dirName", $fileName); $template->update([ 'source_path' => "$dirName/$fileName", ]); } $template->update([ 'name' => $data['name'], 'description' => $data['description'], 'variables' => $data['variables'], ]); } public function store(Request $request, DocxParser $parser) { $data = $request->validate([ 'file' => 'required|file|mimes:docx|max:10240', 'name' => 'nullable|string|max:255', 'description' => 'nullable|string', 'variables' => 'nullable|array', ]); $file = $request->file('file'); $templateFolderName = md5(uniqid(rand(), true)); $templateFileName = 'source.' . $file->getClientOriginalExtension(); $laravelPath = 'templates/' . $templateFolderName; $file->move("storage/$laravelPath", $templateFileName); $template = DocumentTemplate::create([ 'name' => $data['name'] ?? 'Тест', 'description' => $data['description'], 'content' => 'content', 'variables' => $data['variables'] ?? [], 'source_path' => "storage/$laravelPath" . '/' . $templateFileName, ]); } private function cleanHtml($html) : string { // Убираем ненужные теги и атрибуты $html = preg_replace('/]*>/', '', $html); $html = preg_replace('/]*>/', '', $html); $html = preg_replace('/<\/html>/', '', $html); $html = preg_replace('/.*?<\/head>/si', '', $html); $html = preg_replace('/]*>/', '', $html); $html = preg_replace('/<\/body>/', '', $html); // Убираем пустые теги и лишние пробелы $html = preg_replace('/]*>(\s| )*<\/p>/', '', $html); $html = preg_replace('/\s*/', '
', $html); // Очищаем ссылки ConsultantPlus $html = preg_replace('/]*consultantplus[^>]*>([^<]*)<\/a>/', '$1', $html); // Упрощаем теги font $html = preg_replace('/]*face="([^"]*)"[^>]*>/', '', $html); // $html = preg_replace('/]*size="([^"]*)"[^>]*>/', '', $html); $html = preg_replace('/]*color="([^"]*)"[^>]*>/', '', $html); $html = str_replace('', '', $html); // Обрабатываем вложенные font теги $html = preg_replace('/]*>]*>/', '', $html); $html = preg_replace('/<\/span><\/span>/', '', $html); // Заменяем закладки и якоря $html = preg_replace('/<\/a>/', '', $html); // Обрабатываем шаблонные переменные {{ }} $html = preg_replace('/\{\{<\/b><\/span>/', '{{', $html); $html = preg_replace('/([^<]*)<\/b><\/span>\}\}<\/b><\/span>/', '$1}}', $html); // Улучшаем таблицы $html = preg_replace('/]*>/', '', $html); $html = preg_replace('/]*>/', '
', $html); $html = preg_replace('/]*>/', '', $html); // Убираем лишние классы western $html = str_replace('class="western"', '', $html); // Стили для красивого отображения $styles = ' '; // Добавляем классы для выравнивания $html = preg_replace('/]*align="center"[^>]*>/', '

', $html); $html = preg_replace('/]*align="left"[^>]*>/', '

', $html); $html = preg_replace('/]*align="right"[^>]*>/', '

', $html); // Добавляем классы для отступов $html = preg_replace('/style="[^"]*text-indent:\s*1\.25cm[^"]*"/', 'class="indent-1"', $html); $html = preg_replace('/style="[^"]*text-indent:\s*0\.95cm[^"]*"/', 'class="indent-095"', $html); $html = preg_replace('/style="[^"]*text-indent:\s*1cm[^"]*"/', 'class="indent-1cm"', $html); // Обрабатываем шаблонные переменные $html = preg_replace('/\{\{([^}]+)\}\}/', '{{$1}}', $html); return '

' . $html . '
' . $styles; } public function previewVariables(Request $request, DocxVariableExtractor $extractor) { $rules = [ 'name' => 'required|string', 'doc_file' => 'required|file|mimes:docx,doc|max:10240', ]; $messages = [ 'name.required' => 'Наименование не может быть пустым', 'doc_file.required' => 'Вы не приложили документ', ]; $validator = \Validator::make($request->all(), $rules, $messages); $validator->validate(); try { $file = $request->file('doc_file'); $tempPath = $file->getRealPath(); $variables = $extractor->extractVariables($tempPath); return response()->json([ 'success' => true, 'variables' => $variables, 'count' => count($variables) ]); } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => 'Ошибка при анализе файла: ' . $e->getMessage() ], 500); } } }