Skip to content

Commit

Permalink
Fix #329 - keywords for custom fields for which the current person ha…
Browse files Browse the repository at this point in the history
…s no value are now replaced with blanks instead of being left unreplaced
  • Loading branch information
tbar0970 committed Feb 26, 2017
1 parent 10f6fe0 commit fc006a0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion calls/call_odf_merge.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ function mergeDOCX($source_file, $merged_file)
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(TRUE);
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($source_file);

$vars = $templateProcessor->getVariables();
if (!$templateProcessor->cloneBlock('MERGEBLOCK', count($data))) {
$vars = $templateProcessor->getVariables();
if (empty($vars)) {
trigger_error("You don't seem to have included any \${keywords} in your file; cannot merge");
return;
Expand All @@ -65,8 +65,16 @@ function mergeDOCX($source_file, $merged_file)

}
foreach ($data as $num => $row) {
$setVars = Array();
foreach ($row as $k => $v) {
$templateProcessor->setValue(strtoupper($k).'#'.($num+1), $this->xmlEntities($v));
$setVars[strtoupper($k)] = TRUE;
}
// Set blank value for any remaining keywords
foreach ($vars as $v) {
if (!isset($setVars[strtoupper($v)])) {
$templateProcessor->setValue(strtoupper($v).'#'.($num+1), '');
}
}
}
$templateProcessor->saveAs($merged_file);
Expand All @@ -78,6 +86,7 @@ function mergeODT($source_file, $merged_file)
require_once 'include/odf_tools.class.php';

$content = ODF_Tools::getXML($source_file, $xml_filename);
$keywords = ODF_Tools::getKeywords($source_file, $xml_filename);
if (empty($content)) {
trigger_error('Could not find content within this '.$extension.' file');
return;
Expand All @@ -100,8 +109,16 @@ function mergeODT($source_file, $merged_file)
foreach ($this->getMergeData() as $id => $row) {
if (empty($row)) continue;
$this_middle = $middle_template;
$replaced = Array();
foreach ($row as $k => $v) {
$this_middle = str_replace('%'.strtoupper($k).'%', ODF_Tools::odfEntities(trim($v)), $this_middle);
$replaced[strtoupper($k)] = TRUE;
}
// replace keywords without values with blanks
foreach ($keywords as $k) {
if (!isset($replaced[strtoupper($k)])) {
$this_middle = str_replace('%'.strtoupper($k).'%', '', $this_middle);
}
}
$merged_middle .= $this_middle;
}
Expand Down

0 comments on commit fc006a0

Please sign in to comment.