Forum


Replies: 6   Views: 168
Using cloneblock to clone elements
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by philip0908  · 10-05-2022 - 00:06

I am trying for hours now but I can't get it to work. I am using my own wrapper class called "MSWord". What am I missing? Replacing template variables works, so there is no error with synchronization between the wrapper class and phpdocx. The content in the BLOCK in the word document is a row from a table - if that makes any difference.

class MSWord {

  private $framework_object = false;

  private $path = "ready_to_download";
  private $targets = array('header', 'document', 'footer', 'footnote', 'footnotes', 'endnote', 'endnotes', 'comment', 'comments');

  public function __construct($path_to_template_file = false) {
        
    require_once(K :: gI('K')->getPhitPath()."features/phpdocx/classes/CreateDocx.php");
    error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); // In case phpdocx turned it on
            
      if ($path_to_template_file) {

        $this->framework_object = new CreateDocxFromTemplate($path_to_template_file);
        $this->framework_object->setTemplateSymbol('${', '}');

      }

  }

  public function getTemplateVariablesNew() {

    return $this->framework_object->getTemplateVariables();

  }

  public function replaceTemplateVariables($replacement_variables) {

     $options = array('parseLineBreaks' => true, "type" => "inline");

    foreach ($this->targets as $target) {

      $options['target'] = $target;
      $this->framework_object->replaceVariableByText($replacement_variables, $options);

    }

  }

  public function cloneBlock($block_name, $copies, $replacement_variables = false, $settings = false) {

    $this->framework_object->cloneBlock($block_name, $copies);        

  }

  public function saveFinalDocument($path_to_new_document) {

    $this->framework_object->createDocx($path_to_new_document);

  }

}

And I try to call it like this:

 

$word_object = new MSWord($path_to_submission_file);


// Clone blocks
$word_object->cloneBlock("CONTACT_PERSONS", 5);

// Save new document
$word_object->saveFinalDocument($submission_templates_path."/ready_to_download/".uniqid());

Word doc looks like this:

${BLOCK_CONTACT_PERSONS}
- ${contact_person_name}        
- ${contact_person_email}       
- ${contact_person_phone}
${BLOCK_CONTACT_PERSONS}