Completed
Push — master ( c84c43...54ac69 )
by Robbie
16s
created

DocumentConversionField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
dl 0
loc 80
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 65 3
A getInnerField() 0 2 1
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
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment FieldSet/array at position 0 could not be parsed: Unknown type name 'FieldSet/array' at position 0 in FieldSet/array.
Loading history...
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', 
0 ignored issues
show
Bug introduced by
'FileWarningHeader' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
				/** @scrutinizer ignore-type */ 'FileWarningHeader', 
Loading history...
41
				_t(
42
					__CLASS__ . '.FileWarningHeader',
43
					'Warning: import will remove all content and subpages of this page.'
44
				), 
45
				4
0 ignored issues
show
Bug introduced by
4 of type integer is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
				/** @scrutinizer ignore-type */ 4
Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::__construct($fields) targeting SilverStripe\Forms\CompositeField::__construct() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
	}
97
98
	public function getInnerField() {
99
		return $this->innerField;
100
	}
101
}
102