-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebform_paymethod_select.install
More file actions
113 lines (108 loc) · 4.33 KB
/
webform_paymethod_select.install
File metadata and controls
113 lines (108 loc) · 4.33 KB
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
<?php
/**
* @file
*/
use \Drupal\webform_paymethod_select\WebformPaymentContext;
/**
* Implements hook_enable().
*
* we're setting the module weight in order to have hook_form_alter
* being called *after* the same hook from the webform module
* we want this to ensure our form submit handler is set *after*
* the webform submit handler so our payment methods can redirect
* in the payment->execute() call
*
* @TODO is this really still needed?
*/
function webform_paymethod_select_enable() {
db_query("UPDATE {system} a INNER JOIN {system} b ON a.name = 'webform_paymethod_select' AND b.name = 'webform' SET a.weight = b.weight + 1");
}
/**
* Implements hook_requirements().
*
* Check for jQuery>=1.6.
*/
function webform_paymethod_select_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$version = variable_get('jquery_update_jquery_version', '1.10');
if (!module_exists('jquery_update') || $version === 'default') {
$version = '1.4';
}
if (version_compare($version, '1.6', '<')) {
$requirements['webform_paymethod_select_jquery'] = [
'title' => t('Webform paymethod needs jQuery 1.6 or newer.'),
'value' => $version,
'description' => t('You can change the jQuery version on the !admin_page .', ['!admin_page' => l(t('jquery_update configuration page'), 'admin/config/development/jquery_update')]),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Update / unify payment context data.
*/
function webform_paymethod_select_update_1() {
module_enable(array('payment_context'));
$last_pid = 0;
// Get the first paymethod_select component for each node with payments.
// We need them later to map context data without cid.
$sql = "SELECT nid, cid FROM {webform_component} WHERE type='paymethod_select' GROUP BY nid ORDER BY cid";
$cid_by_nid = db_query($sql)->fetchAssoc();
// Loop over all stored payments.
$sql = 'SELECT pid FROM {payment} WHERE pid>:last_pid LIMIT 100';
while ($pids = db_query($sql, array(':last_pid' => $last_pid))->fetchCol()) {
foreach ($pids as $pid) {
try {
$payment = entity_load_single('payment', $pid);
}
catch (\UnexpectedValueException $e) {
watchdog('webform_paymethod_select', 'Cannot load payment: !message', array('!message' => $e->getMessage(), WATCHDOG_WARNING));
// Node or submission have vanished. We need to parse the serialized data
// to extract the nid and sid because the payment can't be loaded.
$cd_string = db_query('SELECT context_data FROM {payment} WHERE pid=:pid', array(':pid' => $pid))->fetchField();
$cd = array();
if (($p = strpos($cd_string, 's:3:"nid";')) !== FALSE) {
$p += 10;
$end = strpos($cd_string, ';', $p);
$cd['nid'] = (int) unserialize(substr($cd_string, $p, $end - $p + 1));
}
if (($p = strpos($cd_string, 's:3:"sid";')) !== FALSE) {
$p += 10;
$end = strpos($cd_string, ';', $p);
$cd['sid'] = (int) unserialize(substr($cd_string, $p, $end - $p + 1));
}
$fields = array();
if (isset($cd['nid']) && isset($cd['sid'])) {
$cd['cid'] = $cid_by_nid[$cd['nid']];
$fields['context'] = 'webform_paymethod_select';
}
$fields['context_data'] = serialize($cd);
db_update('payment')->fields($fields)->condition('pid', $pid)->execute();
continue;
}
$cd = &$payment->context_data;
if ($payment->contextObj instanceof WebformPaymentContext) {
// Everything is perfect - nothing to do.
}
// Try to get the context from ->context_data['context'].
elseif (!empty($cd['context']) && $cd['context'] instanceof WebformPaymentContext) {
$payment->contextObj = $cd['context'];
unset($cd['context']);
entity_save('payment', $payment);
}
// Try to get the context data from an array containing nid and sid.
elseif (is_array($cd) && !empty($cd['nid']) && !empty($cd['sid'])) {
if (!isset($cd['cid']) && isset($cid_by_nid[$cd['nid']])) {
$cd['cid'] = $cid_by_nid[$cd['nid']];
}
if (!empty($cd['cid'])) {
$payment->contextObj = WebformPaymentContext::fromContextData($cd);
entity_save('payment', $payment);
}
}
$last_pid = $pid;
}
}
}