forked from colemanw/webform_civicrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebform_civicrm.install
186 lines (179 loc) · 6.35 KB
/
webform_civicrm.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
<?php
/**
* @file
* Webform CiviCRM module's install, uninstall and upgrade code.
*/
use Drupal\webform_civicrm\Utils;
use Drupal\node\Entity\Node;
use Drupal\webform\Entity\Webform;
/**
* Implements hook_requirements().
*/
/*
function webform_civicrm_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
$t = get_t();
$status = _webform_civicrm_status();
if (!$status['webform_civicrm']) {
$requirements['webform_civicrm'] = array(
'title' => 'Webform CiviCRM Integration',
'value' => $t('Version error'),
'severity' => REQUIREMENT_ERROR,
'description' => $t('The versions of the Webform CiviCRM Integration, Webform, CiviCRM enabled are not compatible. ') .
l($t('See the Webform CiviCRM Integration project page for version compatibility'), 'https://drupal.org/project/webform_civicrm'),
);
}
else {
$requirements['webform_civicrm'] = array(
'title' => 'Webform CiviCRM Integration',
'severity' => REQUIREMENT_OK,
'value' => t('Required version of CiviCRM and Webform are enabled.'),
);
}
}
return $requirements;
}
*/
/**
* Implements hook_schema().
*/
function webform_civicrm_schema() {
$schema = array();
$schema['webform_civicrm_forms'] = array(
'description' => 'CiviCRM settings for individual Webform nodes.',
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Webform Node ID',
),
'data' => array(
'type' => 'text',
'serialize' => TRUE,
'description' => 'Array of entity data for this webform',
),
'prefix_known' => array(
'description' => 'Form prefix for known users.',
'type' => 'text',
'not null' => TRUE,
),
'prefix_unknown' => array(
'description' => 'Form prefix for unknown users.',
'type' => 'text',
'not null' => TRUE,
),
'message' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Message to show to known users',
),
'confirm_subscription' => array(
'description' => 'Send confirmation for mailing list subscriptions.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'block_unknown_users' => array(
'description' => 'Only allow known contacts to use form.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'create_new_relationship' => array(
'description' => 'Create new relationship if duplicate record exists and is expired or inactive.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'create_fieldsets' => array(
'description' => 'Add fieldsets around contacts.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'new_contact_source' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Source label for newly created contacts',
),
),
'primary key' => array('nid'),
);
$schema['webform_civicrm_submissions'] = array(
'description' => 'Link between form submissions and CiviCRM data.',
'fields' => array(
'sid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Webform Submission ID',
),
'contact_id' => array(
'type' => 'varchar',
'length' => 2000,
'not null' => TRUE,
'default' => '',
'description' => 'CiviCRM contact ids from this submission',
),
'civicrm_data' => array(
'type' => 'text',
'serialize' => TRUE,
'description' => 'Array of entity ids for this submission',
),
),
'primary key' => array('sid'),
);
return $schema;
}
/**
* Update receipt setting as per the value set in the contribution page.
*/
function webform_civicrm_update_8001() {
\Drupal::service('civicrm')->initialize();
$utils = \Drupal::service('webform_civicrm.utils');
$webforms = Webform::loadMultiple();
foreach ($webforms as $webform) {
$handler = $webform->getHandlers('webform_civicrm');
$config = $handler->getConfiguration();
if (empty($config['webform_civicrm'])) {
continue;
}
$contribution = wf_crm_aval($config, "webform_civicrm:settings:data:contribution:1:contribution:1", []);
if (!empty($contribution['contribution_page_id'])) {
$returnParams = [
"financial_type_id", "currency", "bcc_receipt", "cc_receipt",
"receipt_text", "pay_later_receipt", "receipt_from_name", "receipt_from_email", "is_email_receipt"
];
$contribution_page = current($utils->wf_crm_apivalues('ContributionPage', 'get', [
'return' => $returnParams,
'id' => $contribution['contribution_page_id'],
]));
$settings = &$config['webform_civicrm']['settings'];
$settings['civicrm_1_contribution_1_contribution_enable_contribution'] = $settings['data']['contribution'][1]['contribution'][1]["enable_contribution"] = 1;
unset($settings['data']['contribution'][1]['contribution'][1]["contribution_page_id"]);
$settings['civicrm_1_contribution_1_contribution_financial_type_id'] = $settings['data']['contribution'][1]['contribution'][1]["financial_type_id"] = $contribution_page['financial_type_id'] ?? '';
$settings['contribution_1_settings_currency'] = $settings['data']['contribution'][1]['currency'] = $contribution_page['currency'] ?? '';
if (!empty($contribution_page['is_email_receipt'])) {
$settings['receipt_1_number_of_receipt'] = $settings['data']['receipt']['number_number_of_receipt'] = 1;
$receiptValues = ['cc_receipt', 'bcc_receipt', 'receipt_text', 'pay_later_receipt', 'receipt_from_name', 'receipt_from_email'];
foreach ($receiptValues as $val) {
$settings["receipt_1_number_of_receipt_{$val}"] = $settings['data']['receipt']["number_number_of_receipt_{$val}"] = $contribution_page[$val] ?? '';
}
}
$handler->setConfiguration($config);
$webform->save();
}
}
}