This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvitation_manager.module
303 lines (257 loc) · 8.95 KB
/
invitation_manager.module
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* @file
* Module gives statistics on different content types and their languages
*
*/
/* Implements hook_permission
*/
function invitation_manager_permission() {
return array(
'administer invitation manager' => array(
'title' => t('Manage Invitation Manager'),
'description' => t('Manage the Invitation Manager system. Pages where it is included and the configuration file.'),
),
);
}
/**
* Creates the settings page for the module
* Implements hook_menu
*/
function invitation_manager_menu() {
$items = array();
$tab_base_path = 'admin/structure/invitation_manager';
$items[$tab_base_path] = array(
'title' => t('Custom Invitation Manager'),
'description' => t('Manage the custom Invitation Manager'),
'page callback' => 'drupal_get_form',
'page arguments' => array('invitation_manager_surveys'),
'access callback' => 'user_access',
'access arguments' => array('administer invitation manager'),
'file' => 'invitation_manager.admin.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => 20,
);
$items[$tab_base_path.'/config'] = array(
'title' => t('Invitation Manager Surveys'),
'description' => t('Manage the custom Invitation Manager configuration (im.json)'),
'page callback' => 'drupal_get_form',
'page arguments' => array('invitation_manager_surveys'),
'access callback' => 'user_access',
'access arguments' => array('administer invitation manager'),
'file' => 'invitation_manager.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 10,
);
$items[$tab_base_path.'/admin'] = array(
'title' => t('Invitation Manager Settings'),
'description' => t('Manage the custom Invitation Manager'),
'page callback' => 'drupal_get_form',
'page arguments' => array('invitation_manager_admin'),
'access callback' => 'user_access',
'access arguments' => array('administer invitation manager'),
'file' => 'invitation_manager.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
return $items;
}
function invitation_manager_get_menu_path() {
global $language;
// If the module is turned off use the menu from the distribution
$enabled = variable_get('invitation_manager_enabled', 'on');
$token_on = variable_get('invitation_manager_token', 'on');
$token = intval(time() / 1000);
if ($enabled !== 'on') {
return FALSE;
}
$mode = variable_get('invitation_manager_mode', 'file');
$return = FALSE;
//$language->language
$file = 'public://invitation_manager/im.json';
$return = file_create_url($file);
$token = getlastmod($return);
if ($mode != 'file') {
// return the tracked URL
$return = url('/invitation_manager/im.json');
}
if ($token_on) {
$return .= "?t=$token";
}
return $return;
}
function invitation_manager_prep_library() {
$mode = variable_get('invitation_manager_filemode', 'local');
$aa_enabled = variable_get('invitation_manager_aa_enabled', 'on') == 'on';
$library_folder = 'public://invitation_manager';
if (!file_prepare_directory($library_folder, FILE_CREATE_DIRECTORY)) {
watchdog('invitation_manager', 'Failed to create invitation_manager directory: %dirpath', array('%dirpath' => $library_folder_directory), WATCHDOG_ERROR);
return FALSE;
}
// Write config.json
$library_path = drupal_realpath($library_folder) . '/';
//dpm($library_path, 'library_path');
$config_file = $library_path . 'config.JSON';
$imjson_file_path = file_create_url($library_folder . '/' . 'im.json');
$fp = fopen($config_file, 'w');
/*
$config = array(
'dbURL' => $imjson_file_path,
);
fwrite($fp, json_encode($config));
*/
$aa_tracking = '';
if ($aa_enabled) {
$aa_tracking = ',
"Adobe" : "no"';
}
fwrite($fp, <<< JSONDATA
{
"dbURL": "$imjson_file_path"$aa_tracking
}
JSONDATA
);
fclose($fp);
// Create the htaccess file
$htaccess_file = $library_path . '.htaccess';
$fp = fopen($htaccess_file, 'w');
fwrite($fp, <<< HTACCESSDATA
Header set Access-Control-Allow-Origin "*"
HTACCESSDATA
);
fclose($fp);
// Files to copy
$files = array(
//'config.JSON',
'Overlay.js',
'Overlay.css',
'InvitationManager.js',
);
if ($mode == 'remote') {
$remote_base = 'https://raw.githubusercontent.com/ServiceCanada/invitation-manager/master/src/';
}
else {
$remote_base = drupal_get_path('module', 'invitation_manager')."/library/src/";
//$remote_base = getcwd() . '/' . drupal_get_path('module', 'invitation_manager')."/library/src/";
}
foreach ($files as $file) {
$filepath = $library_path . $file;
if ($mode == 'remote') {
$source = $remote_base . $file;
$remote = drupal_http_request($source);
$data = $remote->data;
}
else {
// Local
$source = $remote_base . $file;
$data = file_get_contents($source);
}
if (function_exists('cloudfront_path_invalidate_invalidate_on_cloudfront')) {
cloudfront_path_invalidate_invalidate_on_cloudfront([
drupal_get_path('module', 'invitation_manager'). '/*',
]);
}
if ($file_saved = file_unmanaged_save_data($data, $filepath, FILE_EXISTS_REPLACE)) {
watchdog('invitation_manager', 'Saved file from: %source to file: %filename', array('%source' => $source, '%filename' => $file_saved));
}
}
}
function invitation_manager_get_default_imjson() {
$mode = variable_get('invitation_manager_filemode', 'remote');
if ($mode == 'remote') {
// Remote
$url = 'https://raw.githubusercontent.com/ServiceCanada/invitation-manager/master/docs/im.json';
$readfile = drupal_http_request($url);
$content = $readfile->data;
}
else {
// Local
$filepath = drupal_get_path('module', 'invitation_manager')."/library/docs/im.json";
$content = file_get_contents($filepath);
}
//dpm($filepath, $mode); dpm(array($content));
return $content;
}
/**
* Implements hook_init()
*
* @link https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_init/7.x
*/
function invitation_manager_init() {
global $theme;
$enabled = variable_get('invitation_manager_enabled', 'on') == 'on';
$visibility = variable_get('invitation_manager_visibility', BLOCK_VISIBILITY_NOTLISTED);
$pages = variable_get('invitation_manager_pages', '');
if (!$enabled) {
return;
}
// Per-role visibility.
$roles = variable_get('invitation_manager_roles', array());
global $user;
$allow_user = FALSE;
$blank = TRUE;
foreach ($roles as $rid => $active) {
if ($active) {
$blank = FALSE;
if (user_has_role($rid)) {
$allow_user = TRUE;
}
}
}
if ($blank) {
$allow_user = TRUE;
}
if ($allow_user === FALSE) {
return FALSE;
}
// By Page Visibility
// Limited visibility blocks must list at least one page.
if ($visibility == BLOCK_VISIBILITY_LISTED && empty($pages)) {
return FALSE;
}
$main_theme = FALSE;
$theme_default = variable_get('theme_default');
if ($theme === $theme_default) {
$main_theme = TRUE;
}
if (!$main_theme) {
return FALSE;
}
// Match path if necessary.
if (!empty($pages)) {
//dpm($visibility, 'visiblity');
//dpm($pages, 'pages');
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower($pages);
if ($visibility < BLOCK_VISIBILITY_PHP) {
// Convert the Drupal path to lowercase.
$path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
// When $visibility has a value of 0
// (BLOCK_VISIBILITY_NOTLISTED), the block is displayed on all pages
// except those listed in $pages. When set to 1
// (BLOCK_VISIBILITY_LISTED), it is displayed only on those pages
// listed in $pages.
$page_match = !($visibility xor $page_match);
}
else {
$page_match = FALSE;
}
}
else {
$page_match = TRUE;
}
if (!$page_match) {
return FALSE;
}
//drupal_set_message('INCLUDING INVITATION MANAGER ON PAGE', 'warning');
$library_folder = 'public://invitation_manager';
drupal_add_css($library_folder . '/' . 'Overlay.css', array('group' => CSS_DEFAULT, 'every_page' => TRUE));
drupal_add_js(file_create_url($library_folder . '/' . 'Overlay.js'), array('type' => 'file', 'preprocess' => FALSE, 'scope' => 'footer', 'weight' => 5, 'group' => JS_LIBRARY, 'every_page' => TRUE, 'defer' => TRUE));
drupal_add_js(file_create_url($library_folder . '/' . 'InvitationManager.js'), array('type' => 'file', 'preprocess' => FALSE, 'scope' => 'footer', 'weight' => 6, 'group' => JS_LIBRARY, 'every_page' => TRUE, 'defer' => TRUE));
}