Passed
Push — 1.0 ( 83f8df...7a87e8 )
by Morven
02:42
created

ContactExtension::updateCMSFields()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
nc 4
nop 1
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\GridField\GridFieldDetailForm;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Security\Member;
11
use SilverCommerce\OrdersAdmin\Forms\GridField\OrdersDetailForm;
12
use SilverCommerce\OrdersAdmin\Model\Invoice;
13
use SilverCommerce\OrdersAdmin\Model\Estimate;
14
15
/**
16
 * Add additional functions to a contact
17
 */
18
class ContactExtension extends DataExtension
19
{
20
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
21
        "Member" => Member::class
22
    ];
23
    
24
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
25
        "Invoices" => Invoice::class,
26
        "Estimates"=> Estimate::class
27
    ];
28
29
    public function updateCMSFields(FieldList $fields)
30
    {
31
        // Filter the invoice list
32
        $invoices_field = $fields->dataFieldByName("Invoices");
33
        $estimate_field = $fields->dataFieldByName("Estimates");
34
35
        if ($invoices_field) {
36
            $invoices_field
37
                ->getConfig()
38
                ->removeComponentsByType(GridFieldDetailForm::class)
39
                ->addComponent(OrdersDetailForm::create());
0 ignored issues
show
Bug introduced by
The method create() does not exist on SilverCommerce\OrdersAdm...dField\OrdersDetailForm. ( Ignorable by Annotation )

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

39
                ->addComponent(OrdersDetailForm::/** @scrutinizer ignore-call */ create());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
        }
41
42
        if ($estimate_field) {
43
            $list = $estimate_field->getList();
44
            $list = $list->filter("ClassName", Estimate::class);
45
            $estimate_field->setList($list);
46
            $estimate_field
47
                ->getConfig()
48
                ->removeComponentsByType(GridFieldDetailForm::class)
49
                ->addComponent(OrdersDetailForm::create());
50
        }
51
52
        // Add a dropdown to select user account
53
        $fields->addFieldToTab(
54
            "Root.Main",
55
            DropdownField::create(
56
                "MemberID",
0 ignored issues
show
Bug introduced by
'MemberID' 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

56
                /** @scrutinizer ignore-type */ "MemberID",
Loading history...
57
                _t(
58
                    "OrdersAdmin.LinkContactToAccount",
59
                    "Link this contact to a user account"
60
                ),
61
                Member::get()->map()
0 ignored issues
show
Bug introduced by
SilverStripe\Security\Member::get()->map() of type SilverStripe\ORM\Map 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

61
                /** @scrutinizer ignore-type */ Member::get()->map()
Loading history...
62
            )->setEmptyString("")
63
        );
64
    }
65
66
    /**
67
     * Get all orders that have been generated and are marked
68
     * as paid or processing
69
     *
70
     * @return DataList
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Extensions\DataList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
     */
72
    public function OutstandingInvoices()
73
    {
74
        return $this
75
            ->owner
76
            ->Invoices()
77
            ->filter("Status", Config::inst()->get(Invoice::class, "outstanding_statuses"));
78
    }
79
80
    /**
81
     * Get all orders that have been generated and are marked
82
     * as dispatched or canceled
83
     *
84
     * @return DataList
85
     */
86
    public function HistoricInvoices()
87
    {
88
        return $this
89
            ->owner
90
            ->Invoices()
91
            ->filter("Status", Config::inst()->get(Invoice::class, "historic_statuses"));
92
    }
93
94
    public function canView($member)
95
    {
96
        // Members can view their own records
97
        if ($member && $member->exists() && $member->ID == $this->owner->MemberID) {
98
            return true;
99
        }
100
    }
101
102
    public function canEdit($member)
103
    {
104
        // Members can edit their own records
105
        if ($member && $member->exists() && $member->ID == $this->owner->MemberID) {
106
            return true;
107
        }
108
    }
109
110
    public function canDelete($member)
111
    {
112
        // Members can delete their own records
113
        if ($member && $member->exists() && $member->ID == $this->owner->MemberID) {
114
            return true;
115
        }
116
    }
117
118
}