1 | <?php |
||
6 | class UserFormsUpgradeService |
||
|
|||
7 | { |
||
8 | |||
9 | /** |
||
10 | * @var bool |
||
11 | */ |
||
12 | protected $quiet; |
||
13 | |||
14 | 2 | public function run() |
|
15 | { |
||
16 | 2 | $this->log("Upgrading formfield rules and custom settings"); |
|
17 | |||
18 | // List of rules that have been created in all stages |
||
19 | 2 | $fields = Versioned::get_including_deleted('EditableFormField'); |
|
20 | 2 | foreach ($fields as $field) { |
|
21 | 2 | $this->upgradeField($field); |
|
22 | } |
||
23 | 2 | } |
|
24 | |||
25 | /** |
||
26 | * Migrate a versioned field in all stages |
||
27 | * |
||
28 | * @param EditableFormField $field |
||
29 | */ |
||
30 | 2 | protected function upgradeField(EditableFormField $field) |
|
31 | { |
||
32 | 2 | $this->log("Upgrading formfield ID = ".$field->ID); |
|
33 | |||
34 | // Check versions this field exists on |
||
35 | 2 | $filter = sprintf('"EditableFormField"."ID" = \'%d\' AND "Migrated" = 0', $field->ID); |
|
36 | 2 | $stageField = Versioned::get_one_by_stage('EditableFormField', 'Stage', $filter); |
|
37 | 2 | $liveField = Versioned::get_one_by_stage('EditableFormField', 'Live', $filter); |
|
38 | |||
39 | 2 | if ($stageField) { |
|
40 | 2 | $this->upgradeFieldInStage($stageField, 'Stage'); |
|
41 | } |
||
42 | |||
43 | 2 | if ($liveField) { |
|
44 | $this->upgradeFieldInStage($liveField, 'Live'); |
||
45 | } |
||
46 | 2 | } |
|
47 | |||
48 | /** |
||
49 | * Migrate a versioned field in a single stage |
||
50 | * |
||
51 | * @param EditableFormField $field |
||
52 | * @param stage $stage |
||
53 | */ |
||
54 | 2 | protected function upgradeFieldInStage(EditableFormField $field, $stage) |
|
68 | |||
69 | /** |
||
70 | * Migrate custom rules for the given field |
||
71 | * |
||
72 | * @param EditableFormField $field |
||
73 | * @param string $stage |
||
74 | */ |
||
75 | 2 | protected function migrateRules(EditableFormField $field, $stage) |
|
76 | { |
||
77 | 2 | $rulesData = $field->CustomRules |
|
78 | 2 | ? unserialize($field->CustomRules) |
|
79 | 2 | : array(); |
|
80 | |||
81 | // Skip blank rules or fields with custom rules already |
||
82 | 2 | if (empty($rulesData) || $field->DisplayRules()->count()) { |
|
83 | 2 | return; |
|
84 | } |
||
85 | |||
86 | // Check value of this condition |
||
87 | 2 | foreach ($rulesData as $ruleDataItem) { |
|
88 | 2 | if (empty($ruleDataItem['ConditionOption']) || empty($ruleDataItem['Display'])) { |
|
89 | continue; |
||
90 | } |
||
91 | |||
92 | // Get data for this rule |
||
93 | 2 | $conditionOption = $ruleDataItem['ConditionOption']; |
|
94 | 2 | $display = $ruleDataItem['Display']; |
|
95 | 2 | $conditionFieldName = empty($ruleDataItem['ConditionField']) ? null : $ruleDataItem['ConditionField']; |
|
96 | 2 | $value = isset($ruleDataItem['Value']) |
|
97 | 2 | ? $ruleDataItem['Value'] |
|
98 | 2 | : null; |
|
99 | |||
100 | // Create rule |
||
101 | 2 | $rule = $this->findOrCreateRule($field, $stage, $conditionOption, $display, $conditionFieldName, $value); |
|
102 | 2 | $this->log("Upgrading rule ID = " . $rule->ID); |
|
103 | } |
||
104 | 2 | } |
|
105 | |||
106 | |||
107 | |||
108 | /** |
||
109 | * Migrate custom settings for the given field |
||
110 | * |
||
111 | * @param EditableFormField $field |
||
112 | * @param string $stage |
||
113 | */ |
||
114 | 2 | protected function migrateCustomSettings(EditableFormField $field, $stage) |
|
115 | { |
||
116 | // Custom settings include: |
||
117 | // - ExtraClass |
||
118 | // - RightTitle |
||
119 | // - ShowOnLoad (show or '' are treated as true) |
||
120 | // |
||
121 | // - CheckedDefault (new field on EditableCheckbox - should be read from old "default" value) |
||
122 | // - Default (EditableCheckbox) |
||
123 | // - DefaultToToday (EditableDateField) |
||
124 | // - Folder (EditableFileField) |
||
125 | // - Level (EditableFormHeading) |
||
126 | // - HideFromReports (EditableFormHeading / EditableLiteralField) |
||
127 | // - Content (EditableLiteralField) |
||
128 | // - GroupID (EditableMemberListField) |
||
129 | // - MinValue (EditableNumericField) |
||
130 | // - MaxValue (EditableNumericField) |
||
131 | // - MinLength (EditableTextField) |
||
132 | // - MaxLength (EditableTextField) |
||
133 | // - Rows (EditableTextField) |
||
134 | // - Placeholder (EditableTextField / EditableEmailField / EditableNumericField) |
||
135 | |||
136 | 2 | $customSettings = $field->CustomSettings |
|
137 | 2 | ? unserialize($field->CustomSettings) |
|
138 | 2 | : array(); |
|
139 | |||
140 | // Skip blank rules or fields with custom rules already |
||
141 | 2 | if (empty($customSettings)) { |
|
142 | 2 | return; |
|
143 | } |
||
144 | |||
145 | 2 | $field->migrateSettings($customSettings); |
|
146 | |||
147 | 2 | if ($field->config()->has_placeholder) { |
|
148 | 2 | $this->migratePlaceholder($field, $field->ClassName); |
|
149 | } |
||
150 | |||
151 | 2 | $field->write(); |
|
152 | 2 | } |
|
153 | |||
154 | /** |
||
155 | * Create or find an existing field with the matched specification |
||
156 | * |
||
157 | * @param EditableFormField $field |
||
158 | * @param string $stage |
||
159 | * @param string $conditionOption |
||
160 | * @param string $display |
||
161 | * @param string $conditionFieldName |
||
162 | * @param string $value |
||
163 | * @return EditableCustomRule |
||
164 | */ |
||
165 | 2 | protected function findOrCreateRule(EditableFormField $field, $stage, $conditionOption, $display, $conditionFieldName, $value) |
|
166 | { |
||
167 | // Get id of field |
||
168 | 2 | $conditionField = $conditionFieldName |
|
169 | 2 | ? EditableFormField::get()->filter('Name', $conditionFieldName)->first() |
|
170 | 2 | : null; |
|
171 | |||
172 | // If live, search stage record for matching one |
||
173 | 2 | if ($stage === 'Live') { |
|
174 | $list = Versioned::get_by_stage('EditableCustomRule', 'Stage') |
||
175 | ->filter(array( |
||
176 | 'ParentID' => $field->ID, |
||
177 | 'ConditionFieldID' => $conditionField ? $conditionField->ID : 0, |
||
178 | 'Display' => $display, |
||
179 | 'ConditionOption' => $conditionOption |
||
180 | )); |
||
181 | if ($value) { |
||
182 | $list = $list->filter('FieldValue', $value); |
||
183 | } else { |
||
184 | $list = $list->where('"FieldValue" IS NULL OR "FieldValue" = \'\''); |
||
185 | } |
||
186 | $rule = $list->first(); |
||
187 | if ($rule) { |
||
188 | $rule->write(); |
||
189 | $rule->publish("Stage", "Live"); |
||
190 | return $rule; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | // If none found, or in stage, create new record |
||
195 | 2 | $rule = new EditableCustomRule(); |
|
196 | 2 | $rule->ParentID = $field->ID; |
|
197 | 2 | $rule->ConditionFieldID = $conditionField ? $conditionField->ID : 0; |
|
198 | 2 | $rule->Display = $display; |
|
199 | 2 | $rule->ConditionOption = $conditionOption; |
|
200 | 2 | $rule->FieldValue = $value; |
|
201 | 2 | $rule->write(); |
|
202 | 2 | return $rule; |
|
203 | } |
||
204 | |||
205 | 2 | public function log($message) |
|
206 | { |
||
207 | 2 | if ($this->getQuiet()) { |
|
208 | 2 | return; |
|
209 | } |
||
210 | if (Director::is_cli()) { |
||
211 | echo "{$message}\n"; |
||
212 | } else { |
||
213 | echo "{$message}<br />"; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Set if this service should be quiet |
||
219 | * |
||
220 | * @param bool $quiet |
||
221 | * @return $ths |
||
222 | */ |
||
223 | 2 | public function setQuiet($quiet) |
|
228 | |||
229 | 2 | public function getQuiet() |
|
233 | |||
234 | |||
235 | /** |
||
236 | * Migrate Placeholder data from field specific table to the EditableFormField table |
||
237 | * |
||
238 | * @param EditableFormField $field |
||
239 | * @param string $tableName |
||
240 | */ |
||
241 | 2 | private function migratePlaceholder($field, $tableName) |
|
291 | } |
||
292 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.