<?php
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
//SiteLibs::GetVarDump($_POST);
$ArrPost = explode('&', $_POST['PrintSvtQrDocx']);
foreach($ArrPost as $value){
  $ArrT = explode('=', $value);  
  if(false === strpos($ArrT[0], 'colspan')){
    $ArrValues[$ArrT[0]][]=$ArrT[1];    
  }
  else{
    $ArrValues['colspan'][str_replace('colspan[','',str_replace(']','',$ArrT[0]))]=$ArrT[1];
  }
}
$PHPWord  = new PHPWord();
$section  = $PHPWord->createSection();
$styleTable = array('borderSize'=>6, 'cellMargin'=>80);

$styleCell = array('valign'=>'center');

$fontStyleTH = array('bold'=>true, 'align'=>'left', 'spaceAfter'=>0);
$fontStyle = array('bold'=>false, 'align'=>'left');
$paragStyle = array('spaceAfter'=>0);

$PHPWord->addTableStyle('gridprint', $styleTable);
$table = $section->addTable('gridprint');
$table->addRow();
foreach ($ArrValues['thead'] as $value) {
  
  $table->addCell($styleCell)->addText($value, $fontStyleTH, $paragStyle);
}
$i = count($ArrValues['colspan'][0]);
$table->addRow();
$VarFirst = true;

include($_SERVER['DOCUMENT_ROOT'] .'/script/phpqrcode/qrlib.php'); 
function CreateQr($VarId = 0){
  $pngAbsoluteFilePath = '';
  if(true === is_numeric($VarId)){
    $VarContents = SiteLibs::scheme_url() . $_SERVER['HTTP_HOST'] . '/SvtInformation.php?svt_id='. $VarId;
    $tempDir = $_SERVER['DOCUMENT_ROOT'] . '/../tmp/'; 
    $fileName = 'qr_svt_'.$VarId .'.png';
    $pngAbsoluteFilePath = $tempDir.$fileName; 
    if (!file_exists($pngAbsoluteFilePath)) { 
      QRcode::png($VarContents, $pngAbsoluteFilePath); 
    }
  }
  return $pngAbsoluteFilePath;
}

foreach ($ArrValues['tbody'] as $key => $value) {
  if(true === $VarFirst || $i == count($ArrValues['thead'])){
    $VarFirst = false;
    $value = CreateQr($value);
    if ('' != $value){
      $imageStyle = array('width'=>$MS_SP->Sp_Config['QrCodesize'], 'height'=>$MS_SP->Sp_Config['QrCodesize']);
      $VarImage = true;
    }
    else{
      $value = '';
    }
  }
  if($i == count($ArrValues['thead'])){
    $table->addRow();
    $i = count($ArrValues['colspan'][0]);
  }
  if(isset($ArrValues['colspan'][$key]) && 0 < $ArrValues['colspan'][$key]){
    $styleCellC=array('gridSpan' => $ArrValues['colspan'][$key]);
    if(true === $VarImage){
      $table->addCell(1000,$styleCellC)->addImage($value, $imageStyle);
      $VarImage = false;
    }
    else{
      $table->addCell(10,$styleCellC)->addText($value, $fontStyle, $paragStyle);
    }
  }
  else{
    if(true === $VarImage){
      $table->addCell(1000)->addImage($value, $imageStyle);
      $VarImage = false;
    }
    else{
      $table->addCell(10)->addText($value, $fontStyle, $paragStyle);
    }
  }
  $i++;
}
$file = $ArrValues['grid'][0].'_'. date("d_m_Y_H_i_s").'.docx';
$filePath = $_SERVER['DOCUMENT_ROOT'] .'/../tmp/'.$file;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($filePath);
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
if(!$filePath) {     
  die('file not found'); 
} 
else {
  ob_clean();
  header("Content-Disposition: attachment; filename=".$file);     
  header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");     
  header('Content-Length: '. filesize($filePath));    
  readfile($filePath); 
  unlink($filePath);
  exit;
}
?>
