| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Product;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class ExportController extends Controller
- {
- public function export_docx(Request $request){
- $ids = json_decode($request->ids,true);
- $products = Product::query()->whereIn('id', $ids)->orderBy('series', 'asc')->get();
- $my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('templates/template_maf.docx'));
- $vars_for_template = [];
- $i = 1;
- foreach ($products as $product){
- switch ($request->descr) {
- case 1:
- $descr = $product->characteristics . "\r\n" . $product->tech_description_short;
- break;
- case 2:
- $descr = $product->characteristics . "\r\n" . $product->tech_description;
- break;
- case 3:
- $descr = $product->tech_description_short;
- break;
- case 4:
- $descr = $product->tech_description;
- break;
- case 5:
- default:
- $descr = $product->characteristics;
- break;
- }
- $vars_for_template = array_merge($vars_for_template, [
- 'series#' . $i => $product->series,
- 'product_group#' .$i => $product->product_group,
- 'num#' . $i => $i,
- 'name#' . $i => $product->name,
- 'name_for_form#' . $i => $product->name_for_form,
- 'price#' . $i => number_format($product->price, 0, '', ' '),
- 'image#' . $i => $product->image_path,
- 'description#' . $i => str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $descr),
- ]);
- $i++;
- }
- // dd($vars_for_template);
- $my_template->cloneBlock('product_block', count($products), true, true, $vars_for_template);
- foreach ($vars_for_template as $k => $v){
- $file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v;
- if(str_starts_with($k, 'image') && file_exists($file) && $v !== ''){
- $my_template->setImageValue($k, ['path' => $file, 'width' => 270, 'height' => 180]);
- } else {
- $my_template->setValue($k, $v);
- }
- }
- // dd($vars_for_template);
- $file = 'Наш_Двор_' . date('YmdHis') . '.docx';
- header("Content-Description: File Transfer");
- header('Content-Disposition: attachment; filename="' . $file . '"');
- header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Expires: 0');
- ob_clean();
- $my_template->saveAs("php://output");
- Log::notice('Execution time: ' . microtime(true) - LARAVEL_START);
- exit();
- }
- }
|