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.helpers.inc
582 lines (483 loc) · 15.9 KB
/
culturefeed.helpers.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* Load callback for a culturefeed user.
*/
function cf_user_load($uid) {
try {
return DrupalCultureFeed::getUser($uid);
}
catch (Exception $e) {
watchdog_exception('culturefeed', $e);
}
}
/**
* Load the logged in user
*/
function culturefeed_load_logged_in_user() {
try {
return DrupalCultureFeed::getLoggedInUser();
}
catch (Exception $e) {
watchdog_exception('culturefeed', $e);
}
}
/**
* Check if a user is a culturefeed user.
*/
function culturefeed_is_culturefeed_user($uid = NULL) {
return DrupalCultureFeed::isCultureFeedUser($uid);
}
/**
* Fetch a unique username for the given CultureFeed user id.
*/
function culturefeed_unique_username($cf_uid, $nick) {
if (!$nick) {
$suffix = substr($cf_uid, -4);
$nick = t('Anonymous') . '_' . $suffix;
}
$name = db_query("SELECT DISTINCT name FROM {culturefeed_user} cfu INNER JOIN {users} u ON u.uid = cfu.uid WHERE cfu.cf_uid = :cf_uid", array(':cf_uid' => $cf_uid))->fetchField();
if ($name) {
return $name;
}
$name = $nick;
$count = 0;
while ($name_exists = db_query("SELECT name FROM {users} WHERE LOWER(name) = :name", array(':name' => strtolower($name)))->fetchField()) {
$count++;
$name = $name . '_' . $count;
}
return $name;
}
/**
* Create a new Drupal user account based on a fake Culturefeed User object
* $object has only the properties [cf_uid, nick]
* @param CultureFeed_User $cf_account
* @return boolean|Ambigous <A, boolean, (optional), stdClass>
*/
function culturefeed_create_user($cf_account, $user = NULL) {
// Use a database transaction, as we need to handle what follows as a unit of work.
$txn = db_transaction();
try {
// If no CultureFeed user was passed, we can't create the user.
if (!$cf_account) {
return FALSE;
}
if (isset($user->uid) && $user->uid) {
// User is already Drupaluser, so map his Drupal and CultureFeed account
$uid = db_query("SELECT uid FROM {culturefeed_user} cfu WHERE cfu.uid = :uid", array(':uid' => $user->uid))->fetchField();
if ($user->uid != $uid) {
$account = $user;
}
}
else {
// Find a unique nick based on the given nick. In case the nick is not used yet, the passed nick will be used. Else the nick will be suffixed with an underscore and counter (example nick_12).
$unique_nick = culturefeed_unique_username($cf_account->id, $cf_account->nick);
// Create the user account.
$account = user_save(
drupal_anonymous_user(),
array(
'name' => $unique_nick,
'pass' => '', // We save an empty password (don't generate a random one) because this is much more performant.
'init' => $unique_nick,
'roles' => array(),
'status' => 1,
'timezone' => variable_get('date_default_timezone', date_default_timezone_get()),
'access' => REQUEST_TIME,
'mail' => '',
)
);
}
// Save the mapping between CultureFeed User ID and Drupal user id.
db_insert('culturefeed_user')
->fields(array(
'uid' => $account->uid,
'cf_uid' => $cf_account->id,
))
->execute();
// Save the fact that this is an external account for culturefeed.
user_set_authmaps($account, array('authname_culturefeed' => $account->name));
module_invoke_all('culturefeed_user_create', $account, $cf_account);
}
catch (Exception $e) {
$txn->rollback();
throw $e;
}
return $account;
}
function culturefeed_get_uid_for_cf_uid($cf_uid, $nick) {
$user = new stdClass();
$user->id = $cf_uid;
$user->nick = $nick;
$uids = culturefeed_get_uids_for_cf_uids(array($cf_uid => $user));
return reset($uids);
}
/**
* Get the Drupal user ids for a list of CultureFeed user ids.
*
* If a user account does not exist locally for a given user id, a new account is created for it.
*
* @param array $users
* @return array
* An associative array where the keys are the passed CultureFeed user ids.
* The values are the matching Drupal user id for each CultureFeed user id.
*/
function culturefeed_get_uids_for_cf_uids($users) {
$uids = array();
if (empty($users)) {
return array();
}
$result = db_query('SELECT cfu.cf_uid, u.uid FROM {culturefeed_user} cfu INNER JOIN {users} u ON u.uid = cfu.uid WHERE cfu.cf_uid IN (:cf_uids)', array(':cf_uids' => array_keys($users)));
foreach ($result as $record) {
$uids[$record->cf_uid] = $record->uid;
}
foreach ($users as $cf_uid => $fakeCulturefeedAccount) {
if (!isset($uids[$cf_uid])) {
$account = culturefeed_create_user($fakeCulturefeedAccount);
if ($account) {
$uids[$cf_uid] = $account->uid;
}
}
}
return $uids;
}
function culturefeed_get_uids_for_activities($activities) {
$ids = array();
foreach ($activities as $activity) {
$ids[$activity->userId] = new stdClass();
$ids[$activity->userId]->nick = $activity->nick;
$ids[$activity->userId]->id = $activity->userId;
}
return culturefeed_get_uids_for_cf_uids($ids);
}
/**
* Get the drupal uids for all given memberships.
*/
function culturefeed_get_uids_for_memberships($memberships) {
$ids = array();
foreach ($memberships as $membership) {
$ids[$membership->user->id] = new stdClass();
$ids[$membership->user->id]->nick = $membership->user->nick;
$ids[$membership->user->id]->id = $membership->user->id;
}
return culturefeed_get_uids_for_cf_uids($ids);
}
function culturefeed_get_uids_for_users($users) {
$ids = array();
foreach ($users as $user) {
$ids[$user->id] = new stdClass();
$ids[$user->id]->nick = $user->nick;
$ids[$user->id]->id = $user->id;
}
return culturefeed_get_uids_for_cf_uids($ids);
}
function culturefeed_get_consumer_shared_secret($application_key) {
foreach (module_implements('culturefeed_consumer_shared_secret') as $module) {
$shared_secret = module_invoke($module, 'culturefeed_consumer_shared_secret', $application_key);
if (isset($shared_secret)) {
return $shared_secret;
}
}
watchdog('culturefeed', 'The shared secret for Application key (@app_key) cannot be found!', array('@app_key' => $application_key), WATCHDOG_ERROR);
return NULL;
}
/**
*
* @param String $type
* @return Ambigous <string>|string
*/
function culturefeed_get_content_type($type) {
$mapping = array(
'content' => CultureFeed_Activity::CONTENT_TYPE_CONTENT,
'bibnet_book' => CultureFeed_Activity::CONTENT_TYPE_BOOK,
'actor' => CultureFeed_Activity::CONTENT_TYPE_ACTOR,
'event' => CultureFeed_Activity::CONTENT_TYPE_EVENT,
'production' => CultureFeed_Activity::CONTENT_TYPE_PRODUCTION,
'CultureFeed_Activity' => CultureFeed_Activity::CONTENT_TYPE_ACTIVITY,
);
if (isset($mapping[$type])) {
return $mapping[$type];
}
return CultureFeed_Activity::CONTENT_TYPE_NODE;
}
/**
* Returns a human readable string for the activity type.
*
* @param int $type
* The activity type.
*
* @return string
* The readable string
*/
function culturefeed_get_human_readable_activity_type($type) {
$types = array(
CultureFeed_Activity::TYPE_VIEW => t('view'),
CultureFeed_Activity::TYPE_DETAIL => t('detail'),
CultureFeed_Activity::TYPE_LIKE => t('like'),
CultureFeed_Activity::TYPE_MAIL => t('mail'),
CultureFeed_Activity::TYPE_PRINT => t('print'),
CultureFeed_Activity::TYPE_FACEBOOK => t('Facebook'),
CultureFeed_Activity::TYPE_TWITTER => t('Twitter'),
CultureFeed_Activity::TYPE_IK_GA => t('attend'),
CultureFeed_Activity::TYPE_TICKET => t('ticket'),
CultureFeed_Activity::TYPE_ROUTE => t('route'),
CultureFeed_Activity::TYPE_MORE_INFO => t('more info'),
CultureFeed_Activity::TYPE_UITPAS => t('uitpas checkin'),
CultureFeed_Activity::TYPE_REGULAR_CHECKIN => t('regular checkin'),
CultureFeed_Activity::TYPE_COMMENT => t('comment'),
CultureFeed_Activity::TYPE_RECOMMEND => t('recommend'),
CultureFeed_Activity::TYPE_FOLLOW => t('follow'),
CultureFeed_Activity::TYPE_PAGE_MEMBER => t('page member'),
CultureFeed_Activity::TYPE_PAGE_ADMIN => t('page admin'),
CultureFeed_Activity::TYPE_NEW_EVENT => t('new event'),
CultureFeed_Activity::TYPE_REVIEW => t('review'),
CultureFeed_Activity::TYPE_MEDIA_PHOTO => t('media photo'),
CultureFeed_Activity::TYPE_MEDIA_VIDEO => t('media video'),
CultureFeed_Activity::TYPE_PAGE_CREATED => t('page created'),
CultureFeed_Activity::TYPE_NEWS => t('news'),
CultureFeed_Activity::TYPE_CASHIN => t('cashin'),
);
return isset($types[$type]) ? $types[$type] : '';
}
/**
* Get the preferred language of current cf user.
*/
function culturefeed_get_preferred_language() {
$preferredLanguage = &drupal_static(__FUNCTION__, FALSE);
global $language;
if (!$preferredLanguage) {
// Defaults to site language.
$preferredLanguage = $language->language;
// Try catching the preferred language from the user.
try {
$cf_account = DrupalCultureFeed::getLoggedInUser();
if (!empty($cf_account->preferredLanguage)) {
$preferredLanguage = $cf_account->preferredLanguage;
}
}
catch (Exception $e) {
watchdog_exception('culturefeed_search', $e);
return $language->language;
}
}
return $preferredLanguage;
}
/**
* Check if we are connected to acceptance API.
*/
function culturefeed_acceptance_mode() {
// Get culturefeed_api_location
$location = variable_get('culturefeed_api_location');
if (strpos($location,'www') === FALSE) {
return TRUE;
}
// Check for test, acc or dev environments that are connected with production
else {
global $base_root;
$dev_ids = array('culturefeed', 'dev.', 'acc.', 'test.');
foreach($dev_ids as $id) {
if (stripos($base_root, $id) !== false) {
return TRUE;
}
}
return FALSE;
}
}
/**
* Set the "noindex, follow" meta tag when needed.
*/
function culturefeed_set_noindex_metatag() {
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'robots',
'content' => 'noindex, nofollow',
),
);
drupal_add_html_head($element, 'robots');
}
/**
* Store the page image in the drupal files temporary.
* @return file object or false if failed.
*/
function culturefeed_create_temporary_image($image, $destination) {
if (strpos($image, 'http') === false) {
if (isset($_SERVER['HTTPS'])) {
$image = 'https:' . $image;
}
else {
$image = 'http:' . $image;
}
}
$image_data = file_get_contents($image);
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
$uri = file_unmanaged_save_data($image_data, $destination . '/' . drupal_basename($image));
if (empty($uri)) {
return FALSE;
}
global $user;
$file = new stdClass();
$file->uri = $uri;
$file->filename = drupal_basename($uri);
$file->filemime = file_get_mimetype($file->uri);
$file->uid = $user->uid;
$file->status = FALSE;
return file_save($file);
}
/**
* Load a block through ajax.
*/
function culturefeed_ui_block_ajaxload($config) {
$block['subject'] = $config['title'];
$block['content'] = array(
'#markup' => '<div id="' . $config['id'] . '">' . t('Loading') . '</div>',
'#id' => $config['id'],
'#ajax' => array(
'event' => 'ajaxload',
'path' => $config['path'],
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'culturefeed') . '/js/culturefeed_ajaxload.js',
array('data' => array(), 'type' => 'setting'),
),
),
);
$block['content'] = ajax_pre_render_element($block['content']);
return $block;
}
/**
* Generate an image style url for an external image.
*/
function culturefeed_external_image_style($image_url, $image_style) {
global $base_url;
// Get the URL parts without protocol.
$pattern = '#^https?://#';
$curr_url_parts = explode('/', preg_replace($pattern, '', $base_url));
$image_url_parts = explode('/', preg_replace($pattern, '', $image_url));
$file_directory = variable_get('file_public_path', 'sites/default/files');
// If this file is original of current host, just use the original path.
if ($image_url_parts[0] === $curr_url_parts[0]) {
$local_uri = trim(str_replace($base_url . '/', '', $image_url), '/');
$local_uri = str_replace($file_directory . '/', 'public://', $local_uri);
}
// If the file is of another site, download it locally. This is needed to use imagecache.
else {
$file = new stdClass();
$file->filename = $image_url;
$validation_errors = file_validate_extensions($file, 'jpg jpeg png gif');
if ($validation_errors) {
return;
}
$name_parts = explode('/', $image_url);
$file_name = $name_parts[count($name_parts) - 1];
$local_uri = 'public://external/' . $file_name;
$directory = $file_directory . '/external';
// Not needed if the file already exists.
if (!file_exists($local_uri)) {
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
// Make sure we have a proper protocol in front of the URL.
$image_url_get = $image_url;
if (!preg_match($pattern, $image_url_get)) {
$image_url_get = 'http:' . $image_url_get;
}
$image_content = @file_get_contents($image_url_get);
if ($image_content) {
file_put_contents($directory . '/' . $file_name, $image_content);
}
else {
return;
}
}
}
return image_style_url($image_style, $local_uri);
}
/**
* Ajax command for a modal.
* This will check if culturefeed_bootstrap's modal command is available.
* If not, it will do a html insert.
*/
function culturefeed_ajax_command_modal($selector, $html) {
return array(
'command' => 'culturefeedModal',
'selector' => $selector,
'data' => $html,
);
}
/**
* Ajax command to reload current page.
*/
function culturefeed_ajax_command_goto($url) {
return array(
'command' => 'culturefeedGoto',
'url' => $url,
);
}
/*
f
*/
function culturefeed_share_link($share_url, $platform, $title) {
// Set prefix for share URL's.
$facebook['prefix'] = 'https://www.facebook.com/sharer/sharer.php?u=';
$twitter['prefix'] = 'https://twitter.com/intent/tweet?text=';
$whatsapp['prefix'] = 'whatsapp://send?text=';
$messenger['prefix'] = 'fb-messenger://share/?link=';
// Set UTM parameters for share URL's.
$utm['campaign'] = 'share';
$utm['medium'] = 'social';
$facebook['utm'] = array (
'utm_campaign' => $utm['campaign'],
'utm_medium' => $utm['medium'],
'utm_source' => 'facebook.com',
);
$twitter['utm'] = array (
'utm_campaign' => $utm['campaign'],
'utm_medium' => $utm['medium'],
'utm_source' => 'twitter.com',
);
$whatsapp['utm'] = array (
'utm_campaign' => $utm['campaign'],
'utm_medium' => $utm['medium'],
'utm_source' => 'whatsapp',
);
$mail['utm'] = array (
'utm_campaign' => $utm['campaign'],
'utm_medium' => 'email',
'utm_source' => 'forward-mail',
);
$messenger['utm'] = array (
'utm_campaign' => $utm['campaign'],
'utm_medium' => $utm['medium'],
'utm_source' => 'messenger',
);
switch($platform)
{
case 'facebook' :
return $facebook['prefix'] . urlencode(url($share_url, array('query' => $facebook['utm'])));
break;
case 'twitter' :
return $twitter['prefix'] . urlencode(url($share_url, array('query' => $twitter['utm'])));
break;
case 'whatsapp' :
return $whatsapp['prefix'] . urlencode(url($share_url, array('query' => $whatsapp['utm'])));
break;
case 'messenger' :
return $messenger['prefix'] . urlencode(url($share_url, array('query' => $messenger['utm'])));
break;
case 'mail' :
return 'mailto:?subject=' . $title . '&body=' . t('I thought this might interest you:') . '%0D%0A' . urlencode(url(current_path(), array('absolute' => TRUE, 'query' => $mail['utm'])));
break;
}
}
/**
* Known versions of cdb xml.
*
* @return array
*/
function culturefeed_cdb_xml_versions() {
$cdb_xml_versions = array(
'3.0',
'3.1',
'3.2',
'3.3',
);
return $cdb_xml_versions;
}