This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
eventtimezone.php
311 lines (284 loc) · 10.4 KB
/
eventtimezone.php
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
<?php
require_once 'eventtimezone.civix.php';
use CRM_Eventtimezone_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function eventtimezone_civicrm_config(&$config) {
_eventtimezone_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function eventtimezone_civicrm_xmlMenu(&$files) {
_eventtimezone_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function eventtimezone_civicrm_install() {
_eventtimezone_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function eventtimezone_civicrm_postInstall() {
_eventtimezone_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function eventtimezone_civicrm_uninstall() {
_eventtimezone_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function eventtimezone_civicrm_enable() {
_eventtimezone_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function eventtimezone_civicrm_disable() {
_eventtimezone_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function eventtimezone_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _eventtimezone_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function eventtimezone_civicrm_managed(&$entities) {
_eventtimezone_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types.
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function eventtimezone_civicrm_caseTypes(&$caseTypes) {
_eventtimezone_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
*/
function eventtimezone_civicrm_angularModules(&$angularModules) {
_eventtimezone_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function eventtimezone_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_eventtimezone_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_postProcess().
*/
function eventtimezone_civicrm_postProcess($formName, &$form) {
if ($formName == 'CRM_Event_Form_ManageEvent_EventInfo') {
$submit = $form->getVar('_submitValues');
$timezone = $submit['timezone'];
if (empty($form->_id) && !empty($submit['timezone'])) {
$result = civicrm_api3('Event', 'get', array(
'sequential' => 1,
'return' => array("id"),
'title' => $submit['title'],
'event_type_id' => $submit['event_type_id'],
'default_role_id' => $submit['default_role_id'],
));
if ($result['count'] == 1) {
$event_id = $result['values'][0]['id'];
$query = "
UPDATE civicrm_event
SET timezone = '$timezone'
WHERE id = $event_id
";
CRM_Core_DAO::executeQuery($query);
}
}
else {
$event_id = $form->_id;
$query = "
UPDATE civicrm_event
SET timezone = '$timezone'
WHERE id = $event_id
";
CRM_Core_DAO::executeQuery($query);
}
}
}
/**
* Implements hook_civicrm_apiWrappers().
*/
function eventtimezone_civicrm_entityTypes(&$entityTypes) {
$entityTypes['CRM_Event_DAO_Event']['fields_callback'][]
= function ($class, &$fields) {
$fields['timezone'] = array(
'name' => 'timezone',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Timezone') ,
'description' => 'Event Timezone',
'table_name' => 'civicrm_event',
'entity' => 'Event',
'bao' => 'CRM_Event_BAO_Event',
'localizable' => 0,
);
};
}
/**
* Implements hook_civicrm_alterContent().
*/
function eventtimezone_civicrm_alterContent( &$content, $context, $tplName, &$object ) {
$eventInfoFormContext = ($context == 'form' && $tplName == 'CRM/Event/Form/ManageEvent/EventInfo.tpl');
$eventInfoPageContext = ($context == 'page' && $tplName == 'CRM/Event/Page/EventInfo.tpl');
$eventConfirmFormContext = ($context == 'form' && $tplName == 'CRM/Event/Form/Registration/Confirm.tpl');
$eventConfirmPageContext = ($context == 'form' && $tplName == 'CRM/Event/Form/Registration/ThankYou.tpl');
if ($eventInfoFormContext || $eventInfoPageContext) {
$result = civicrm_api3('Event', 'get', array(
'sequential' => 1,
'return' => array("timezone"),
'id' => $object->_id,
));
if(isset($result['values'][0])){
$timezone = $result['values'][0]['timezone'];
}
if($eventInfoPageContext && $timezone != '_none' && !empty($timezone)) {
// Add timezone besides the date data
$content = str_replace("</abbr>", " " . $timezone . " </abbr>", $content);
} elseif ($eventInfoFormContext) {
$timezone_identifiers = DateTimeZone::listIdentifiers();
$timezone_field = '<tr class="crm-event-manage-eventinfo-form-block-timezone">
<td class="label"><label for="timezone">Timezone</label></td>
<td>
<select name="timezone" id="timezone" class="crm-form-select">
<option value="_none">Select Timezone</option>';
foreach ($timezone_identifiers as $key => $value) {
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($value));
$timezone_db = $dateTime->format('T');
$timezone_field .= '<option value="' . $timezone_db . '">' . $value . '</option>';
}
$timezone_field .= '</select>
</td>
</tr>
<tr class="crm-event-manage-eventinfo-form-block-max_participants">';
$content = str_replace('<tr class="crm-event-manage-eventinfo-form-block-max_participants">', $timezone_field, $content);
}
}
elseif ($eventConfirmFormContext || $eventConfirmPageContext) {
$result = $result = civicrm_api3('Event', 'get', array(
'sequential' => 1,
'return' => array("start_date","end_date", "timezone"),
'id' => $object->_eventId,
));
$event_start_date = $result['values'][0]['event_start_date'];
$event_end_date = $result['values'][0]['event_end_date'];
$timezone = $result['values'][0]['timezone'];
$start_date_con = new DateTime($event_start_date);
$start_date_st = date_format($start_date_con, 'F jS, Y g:iA');
$start_date = date_format($start_date_con, 'F jS');
$end_date_con = new DateTime($event_end_date);
$end_date_st = date_format($end_date_con, 'F jS, Y g:iA');
$end_date = date_format($end_date_con, 'F jS');
$end_date_time = new DateTime($event_end_date);
$end_time = date_format($end_date_time, 'g:iA');
if($timezone != '_none' && !empty($timezone && !empty($event_end_date))) {
// Add timezone besides the date data
if ($start_date == $end_date) {
$replacement = "<td width='90%'>" . $start_date_st . " " . $timezone . " through " . $end_time . " " . $timezone . "</td>";
$content = preg_replace('#(<td width="90%">)(.*?)(</td>)#si', $replacement, $content);
}
else {
$replacement = "<td width='90%'>" . $start_date_st . " " . $timezone . " through " . $end_date_st . " " . $timezone . "</td>";
$content = preg_replace('#(<td width="90%">)(.*?)(</td>)#si', $replacement, $content);
}
}
elseif (empty($event_end_date)) {
$replacement = "<td width='90%'>" . $start_date_st . " " . $timezone . "</td>";
$content = preg_replace('#(<td width="90%">)(.*?)(</td>)#si', $replacement, $content);
}
}
}
/**
* Implements hook_civicrm_tokens().
*/
function eventtimezone_civicrm_tokens( &$tokens ) {
$tokens['timezone'] = array(
'timezone.start_date_timezone' => ts('Event Start Date with timezone'),
'timezone.end_date_timezone' => ts('Event End Date with timezone'),
);
}
function eventtimezone_civicrm_tokenValues(&$values, &$cids, $job = null, $tokens = array(), $context = null) {
if(empty($tokens['timezone'])) {
return;
}
if (!is_array($cids)) {
return;
}
foreach ($cids as $cidkey => $cidvalue) {
$result = civicrm_api3('Participant', 'get', [
'sequential' => 1,
'return' => ["event_id"],
'contact_id' => $cidvalue,
]);
foreach($result['values'] as $resultvalue) {
$event_result = civicrm_api3('Event', 'get', [
'sequential' => 1,
'return' => ["timezone", "start_date", "end_date"],
'id' => $resultvalue['event_id'],
]);
}
if(!empty($event_result['values'])){
$timeZone = $event_result['values'][0]['timezone'];
// Set default site timezone if event timezone field is not set.
if ($event_result['values'][0]['timezone'] == '_none') {
$timeZone = date_default_timezone_get();
}
$startDateTimestamp = new DateTime($event_result['values'][0]['event_start_date'], new DateTimeZone($timeZone));
$startDateTimezoneFormat = date_format($startDateTimestamp, 'M jS Y g:iA T');
$endDateTimestamp = new DateTime($event_result['values'][0]['event_end_date'], new DateTimeZone($timeZone));
$endDateTimezoneFormat = date_format($endDateTimestamp, 'M jS Y g:iA T');
// Set format for start & end date timezone tokens.
$values[$cidvalue]['timezone.start_date_timezone'] = $startDateTimezoneFormat;
$values[$cidvalue]['timezone.end_date_timezone'] = $endDateTimezoneFormat;
}
}
}