1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\DocumentConverter; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Assets\Folder; |
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
8
|
|
|
use SilverStripe\Forms\CompositeField; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\HeaderField; |
12
|
|
|
use SilverStripe\Forms\TreeDropdownField; |
13
|
|
|
use SilverStripe\View\Requirements; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Provides a document import capability through the use of an external service. |
17
|
|
|
* Includes several options fields, which are bundled together with an UploadField |
18
|
|
|
* into a CompositeField. |
19
|
|
|
*/ |
20
|
|
|
class DocumentConversionField extends CompositeField { |
21
|
|
|
/** |
22
|
|
|
* Reference to the inner upload field (DocumentImporterField). |
23
|
|
|
*/ |
24
|
|
|
private $innerField = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Augments a simple CompositeField with uploader and import options. |
28
|
|
|
* |
29
|
|
|
* @param $children FieldSet/array Any additional children. |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
public function __construct($children = null) { |
32
|
|
|
if (is_string($children)) throw new InvalidArgumentException('DocumentConversionField::__construct does not accept a name as its parameter, it defaults to "ImportedFromFile" instead. Use DocumentConversionField::getInnerField()->setName() if you want to change it.'); |
33
|
|
|
if ($children) throw new InvalidArgumentException('DocumentConversionField::__construct provides its own fields and does not accept additional children.'); |
34
|
|
|
|
35
|
|
|
// Add JS specific to this field. |
36
|
|
|
Requirements::javascript('silverstripe/documentconverter: javascript/DocumentConversionField.js'); |
37
|
|
|
|
38
|
|
|
$fields = FieldList::create(array( |
39
|
|
|
HeaderField::create( |
40
|
|
|
'FileWarningHeader', |
|
|
|
|
41
|
|
|
_t( |
42
|
|
|
__CLASS__ . '.FileWarningHeader', |
43
|
|
|
'Warning: import will remove all content and subpages of this page.' |
44
|
|
|
), |
45
|
|
|
4 |
|
|
|
|
46
|
|
|
), |
47
|
|
|
$splitHeader = DropdownField::create( |
48
|
|
|
'DocumentConversionField-SplitHeader', |
49
|
|
|
_t( |
50
|
|
|
__CLASS__ . '.SplitHeader', |
51
|
|
|
'Split document into pages' |
52
|
|
|
), |
53
|
|
|
array( |
54
|
|
|
0 => _t(__CLASS__ . '.No','no'), |
55
|
|
|
1 => _t(__CLASS__ . '.EachH1','for each heading 1'), |
56
|
|
|
2 => _t(__CLASS__ . '.EachH2','for each heading 2') |
57
|
|
|
) |
58
|
|
|
), |
59
|
|
|
$keepSource = CheckboxField::create( |
60
|
|
|
'DocumentConversionField-KeepSource', |
61
|
|
|
_t( |
62
|
|
|
__CLASS__ . '.KeepSource', |
63
|
|
|
'Keep the original document. Adds a link to it on TOC, if enabled.' |
64
|
|
|
) |
65
|
|
|
), |
66
|
|
|
$chosenFolderID = TreeDropdownField::create( |
67
|
|
|
'DocumentConversionField-ChosenFolderID', |
68
|
|
|
_t(__CLASS__ . '.ChooseFolder', 'Choose a folder to save this file'), |
69
|
|
|
Folder::class |
70
|
|
|
), |
71
|
|
|
$includeTOC = CheckboxField::create( |
72
|
|
|
'DocumentConversionField-IncludeTOC', |
73
|
|
|
_t(__CLASS__ . '.IncludeTOC', 'Replace this page with a Table of Contents.') |
74
|
|
|
), |
75
|
|
|
$publishPages = CheckboxField::create( |
76
|
|
|
'DocumentConversionField-PublishPages', |
77
|
|
|
_t( |
78
|
|
|
__CLASS__ . '.publishPages', |
79
|
|
|
'Publish modified pages (not recommended unless you are sure about the conversion outcome)' |
80
|
|
|
) |
81
|
|
|
), |
82
|
|
|
$this->innerField = DocumentImporterField::create( |
83
|
|
|
'ImportedFromFile', |
84
|
|
|
_t(__CLASS__ . '.ImportedFromFile','Import content from a word document') |
85
|
|
|
), |
86
|
|
|
)); |
87
|
|
|
|
88
|
|
|
// Prevent the warning popup that appears when navigating away from the page. |
89
|
|
|
$splitHeader->addExtraClass('no-change-track'); |
90
|
|
|
$keepSource->addExtraClass('no-change-track'); |
91
|
|
|
$chosenFolderID->addExtraClass('no-change-track'); |
92
|
|
|
$includeTOC->addExtraClass('no-change-track'); |
93
|
|
|
$publishPages->addExtraClass('no-change-track'); |
94
|
|
|
|
95
|
|
|
return parent::__construct($fields); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getInnerField() { |
99
|
|
|
return $this->innerField; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|