Passed
Push — 1.0 ( 929063...d6f2a0 )
by Morven
03:15
created

MemberExtension::DefaultLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverCommerce\ContactAdmin\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\ReadonlyField;
8
use SilverCommerce\ContactAdmin\Model\Contact;
9
use SilverStripe\Core\Config\Config;
10
11
/**
12
 * Add additional settings to a memeber object
13
 *
14
 * @package orders-admin
15
 * @subpackage extensions
16
 */
17
class MemberExtension extends DataExtension
18
{
19
    private static $db = [
20
        "Company" => "Varchar(255)",
21
        "Phone" => "Varchar(15)",
22
        "Mobile" => "Varchar(15)"
23
    ];
24
25
    private static $belongs_to = [
26
        'Contact' => Contact::class . '.Member'
27
    ];
28
29
    private static $casting = [
30
        "ContactTitle" => "Varchar"
31
    ];
32
33
    /**
34
     * Get the locations from a contact
35
     */
36
    public function Locations()
37
    {
38
        return $this->getOwner()->Contact()->Locations();
39
    }
40
41
    /**
42
     * Return the "default" location for the associated contact.
43
     *
44
     * @return ContactLocation
0 ignored issues
show
Bug introduced by
The type SilverCommerce\ContactAd...ensions\ContactLocation 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...
45
     */
46
    public function DefaultLocation()
47
    {
48
        return $this->getOwner()->Contact()->DefaultLocation();
49
    }
50
51
    public function updateCMSFields(FieldList $fields)
52
    {
53
        if ($this->owner->ID) {
54
            $fields->addFieldToTab(
55
                "Root.Main",
56
                ReadonlyField::create("ContactTitle")
57
            );
58
        }
59
    }
60
61
    /**
62
     * The name of the contact assotiated with this account
63
     *
64
     * @return void
65
     */
66
    public function getContactTitle()
67
    {
68
        $contact = $this->owner->Contact();
69
70
        return $contact->Title;
71
    }
72
73
    /**
74
     * Update an associated member with the data from this contact
75
     * 
76
     * @return void
77
     */
78
    public function syncToContact()
79
    {
80
        $owner = $this->getOwner();
81
        $contact = $owner->Contact();
82
        $sync = Config::inst()->get(Contact::class, 'sync_fields');
83
        $write = false;
84
85
        if (!$contact->exists()) {
86
            return;
87
        }
88
89
        foreach ($owner->getChangedFields() as $field => $change) {
90
            // If this field is a field to sync, and it is different
91
            // then update contact
92
            if (in_array($field, $sync) && $contact->$field != $owner->$field) {
93
                $contact->$field = $owner->$field;
94
                $write = true;
95
            }
96
        }
97
98
        if ($write) {
99
            $contact->write();
100
        }
101
    }
102
103
    /**
104
     * If no contact exists for this account, then create one
105
     *
106
     * @return void
107
     */
108
    public function onAfterWrite()
109
    {
110
        parent::onAfterWrite();
111
112
        if (!$this->getOwner()->Contact()->exists()) {
113
            $sync = Config::inst()->get(Contact::class, 'sync_fields');
114
            $contact = Contact::create();
115
116
            foreach ($sync as $field) {
117
                $contact->$field = $this->getOwner()->$field;
118
            }
119
120
            $contact->MemberID = $this->getOwner()->ID;
121
            $contact->write();
122
        } else {
123
            $this->getOwner()->syncToContact();
124
        }
125
126
127
    }
128
}
129