This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
culturefeed.module
329 lines (282 loc) · 10.7 KB
/
culturefeed.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
require_once('culturefeed.helpers.inc');
define('CULTUREFEED_CACHE_DISABLED', 0);
define('CULTUREFEED_CACHE_ENABLED', 1);
define('CULTUREFEED_CACHE_EXPIRES', 3600);
define('CULTUREFEED_API_LOCATION', 'http://acc.uitid.be/uitid/rest/');
define('CULTUREFEED_ENTRY_API_PATH', 'entry/test.rest.uitdatabank.be/api/v2/');
define('CULTUREFEED_GOOGLE_MAPS_JS_API', 'https://maps.googleapis.com/maps/api/js?sensor=true&v=3.5®ion=BE&hl=nl&language=nl');
define('CULTUREFEED_IMAGES_MAX_SIZE', 2097152);
define('CULTUREFEED_CDB_DEFAULT_VERSION', '3.2');
define('CULTUREFEED_DEFAULT_COMPOSER_LOCK_PATH', '.');
/**
* Implements hook_init().
*/
function culturefeed_init() {
drupal_add_js(drupal_get_path('module', 'culturefeed') . '/js/culturefeed.js');
if (isset($_GET['closepopup'])) {
drupal_add_js('try { window.opener.location.reload(); } catch (err) { } window.close();', 'inline');
}
drupal_add_js(array('culturefeed' => array('isCultureFeedUser' => DrupalCultureFeed::isCultureFeedUser())), 'setting');
// Set noindex metatag when API location points to acceptance.
if (culturefeed_acceptance_mode()) {
culturefeed_set_noindex_metatag();
}
}
/**
* Implements hook_theme().
*/
function culturefeed_theme() {
return array(
'culturefeed_authenticated_page' => array(
'variables' => array(),
'path' => drupal_get_path('module', 'culturefeed') . '/theme',
'template' => 'culturefeed-authenticated-page',
'file' => 'theme.inc',
),
);
}
/**
* Implements hook_menu().
*/
function culturefeed_menu() {
$items['admin/config/culturefeed'] = array(
'title' => 'CultureFeed',
'description' => 'Settings related to CultureFeed.',
'position' => 'right',
'weight' => 0,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/culturefeed/api-settings'] = array(
'title' => 'CultureFeed',
'description' => 'Change CultureFeed API setting like API Appliction key, location, ...',
'page callback' => 'drupal_get_form',
'page arguments' => array('culturefeed_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'culturefeed.admin.inc',
);
$items['culturefeed/oauth/connect'] = array(
'title' => 'Connect',
'page callback' => 'culturefeed_oauth_connect',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'culturefeed.pages.inc',
);
$items['culturefeed/oauth/connect/register'] = array(
'title' => 'Connect',
'page callback' => 'culturefeed_oauth_connect',
'page arguments' => array(CultureFeed::AUTHORIZE_TYPE_REGISTER),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'culturefeed.pages.inc',
);
$items['culturefeed/oauth/authorize'] = array(
'title' => 'Connect',
'page callback' => 'culturefeed_oauth_authorize',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'culturefeed.pages.inc',
);
$items['authenticated'] = array(
'title' => 'Log in',
'page callback' => 'culturefeed_authenticated_action_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'culturefeed.pages.inc',
);
return $items;
}
/**
* Implements hook_flush_caches().
*/
function culturefeed_flush_caches() {
return array('cache_culturefeed');
}
/**
* Implements hook_user_load().
*/
function culturefeed_user_load($users) {
$result = db_query('SELECT cfu.uid, cfu.cf_uid, cft.application_key, cft.token, cft.secret FROM {culturefeed_user} cfu LEFT JOIN {culturefeed_token} cft ON cft.cf_uid = cfu.cf_uid WHERE cfu.uid IN (:uids)', array(':uids' => array_keys($users)));
foreach ($result as $record) {
$users[$record->uid]->culturefeed_uid = $record->cf_uid;
if (!isset($users[$record->uid]->tokens)) {
$users[$record->uid]->tokens = array();
}
if ($record->token) {
$users[$record->uid]->tokens[$record->application_key] = new stdClass();
$users[$record->uid]->tokens[$record->application_key]->token = $record->token;
$users[$record->uid]->tokens[$record->application_key]->secret = $record->secret;
}
}
}
/**
* Implements hook_user_delete().
*/
function culturefeed_user_delete($account) {
$culturefeed_uid = db_query("SELECT cf_uid FROM {culturefeed_user} WHERE uid = :uid", array(':uid' => $account->uid))->fetchField();
db_delete('culturefeed_user')
->condition('uid', $account->uid)
->execute();
db_delete('culturefeed_token')
->condition('cf_uid', $culturefeed_uid)
->execute();
}
/**
* Implements hook_form_alter().
*/
function culturefeed_form_system_performance_settings_alter(&$form, $form_state) {
// We want our stuff before the clear cache fieldset and button.
$form['buttons']['#weight'] = 3;
$form['clear_cache']['#weight'] = 2;
// Adding API cache settings to the performance settings form.
$form['culturefeed_cache'] = array(
'#type' => 'fieldset',
'#title' => t('CultureFeed cache'),
'#weight' => 1,
'#description' => t('Enabling the CultureFeed cache will cache some parsed results of requests to the CultureFeed API. This will reduce the number of requests made directly to the API service.'),
);
$form['culturefeed_cache']['culturefeed_cache_status'] = array(
'#type' => 'checkbox',
'#title' => t('Cache API requests'),
'#default_value' => variable_get('culturefeed_cache_status', CULTUREFEED_CACHE_DISABLED),
);
}
/**
* Process function to expand the file element type with an image preview.
*/
function culturefeed_image_file_process($element, &$form_state, $form) {
$fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0;
if ($fid && $element['#file']) {
$element['filename'] = array(
'#type' => 'markup',
'#markup' => theme('image_style', array('style_name' => 'thumbnail', 'path' => $element['#file']->uri)),
'#weight' => -10,
);
}
return $element;
}
/**
* Implements hook_page_build().
* Please don't remove this. It will be used for user behavior tracking.
* Some day your client will benefit from our aggregated insights & benchmarks too.
* See https://github.com/cultuurnet/culturefeed/wiki/Culturefeed-tracking
* Thanks!
*/
function culturefeed_page_build(&$page) {
// Add Google Tag Manager
$container_id = variable_get('culturefeed_gtm_container_id', 'GTM-WPZSG2');
// Set up global variables.
$gtm_globals = array();
// Check if it is a search page.
if (module_exists('culturefeed_search_ui')) {
$agenda_search_paths = array();
$types = culturefeed_get_searchable_types();
foreach ($types as $key => $type) {
if (isset($type['class_name']) && $type['class_name'] == 'CultureFeedAgendaPage' && isset($type['path'])) {
$agenda_search_paths[$type['path']] = $key;
}
}
$gtm_globals['is_agenda_search'] = in_array(current_path(), array_keys($agenda_search_paths));
}
else {
$gtm_globals['is_agenda_search'] = FALSE;
}
if ($gtm_globals['is_agenda_search']) {
$gtm_globals['search_query'] = json_encode(drupal_get_query_parameters());
}
// Check if it is a detail page.
$gtm_globals['is_detail'] = (arg(0) == 'agenda' && in_array(arg(1), array('e', 'a', 'p', 'g')));
if ($gtm_globals['is_detail']) {
// Make this backwards compatible
$gtm_globals['is_event_detail'] = TRUE;
if (arg(1) == 'e') {
$event = menu_get_object('culturefeed_agenda_event', 3);
if ($event) {
$gtm_globals['detail_title'] = culturefeed_agenda_detail_title($event);
$gtm_globals['detail_type'] = 'event';
$gtm_globals['cdbid'] = arg(3);
}
}
else if (arg(1) == 'a') {
$actor = menu_get_object('culturefeed_agenda_actor', 3);
if ($actor) {
$gtm_globals['detail_title'] = culturefeed_agenda_detail_title($actor);
$gtm_globals['detail_type'] = 'actor';
$gtm_globals['cdbid'] = arg(3);
}
}
else if (arg(1) == 'p') {
$production = menu_get_object('culturefeed_agenda_production', 3);
if ($production) {
$gtm_globals['detail_title'] = culturefeed_agenda_detail_title($production);
$gtm_globals['detail_type'] = 'production';
$gtm_globals['cdbid'] = arg(3);
}
}
else if (arg(1) == 'g') {
$cfpage = menu_get_object('culturefeed_pages_page', 3);
if ($cfpage) {
$gtm_globals['detail_title'] = culturefeed_pages_detail_title($cfpage);
$gtm_globals['detail_type'] = 'page';
$gtm_globals['cdbid'] = arg(3);
}
}
}
// Add custom dimensions.
$gtm_globals['consumer_key'] = variable_get('culturefeed_api_application_key', '');
$gtm_globals['consumer_name'] = variable_get('site_name', '');
$gtm_globals['login_status'] = user_is_logged_in();
$gtm_globals['user_id'] = '';
if (DrupalCultureFeed::isCultureFeedUser()) {
$gtm_globals['user_id'] = DrupalCultureFeed::getLoggedInUserId();
}
// Set the script code.
// Add global variables.
$snippet = '<script>';
$snippet .= 'dataLayer = [' . drupal_json_encode($gtm_globals) . '];';
$snippet .= '</script>';
// GTM Code.
$snippet .= '<!-- Google Tag Manager -->';
$snippet .= '<noscript><iframe src="//www.googletagmanager.com/ns.html?id=' . $container_id . '"';
$snippet .= 'height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>';
$snippet .= '<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({\'gtm.start\':';
$snippet .= 'new Date().getTime(),event:\'gtm.js\'});var f=d.getElementsByTagName(s)[0],';
$snippet .= 'j=d.createElement(s),dl=l!=\'dataLayer\'?\'&l=\'+l:\'\';j.async=true;j.src=';
$snippet .= '\'//www.googletagmanager.com/gtm.js?id=\'+i+dl;f.parentNode.insertBefore(j,f);';
$snippet .= '})(window,document,\'script\',\'dataLayer\',\'' . $container_id . '\');</script>';
$snippet .= '<!-- End Google Tag Manager -->';
$page['page_top']['google_tag_manager'] = array(
'#markup' => $snippet,
);
}
/**
* Prepares an object to upload with CURL, based on a Drupal managed file.
*
* @param object $file
* A Drupal managed file object.
*
* @return CultureFeed_FileUpload|CURLFile
* A CURLFile on PHP 5.5 +, or a CultureFeed_FileUpload object on older PHP
* versions.
*/
function culturefeed_prepare_curl_upload_from_file($file) {
// Make use of PHPs CURLFile when it's available (>= PHP 5.5).
if (class_exists('CURLFile')) {
$file_upload = new CURLFile(
drupal_realpath($file->uri),
$file->filemime,
$file->filename
);
}
else {
$file_upload = new CultureFeed_FileUpload(
drupal_realpath($file->uri),
$file->filemime,
$file->filename
);
}
return $file_upload;
}