-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcu_rave_alerts.module
99 lines (93 loc) · 3.6 KB
/
cu_rave_alerts.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
<?php
/**
* @file
* Module file for cu_rave_alerts.
*/
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Implements hook_preprocess_block().
*/
function cu_rave_alerts_preprocess_block(&$variables) {
if ($variables['plugin_id'] == 'block_content:2bc8be86-ad95-4eee-93da-62edd00095a2') {
$variables['#cache']['max-age'] = 0;
}
}
/**
* Implements hook_cron().
*/
function cu_rave_alerts_cron() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.getrave.com/rss/cuboulder/channel1');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "SomeUserAgent");
curl_setopt($ch, CURLOPT_COOKIE, 'AspxAutoDetectCookieSupport=1');
$output = curl_exec($ch);
if ($output === FALSE) {
$error = curl_error($ch);
\Drupal::logger('cu_rave_alerts')->error($error);
} else {
// Convert xml string into an object
$xml = simplexml_load_string($output);
// Convert into json
$json = json_encode($xml);
// Convert into associative array
$rave = json_decode($json, true);
$title = $rave['channel']['item']['title'];
$pub_date = strtotime($rave['channel']['item']['pubDate']);
$rave_clear_status = \Drupal::state()->get('cu_rave_alerts.clear_status');
$rave_pub_date = \Drupal::state()->get('cu_rave_alerts.pub_date');
// Don't run unless one of the two cases below or the publish date has
// changed, which is checked in the if statement.
$run = 0;
// Run in the case that the clear has been set, but doesn't reflect local
// state.
if ((preg_match('/\[CLEAR\]/i', $title, $matches)) && ($rave_clear_status == FALSE)) {
$run = 1;
}
// Run in the case that clear is not set on the title and it is set to
// clear locally.
if ((!preg_match('/\[CLEAR\]/i', $title, $matches)) && ($rave_clear_status == TRUE)) {
$run = 1;
}
if (($run) || ($pub_date > $rave_pub_date)) {
// Update text in news subscription block.
$query = \Drupal::database()->select('block_content_field_data', 'bcb');
$query->fields('bcb', ['id']);
$query->condition('bcb.info', 'CU Rave Alerts');
$results = $query->execute();
$results = $results->fetch();
$rave_block_id = $results->id;
$block = \Drupal::service('entity_type.manager')->getStorage('block_content');
$bl = $block->load($rave_block_id);
if (preg_match('/\[CLEAR\]/i', $title, $matches)) {
$bl->set('body', '');
// Clear updates pub date
\Drupal::state()->set('cu_rave_alerts.pub_date', $pub_date);
// Bring inline with the clear status
\Drupal::state()->set('cu_rave_alerts.clear_status', TRUE);
} else {
$description = $rave['channel']['item']['description'];
$rave_link = $rave['channel']['item']['guid'];
if (!empty($rave_link)) {
$url = Url::fromUri($rave_link);
} else {
$url = Url::fromUri('https://alerts.colorado.edu/');
}
$link = Link::fromTextAndUrl(t('Read More'), $url);
$link_to_thing = $link->toString();
$bl->set('body', "<div class='banner banner--oit-cu-rave-alerts'>[svg name=exclamation width=20 color=f2f2f2][/svg] $description $link_to_thing</div>");
// Set new pub date
\Drupal::state()->set('cu_rave_alerts.pub_date', $pub_date);
// Bring inline with the clear status
\Drupal::state()->set('cu_rave_alerts.clear_status', FALSE);
}
$bl->body->format = 'rich_text';
$bl->save();
}
}
curl_close($ch);
}