-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgive.install
312 lines (276 loc) · 9.67 KB
/
give.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* @file
* Install, update and uninstall functions for the give module.
*/
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\give\Entity\GiveForm;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_install().
*/
function give_install() {
// Adjustments to the pre-created donation form.
$default_donate_form_id = 'donate';
// Set the recipient to use the site mail by default.
$mail = \Drupal::config('system.site')->get('mail');
$donate_form = GiveForm::load($default_donate_form_id);
// Check that form exists before trying to set it as default.
if (!$donate_form) {
return;
}
$donate_form->setRecipients([$mail]);
$donate_form->save();
// Set this form as the default donation form.
$give_settings = \Drupal::configFactory()->getEditable('give.settings');
$give_settings->set('default_form', $default_donate_form_id)->save();
}
/**
* Implements hook_schema().
*
* Defines a supplementary database tables used to record problems, if any,
* encountered during a donation attempt.
*
* Note that the main information about donations is stored directly in the
* donation entity, and as such the database schema for donation entity fields
* is defined in Drupal\give\Entity\Donation.php baseFieldDefinitions().
*
* @see hook_schema()
*/
function give_schema() {
$schema = [];
$schema['give_problem'] = [
'description' => 'Stores a log identifiable errors and issues encountered during a donation attempt.',
'fields' => [
'pid' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique problem ID.',
],
'donation_uuid' => [
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'Foreign key to {give_donation}.uuid; uniquely identifies a Give donation to which this log message applies.',
],
'type' => [
'type' => 'varchar_ascii',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'A rough categorization of the problem.',
],
'detail' => [
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Detail regarding the problem.',
],
'timestamp' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when this problem was logged.',
],
],
'primary key' => ['pid'],
'indexes' => [
'type' => ['type'],
'donation_uuid' => ['donation_uuid'],
'timestamp' => ['timestamp'],
],
];
return $schema;
}
/**
* Add 'stripe_token' field to 'donation' entities.
*/
function give_update_8001() {
// Install the definition that this field had in
// \Drupal\give\Entity\Donation::baseFieldDefinitions()
// at the time that this update function was written. If/when code is
// deployed that changes that definition, the corresponding module must
// implement an update function that invokes
// \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition()
// with the new definition.
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Stripe token'))
->setDescription(t('The token returned by Stripe used to tell Stripe to process the donation.'));
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('stripe_token', 'give_donation', 'give', $storage_definition);
}
/**
* Add the default frequencies to the already created entities.
*/
function give_update_8002() {
$entities = \Drupal::entityQuery('give_form');
$ids = $entities->execute();
foreach ($ids as $id) {
$give_form = GiveForm::load($id);
if (empty($give_form->getFrequencies())) {
$default = give_get_default_frequencies();
$give_form->setFrequencies($default);
$give_form->save();
}
}
}
/**
* Add address and card info fields to 'donation' entities.
*/
function give_update_8003() {
$fields = [];
$fields['address_line1'] = BaseFieldDefinition::create('string')
->setLabel(t('Address line 1'))
->setDescription(t('The street address or PO Box of the donor; used in billing address.'));
$fields['address_line2'] = BaseFieldDefinition::create('string')
->setLabel(t('Address line 2'))
->setDescription(t('Optional apartment/suite/unit of the donor; used in billing address.'));
$fields['address_city'] = BaseFieldDefinition::create('string')
->setLabel(t('City or district'))
->setDescription(t('The town of the donor; used in billing address.'));
$fields['address_zip'] = BaseFieldDefinition::create('string')
->setLabel(t('Postal code'))
->setDescription(t('ZIP or postal code of the donor; used in billing address.'));
$fields['address_state'] = BaseFieldDefinition::create('string')
->setLabel(t('State or province'))
->setDescription(t('The state/province/region of the donor; used in billing address.'));
$fields['address_country'] = BaseFieldDefinition::create('string')
->setLabel(t('Country'))
->setDescription(t('The country the donor; used in billing address.'));
$fields['card_brand'] = BaseFieldDefinition::create('string')
->setLabel(t('Card brand'))
->setDescription(t('The card brand (Visa, MasterCard, etc).'));
$fields['card_funding'] = BaseFieldDefinition::create('string')
->setLabel(t('Card funding'))
->setDescription(t('The card funding type (credit, debit).'));
$fields['card_last4'] = BaseFieldDefinition::create('integer')
->setLabel(t('Last four'))
->setDescription(t('The last four digits of the credit/debit card, if applicable.'));
foreach ($fields as $field => $storage_definition) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($field, 'give_donation', 'give', $storage_definition);
}
}
/**
* Remove impossibilities.
*/
function give_update_8004() {
$field_storage_definition = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('recurring', 'give_donation');
$field_storage_definition->setDisplayOptions('form', [
'type' => 'options_select',
'settings' => [
'display_label' => TRUE,
],
'weight' => 15,
]);
\Drupal::entityDefinitionUpdateManager()
->updateFieldStorageDefinition($field_storage_definition);
}
/**
* Add problem log table.
*/
function give_update_8005() {
if (db_table_exists('give_problem')) {
return;
}
$schema = [];
$schema['give_problem'] = [
'description' => 'Stores a log identifiable errors and issues encountered during a donation attempt.',
'fields' => [
'pid' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique problem ID.',
],
'donation_uuid' => [
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'Foreign key to {give_donation}.uuid; uniquely identifies a Give donation to which this log message applies.',
],
'type' => [
'type' => 'varchar_ascii',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'A rough categorization of the problem.',
],
'detail' => [
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Detail regarding the problem.',
],
'timestamp' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The Unix timestamp when this problem was logged.',
],
],
'primary key' => ['pid'],
'indexes' => [
'type' => ['type'],
'donation_uuid' => ['donation_uuid'],
'timestamp' => ['timestamp'],
],
];
db_create_table('give_problem', $schema['give_problem']);
}
/**
* Set default values for the new fields.
*/
function give_update_8006() {
$module_path = drupal_get_path('module', 'give');
$default_config = Yaml::parse(file_get_contents($module_path . '/config/install/give.form.donate.yml'));
$config_factory = \Drupal::configFactory();
$default_values = [
'autoreply',
'collect_address',
'subject_recurring',
'reply_recurring',
'subject',
'reply',
'subject_pledge',
'reply_pledge',
];
// Find all give_form configs.
foreach ($config_factory->listAll('give.form.') as $give_form) {
$give_form = $config_factory->getEditable($give_form);
// Check if the give_form has default values for the new fields and if not
// we set them.
foreach ($default_values as $default_value) {
if (empty($give_form->get($default_value))) {
$give_form->set($default_value, $default_config[$default_value]);
}
}
$give_form->save();
}
}
/**
* Add new fields.
*
* Per https://www.drupal.org/node/2554097
*/
function give_update_8007() {
$fields = [];
$fields['method'] = BaseFieldDefinition::create('list_integer')
->setLabel(t('Method'))
->setDescription(t('The donation method (payment card, check pledge).'));
$fields['telephone'] = BaseFieldDefinition::create('string')
->setLabel(t('Phone'))
->setDescription(t('The telephone number of the donor.'))
->setSetting('max_length', 20);
$fields['check_or_other_information'] = BaseFieldDefinition::create('string')
->setLabel(t('Further information'))
->setDescription(t('Any questions or explain anything needed to arrange for giving donation.'))
->setSetting('max_length', 2000);
foreach ($fields as $field => $storage_definition) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($field, 'give_donation', 'give', $storage_definition);
}
}