Skip to content

Commit 49dec5f

Browse files
authored
Merge pull request #1 from kenorb-contrib/master
Implements JavaScript integration
2 parents 194d28a + 6738f5a commit 49dec5f

5 files changed

Lines changed: 334 additions & 143 deletions

File tree

queueit.admin.inc

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ function queueit_settings_form($form, &$form_state) {
1616
);
1717
// Implementation type.
1818
// * Integration - Validate a user via integration config.
19+
// * JavaScript - Validate a user via JavaScript on the client side.
1920
// * Code - Specify the configuration in code without using the Trigger/Action paradigm.
2021
$form['config']['queueit_mode'] = array(
2122
'#type' => 'radios',
2223
'#title' => t('Queue-it mode.'),
2324
'#options' => [
2425
'integration' => 'Integration config',
26+
'js' => 'JavaScript',
2527
'code' => 'Configuration in code',
26-
// 'js' => 'JavaScript', // @todo
2728
],
28-
'#description' => t('Select the type of Queue-it implementation.<br>Select Integration Config to load the configuration from the Go Queue-it platform.<br>Select Configuration in code if your application server is not allowed to do external GET requests (this is without using the Trigger/Action paradigm).'),
29+
'#description' => t('Select the method used to integrate Queue-it:<br>Integration Config - used to load the configuration from the Go Queue-it platform.<br>JavaScript - protects pages by the queue on the client side.<br>Configuration in code - recommended for testing specific event or when your application server is not allowed to do external GET requests (this is without using the Trigger/Action paradigm).'),
2930
'#default_value' => variable_get('queueit_mode', 'integration'),
3031
);
3132
$form['config']['queueit_debug'] = array(
@@ -40,9 +41,12 @@ function queueit_settings_form($form, &$form_state) {
4041
'#description' => t('When configuration in code is selected, you can specify the following parameters without using the Trigger/Action paradigm. Useful when your application server is not allowed to do external GET requests.'),
4142
'#type' => 'fieldset',
4243
'#collapsible' => TRUE,
43-
'#collapsed' => variable_get('queueit_mode', 'code') == 'integration',
44-
'#disabled' => variable_get('queueit_mode', 'code') == 'integration',
45-
'#states' => ['disabled' => [':input[name="queueit_mode"]' => ['value' => 'integration']]]
44+
'#collapsed' => variable_get('queueit_mode', 'integration') <> 'code',
45+
'#disabled' => variable_get('queueit_mode', 'integration') <> 'code',
46+
'#states' => [
47+
'disabled' => [':input[name="queueit_mode"]' => ['!value' => 'code']],
48+
'collapsed' => [':input[name="queueit_mode"]' => ['!value' => 'code']],
49+
]
4650
);
4751
$form['event_config']['queueit_event_id'] = array(
4852
'#type' => 'textfield',
@@ -52,7 +56,7 @@ function queueit_settings_form($form, &$form_state) {
5256
);
5357
$form['event_config']['queueit_queue_domain'] = array(
5458
'#type' => 'textfield',
55-
'#title' => t('Domain name of the queue - usually in the format [CustomerId].queue-it.net.'),
59+
'#title' => t('Existing domain name of the queue - usually in the format [CustomerId].queue-it.net.'),
5660
'#description' => t('Specify ID of the queue to use.'),
5761
'#default_value' => variable_get('queueit_queue_domain'),
5862
);
@@ -112,25 +116,30 @@ function queueit_settings_form($form, &$form_state) {
112116
'#title' => 'Queue-it Credentials',
113117
'#type' => 'fieldset',
114118
);
115-
$form['creds']['queueit_api_key'] = array(
116-
'#type' => 'textfield',
117-
'#title' => t('API Key'),
118-
'#description' => t('Specify the Api-key which can be supplied through the query string parameter. The key can be found in the GO Queue-it Platform.'),
119-
'#default_value' => variable_get('queueit_api_key'),
120-
);
121119
$form['creds']['queueit_customer_id'] = array(
122120
'#type' => 'textfield',
123121
'#title' => t('Customer ID'),
124122
'#description' => t('Your Queue-it customer ID.'),
125123
'#default_value' => variable_get('queueit_customer_id'),
126124
'#required' => TRUE,
127125
);
126+
$form['creds']['queueit_api_key'] = array(
127+
'#type' => 'textfield',
128+
'#title' => t('API Key'),
129+
'#description' => t('Specify the Api-key which can be supplied through the query string parameter. The key can be found in the GO Queue-it Platform.'),
130+
'#default_value' => variable_get('queueit_api_key'),
131+
'#states' => [
132+
'required' => [':input[name="queueit_mode"]' => ['!value' => 'js']],
133+
]
134+
);
128135
$form['creds']['queueit_secret_key'] = array(
129136
'#type' => 'textfield',
130137
'#title' => t('Secret key'),
131138
'#description' => t('Your 72 char secret key as specified in Go Queue-it self-service platform.'),
132139
'#default_value' => variable_get('queueit_secret_key'),
133-
'#required' => TRUE,
140+
'#states' => [
141+
'required' => [':input[name="queueit_mode"]' => ['!value' => 'js']],
142+
]
134143
);
135144

136145
return system_settings_form($form);
@@ -140,21 +149,57 @@ function queueit_settings_form($form, &$form_state) {
140149
* Form builder.
141150
*/
142151
function queueit_settings_form_validate($form, &$form_state) {
152+
153+
// Fail in case of any existing form validation errors.
154+
if (form_get_errors()) {
155+
return FALSE;
156+
}
157+
158+
// Initialize required variables.
159+
$int_method = $form_state['values']['queueit_mode'];
160+
$customer_id = $form_state['values']['queueit_customer_id'];
161+
$api_key = $form_state['values']['queueit_api_key'];
162+
$secret_key = $form_state['values']['queueit_secret_key'];
163+
164+
// Check the backend method requirements.
165+
if ($int_method <> 'js') {
166+
module_load_include('install', 'queueit');
167+
$requirements = module_invoke('queueit', 'requirements', 'runtime');
168+
$severity = drupal_requirements_severity($requirements);
169+
if ($severity == REQUIREMENT_ERROR) {
170+
form_set_error(NULL, 'KnownUser.V3 library not installed!');
171+
return FALSE;
172+
}
173+
}
174+
143175
// Manually load the file classes.
144176
module_load_include('php', 'queueit', 'src/classes/QueueitBase');
145177
module_load_include('php', 'queueit', 'src/classes/QueueitKnownUser');
178+
146179
// Validate the configuration.
147180
try {
148-
$known_user = new QueueitKnownUser();
149-
if (!$known_user->validateConfig()) {
150-
drupal_set_message(t('Queue-it configuration is not valid.'), 'warning');
151-
}
152-
switch (variable_get('queueit_mode', 'code')) {
181+
182+
switch ($int_method) {
183+
case 'js':
184+
$result = QueueitBase::validateJsEndpoints();
185+
if (!$result) {
186+
drupal_set_message(t('Cannot access JS endpoints'), 'warning');
187+
}
188+
break;
189+
153190
case 'code':
191+
$known_user = new QueueitKnownUser($int_method, $customer_id, $secret_key, $api_key);
192+
if (!$known_user->validateConfig()) {
193+
drupal_set_message(t('Queue-it configuration is not valid.'), 'warning');
194+
}
154195
$result = $known_user->resolveRequestByLocalEventConfig();
155196
break;
156197

157198
case 'integration':
199+
$known_user = new QueueitKnownUser($int_method, $customer_id, $secret_key, $api_key);
200+
if (!$known_user->validateConfig()) {
201+
drupal_set_message(t('Queue-it configuration is not valid.'), 'warning');
202+
}
158203
$config = $known_user->getIntegrationConfig();
159204
if (!$config) {
160205
drupal_set_message(
@@ -170,13 +215,26 @@ function queueit_settings_form_validate($form, &$form_state) {
170215
}
171216
$result = $known_user->validateRequestByIntegrationConfig();
172217
break;
218+
219+
default:
220+
form_set_error('queueit_mode', $e->getMessage());
221+
drupal_set_message(
222+
t('Invalid integration method: !method.',
223+
['!method' => $int_method]),
224+
'error');
173225
}
226+
174227
}
175228
catch (\Exception $e) {
176229
drupal_set_message(t('Exception error: ') . $e->getMessage(), 'error');
177230
watchdog_exception('queueit', $e);
231+
form_set_error('queueit_mode', $e->getMessage());
178232
}
233+
179234
if (!empty($result) && $result) {
180235
drupal_set_message(t('Configuration successfully validated.'), 'status');
181236
}
237+
else {
238+
drupal_set_message(t('Configuration not valid. Please verify your configuration.'), 'warning');
239+
}
182240
}

queueit.install

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,65 @@
1111
function queueit_requirements($phase) {
1212
$requirements = array();
1313

14+
// Load integration method either from POST or variable.
15+
// Reading from POST is used during form validation.
16+
// @see: queueit_settings_form_validate().
17+
$int_method = filter_input(INPUT_POST, 'queueit_mode') ?:
18+
variable_get('queueit_mode', 'integration');
19+
$customer_id = filter_input(INPUT_POST, 'queueit_customer_id') ?:
20+
variable_get('queueit_customer_id');
21+
$api_key = filter_input(INPUT_POST, 'queueit_api_key') ?:
22+
variable_get('queueit_api_key');
23+
$secret_key = filter_input(INPUT_POST, 'queueit_secret_key') ?:
24+
variable_get('queueit_secret_key');
25+
1426
if ($phase == 'runtime') {
1527
$t = get_t();
16-
$known_user_lib = drupal_get_library('queueit', 'queueit/KnownUser');
17-
$known_user_exists = file_exists(libraries_get_path('composer') . '/queueit/knownuserv3' . '/KnownUser.php');
18-
$exists = $known_user_lib && $known_user_exists;
28+
$description = '';
29+
$severity = REQUIREMENT_OK;
30+
$value = '';
31+
switch ($int_method) {
32+
case 'js':
33+
$known_user = new QueueitBase();
34+
$js_exists = $known_user->validateJsEndpoints();
35+
$severity = $js_exists
36+
? REQUIREMENT_OK
37+
: REQUIREMENT_ERROR;
38+
$value = $js_exists
39+
? $t("JavaScript integration: JS files validated.")
40+
: $t("Problem accessing external JS files.");
41+
break;
42+
43+
case 'code':
44+
case 'integration':
45+
$known_user_lib = drupal_get_library('queueit', 'queueit/KnownUser');
46+
$known_user_exists = file_exists(libraries_get_path('composer') . '/queueit/knownuserv3' . '/KnownUser.php');
47+
$exists = $known_user_lib && $known_user_exists;
48+
$severity = $exists ? REQUIREMENT_OK : REQUIREMENT_ERROR;
49+
$value = $t('Backend integration. %lib library %state.',
50+
[
51+
'%lib' => 'KnownUser.V3.PHP',
52+
'%state' => $exists ? 'installed' : 'not installed',
53+
]);
54+
module_load_include('php', 'queueit', 'src/classes/QueueitBase');
55+
if ($exists && class_exists('QueueitBase') && !((new QueueitBase)->validateConfig())) {
56+
$value .= '&nbsp;' . $t("But not configured correctly. Check the configuration page.");
57+
$severity = REQUIREMENT_WARNING;
58+
}
59+
else {
60+
$queueit = new QueueitKnownUser($int_method, $customer_id, $secret_key, $api_key);
61+
$config = $queueit->getIntegrationConfig();
62+
$severity = $config ? REQUIREMENT_OK : REQUIREMENT_WARNING;
63+
$description = json_encode(json_decode($config), TRUE);
64+
}
65+
break;
66+
}
1967
$requirements['queueit'] = array(
2068
'title' => $t('Queue-it'),
21-
'severity' => $exists ? REQUIREMENT_OK : REQUIREMENT_ERROR,
22-
'value' => $t('%lib library %state.',
23-
[
24-
'%lib' => 'KnownUser.V3.PHP',
25-
'%state' => $exists ? 'installed' : 'not installed',
26-
]),
69+
'description' => $description,
70+
'severity' => $severity,
71+
'value' => $value,
2772
);
28-
module_load_include('php', 'queueit', 'src/classes/QueueitBase');
29-
if ($exists && class_exists('QueueitBase') && !((new QueueitBase)->validateConfig())) {
30-
$requirements['queueit']['value'] .= '&nbsp;' . $t("But not configured correctly.");
31-
$requirements['queueit']['severity'] = REQUIREMENT_WARNING;
32-
}
33-
else {
34-
$queueit = new QueueitBase();
35-
$config = $queueit->getIntegrationConfig();
36-
$requirements['queueit']['severity'] = $config ? REQUIREMENT_OK : REQUIREMENT_WARNING;
37-
$requirements['queueit']['description'] = json_encode(json_decode($config), TRUE);
38-
}
3973
}
4074

4175
return $requirements;

queueit.module

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function queueit_init() {
5050
}
5151
}
5252
$known_user = new QueueitKnownUser();
53+
$customer_id = $known_user->getCustomerId();
5354
// Ignore processing if the config is invalid.
5455
if (!$known_user->validateConfig()) {
5556
return;
@@ -73,6 +74,19 @@ function queueit_init() {
7374
$result = $known_user->validateRequestByIntegrationConfig();
7475
break;
7576

77+
case 'js':
78+
drupal_add_js(QueueitBase::QI_JS_CLIENT_URL, 'external');
79+
/* @fixme: https://drupal.stackexchange.com/q/253634
80+
drupal_add_js(QueueitBase::QI_JS_LOADER_URL,
81+
[
82+
'type' => 'external',
83+
'data-queueit-c' => $customer_id,
84+
]);
85+
*/
86+
$js_code = "<script data-queueit-c='$customer_id' type='text/javascript' src='//static.queue-it.net/script/queueconfigloader.min.js'></script>";
87+
drupal_add_js($js_code, 'inline');
88+
break;
89+
7690
default:
7791
watchdog('queueit', 'Unknown method type: %type', ['%type' => $method], WATCHDOG_WARNING);
7892
break;

0 commit comments

Comments
 (0)