forked from digitalutsc/drupal_libcal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libcal.module
179 lines (157 loc) · 4.52 KB
/
libcal.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
<?php
use Drupal\Core\Entity\EntityInterface;
/**
* @file
* Contains libcal.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
function libcal_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'libcal/libcal';
}
/**
* Implements hook_help().
*/
function libcal_help($route_name, RouteMatchInterface $route_match)
{
switch ($route_name) {
// Main module help for the libcal module.
case 'help.page.libcal':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module is integrate Libcal API and feed to drupal') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function libcal_theme()
{
return [
'libcal' => [
'render element' => 'children',
],
];
}
/**
* Implement hook_cron
*/
function libcal_cron()
{
startDownloadLibCalEvents();
}
/**
* Trigger download Events process
*/
function startDownloadLibCalEvents() {
$config = \Drupal::config('libcal.libcalapiconfig');
$service = \Drupal::service('libcal.download');
// set all event exist to pasted then turn on any event downlaodeable from Libcal will be turn off
$service->updatePastFieldEventNode(true);
// Download events regards to the calenedar id(s)
if (!empty($config->get('calendar-id')) || $config->get('tags') != -1 ) {
$ids = $config->get('calendar-id');
if(strpos($ids, ',') !== false ) {
$calendar_ids = explode(',', $ids);
foreach ($calendar_ids as $cid) {
$result = $service->get("events?cal_id=". $cid)->events;
$service->libcalEventToNode($result);
}
}else {
$result = $service->get("events?cal_id=". $ids)->events;
$service->libcalEventToNode($result);
}
}
// Download events regards to tag id(s)
if (!empty($config->get('tags')) || $config->get('tags') != -1 ) {
$tags = $config->get('tags');
if(strpos($tags, ',') !== false ) {
$tag_ids = explode(',', $tags);
foreach ($tag_ids as $tid) {
$result = $service->get("event_search?search=*&tag=$tid&limit=100")->events;
$service->libcalEventToNode($result);
}
}else {
$result =$service->get("event_search?search=*&tag=$tags&limit=100")->events;
$service->libcalEventToNode($result);
}
}
}
/**
* Replace space with special charcater
* @param $str
* @param string $delimiter
* @return string
*/
function createSlug($str, $delimiter = '-')
{
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}
/**
* Generate Path Alias for Event node only
* @param $node
*/
function generateEventAlias($node)
{
$tag = "/events/" . createSlug($node->title->value);
if (!\Drupal::service('path_alias.repository')->lookupByAlias($tag, 'en')) {
//$path = \Drupal::service('path_alias.repository')->save("/node/" . $node->id(), $tag, "en");
$path_alias = \Drupal::entityTypeManager()->getStorage('path_alias')->create([
'path' => "/node/" . $node->id(),
'alias' => $tag,
'langcode' => "en",
]);
$path_alias->save();
}
}
/**
* Implements hook_insert().
*/
function libcal_entity_insert(EntityInterface $node)
{
// Set the URL alias
//if (get_class($node) == 'Drupal\node\Entity\Node') {
if ($node->getEntityType()->id() == 'node' && in_array($node->getType(), ['event'])) {
generateEventAlias($node);
// trigger email sending action.
$action = \Drupal::entityTypeManager()
->getStorage('action')
->load('send_email');
if ($action) {
$action->execute([$node]);
}
}
}
function libcal_entity_update(EntityInterface $node)
{
// Set the URL alias
//if (get_class($node) == 'Drupal\node\Entity\Node') {
if ($node->getEntityType()->id() == 'node' && in_array($node->getType(), ['event'])) {
generateEventAlias($node);
}
}
/**
* Debug function: display any variable to error log
*
* @param $thing
*/
if (!function_exists('logging')) {
function print_log($thing)
{
error_log(print_r($thing, true), 0);
}
}
/**
* Debug function: display any variable to current webpage
* @param $thing
*/
if (!function_exists('logging')) {
function logging($thing)
{
echo "<pre>";
print_r($thing);
echo "</pre>";
}
}