Passed
Push — 1.3 ( 8d68c8...47b397 )
by Morven
04:30
created

OrderAdmin::getExportFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Admin;
4
5
use DateTime;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\Forms\DateField;
8
use Colymba\BulkManager\BulkManager;
9
use SilverStripe\Core\Config\Config;
10
use SilverCommerce\OrdersAdmin\Model\Invoice;
11
use SilverCommerce\OrdersAdmin\Model\Estimate;
12
use Colymba\BulkManager\BulkAction\EditHandler;
13
use Colymba\BulkManager\BulkAction\UnlinkHandler;
14
use SilverStripe\Forms\GridField\GridFieldDetailForm;
15
use ilateral\SilverStripe\ModelAdminPlus\ModelAdminPlus;
16
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
17
use SilverCommerce\CatalogueAdmin\BulkManager\PaidHandler;
18
use SilverCommerce\CatalogueAdmin\BulkManager\CancelHandler;
19
use SilverCommerce\CatalogueAdmin\BulkManager\RefundHandler;
20
use SilverCommerce\CatalogueAdmin\BulkManager\PendingHandler;
21
use SilverCommerce\CatalogueAdmin\BulkManager\PartPaidHandler;
22
use SilverCommerce\CatalogueAdmin\BulkManager\DispatchedHandler;
23
use SilverCommerce\CatalogueAdmin\BulkManager\ProcessingHandler;
24
use SilverCommerce\OrdersAdmin\Forms\GridField\OrdersDetailForm;
25
26
 /**
27
  * Add interface to manage orders through the CMS
28
  *
29
  * @package Commerce
30
  */
31
class OrderAdmin extends ModelAdminPlus
32
{
33
34
    private static $url_segment = 'sales';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
35
36
    private static $menu_title = 'Sales';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
37
38
    private static $menu_priority = 4;
0 ignored issues
show
introduced by
The private property $menu_priority is not used, and could be removed.
Loading history...
39
40
    private static $menu_icon_class = 'font-icon-book-open';
0 ignored issues
show
introduced by
The private property $menu_icon_class is not used, and could be removed.
Loading history...
41
    
42
    private static $managed_models = [
0 ignored issues
show
introduced by
The private property $managed_models is not used, and could be removed.
Loading history...
43
        Invoice::class,
44
        Estimate::class
45
    ];
46
47
    private static $model_importers = [];
0 ignored issues
show
introduced by
The private property $model_importers is not used, and could be removed.
Loading history...
48
49
    public $showImportForm = [];
50
51
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
52
        "SearchForm"
53
    ];
54
55
    /**
56
     * Listen for customised export fields on the currently managed object
57
     *
58
     * @return array
59
     */
60
    public function getExportFields()
61
    {
62
        $model = singleton($this->modelClass);
63
        if ($model->hasMethod('getExportFields')) {
64
            return $model->getExportFields();
65
        }
66
67
        return parent::getExportFields();
68
    }
69
70
    public function getEditForm($id = null, $fields = null)
71
    {
72
        $form = parent::getEditForm($id, $fields);
73
        $fields = $form->Fields();
74
        $gridfield = $fields
75
            ->fieldByName($this->sanitiseClassName($this->modelClass));
76
        $config = $gridfield->getConfig();
77
        
78
        // Adding custom sorting for support of FullRef
79
        $headers = $config->getComponentByType(GridFieldSortableHeader::class);
80
        if ($headers) {
81
            $sorting = $headers->getFieldSorting();
82
            $sorting['FullRef'] = 'Ref';
83
            $headers->setFieldSorting($sorting);
84
        }
85
86
        // Bulk manager
87
        $manager = $config->getComponentByType(BulkManager::class);
88
89
        // Manage orders
90
        if ($this->modelClass == Invoice::class && $gridfield) {
91
            $manager->addBulkAction(CancelHandler::class);
92
            $manager->addBulkAction(RefundHandler::class);
93
            $manager->addBulkAction(PendingHandler::class);
94
            $manager->addBulkAction(PartPaidHandler::class);
95
            $manager->addBulkAction(PaidHandler::class);
96
            $manager->addBulkAction(ProcessingHandler::class);
97
            $manager->addBulkAction(DispatchedHandler::class);
98
        }
99
        
100
        // Set our default detailform and bulk manager
101
        if ($config) {
102
            $config
103
                ->removeComponentsByType(GridFieldDetailForm::class)
104
                ->addComponent(new OrdersDetailForm());
105
        }
106
107
        $this->extend("updateEditForm", $form);
108
109
        return $form;
110
    }
