-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a6cd32
commit 8e4eb0b
Showing
6 changed files
with
141 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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 : ''; | ||
$filepath = tempnam(ASSETS_PATH . $folderName, 'converted'); | ||
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); | ||
$objWriter->save($filepath); | ||
$content = file_get_contents($filepath); | ||
unlink($filepath); | ||
return $content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters