Completed
Push — master ( e38edc...acf857 )
by Bram
02:11
created

MigrateUserFieldsTask::migrateFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
/**
3
 * MigrateUserFieldsTask.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 22/06/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use BuildTask;
12
use DB;
13
use Director;
14
15
/**
16
 * Class MigrateUserFieldsTask
17
 * Migrate the attendee fields to new user fields
18
 *
19
 * @package Broarm\EventTickets
20
 */
21
class MigrateUserFieldsTask extends BuildTask
22
{
23
    protected $title = 'Migrate user fields task';
24
25
    protected $description = 'Migrate the AttendeeExtraFields to the new UserFields';
26
27
    protected $userFields;
28
    protected $userFieldOption;
29
30
    /**
31
     * @param \SS_HTTPRequest $request
32
     */
33
    public function run($request)
34
    {
35
        if (!Director::is_cli()) {
36
            echo '<pre>';
37
        }
38
39
        echo "Start user field migration\n\n";
40
41
        $this->migrateValues();
42
        $this->migrateFields();
43
44
        echo "Finished user field migration task \n";
45
        if (!Director::is_cli()) {
46
            echo '</pre>';
47
        }
48
    }
49
50
    /**
51
     * Migrate the AttendeeExtraFields to UserFields
52
     */
53
    private function migrateFields()
54
    {
55
        if ($attendeeFields = AttendeeExtraField::get()) {
56
            foreach ($attendeeFields as $attendeeField) {
57
                $this->findOrMakeUserFieldFor($attendeeField);
58
            }
59
        }
60
    }
61
62
    /**
63
     * Make a new UserField based on the given AttendeeExtraField
64
     *
65
     * @param AttendeeExtraField $attendeeField
66
     */
67
    private function findOrMakeUserFieldFor(AttendeeExtraField $attendeeField)
68
    {
69
        if (!$field = $this->getUserFields()->byID($attendeeField->ID)) {
70
            /** @var UserField $field */
71
            $class = self::mapFieldType($attendeeField->FieldType, $attendeeField->FieldName);
72
            $field = $class::create();
73
        }
74
75
        $field->ID = $attendeeField->ID;
76
        $field->Name = $attendeeField->FieldName;
77
        $field->Title = $attendeeField->Title;
78
        $field->Default = $attendeeField->DefaultValue;
79
        $field->ExtraClass = $attendeeField->ExtraClass;
80
        $field->Required = $attendeeField->Required;
81
        $field->Editable = $attendeeField->Editable;
82
        $field->EventID = $attendeeField->EventID;
83
        $field->Sort = $attendeeField->Sort;
84
85
        if ($attendeeField->Options()->exists() && $field->ClassName === 'UserOptionSetField') {
86
            /** @var \UserOptionSetField $field */
87
            /** @var AttendeeExtraFieldOption $attendeeOption */
88
            foreach ($attendeeField->Options() as $option) {
89
                $field->Options()->add($this->findOrMakeUserOptionFor($option));
90
                echo "[{$field->ID}] Added AttendeeExtraFieldOption as UserFieldOption\n";
91
            }
92
        }
93
94
        $field->write();
95
        echo "[{$field->ID}] Migrated AttendeeExtraField to $field->ClassName\n";
96
    }
97
98
    /**
99
     * Find or make an option based on the given AttendeeExtraFieldOption
100
     *
101
     * @param AttendeeExtraFieldOption $attendeeOption
102
     *
103
     * @return \DataObject|UserFieldOption
104
     */
105
    private function findOrMakeUserOptionFor(AttendeeExtraFieldOption $attendeeOption)
106
    {
107
        if (!$option = $this->getUserFieldOption()->byID($attendeeOption->ID)) {
108
            $option = UserFieldOption::create();
109
        }
110
111
        $option->ID = $attendeeOption->ID;
112
        $option->Title = $attendeeOption->Title;
113
        $option->Default = $attendeeOption->Default;
1 ignored issue
show
Bug introduced by
The property Default does not seem to exist. Did you mean default_cast?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
114
        $option->Sort = $attendeeOption->Sort;
115
        return $option;
116
    }
117
118
    /**
119
     * Map the given field type to one of the available class names
120
     * Also looks into the current mapping if the field has a new type
121
     *
122
     * @param      $type
123
     * @param null $name
124
     *
125
     * @return string
126
     */
127
    private static function mapFieldType($type, $name = null)
128
    {
129
        $types = array(
130
            'TextField' => 'UserTextField',
131
            'EmailField' => 'UserEmailField',
132
            'CheckboxField' => 'UserCheckboxField',
133
            'OptionsetField' => 'UserOptionSetField'
134
        );
135
136
        $currentDefaults = Attendee::config()->get('default_fields');
137
        if ($currentDefaults && key_exists($name, $currentDefaults)) {
138
            return $currentDefaults[$name]['FieldType'];
139
        } else {
140
            return $types[$type];
141
        }
142
    }
143
144
    /**
145
     * Migrate the set values
146
     */
147
    public function migrateValues()
148
    {
149
        DB::query("
150
           UPDATE `Broarm\EventTickets\Attendee_Fields`
151
            SET `Broarm\EventTickets\UserFieldID` = `Broarm\EventTickets\AttendeeExtraFieldID`
152
        ");
153
154
        echo "\nMigrated the set values\n\n";
155
    }
156
157
    /**
158
     * Get and store the user fields list
159
     *
160
     * @return \DataList
161
     */
162
    private function getUserFields()
163
    {
164
        return $this->userFields ? $this->userFields : UserField::get();
165
    }
166
167
    /**
168
     * Get and store the User field options
169
     *
170
     * @return \DataList
171
     */
172
    private function getUserFieldOption()
173
    {
174
        return $this->userFieldOption ? $this->userFieldOption : UserFieldOption::get();
175
    }
176
}
177