111
    
112
    public function getList()
113
    {
114
        $list = parent::getList();
115
        $query = $this->getSearchData();
116
        $db = DB::get_conn();
117
118
        $start = null;
119
        $end = null;
120
        $date_filter = null;
121
        
122
        // Ensure that we only show Order objects in the order tab
123
        if ($this->modelClass == Estimate::class) {
124
            $list = $list
125
                ->addFilter(["ClassName" => Estimate::class]);
126
        }
127
128
        if ($query && array_key_exists("Start", $query)) {
0 ignored issues
show
introduced by
$query is an empty array, thus is always false.
Loading history...
Bug Best Practice introduced by
The expression $query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129
            $start = new DateTime($query["Start"]);
130
        }
131
132
        if ($query && array_key_exists("End", $query)) {
0 ignored issues
show
introduced by
$query is an empty array, thus is always false.
Loading history...
Bug Best Practice introduced by
The expression $query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
            $end = new DateTime($query["End"]);
134
        }
135
136
        $format = "%Y-%m-%d";
137
        $start_field = $db->formattedDatetimeClause(
138
            '"Estimate"."StartDate"',
139
            $format
140
        );
141
        $end_field = $db->formattedDatetimeClause(
142
            '"Estimate"."EndDate"',
143
            $format
144
        );
145
146
        if ($start && $end) {
0 ignored issues
show
introduced by
$start is of type null, thus it always evaluated to false.
Loading history...
147
            $date_filter = [
148
                $start_field . ' <= ?' =>  $end->format("Y-m-d"),
149
                $start_field . ' >= ?' =>  $start->format("Y-m-d"),
150
                $end_field . ' >= ?' =>  $start->format("Y-m-d"),
151
                $end_field . ' <= ?' =>  $end->format("Y-m-d")
152
            ];
153
        } elseif ($start && !$end) {
0 ignored issues
show
introduced by
$start is of type null, thus it always evaluated to false.
Loading history...
154
            $date_filter = [
155
                $start_field . ' <= ?' =>  $start->format("Y-m-d"),
156
                $end_field . ' >= ?' =>  $start->format("Y-m-d")
157
            ];
158
        }
159
160
        if ($date_filter) {
0 ignored issues
show
introduced by
$date_filter is of type null, thus it always evaluated to false.
Loading history...
161
            $list = $list->where($date_filter);
162
        }
163
                
164
        $this->extend("updateList", $list);
165
166
        return $list;
167
    }
168
169
    public function SearchForm()
170
    {
171
        $form = parent::SearchForm();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Admin\ModelAdmin::SearchForm() has been deprecated: 4.3.0 ( Ignorable by Annotation )

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

171
        $form = /** @scrutinizer ignore-deprecated */ parent::SearchForm();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
172
        $fields = $form->Fields();
173
        $data = $this->getSearchData();
174
        $singleton = Estimate::singleton();
175
176
        // Replace the start field
177
        $fields->replaceField(
178
            "StartDate",
179
            DateField::create(
180
                "Start",
181
                $singleton->fieldLabel("StartDate")
182
            )
183
        );
184
185
        // Replace the start field
186
        $fields->replaceField(
187
            "EndDate",
188
            DateField::create(
189
                "End",
190
                $singleton->fieldLabel("EndDate")
191
            )
192
        );
193
194
        $form->loadDataFrom($data);
195
196
        return $form;
197
    }
198
}
199