Skip to content

Commit

Permalink
NEW phpoffice/phpword support
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Sep 15, 2023
1 parent 5a6cd32 commit 074539a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,29 @@ Install with [composer](https://getcomposer.org/) by running `composer require s

## Configuration

You will need to set the following three environment variables:
**Note:** Using of of docvert is primarily designed for Common Web Platform (CWP) clients

If you are using docver then you will need to set the following three environment variables:
- `DOCVERT_USERNAME`
- `DOCVERT_PASSWORD`
- `DOCVERT_URL`

**Note:** This module is primarily designed for Common Web Platform (CWP) clients. There will be additional setup required to use this module as intended, if you are not using the CWP government edition.
If do not have the cwp/cwp-core module installed then enable document converter with the following configuration - note will be automatically applied if you also have the cwp/cwp-core module installed and the `DOCVERT_USERNAME` environment variable set.

```yaml
Page:
extensions:
- SilverStripe\DocumentConverter\PageExtension
```
By default this module will use docvert, though it's highly recommend you instead use the phpoffice/phpword module instead. Enable this with the following configuration:
```yaml
SilverStripe\DocumentConverter\ImportField:
importer_class: SilverStripe\DocumentConverter\PHPWordImporter
```
docvert support is now deprecated and will be removed in the next major version
## User Guide
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"require": {
"php": "^7.4 || ^8.0",
"silverstripe/cms": "^4",
"silverstripe/asset-admin": "^1"
"silverstripe/asset-admin": "^1",
"phpoffice/phpword": "^1.1"
},
"require-dev": {
"ext-curl": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/ImportField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Upload;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Control\Director;
Expand Down Expand Up @@ -286,6 +285,7 @@ public function importFromPOST($tmpFile, $splitHeader = false, $publishPages = f

$sourcePage = $this->form->getRecord();
$importerClass = $this->config()->get('importer_class');
/** @var Importer $importer */
$importer = Injector::inst()->create($importerClass, $fileDescriptor, $chosenFolderID);
$content = $importer->import();

Expand Down
10 changes: 10 additions & 0 deletions src/Importer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\DocumentConverter;

interface Importer
{
public function __construct($fileDescriptor, $chosenFolderID = null);

public function import();
}
52 changes: 52 additions & 0 deletions src/PHPWordImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace SilverStripe\DocumentConverter;

use GraphQL\Exception\InvalidArgument;
use SilverStripe\ORM\DataObject;

class PHPWordImporter implements Importer
{
private array $fileDescriptor;
private ?int $chosenFolderID;

/**
* @param array $fileDescriptor
* @param int|null $chosenFolderID
*/
public function __construct($fileDescriptor, $chosenFolderID = null)
{
if (!is_array($fileDescriptor)) {
throw new InvalidArgument('fileDescriptor must be an array');
}
if (!is_int($chosenFolderID) && !is_null($chosenFolderID)) {
throw new InvalidArgument('chosenFolderID must be an int or null');
}
$this->fileDescriptor = $fileDescriptor;
$this->chosenFolderID = $chosenFolderID;
}

/**
* @return string
*/
public function import()
{
// read word doc
$source = $this->fileDescriptor['path'];
$ext = pathinfo($this->fileDescriptor['name'], PATHINFO_EXTENSION);
$readerName = 'Word2007';
if ($ext === 'doc') {
// Word 1997
$readerName = 'MsDoc';
}
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, $readerName);
// write it out as HTML
$chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null;
$folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : '';
$path = ASSETS_PATH . $folderName . '/index.html';
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save($path);
$content = file_get_contents($path);
return $content;
}
}
2 changes: 2 additions & 0 deletions src/ServiceConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use ZipArchive;

/**
* This class uses the legacy docvert service
*
* Utility class hiding the specifics of the document conversion process.
*/
class ServiceConnector
Expand Down

0 comments on commit 074539a

Please sign in to comment.