-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathding_event.module
232 lines (207 loc) · 6.38 KB
/
ding_event.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
<?php
/**
* @file
* Code for the Ding event feature.
*/
/**
* Implements hook_ding_library_menu_links().
*/
function ding_event_ding_library_menu_links() {
return array(
'events' => array(
'title' => 'Events',
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function ding_event_field_formatter_info() {
return array(
'date_time_seperate_lines' => array(
'label' => t('Date and time on seperate lines'),
'field types' => array('datetime'),
'settings' => array(),
),
'ding_event_date' => array(
'label' => t('Ding event date'),
'field types' => array('datetime'),
'settings' => array(),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function ding_event_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$formatter = $display['type'];
$vars = array(
'timezone' => NULL,
'dates' => NULL,
'attributes' => array(),
);
switch ($formatter) {
case 'date_time_seperate_lines':
foreach ($items as $delta => $item) {
$vars['dates'] = date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
$element[$delta] = array('#markup' => theme('date_time_seperate_lines', $vars));
}
break;
case 'ding_event_date':
foreach ($items as $delta => $item) {
$format = 'ding_event';
if (substr($item['value'], -8) == '23:00:00') {
$format = 'ding_event_all_day';
}
$date = new DateTime(
$item['value'],
new DateTimeZone($item['timezone_db'])
);
$ts = $date->getTimestamp();
$str = format_date($ts, $format, '', $item['timezone']);
if ($item['value'] != $item['value2']) {
$date = new DateTime(
$item['value2'],
new DateTimeZone($item['timezone_db'])
);
$ts = $date->getTimestamp();
$str .= ' - ' . format_date($ts, $format, '', $item['timezone']);
}
$element[$delta] = array('#markup' => $str);
}
break;
}
return $element;
}
/**
* Implements hook_theme().
*/
function ding_event_theme() {
return array(
'date_time_seperate_lines' => array(
'variables' => array(
'variables' => array(
'timezone' => NULL,
'dates' => NULL,
'attributes' => NULL,
),
),
),
);
}
/**
* Theme function which shows two lines with date and time.
*
* Used on ding_events.
*/
function theme_date_time_seperate_lines($vars) {
if (isset($vars['dates'])) {
$dates = $vars['dates'];
$time = $dates['value']['formatted_time'];
$date = format_date(strtotime($dates['value']['formatted_iso']), 'date_only');
return '<div class="time field-label-inline clearfix"><div class="field-label">' . t('Time:') . '</div>' .
'<div class="field-items">' . $time . '</div></div>' .
'<div class="date field-label-inline clearfix"><div class="field-label">' . t('Date:') . '</div>' .
'<div class="field-items">' . $date . '</div></div>';
}
return FALSE;
}
/**
* Implements hook_form_FORMID_alter().
*
* Adds extra validation to event edit form.
*/
function ding_event_form_ding_event_node_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'ding_event_node_form_validate';
}
/**
* Additional validation of ding event edit form.
*/
function ding_event_node_form_validate($form, &$form_state) {
$lng = $form_state['values']['language'];
$fee = $form_state['values']['field_event_fee'][$lng][0]['value'];
if (!is_numeric($fee) || intval($fee) < 0) {
form_set_error('field_event_fee', t('Fee should be 0 or any positive number'));
}
}
/**
* Check if page type of ding event
*/
function _ding_event_check_page() {
$item = menu_get_item();
if (empty($item['map'][1]->type) && $item['map'][1]->type != 'ding_event') {
return FALSE;
}
return TRUE;
}
/**
* Implements hook_page_alter().
*
* Adds a microdata wrapper for event page.
*/
function ding_event_page_alter(&$page) {
if (_ding_event_check_page() === FALSE) {
return;
}
$prefix = '<div itemscope itemtype="http://data-vocabulary.org/Event">';
$prefix .= '<span itemprop="location" itemscope itemtype="http://data-vocabulary.org/Organization">';
$prefix .= '<span itemprop="name">' . variable_get('site_name') . '</span></span>';
$page['content']['system_main']['#prefix'] = $prefix;
$page['content']['system_main']['#suffix'] = '</div>';
}
/**
* Implements template_preprocess_field().
*/
function ding_event_preprocess_field(&$variables, $hook) {
if (_ding_event_check_page() === FALSE) {
return;
}
$element = &$variables['element'];
if ($element['#field_name'] == 'field_main_image') {
$variables['items'][0]['#item']['attributes']['itemprop'] = 'photo';
}
if ($element['#field_name'] == 'field_ding_body') {
$variables['attributes_array']['itemprop'] = 'description';
}
if ($element['#field_name'] == 'field_event_date') {
$shown = &drupal_static('ding_event_dates');
if (empty($shown)) {
$shown = array('start' => FALSE, 'end' => FALSE);
}
// Set start date for first field_event_date.
if (!$shown['start']) {
$date = new DateTime(
$element['#items'][0]['value'],
new DateTimeZone($element['#items'][0]['timezone'])
);
$variables['items'][0]['#markup'] .=
'<meta itemprop="startDate"'
. ' datetime="' . $date->format(DateTime::ISO8601) . '"'
. ' content="' . $date->format('D, d M Y H:i') . '">';
$shown['start'] = TRUE;
}
// Set end date for second field_event_date.
elseif (!$shown['end']) {
$date = new DateTime(
$element['#items'][0]['value'],
new DateTimeZone($element['#items'][0]['timezone'])
);
$variables['items'][0]['#markup'] .=
'<meta itemprop="endDate"'
. ' datetime="' . $date->format(DateTime::ISO8601) . '"'
. ' content="' . $date->format('D, d M Y H:i') . '">';
$shown['end'] = TRUE;
}
}
}
/**
* Implements template_preprocess_views_view_fields().
*/
function ding_event_preprocess_views_view_field(&$variables) {
if ($variables['field']->field === 'title' && _ding_event_check_page()) {
$output = '<span itemprop="summary">' . $variables['output'] . '</span>';
$variables['output'] = $output;
}
}
include_once 'ding_event.features.inc';