| Server IP : 172.67.201.108 / Your IP : 216.73.216.11 Web Server : Apache/2.4.68 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-69-123.ec2.internal 6.1.176-223.369.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 24 13:34:27 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/tgnewdev/admin/classes/ |
Upload File : |
<?php
use PhpOffice\PhpWord\Settings;
class lcs_db_report_doc extends lcs_db_report_base implements report_interface {
protected $phpWord;
protected $section;
protected $table;
protected $cell;
protected $textrun;
protected $header;
protected $footer;
protected $sectionStyleP = [
'orientation' => 'portrait',
'pageSizeW' => 8.5 * 1440,
'pageSizeH' => 11 * 1440,
'marginTop' => 0.5 * 1440,
'marginBottom' => 0.5 * 1440,
'marginLeft' => 0.4 * 1440,
'marginRight' => 0.4 * 1440,
];
protected $sectionStyleL = [
'orientation' => 'landscape',
'pageSizeW' => 11 * 1440,
'pageSizeH' => 8.5 * 1440,
'marginTop' => 0.4 * 1440,
'marginBottom' => 0.4 * 1440,
'marginLeft' => 0.5 * 1440,
'marginRight' => 0.5 * 1440,
];
protected $tableStyle = [
//'cellMarginLeft' => $cell_margin_left * 1440,
//'borderSize' => 0,
];
protected $cellStyle = [
'valign' => 'top',
];
protected $paragraphStyle = ['align' => 'left'];
protected $paragraphStyleHeader = ['align' => 'center'];
protected $fontStyle = ['name' => 'Arial', 'size' => 10, 'bold' => false, ];
protected $fontStyleHeader = ['name' => 'Arial', 'size' => 10, 'bold' => true, ];
public function __construct() {
Settings::loadConfig();
$this->phpWord = new \PhpOffice\PhpWord\PhpWord();
}
public function report_start()
{
$this->phpWord->addParagraphStyle('pStyle', $this->paragraphStyle);
$this->section = $this->phpWord->addSection($this->{'sectionStyle'.$_POST['orientation']});
//$section->addText('Hello World!');
$section_style = $this->section->getStyle();
$position = $section_style->getPageSizeW() - $section_style->getMarginRight() - $section_style->getMarginLeft();
$this->phpWord->addParagraphStyle("leftRight", array("tabs" => [
new \PhpOffice\PhpWord\Style\Tab("center", $position / 2),
new \PhpOffice\PhpWord\Style\Tab("right", $position),
]));
$this->header = $this->section->addHeader();
$this->header->addText(APP_COMPANY."\t".APP_TITLE."\t", [], 'leftRight');
$this->header->addText("\t".htmlspecialchars($this->get_report_title()), ['size' => 14, 'bold' => true, ], 'leftRight');
$this->footer = $this->section->addFooter();
$this->footer->addPreserveText(date('m/d/Y')."\t".'{PAGE}'."\t".'User: '.$_SESSION['user_full_name'], [], 'leftRight');
$this->table = $this->section->addTable($this->tableStyle);
}
public function report_end()
{
}
public function header_start()
{
}
public function header_end()
{
}
public function header_row_start()
{
$this->table->addRow();
}
public function header_row_end()
{
}
public function header_cell_add($data = '', $args = [])
{
$this->format_cell($data, $args, $this->paragraphStyleHeader, $this->fontStyleHeader);
}
public function footer_start()
{
}
public function footer_end()
{
}
public function footer_row_start()
{
$this->table->addRow();
}
public function footer_row_end()
{
}
public function footer_cell_add($data = '', $args = [])
{
$this->format_cell($data, $args, $this->paragraphStyle, $this->fontStyle);
}
public function data_start()
{
}
public function data_end()
{
}
public function data_row_start()
{
$this->table->addRow();
}
public function data_row_end()
{
}
public function data_cell_add($data = '', $args = [])
{
$this->format_cell($data, $args, $this->paragraphStyle, $this->fontStyle);
}
public function output_report($filename = '', $save_file = false)
{
if (empty($filename) && !empty($_POST['save_file_name'])) :
$filename = $_POST['save_file_name'];
endif;
if (!$save_file && !empty($_POST['save_file'])) :
$save_file = true;
endif;
$writer = 'Word2007';
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($this->phpWord, $writer);
if ($save_file) :
$upload_dir = APP_UPLOAD_DIR . '/reports/';
if (!is_dir($upload_dir)) :
mkdir($upload_dir, 0777, true);
endif;
$filename = !empty($filename) ? $filename . '.docx' : 'Report.xlsx';
$upload_path = $upload_dir . $filename;
$xmlWriter->save($upload_path);
$_POST['email_attachment_path'] = $upload_path;
else :
$file = 'Report.docx';
$content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: '.$content_type);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter->save("php://output");
endif;
}
private function format_cell($data, $args, $par_style, $font_style)
{
$attr = '';
$width = 1.5;
$colspan = 1;
foreach ($args as $key => $value) :
$attr .= ' '.$key.'="'.$value.'"';
switch ($key) :
case 'width' :
$width = floatval($value) / 96;
break;
case 'colspan' :
$colspan = intval($value);
break;
case 'align' :
$par_style['align'] = $value;
break;
endswitch;
endforeach;
$this->cell = $this->table->addCell($width * 1440, $this->cellStyle);
if ($colspan > 1) :
$this->cell->getStyle()->setGridSpan($colspan);
endif;
$data = str_ireplace('<br />', '<br>', $data);
$data = strip_tags($data, '<br>');
$ar_data = explode('<br>', $data);
$ar_data = array_map('trim', $ar_data);
$arlen = count($ar_data);
$i = 0;
$this->textrun = $this->cell->addTextRun($par_style);
foreach($ar_data as $value) :
$i++;
$this->textrun->addText(htmlspecialchars($value), $font_style);
if ($i < $arlen) :
$this->textrun->addTextBreak();
endif;
endforeach;
}
}