-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_user_consent_token.module
More file actions
173 lines (158 loc) · 5.85 KB
/
ding_user_consent_token.module
File metadata and controls
173 lines (158 loc) · 5.85 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
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
<?php
/**
* @file
* Ding user consent token functionality.
*/
/**
* ding_user_consent_token_quick_accept form builder
*/
function ding_user_consent_token_quick_accept($form, &$form_state) {
global $user;
$account = (isset($form_state['user'])) ? $form_state['user'] : NULL;
if (!isset($account)) {
$account = $user;
}
if (ding_user_is_provider_user($account)) {
$consent_id = (isset($form_state['consent_id'])) ? $form_state['consent_id'] : 'loan_history_store';
// Try to get the user consent
try {
$consent = ding_user_consent_get_consent($consent_id);
}
catch (Exception $e) {
// Catch the DingProviderConsentException, which is thrown if the user has been presented with the
// consent options yet or if the user has denied consent but consent is required for this type.
$consent = NULL;
}
// If the user is valid but hasn't given consent, present a submit form which allows the user to instantly
// give consent.
if (!$consent) {
$html_id = drupal_html_id('ding-user-consent-submit');
$form['ding_user_consent_submit'] = array(
'#type' => 'submit',
'#value' => t('Allow storage of loans and other personal data'),
'#attributes' => array('id' => $html_id),
'#ajax' => array(
'callback' => 'ding_user_consent_token_quick_accept_callback',
'wrapper' => $html_id,
'method' => 'replace',
'effect' => 'fade',
),
);
// In case the hide description state is not set, display a short description of this form.
if (!isset($form_state['hide_description']) || !$form_state['hide_description']) {
$description = variable_get($consent_id . '_description_token', '');
$description = (is_array($description)) ? $description['value'] : $description;
$form['ding_user_consent'] = array(
'#type' => 'item',
'#markup' => $description,
);
}
}
else {
// if consent has been given display a short message.
$description = variable_get($consent_id . '_description_token_given', '');
$description = (is_array($description)) ? $description['value'] : $description;
$form['ding_user_consent'] = array(
'#type' => 'item',
'#prefix' => '<span class="consent-given-icon"></span>',
'#markup' => token_replace($description, array('user' => $account)),
);
}
}
else {
// Provide a link to login before allowing her to give consent.
$form['ding_user_consent'] = array(
'#type' => 'item',
'#markup' => t('Please !login as a user of the library to use this function', array('!login' => l(t('login'), 'user'))),
);
}
return $form;
}
/**
* Submit function for ding_user_consent_token_quick_accept_submit form submit
*/
function ding_user_consent_token_quick_accept_submit($form, &$form_state) {
global $user;
if (!empty($form['#user'])) {
$account = $form['#user'];
} else {
$account = $user;
}
$consent_id = (isset($form_state['consent_id'])) ? $form_state['consent_id'] : 'loan_history_store';
$result = ding_provider_invoke('user_consent', 'add', $account, $consent_id, 1);
if ($result) {
if (empty($account->data['consent']) || !isset($account->data['consent'])) {
$su = clone $account;
$auth_res['user']['data'] = array('consent' => 1);
ding_user_update_user($auth_res, $su);
}
drupal_set_message(t('Thank you for allowing us to store your personal data.'));
module_invoke_all('ding_user_consent_changed', array($consent_id => 1));
}
}
/**
* Submit function for ding_user_consent_token_quick_accept_callback
*/
function ding_user_consent_token_quick_accept_callback($form, $form_state) {
$description = variable_get($consent_id . '_description_token_given', '');
$description = (is_array($description)) ? $description['value'] : $description;
$return = array();
$return['ding_user_consent'] = array(
'#type' => 'item',
'#prefix' => '<span class="consent-given-icon"></span>',
'#markup' => $description,
);
return $return;
}
/**
* Implements hook_token_info().
*/
function ding_user_consent_token_token_info() {
$type = array(
'name' => t('Ding user consent'),
'description' => t('Tokens related to user consent.'),
);
// Core tokens for nodes.
$consent['consent'] = array(
'name' => t("User consent status"),
'description' => t("The states of the current users consent settings."),
);
return array(
'types' => array('ding_user_consent' => $type),
'tokens' => array('ding_user_consent' => $consent),
);
}
/**
* Implements hook_tokens
*/
function ding_user_consent_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array('absolute' => TRUE);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'ding_user_consent') {
foreach ($tokens as $name => $original) {
$args = explode(':', $name);
$consent_id = (isset($args[1])) ? $args[1] : 'loan_history_store';
$form_state = array('consent_id' => $consent_id);
if(isset($args[2])) {
switch ($args[2]) {
// Simple key values on the node.
case 'accept':
$form_state += array('hide_description' => TRUE);
break;
}
}
$form = drupal_build_form('ding_user_consent_token_quick_accept', $form_state);
$consent_form = drupal_render($form);
$replacements[$original] = $consent_form;
}
}
return $replacements;
}