-
Notifications
You must be signed in to change notification settings - Fork 0
/
power_bi_reports.admin.inc
181 lines (161 loc) · 5.76 KB
/
power_bi_reports.admin.inc
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
<?php
/**
* Menu callback to configure module settings.
*/
function power_bi_reports_admin_form($form, &$form_state) {
$form['power_bi_reports_tenant_id'] = [
'#type' => 'textfield',
'#title' => t('Tenant ID'),
'#default_value' => variable_get('power_bi_reports_tenant_id'),
'#required' => TRUE,
];
$form['power_bi_reports_resource'] = [
'#type' => 'textfield',
'#title' => t('Resource'),
'#default_value' => variable_get('power_bi_reports_resource', 'https://analysis.windows.net/powerbi/api'),
'#required' => TRUE,
];
$form['power_bi_reports_client_id'] = [
'#type' => 'textfield',
'#title' => t('Client ID'),
'#default_value' => variable_get('power_bi_reports_client_id'),
'#required' => TRUE,
];
$form['power_bi_reports_username'] = [
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('power_bi_reports_username'),
'#required' => TRUE,
];
$existing_password = variable_get('power_bi_reports_password', '');
$form['power_bi_reports_password'] = [
'#type' => 'password',
'#title' => t('Password'),
'#required' => !$existing_password,
];
$form['power_bi_reports_cron_sync'] = [
'#type' => 'checkbox',
'#title' => t('Sync embed tokens with cron'),
'#description' => t('Should the embed tokens be constantly updated with cron?'),
'#default_value' => variable_get('power_bi_reports_cron_sync', FALSE),
];
$form['#submit'][] = 'power_bi_reports_admin_form_submit';
return system_settings_form($form);
}
/**
* Admin form submit callback.
*
* If there's already a stored password and the user left the field empty, make
* sure that the password will not be deleted.
*
* @param $form
* @param $form_state
*/
function power_bi_reports_admin_form_submit($form, &$form_state) {
$existing_password = variable_get('power_bi_reports_password', '');
$typed_password = $form_state['values']['power_bi_reports_password'];
if ($existing_password && !$typed_password) {
$form_state['values']['power_bi_reports_password'] = $existing_password;
}
}
/**
* Menu callback to get module status.
*/
function power_bi_reports_status_form($form, &$form_state) {
if (!power_bi_reports_is_configured()) {
$msg = t('You have been redirected to the settings form.');
drupal_set_message($msg, 'warning');
drupal_goto('admin/config/services/power-bi-reports/settings');
}
$form = [];
$form['token_renew'] = [
'#type' => 'submit',
'#name' => 'token_renew',
'#value' => t('Renew access token'),
];
// Sync reports message.
$reports_last_sync = variable_get('power_bi_reports_last_sync_reports');
$form['reports_last_sync'] = [
'#type' => 'markup',
'#prefix' => '<div>',
'#suffix' => '</div>',
];
if ($reports_last_sync) {
$form['reports_last_sync']['#markup'] = t('The reports were last synced at: !when', ['!when' => date('d/m/Y H:i', $reports_last_sync)]);
}
else {
$form['reports_last_sync']['#markup'] = t('The reports have never been synced.');
}
// Batch sync reports button.
$form['batch_sync_reports'] = [
'#type' => 'submit',
'#name' => 'batch_sync_reports',
'#value' => t('Batch sync reports'),
];
// Batch sync reports button.
$form['renew_embed_tokens'] = [
'#type' => 'submit',
'#name' => 'renew_embed_tokens',
'#value' => t('Renew embed tokens'),
];
// List of reports, groupped by group.
$form['groups_reports'] = [
'#type' => 'container',
'#prefix' => "<h2>" . t('Available reports') . "</h2>",
];
$local_reports = variable_get('power_bi_reports_data');
foreach ($local_reports as $group_id => $group_data) {
$group_element = _power_bi_reports_status_get_group_element($group_data);
$form['groups_reports'][] = $group_element;
}
// Reports form element.
// $form['reports'] = power_bi_reports_get_form_element($form, $form_state);
return $form;
}
function power_bi_reports_status_form_submit($form, &$form_state) {
$trigger = $form_state['triggering_element'];
if ($trigger["#name"] == 'batch_sync_reports') {
power_bi_reports_batch_sync_reports_init();
}
elseif ($trigger["#name"] == 'renew_embed_tokens') {
power_bi_reports_renew_embed_tokens_init();
}
elseif ($trigger["#name"] == 'token_renew') {
$token_renew = power_bi_reports_get_auth_token(TRUE);
if ($token_renew) {
drupal_set_message(t('The access token has successfully been renewed.'));
}
else {
drupal_set_message(t('There was a problem on the access token renewal.'), 'error');
}
}
}
function _power_bi_reports_status_get_group_element($group_data) {
$group_id = $group_data['id'];
$element[$group_id] = [
'#type' => 'fieldset',
'#title' => $group_data['name'],
'#description' => "Group ID: $group_id",
];
$group_reports = $group_data['reports'];
foreach ($group_reports as $report_data) {
$report_id = $report_data['id'];
$report_name = $report_data['name'];
$embed_token = power_bi_reports_get_local_report_embed_token($report_id, $check_expiration = FALSE);
$element[$group_id][$report_id] = [
'#type' => 'fieldset',
'#title' => $report_name,
'#description' => "Report ID: $report_id",
];
$element[$group_id][$report_id]['details'] = [
'#type' => 'markup',
];
if (!$embed_token) {
$msg = t('Possible report problem');
$element[$group_id][$report_id]['details']['#markup'] = '<div class="messages warning">' . $msg . '</div>';
$msg = t('There is not a locally cached embed token for this report. If the message persists after you renew the embed tokens, it might indicate that you don\'t have the necessary access to embed this report.');
$element[$group_id][$report_id]['details']['#markup'] .= '<em>' . $msg . '</em>';
}
}
return $element;
}