Passed
Push — 1.0 ( 07b100...83f8df )
by Morven
05:31
created

SiteConfigExtension::updateCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 103
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 72
nc 1
nop 1
dl 0
loc 103
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
10
use SilverStripe\Forms\ToggleCompositeField;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\DropdownField;
13
use SilverStripe\Forms\NumericField;
14
use SilverStripe\AssetAdmin\Forms\UploadField;
15
use SilverStripe\Forms\GridField\GridField;
16
use SilverStripe\Forms\GridField\GridFieldConfig;
17
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
18
use SilverStripe\Forms\GridField\GridFieldButtonRow;
19
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
20
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
21
use SilverStripe\Assets\Image;
22
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
23
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
24
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
25
use SilverCommerce\OrdersAdmin\Model\Notification as OrderNotification;
26
use SilverCommerce\OrdersAdmin\Model\PostageArea;
27
use SilverCommerce\OrdersAdmin\Model\Discount;
28
29
/**
30
 * Add additional settings to the default siteconfig 
31
 */
32
class SiteConfigExtension extends DataExtension
33
{
34
    
35
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
36
        "EstimateNumberPrefix" => "Varchar(10)",
37
        "InvoiceNumberPrefix" => "Varchar(10)",
38
        "OrderNumberLength" => "Int",
39
        "InvoiceHeaderContent" => "HTMLText",
40
        "InvoiceFooterContent" => "HTMLText",
41
        "EstimateHeaderContent" => "HTMLText",
42
        "EstimateFooterContent" => "HTMLText"
43
    ];
44
    
45
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
46
        "EstimateInvoiceLogo" => Image::class
47
    ];
48
49
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
50
        "OrderNotifications"=> OrderNotification::class,
51
        'PostageAreas'      => PostageArea::class,
52
        'Discounts'         => Discount::class
53
    ];
54
55
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
56
        "OrderNumberLength" => 4
57
    ];
58
    
59
    public function updateCMSFields(FieldList $fields)
60
    {
61
        // Postage Options
62
        $country_html = "<div class=\"field\">";
63
        $country_html .= "<p>First select valid countries using the 2 character ";
64
        $country_html .= "shortcode (see http://fasteri.com/list/2/short-names-of-countries-and-iso-3166-codes).</p>";
65
        $country_html .= "<p>You can add multiple countries seperating them with";
66
        $country_html .= "a comma or use a '*' for all countries.</p>";
67
        $country_html .= "</div><br/>";
68
69
        $country_html_field = LiteralField::create("CountryDescription", $country_html);
0 ignored issues
show
Bug introduced by
'CountryDescription' 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

69
        $country_html_field = LiteralField::create(/** @scrutinizer ignore-type */ "CountryDescription", $country_html);
Loading history...
70
        
71
        $postage_fields = ToggleCompositeField::create(
72
            'PostageSettings',
73
            _t("Orders.PostageSettings", "Postage Settings"),
74
            [
75
                $country_html_field,
76
                GridField::create(
77
                    'PostageAreas',
78
                    '',
79
                    $this->owner->PostageAreas()
80
                )->setConfig(GridFieldConfig_RecordEditor::create())
81
            ]
82
        );
83
84
        // Discount options
85
        $discount_fields = ToggleCompositeField::create(
86
            'DiscountSettings',
87
            _t("Orders.DiscountSettings", "Discount Settings"),
88
            [
89
                LiteralField::create("DiscountPadding", "<br/>"),
90
                GridField::create(
91
                    'Discounts',
92
                    '',
93
                    $this->owner->Discounts()
94
                )->setConfig(GridFieldConfig_RecordEditor::create())
95
            ]
96
        );
97
98
        // Order Notifications
99
        $notification_fields = ToggleCompositeField::create(
100
            'OrderNotificationSettings',
101
            _t("Orders.OrderNotificationSettings", "Order Notification Settings"),
102
            [
103
                LiteralField::create("NotificationPadding", "<br/>"),
104
                GridField::create(
105
                    "OrderNotifications",
106
                    "Order status notifications",
107
                    $this->owner->OrderNotifications()
108
                )->setConfig(GridFieldConfig_RecordEditor::create())
109
            ]
110
        );
111
112
        // Invoice Customisation
113
        $invoice_customisation_fields = ToggleCompositeField::create(
114
            'InvoiceQuoteCustomSettings',
115
            _t("Orders.InvoiceQuoteCustomisation", "Invoice and Quote Customisation"),
116
            [
117
                UploadField::create('EstimateInvoiceLogo'),
118
                TextField::create(
119
                    'EstimateNumberPrefix',
120
                    _t("Orders.EstimatePrefix", "Add prefix to estimate numbers"),
121
                    null,
122
                    9
0 ignored issues
show
Bug introduced by
9 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

122
                    /** @scrutinizer ignore-type */ 9
Loading history...
123
                )->setAttribute(
124
                    "placeholder",
125
                    _t("Orders.OrderPrefixPlaceholder", "EG 'uk-123'")
126
                ),
127
                TextField::create(
128
                    'InvoiceNumberPrefix',
129
                    _t("Orders.InvoicePrefix", "Add prefix to invoice numbers"),
130
                    null,
131
                    9
132
                )->setAttribute(
133
                    "placeholder",
134
                    _t("Orders.OrderPrefixPlaceholder", "EG 'es-123'")
135
                ),
136
                NumericField::create(
137
                    "OrderNumberLength",
138
                    $this->owner->fieldLabel("OrderNumberLength")
139
                )->setDescription(_t(
140
                    "Orders.OrderNumberLengthDescription",
141
                    "The default length invoice/estimate numbers are padded to"
142
                )),
143
                HTMLEditorField::create("InvoiceHeaderContent")
144
                    ->addExtraClass("stacked"),
145
                    HTMLEditorField::create("InvoiceFooterContent")
146
                    ->addExtraClass("stacked"),
147
                HTMLEditorField::create("EstimateHeaderContent")
148
                    ->addExtraClass("stacked"),
149
                HTMLEditorField::create("EstimateFooterContent")
150
                    ->addExtraClass("stacked")
151
            ]
152
        );
153
154
        // Add config sets
155
        $fields->addFieldsToTab(
156
            'Root.Shop',
157
            [
158
                $postage_fields,
159
                $discount_fields,
160
                $notification_fields,
161
                $invoice_customisation_fields
162
            ]
163
        );
164
    }
165
166
    public function onBeforeWrite()
167
    {
168
        if (!$this->owner->OrderNumberLength) {
169
            $this->owner->OrderNumberLength = 4;
170
        }
171
    }
172
}
173