uw_ct_event.module 59.9 KB
Newer Older
ccarigna's avatar
ccarigna committed
1
2
3
4
5
6
7
<?php
/**
 * @file
 * Code for the Event feature.
 */

include_once('uw_ct_event.features.inc');
8
9

/**
l26yan's avatar
l26yan committed
10
 * Implements hook_form_alter().
11
12
 */
function uw_ct_event_form_alter(&$form, &$form_state, $form_id) {
Liam Morland's avatar
Liam Morland committed
13
  // content creation
14
15
16
17
  if ($form_id == 'uw_location_node_form') {
    // Remove the 'delete this location' checkbox on the edit form
    $form['field_location_details']['und'][0]['#after_build'][] = 'uw_ct_event_remove_location_delete';
  }
l26yan's avatar
l26yan committed
18

Liam Morland's avatar
Liam Morland committed
19
  // exposed filters sitewide
20
  if ($form_id == 'views_exposed_form') {
Liam Morland's avatar
Liam Morland committed
21
    // get information about the current path to use for events exposed filters
22
23
    $path = current_path();
    $path = explode("/", $path);
l26yan's avatar
l26yan committed
24

Liam Morland's avatar
Liam Morland committed
25
26
    // if this is the form for the exposed filters at the top of events, use the post method to pass on choices so they're not a query string after the URL.
    if ($form['#id'] == 'views-exposed-form-events-page' || $form['#id'] == 'views-exposed-form-events-events-archive-page'
27
        || $form['#id'] == 'views-exposed-form-events-with-calendar-page-1' || $form['#id'] == 'views-exposed-form-events-with-calendar-page-3') {
kpaxman's avatar
kpaxman committed
28
      // Using POST method from https://drupal.org/comment/6468662#comment-6468662 (requires this patch on views, or an additional module that Lily couldn't get to work)
Liam Morland's avatar
Liam Morland committed
29
      // $form['#attributes']['method'] = 'post';
30
    }
31

Liam Morland's avatar
Liam Morland committed
32
    // Hide dates option '-All-'
33
    if ($form['#id'] == 'views-exposed-form-events-page' || $form['#id'] == 'views-exposed-form-events-events-archive-page' || $form['#id'] == 'views-exposed-form-events-events-tag-page') {
34
35
36
      $date_options = $form['date']['#options'];
      if (isset($date_options['All'])) {
        unset($date_options['All']);
37
      }
38
      $form['date']['#options'] = $date_options;
39
40
    }

kpaxman's avatar
kpaxman committed
41
    // When using "audience" or "type" exposed filter: display the selected option ("tid" from path), then change action to events
42
43
44
45
    if ($form['#id'] == 'views-exposed-form-events-page') {
      if (isset($path[1]) && isset($path[2])) {
        switch ($path[1]) {
          case "audience":
Liam Morland's avatar
Liam Morland committed
46
            // cause the audience term id ("tid") to be selected in the dropdown
47
            $form_state['input']['audience'] = $path[2];
48
49
            break;
          case "type":
Liam Morland's avatar
Liam Morland committed
50
            // cause the event term id ("tid") to be selected in the dropdown
51
            $form_state['input']['type'] = $path[2];
52
53
            break;
        }
Liam Morland's avatar
Liam Morland committed
54
        // make the form submit to the base events path; once they submit to the exposed filter form we can't be sure the URL still makes sense
55
        $form['#action'] = base_path() . 'events';
56
      }
l26yan's avatar
l26yan committed
57
58
    }

kpaxman's avatar
kpaxman committed
59
    // When on "events by date" pages (from archive links), add the chosen date to the "dates" exposed filter and select it from the dropdown
60
    // Dates filter: Click date link, add option composite_views_filter = 0 (date=0), then change action to events
61
    if ($form['#id'] == 'views-exposed-form-events-events-archive-page') {
Liam Morland's avatar
Liam Morland committed
62
63
      // if (!(isset($form_state['input']['date']))) {
        // add the selected date to the dropdown
kpaxman's avatar
kpaxman committed
64
65
        $timestamp = strtotime($path[2]);
        if ($timestamp) {
Liam Morland's avatar
Liam Morland committed
66
          $final_name = strlen($path[2]) > 4 ? t(date('F', $timestamp)) . date(' Y', $timestamp) : $path[2];
67
          $form['date']['#options'][$path[2]] = $final_name;
Liam Morland's avatar
Liam Morland committed
68
69
          // make Drupal think we submitted this value
          // $form_state['input'] = array('date' => $path[2]) + $form_state['input'];
70
          $form_state['input']['date'] = $path[2];
Liam Morland's avatar
Liam Morland committed
71
72
          // if other dropdowns were selected, they are passed in a query string
          // set field_uw_event_type_tid (type) if "type" was selected
73
          if (isset($_GET['type'])) {
74
            $form_state['input']['type'] = $_GET['type'];
75
          }
Liam Morland's avatar
Liam Morland committed
76
          // set field_audience_tid (audience) if "audience" was selected
77
          if (isset($_GET['audience'])) {
78
            $form_state['input']['audience'] = $_GET['audience'];
79
          }
80
        }
Liam Morland's avatar
Liam Morland committed
81
82
        // make the form submit to the base events path, since the exposed filter can't actually deal with this
        // if they select the date, we have code elsewhere that redirects them to the right path
83
        $form['#action'] = base_path() . 'events';
Liam Morland's avatar
Liam Morland committed
84
      // }
l26yan's avatar
l26yan committed
85
    }
l26yan's avatar
l26yan committed
86

87
88
    // When on an events page that is not the "date archive" page
    if ($form['#id'] == 'views-exposed-form-events-page' ) {
89
      // When coming from a submission of the exposed filters that contains a "date archive" value (from the composite_views_filter (date) field) -
90
91
      // and this can only happen from a "date archive" page - since the form destination is set to "events" (in case they didn't pick the date),
      // redirect to the date-based URL. We don't need to add anything to the dropdown since redirecting here will take care of that.
92
      if (isset($form_state['input']['date']) && !in_array($form_state['input']['date'], array('Current', 'Past', 'Today'))
Liam Morland's avatar
Liam Morland committed
93
        /* && $form_state['input']['type'] == 'All' && $form_state['input']['audience'] == 'All' */) {
94
        drupal_goto('events/archive/' . $form_state['input']['date'], array('query' => array('type' => $form_state['input']['type'], 'audience' => $form_state['input']['audience'])));
l26yan's avatar
l26yan committed
95
96
      }
    }
l26yan's avatar
l26yan committed
97
98
99
  }
}

100
/**
Liam Morland's avatar
Liam Morland committed
101
 * Implements hook_views_pre_view().
102
103
 */
function uw_ct_event_views_pre_view(&$view, &$display_id, &$args) {
Liam Morland's avatar
Liam Morland committed
104
105
  // if we are rendering the mini calendar, and are at a url that references a day within a month, make the mini-calendar start at that month
  // note that currently dates are NOT zero-padded, but we might want this in the future; if we change this, the preg_match must change
106
  // e.g.:
l26yan's avatar
l26yan committed
107
  // events/archive/2013-10
108
  // events/calendar/2013-10
109
  // events/day/2013-10-31
Liam Morland's avatar
Liam Morland committed
110
  // ***WE WILL HAVE TO REVIEW AND POSSIBLY CHANGE WHAT URLS AFFECT THE MINI-CALENDAR
l26yan's avatar
l26yan committed
111
  if ($view->name == 'events_with_calendar' && $display_id == 'block_1') {
112
113
    $path = current_path();
    $path = explode("/", $path);
l26yan's avatar
l26yan committed
114
    if (isset($path[2]) && (preg_match('/^(\d{4})$/', $path[2], $match))) {
115
116
117
      if ($path[2] != date("Y")) {
        $view->args[0] = $match[1] . '-1';
      }
l26yan's avatar
l26yan committed
118
    }
119
    if (isset($path[2]) && (preg_match('/^(\d{4}-(?:[123456789]|1[012]))$/', $path[2], $match) || preg_match('/^(\d{4}-(?:0[123456789]|1[012]))$/', $path[2], $match))) {
120
121
      $view->args[0] = $match[1];
    }
122
    if (isset($path[2]) && preg_match('/^(\d{4}-(?:[123456789]|1[012]))-(?:[123456789]|[12]\d|3[01])$/', $path[2], $match)) {
123
124
125
126
127
      $view->args[0] = $match[1];
    }
  }
}

l26yan's avatar
l26yan committed
128
/**
Liam Morland's avatar
Liam Morland committed
129
 * Implements hook_views_query_alter().
l26yan's avatar
l26yan committed
130
131
 */
function uw_ct_event_views_query_alter(&$view, &$query) {
132
133
  $path = current_path();
  $path = explode("/", $path);
Liam Morland's avatar
Liam Morland committed
134

l26yan's avatar
l26yan committed
135
  $today = date("Y-m-d");
136
  $future_date = date("Y-m-d", strtotime('+365 day'));
137

138
  if ($view->name == 'events' && isset($view->exposed_raw_input['date'])) {
139
140
141
142
143
144
145
146

    // Unset query conditions for events/archive view
    if (isset($path[0]) && $path[0] == 'events' && isset($path[1]) && $path[1] == 'archive' && isset($path[2])) {
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
      if (strlen($path[2]) == 4 ) {
        if (preg_match('/^(\d{4})$/', $path[2], $match)) {
          $archive_year = $match[0];
147
          if (isset($archive_year) && $view->exposed_raw_input['date'] == $archive_year) {
Liam Morland's avatar
Liam Morland committed
148
            unset($query->where[1]['conditions'][$key]);
149
150
151
152
153
          }
        }
      }
      else {
        if (preg_match('/^(\d{4}-(?:[123456789]|1[012]))$/', $path[2], $match) || preg_match('/^(\d{4}-(?:0[123456789]|1[012]))$/', $path[2], $match)) {
Liam Morland's avatar
Liam Morland committed
154
          $archive_year_month = $match[0];
155
          if (isset($archive_year_month) && $view->exposed_raw_input['date'] == $archive_year_month) {
Liam Morland's avatar
Liam Morland committed
156
            unset($query->where[1]['conditions'][$key]);
157
158
159
160
161
          }
        }
      }
    }

162
    // Modify query for 'Current and upcoming' to include 'OR' section
163
// Dates now (WCMS 1.10+ w/Date 7.x-2.9-beta2+2-dev-uw_wcms1) seem to be stored in local time format, so we removed offset correction from the SQL statement below
164
    if ($view->exposed_raw_input['date'] == 'Current') {
165
166
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
167
168
      $query->where[1]['conditions'][$key]['field'] = "((DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T') <= :field_data_field_event_date_field_event_date_value)
          AND (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T') >= :field_data_field_event_date_field_event_date_value2))
Liam Morland's avatar
Liam Morland committed
169
        OR
170
          (DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T') >= :field_data_field_event_date_field_event_date_value)";
171
172
173
      $query->where[1]['conditions'][$key]['value'] = array(':field_data_field_event_date_field_event_date_value' => $today, ':field_data_field_event_date_field_event_date_value2' => $today);
    }
    // Modify query for 'Today' to include 'OR' section
174
// Dates now (WCMS 1.10+ w/Date 7.x-2.9-beta2+2-dev-uw_wcms1) seem to be stored in local time format, so we removed offset correction from the SQL statement below
175
    if ($view->exposed_raw_input['date'] == 'Today') {
176
177
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
178
      $query->where[1]['conditions'][$key]['field'] =
179
180
        "((DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%d') <= :field_data_field_event_date_field_event_date_value)
          AND (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%d') >= :field_data_field_event_date_field_event_date_value2))
181
        OR
182
          (DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T') = :field_data_field_event_date_field_event_date_value)";
183
184
185
      $query->where[1]['conditions'][$key]['value'] = array(':field_data_field_event_date_field_event_date_value' => $today, ':field_data_field_event_date_field_event_date_value2' => $today);
    }
    // Modify query for 'Past' to sort by desc
186
    if ($view->exposed_raw_input['date'] == 'Past') {
187
188
189
190
191
      foreach ($query->orderby as $key => $data) {
        // We search for these fields so that it doesn't matter where they are; they moved once and broke this.
        if ($data['field'] == 'field_data_field_event_date_field_event_date_value' || $data['field'] == 'field_data_field_event_date_field_event_date_value2') {
          $query->orderby[$key]['direction'] = 'DESC';
        }
192
193
      }
    }
l26yan's avatar
l26yan committed
194
  }
195
196
197
  if ($view->name == 'events_with_calendar' && $view->current_display == 'feed_1') {
    end($query->where[1]['conditions']);
    $key = key($query->where[1]['conditions']);
Liam Morland's avatar
Liam Morland committed
198
    $query->where[1]['conditions'][$key]['field'] =
199
      "((DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
200
            <= :field_data_field_event_date_field_event_date_value)
Liam Morland's avatar
Liam Morland committed
201
         AND
202
           (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T')
203
204
            >= :field_data_field_event_date_field_event_date_value2)
         AND
205
           (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T')
206
207
208
           <= :future_date)
       )
       OR
209
       ((DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
210
211
         >= :field_data_field_event_date_field_event_date_value)
         AND
212
           (DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
213
214
215
          <= :future_date))";
    $query->where[1]['conditions'][$key]['value'] = array(':field_data_field_event_date_field_event_date_value' => $today, ':field_data_field_event_date_field_event_date_value2' => $today, ':future_date' => $future_date);
  }
216
217
218
}

/**
lkmorlan's avatar
lkmorlan committed
219
 * Implements hook_node_form_ID_alter().
Liam Morland's avatar
Liam Morland committed
220
221
222
223
224
 *
 * Adds location data (through javascript settings) to the form, which allows for quickly filling in
 * common campus locations
 * There needs to be a central website to get location data from (that has the uw_ct_location feature enabled, populated with content)
 * The location of that site is in a Drupal variable, uw_location_data_json_url, and defaults to the current site if it is not set
225
226
 */
function uw_ct_event_form_uw_event_node_form_alter(&$form, &$form_state, $form_id) {
227
228
229
230
231
232
  // check if we have the data in cache, if not, retrieve it
  $cache = cache_get('uw_ct_event:locations', 'cache');
  if ($cache) {
    $locations = $cache->data;
  }
  else {
233
    $request = variable_get('uw_location_data_json_url', 'https://uwaterloo.ca/web-resources') . '/location/json/list';
234
235
    $response = drupal_http_request($request);
    $locations = json_decode($response->data);
mnshantz's avatar
mnshantz committed
236

237
238
239
240
241
    if (isset($locations) && !isset($locations->error)) {
      // set the cache
      cache_set('uw_ct_event:locations', $locations, 'cache', CACHE_TEMPORARY);
    }
  }
lkmorlan's avatar
lkmorlan committed
242

243
  if (isset($locations)) {
244
    // Add javascript for pre-filling in location data
245
246
    $form['#attached']['js'][] = drupal_get_path('module', 'uw_ct_event') . '/js/location-autofill.js';
    $location_container_id = 'uw_event_location';
mnshantz's avatar
mnshantz committed
247

248
249
250
251
252
253
254
255
256
257
258
259
260
    // Add Div Wrapper around form so the javascript knows where to add the location lookup dropdown
    $form['field_event_location']['#prefix'] = '<div id="' . $location_container_id . '">';
    $form['field_event_location']['#suffix'] = '</div>';

    // settings used in location-autofill.js
    $settings = array(
      'label' => t('Location lookup'),
      'description' => t('Choose a location or enter location information below.'),
      'custom' => t('- Custom location -'),
      'data' => $locations,
      'selectId' => 'autofill-location-data',
      'containerId' => $location_container_id,
    );
mnshantz's avatar
mnshantz committed
261

262
263
264
265
266
    $form['#attached']['js'][] = array(
      'data' => array('uw_ct_event' => $settings),
      'type' => 'setting',
    );
  }
267
268
269
270
271
  // Remove the 'delete this location' checkbox on the edit form
  $form['field_event_location']['und'][0]['#after_build'][] = 'uw_ct_event_remove_location_delete';
}

/**
272
273
274
 * Remove the delete checkbox from location element.
 * Code snippet courtesy of Fabian - ref: http://drupal.org/node/381458#comment-4627442
 */
275
function uw_ct_event_remove_location_delete(&$form_element, &$form_state) {
Liam Morland's avatar
Liam Morland committed
276
277
  // unset($form_element['delete_location']);
  // unset($form_element['#value']);
278
279
280
  $form_element['name']['#description'] = t('e.g. a building or venue');
  $form_element['additional']['#description'] = t('e.g. room number or meeting point');
  $form_element['locpick']['instructions']['#markup'] = t('Latitude and longitude is used for generating map links. If you do not have latitude and longitude coordinates, you can add a specific map link below.');
281
282
  return $form_element;
}
283

284
285
286
287
/**
 * Implements hook_block_info().
 */
function uw_ct_event_block_info() {
288
289
290
291
  $blocks['front_page'] = array(
    'info' => t('Events: front page'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
292
    $blocks['events_public_feed'] = array(
mnshantz's avatar
mnshantz committed
293
294
295
    'info' => t('Events: public feed'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
l26yan's avatar
l26yan committed
296
  $blocks['event-calendar-block-id'] = array(
lkmorlan's avatar
lkmorlan committed
297
298
    'info' => t('Event calendar block'),
    'cache' => DRUPAL_CACHE_PER_PAGE,
l26yan's avatar
l26yan committed
299
  );
300
301
302
303
  $blocks['events_by_audience'] = array(
    'info' => t('Events by audience'),
    'cache' => DRUPAL_CACHE_PER_PAGE,
  );
304
305
306
307
308
309
310
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function uw_ct_event_block_view($delta = '') {
311

312
  $block = array();
lkmorlan's avatar
lkmorlan committed
313

314
  switch ($delta) {
315
    case 'front_page':
316
      // Use timestampadd to correct for time zone (NOTE: having to do this MAY be a bug)
317
      // if timezone offset is 0, then use -18000 which is correct for Waterloo, and hope we don't have anyone set to Greenwich Mean Time
318
      // Dates now (WCMS 1.10+ w/Date 7.x-2.9-beta2+2-dev-uw_wcms1) seem to be stored in local time format, so we removed offset correction from the SQL statement below
319
      $result = db_query("
lkmorlan's avatar
lkmorlan committed
320
        SELECT node.nid, node.title, node.sticky,
321
322
          DATE(field_data_field_event_date.field_event_date_value) AS date_start,
          DATE(field_data_field_event_date.field_event_date_value2) AS date_end
323
324
          FROM {node} node LEFT JOIN {field_data_field_event_date} field_data_field_event_date ON node.nid = field_data_field_event_date.entity_id
          WHERE node.type = 'uw_event' AND node.status = 1 AND node.promote = 1
lkmorlan's avatar
lkmorlan committed
325
          -- Show any event that *ends* in the future.
326
          HAVING date_end >= CURRENT_DATE
lkmorlan's avatar
lkmorlan committed
327
          ORDER BY node.sticky DESC, date_start ASC, date_end ASC
328
329
330
          LIMIT 3
      ");
      $items = array();
331
      $friendly_months = array('Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June', 'July', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.');
332
      foreach ($result as $record) {
lkmorlan's avatar
lkmorlan committed
333
334
335
336
        $date_start = strtotime($record->date_start);
        $date_end = strtotime($record->date_end);
        // Start and end on same date. "Nov. 24, 2005"
        if ($date_start === $date_end) {
337
          $date_formatted = $friendly_months[date('n', $date_start) - 1] . '&nbsp;' . date('j, Y', $date_start);
lkmorlan's avatar
lkmorlan committed
338
339
340
341
        }
        elseif (date('Y', $date_start) === date('Y', $date_end)) {
          // Same month and year, different day. "Nov. 24-26, 2005"
          if (date('m', $date_start) === date('m', $date_end)) {
342
            $date_formatted = $friendly_months[date('n', $date_start) - 1] . '&nbsp;' . date('j', $date_start) . '&ndash;' . date('j, Y', $date_end);
lkmorlan's avatar
lkmorlan committed
343
344
345
          }
          // Same year. "Nov. 24 to Dec. 2, 2005"
          else {
l26yan's avatar
l26yan committed
346
            $date_formatted = $friendly_months[date('n', $date_start) - 1] . '&nbsp;' . date('j', $date_start) . ' to ' . $friendly_months[date('n', $date_end) - 1] . '&nbsp;' . date('j, Y', $date_end);
lkmorlan's avatar
lkmorlan committed
347
348
349
350
          }
        }
        // Fully different dates."Nov. 24, 2005 to Jan. 2, 2006"
        else {
l26yan's avatar
l26yan committed
351
          $date_formatted = $friendly_months[date('n', $date_start) - 1] . '&nbsp;' . date('j, Y', $date_start) . ' to ' . $friendly_months[date('n', $date_end) - 1] . '&nbsp;' . date('j, Y', $date_end);
lkmorlan's avatar
lkmorlan committed
352
        }
353
        $items[] = '<span class="home-listing-block-date' . (($record->sticky) ? ' sticky' : '') . '">' . $date_formatted . '</span><span class="home-listing-block-link">' . l($record->title, 'node/' . $record->nid) . '</span>';
354
355
356
357
      }

      if (!empty($items)) {
        $block['subject'] = t('Events');
Liam Morland's avatar
Liam Morland committed
358
        // setup array to be used by theme_item_list
359
360
361
362
363
364
        $content = array(
          'items' => $items,
          'title' => '',
          'type' => 'ol',
          'attributes' => array(),
        );
365
        $block['content'] = theme('item_list', $content) . '<span class="home-listing-block-more-link">' . l(t("All upcoming events"), 'events') . '</span>';
366
      }
367
      break;
368

369
370
371
    // Create a link to the rss feed for news
    case 'events_public_feed':
      $block['content'] = events_public_feed_link();
372
      break;
mnshantz's avatar
mnshantz committed
373

374
375
376
377
    // Create a link to expose event calendar
    case 'event-calendar-block-id':
      $block['content'] = uw_ct_event_calendar_contents();
      break;
lkmorlan's avatar
lkmorlan committed
378

379
380
381
382
383
    case 'events_by_audience':
      $html = '<div class="item-list">';
      $html .= uw_ct_event_audience_taxonomy_tree(0);
      $html .= '</div>';
      if (uw_ct_event_audience_taxonomy_tree(0)) {
l26yan's avatar
l26yan committed
384
        $block['subject'] = t('<button>Events by audience</button>');
385
        $block['content'] = $html;
l26yan's avatar
l26yan committed
386
387
      }
      else {
388
389
390
391
392
393
394
395
396
397
398
399
400
        $block['subject'] = t('');
        $block['content'] = $html;
      }
    break;
  }
  return $block;
}

/**
 * Implements hook_block_view_alter().
 * Add events by date under calendar block
 */
function uw_ct_event_block_view_alter(&$data, $block) {
l26yan's avatar
l26yan committed
401
402
  $event_exist = db_query("SELECT count(nid) FROM {node} node WHERE type = 'uw_event'")->fetchField();

403
  if ($block->delta == 'events_with_calendar-block_1') {
l26yan's avatar
l26yan committed
404
405
    if ($event_exist) {
      $by_date = uw_ct_event_by_date();
l26yan's avatar
l26yan committed
406
      $header = '<h2><button>Events by date</button></h2>';
l26yan's avatar
l26yan committed
407
408
409
410
411
412
      $before = $data['content']['#markup'];
      $data['content']['#markup'] = $header . $before . $by_date;
    }
    else {
      $data['content']['#markup'] = '';
    }
413
  }
l26yan's avatar
l26yan committed
414
415
416
417
418
419

  if ($block->delta == 'event_type_block-events_by_type') {
    if ($event_exist) {
      $data['subject'] = '<button>' . $data['subject'] . '</button>';
    }
  }
420
}
421

l26yan's avatar
l26yan committed
422
423
/**
 * Implements hook_init().
kpaxman's avatar
kpaxman committed
424
 * Redirect events/archive, events/audience, events/type pages to events
425
 * Redirect invalid term ids and dates on above paths to events
l26yan's avatar
l26yan committed
426
427
428
429
430
431
432
433
434
435
436
 */
function uw_ct_event_init() {
  global $base_url;
  $path = current_path();

  // Redirect events/archive, events/audience, events/type pages to events
  if ($path == 'events/archive' || $path == 'events/audience' || $path == 'events/type') {
    drupal_goto($base_url . '/events');
  }

  $path = explode("/", $path);
l26yan's avatar
l26yan committed
437

438
439
440
441
442
443
444
445
  // Find out TIDS within uwaterloo_audience
  $uw_ct_event_audience_tids = uw_ct_event_get_tid_from_machine_name('uwaterloo_audience');

  // Find out TIDS within uw_event_type
  $uw_ct_event_type_tids = uw_ct_event_get_tid_from_machine_name('uw_event_type');

  // Find out TIDS within uw_event_tags
  $uw_ct_event_tag_tids = uw_ct_event_get_tid_from_machine_name('uw_event_tags');
446

447
448
  // Redirect from events/category/$PATH[2] to events/audience/$PATH[2] -- e.g. https://uwaterloo.ca/environment/events/category/1
  if ($path[0] == 'events' && isset($path[1]) && ($path[1] == 'category') && isset($path[2])) {
449
    if (is_numeric($path[2]) && in_array($path[2], $uw_ct_event_audience_tids)) {
450
      drupal_goto($base_url . '/events/audience/' . $path[2]);
451
452
453
    }
  }

l26yan's avatar
l26yan committed
454
455
  // Redirect events/audience/$PATH[2], when $PATH[2] is NOT taxonomy term id of audience
  if ($path[0] == 'events' && isset($path[1]) && ($path[1] == 'audience') && isset($path[2])) {
456
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_audience_tids)) {
l26yan's avatar
l26yan committed
457
      drupal_set_message(t('Invalid event audience URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
458
459
460
461
462
463
      drupal_goto($base_url . '/events');
    }
  }

  // Redirect events/type/$PATH[2], when $PATH[2] is NOT taxonomy term id of type
  if ($path[0] == 'events' && isset($path[1]) && ($path[1] == 'type') && isset($path[2])) {
464
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_type_tids)) {
l26yan's avatar
l26yan committed
465
      drupal_set_message(t('Invalid event type URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
466
467
468
      drupal_goto($base_url . '/events');
    }
  }
kpaxman's avatar
kpaxman committed
469
470
471

  // Redirect events/archive/$PATH[2], when $PATH[2] is NOT in a date format we would pass
  if ($path[0] == 'events' && isset($path[1]) && ($path[1] == 'archive') && isset($path[2])) {
472
    if (!preg_match('/^\d{4}(?:-(?:[123456789]|1[012]))?$/', $path[2])) {
l26yan's avatar
l26yan committed
473
      drupal_set_message(t('Invalid event date URL. You have been redirected to the main "events" page.'), 'warning');
kpaxman's avatar
kpaxman committed
474
      drupal_goto($base_url . '/events');
Liam Morland's avatar
Liam Morland committed
475
    }
kpaxman's avatar
kpaxman committed
476
  }
l26yan's avatar
l26yan committed
477
478
479

  // Redirect event-tags/$PATH[1], when $PATH[1] is taxonomy term name of tag
  if ($path[0] == 'taxonomy' && $path[1] == 'term' && isset($path[2])) {
480
    if (in_array($path[2], $uw_ct_event_tag_tids)) {
l26yan's avatar
l26yan committed
481
482
483
484
485
486
      drupal_goto($base_url . '/events/tag/' . $path[2]);
    }
  }

  // Redirect events/tag/$PATH[2], when $PATH[2] is NOT taxonomy term id of tag
  if ($path[0] == 'events' && isset($path[1]) && ($path[1] == 'tag') && isset($path[2])) {
487
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_tag_tids)) {
l26yan's avatar
l26yan committed
488
      drupal_set_message(t('Invalid event tag URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
489
490
491
      drupal_goto($base_url . '/events');
    }
  }
492
493
494
495
496
497
498
499
500
501
502
503
  
  // Redirect events/calendar/NOTADATE
  if ($path[0] == 'events' && isset($path[1]) && $path[1] == 'calendar' && isset($path[2]) && !preg_match('/^[0-9]{4}-(0?[1-9]|1[0-2])$/', $path[2])) {
    drupal_set_message(t('Invalid calendar URL. You have been redirected to the main "calendar" page.'), 'warning');
    drupal_goto($base_url . '/events/calendar');
  }

  // Redirect events/day/NOTADATE
  if ($path[0] == 'events' && isset($path[1]) && $path[1] == 'day' && isset($path[2]) && !preg_match('/^[0-9]{4}-(0?[1-9]|1[0-2])(-[0-9]{1,2})$/', $path[2])) {
    drupal_set_message(t('Invalid day URL. You have been redirected to today\'s "day" page.'), 'warning');
    drupal_goto($base_url . '/events/day');
  }
l26yan's avatar
l26yan committed
504
505
}

506
507
508
509
510
511
/**
 * Implements hook_page_build().
 *
 * Add the breadcrumbs for the management view
 */
function uw_ct_event_page_build() {
512
  $path = current_path();
513
514
  if ($path == 'admin/workbench/manage/events') {
    $breadcrumb = drupal_get_breadcrumb();
Liam Morland's avatar
Liam Morland committed
515
516
517
    $breadcrumb[1] = l(t('Administration'), 'admin');
    $breadcrumb[2] = l(t('My Workbench'), 'admin/workbench');
    $breadcrumb[3] = l(t('Create/Manage Content'), 'admin/workbench/create');
518
    drupal_set_breadcrumb($breadcrumb);
519

520
521
  }
  else {
522
523
    $path = explode("/", $path);
    switch ($path[0]) {
524
525
526
527
      case 'node':
        $node = isset($path[1]) && $path[1] != 'add' ? node_load($path[1]) : NULL;
        if (isset($node) && is_object($node) && $node->type == 'uw_event') {
          $breadcrumb = drupal_get_breadcrumb();
Liam Morland's avatar
Liam Morland committed
528
          $breadcrumb[1] = l(t('Events'), 'events');
l26yan's avatar
l26yan committed
529
          drupal_add_js(drupal_get_path('module', 'uw_ct_event') . '/js/uw_ct_event.js');
lkmorlan's avatar
lkmorlan committed
530

531
532
          // TODO: determine how to handle repeating dates (edge case)
          // currently we only check the very first start date
Liam Morland's avatar
Liam Morland committed
533
          // add the timezone offset to the stored datetime to get the actual datetime (NOTE: having to do this MAY be a bug)
534
535
536
537
          $start_date = strtotime($node->field_event_date[$node->language][0]['value'])+$node->field_event_date[$node->language][0]['offset'];
          $year = format_date($start_date, 'custom', 'Y');
          $month_name = format_date($start_date, 'custom', 'F');
          $month_number = format_date($start_date, 'custom', 'm');
lkmorlan's avatar
lkmorlan committed
538
539
          $breadcrumb[2] = l($year, 'events/archive/' . $year);
          $breadcrumb[3] = l($month_name, 'events/archive/' . $year . '-' . $month_number);
540
541
542
          drupal_set_breadcrumb($breadcrumb);
        }
        break;
lkmorlan's avatar
lkmorlan committed
543

544
      case 'events':
545
        drupal_add_js(drupal_get_path('module', 'uw_ct_event') . '/js/uw_ct_event.js');
l26yan's avatar
l26yan committed
546
        if (isset($path[1]) && $path[1] == 'archive' && isset($path[2])) {
547
          $breadcrumb = drupal_get_breadcrumb();
l26yan's avatar
l26yan committed
548
          if (isset($breadcrumb[1])) {
l26yan's avatar
l26yan committed
549
            unset($breadcrumb[1]);
550
          }
l26yan's avatar
l26yan committed
551
552
          drupal_set_breadcrumb($breadcrumb);
        }
l26yan's avatar
l26yan committed
553
        if (isset($path[1]) && ($path[1] == 'calendar' || $path[1] == 'day')) {
554
          $breadcrumb = drupal_get_breadcrumb();
l26yan's avatar
l26yan committed
555
          if (isset($breadcrumb[1])) {
l26yan's avatar
l26yan committed
556
            unset($breadcrumb[1]);
557
558
559
          }
          drupal_set_breadcrumb($breadcrumb);
        }
l26yan's avatar
l26yan committed
560
        break;
561
    }
562
563
  }
}
564

mnshantz's avatar
mnshantz committed
565
/**
lkmorlan's avatar
lkmorlan committed
566
 * Implements hook_element_info_alter().
mnshantz's avatar
mnshantz committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
 */
function uw_ct_event_element_info_alter(&$type) {
  if (isset($type['link_field'])) {
    $type['link_field']['#process'][] = 'uw_ct_event_link_field_process';
  }
}

/**
 * Process the link type element before displaying the field.
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 * The $fields array is in $complete_form['#field_info'][$element['#field_name']].
 */
function uw_ct_event_link_field_process($element, $form_state, $complete_form) {
  switch ($element['#field_name']) {
lkmorlan's avatar
lkmorlan committed
582

583
    case 'field_event_host_link':
mnshantz's avatar
mnshantz committed
584
585
      $element['title']['#title'] = t('Link text');
      $element['url']['#title'] = t('URL');
lkmorlan's avatar
lkmorlan committed
586
      $element['title']['#description'] = t('The link text is limited to ' . $element['title']['#maxlength'] . ' characters maximum.');
mnshantz's avatar
mnshantz committed
587
      break;
lkmorlan's avatar
lkmorlan committed
588

589
    case 'field_event_link':
mnshantz's avatar
mnshantz committed
590
591
      $element['title']['#title'] = t('Link text');
      $element['url']['#title'] = t('URL');
lkmorlan's avatar
lkmorlan committed
592
      $element['title']['#description'] = t('The link text is limited to ' . $element['title']['#maxlength'] . ' characters maximum.');
mnshantz's avatar
mnshantz committed
593
      break;
lkmorlan's avatar
lkmorlan committed
594

mnshantz's avatar
mnshantz committed
595
596
597
598
  }
  return $element;
}

599
600
601
602
/**
 * Implements hook_theme_registry_alter().
 */
function uw_ct_event_theme_registry_alter(&$theme_registry) {
603
604
605
606
607
608
609
610
611
612
  $theme_registry['node__uw_event'] = array(
    'template' => 'node--uw-event',
    'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
    'type' => 'theme',
  );
  $theme_registry['field__uw_event'] = array(
    'template' => 'field--uw-event',
    'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
    'type' => 'theme',
  );
613
614
615
616
617
  $theme_registry['block__uw_ct_event'] = array(
    'template' => 'block--uw-ct-event',
    'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
    'type' => 'theme',
  );
l26yan's avatar
l26yan committed
618
619
  $theme_registry['date_views_pager'] = array(
    'template' => 'date-views-pager',
620
    'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
621
622
    'theme path' => drupal_get_path('module', 'date') . '/date_views',
    'type' => 'module',
l26yan's avatar
l26yan committed
623
    'preprocess functions' => array(
624
      'template_preprocess_event_date_views_pager',
l26yan's avatar
l26yan committed
625
626
    ),
  );
l26yan's avatar
l26yan committed
627
628
629
630
631
632
  $theme_registry['calendar_datebox'] = array(
      'template' => 'calendar-datebox',
      'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
      'theme path' => drupal_get_path('module', 'calendar'),
      'type' => 'module',
      'preprocess functions' => array(
l26yan's avatar
l26yan committed
633
634
635
636
637
638
639
640
641
642
        'template_preprocess_event_calendar_datebox',
      ),
  );
  $theme_registry['calendar_mini'] = array(
      'template' => 'calendar-mini',
      'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
      'theme path' => drupal_get_path('module', 'calendar'),
      'type' => 'module',
      'preprocess functions' => array(
        'template_preprocess_event_calendar_mini',
l26yan's avatar
l26yan committed
643
644
      ),
  );
645
646
647
648
649
  $theme_registry['calendar_empty_day'] = array(
    'path' => drupal_get_path('module', 'calendar') . '/theme',
    'theme path' => drupal_get_path('module', 'calendar'),
    'function' => 'uw_ct_event_calendar_empty_day',
  );
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
}

/**
 * Implements hook_theme().
 * Adds views-related template files
 */
function uw_ct_event_theme($existing, $type, $theme, $path) {
  return array(
    'views_view_unformatted__events__page' => array(
      'variables' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
      'template' => 'views-view-unformatted--events--page',
      'base hook' => 'views_view_unformatted',
      'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
      'preprocess functions' => array(
        'template_preprocess',
        'template_preprocess_views_view_unformatted',
lkmorlan's avatar
lkmorlan committed
666
      ),
667
    ),
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
    'views_view_unformatted__events_with_calendar__page_3' => array(
      'variables' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
      'template' => 'views-view-unformatted--events--page',
      'base hook' => 'views_view_unformatted',
      'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
      'preprocess functions' => array(
        'template_preprocess',
        'template_preprocess_views_view_unformatted',
      ),
    ),
    'views_view_unformatted__events_with_calendar__attachment_1' => array(
      'variables' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
      'template' => 'views-view-unformatted--events--page',
      'base hook' => 'views_view_unformatted',
      'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
      'preprocess functions' => array(
        'template_preprocess',
        'template_preprocess_views_view_unformatted',
      ),
    ),
688
689
690
691
692
693
694
695
696
697
    'views_view_rss__events' => array(
      'variables' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
      'template' => 'views-view-rss--events',
      'base hook' => 'views_view_rss',
      'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
      'preprocess functions' => array(
        'template_preprocess',
        'template_preprocess_views_view_rss',
      ),
    ),
698
  );
699
}
mnshantz's avatar
mnshantz committed
700

l26yan's avatar
l26yan committed
701
702
703
/**
 * custom html block: To add the feed link to a block
 */
lkmorlan's avatar
lkmorlan committed
704
function events_public_feed_link() {
kpaxman's avatar
kpaxman committed
705
  $basepath = base_path() . 'events/events.xml';
lkmorlan's avatar
lkmorlan committed
706
  return '<div class="rss_link"><a href="' . $basepath . '">Subscribe to events feed</a></div>';
mnshantz's avatar
mnshantz committed
707
}
l26yan's avatar
l26yan committed
708
709
710
711
712
713

/**
 * custom html block: display event calendar link
 * @return string
 */
function uw_ct_event_calendar_contents() {
l26yan's avatar
l26yan committed
714
  global $base_url;
715
  $link = $base_url . '/events/calendar.ics';
Liam Morland's avatar
Liam Morland committed
716
  // $image_path = drupal_get_path('module', 'uw_ct_event') . '/images/ical-feed-icon-34x14.png';
l26yan's avatar
l26yan committed
717
718
  $html = '<div class="feed-icon">';

719
  $event_exist = db_query("SELECT count(node.nid)
l26yan's avatar
l26yan committed
720
721
    FROM {node} AS node LEFT JOIN {field_data_field_event_date} AS ed ON node.nid = ed.entity_id
    WHERE node.type = 'uw_event' AND ed.field_event_date_value >= CURRENT_DATE AND node.status = 1
722
    LIMIT 1")->fetchField();
l26yan's avatar
l26yan committed
723
724
725
726
727
728
729
730

  if ($event_exist > 0) {
    $html .= '<a class="ical-icon" href="' . $link . '">';
    $html .= 'Export event calendar';
    $html .= '</a>';
  }
  $html .= '</div>';
  return $html;
l26yan's avatar
l26yan committed
731
}
732
733
734

/**
 * Implements hook_preprocess_HOOK().
l26yan's avatar
l26yan committed
735
 * 1. Add scope="col" for web accessibility.
l26yan's avatar
l26yan committed
736
 * Remove headers= "Sunday"... for web accessibility
l26yan's avatar
l26yan committed
737
 * 2. Disable the options of the select list for type and audience
Liam Morland's avatar
Liam Morland committed
738
 * which are not selected by event node
739
740
741
742
743
744
745
746
 */
function uw_ct_event_preprocess_html(&$variables) {
  $path = current_path();
  $path = explode("/", $path);
  if (isset($path[0]) && $path[0] == 'events') {
    $search  = '<th class="';
    $replace = '<th scope="col" class="';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
l26yan's avatar
l26yan committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    $search  = 'headers="Sunday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
    $search  = 'headers="Monday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
    $search  = 'headers="Tuesday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
    $search  = 'headers="Wednesday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
    $search  = 'headers="Thursday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
    $search  = 'headers="Friday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
    $search  = 'headers="Saturday"';
    $replace = '';
    $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
768
  }
Liam Morland's avatar
Liam Morland committed
769

l26yan's avatar
l26yan committed
770
771
  if (isset($path[0]) && $path[0] == 'events') {

772
    $type_tid = uw_ct_event_get_tid_from_machine_name('uw_event_type');
l26yan's avatar
l26yan committed
773
    $using_type_tid = uw_ct_event_get_using_taxonomy_term('uw_event_type');
774

l26yan's avatar
l26yan committed
775
    foreach ($type_tid as $item) {
776
777
778
779
780
781
782
783
      if (is_array($using_type_tid)) {
        if (!in_array($item, $using_type_tid)) {
          $search = '<option value="' . $item . '">';
          $replace = '<option value="' . $item . '" disabled="true">';
          $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
        }
      }
      else {
l26yan's avatar
l26yan committed
784
785
786
        $search = '<option value="' . $item . '">';
        $replace = '<option value="' . $item . '" disabled="true">';
        $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
Liam Morland's avatar
Liam Morland committed
787
788
      }
    }
l26yan's avatar
l26yan committed
789

790
    $audience_tid = uw_ct_event_get_tid_from_machine_name('uwaterloo_audience');
l26yan's avatar
l26yan committed
791
    $using_audience_tid = uw_ct_event_get_using_taxonomy_term('uwaterloo_audience');
792

l26yan's avatar
l26yan committed
793
    foreach ($audience_tid as $item) {
794
795
796
797
798
799
800
801
      if (is_array($using_audience_tid)) {
        if (!in_array($item, $using_audience_tid)) {
          $search = '<option value="' . $item . '">';
          $replace = '<option value="' . $item . '" disabled="true">';
          $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
        }
      }
      else {
l26yan's avatar
l26yan committed
802
803
804
805
806
807
        $search = '<option value="' . $item . '">';
        $replace = '<option value="' . $item . '" disabled="true">';
        $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
      }
    }
  }
808
}
809
810

function uw_ct_event_audience_taxonomy_tree($tid) {
811
812
  $today = date("Y-m-d");
  $taxonomy = uw_ct_event_audience_get_taxonomy_parents($tid, $today);
813
814
  if ($taxonomy) {
    $html = '<ul>';
l26yan's avatar
l26yan committed
815
    foreach ($taxonomy as $id => $item) {
816
      $html .= '<li>';
l26yan's avatar
l26yan committed
817
      $html .= l($item['name'], 'events/audience/' . $item['tid']);
818
819
820
821
822
823
824
825
      $html .= ' (' . $item['count'] . ')';
      $html .= uw_ct_event_audience_taxonomy_tree($item['tid']);
      $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
  }
}
826
// Dates now (WCMS 1.10+ w/Date 7.x-2.9-beta2+2-dev-uw_wcms1) seem to be stored in local time format, so we removed offset correction from the SQL statement below
827
function uw_ct_event_audience_get_taxonomy_parents($tid, $today) {
828
  $query = "SELECT
829
830
831
832
833
      taxonomy_term_data.tid AS tid,
      taxonomy_term_data.name AS name,
      taxonomy_term_data.vid AS vid,
      taxonomy_vocabulary.machine_name AS machine_name,
      COUNT(field_audience_taxonomy_term_data.nid) AS count
834
    FROM
835
      {taxonomy_term_data} taxonomy_term_data
836
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
837
838
839
      {field_data_field_audience} field_data_field_audience ON
        taxonomy_term_data.tid = field_data_field_audience.field_audience_tid
          AND
840
        (field_data_field_audience.entity_type = 'node' AND field_data_field_audience.deleted = '0')
841
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
842
      {node} field_audience_taxonomy_term_data ON
843
        field_data_field_audience.entity_id = field_audience_taxonomy_term_data.nid
844
    LEFT JOIN
845
846
      {taxonomy_vocabulary} taxonomy_vocabulary ON
        taxonomy_term_data.vid = taxonomy_vocabulary.vid
847
848
849
    LEFT JOIN
      {taxonomy_term_hierarchy} taxonomy_term_hierarchy ON
        taxonomy_term_data.tid = taxonomy_term_hierarchy.tid
850
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
851
852
853
      {field_data_field_event_date} field_audience_taxonomy_term_data__field_data_field_event_date ON
        field_audience_taxonomy_term_data.nid = field_audience_taxonomy_term_data__field_data_field_event_date.entity_id
          AND (field_audience_taxonomy_term_data__field_data_field_event_date.entity_type = 'node'
854
          AND field_audience_taxonomy_term_data__field_data_field_event_date.deleted = '0')
855
    WHERE
Liam Morland's avatar
Liam Morland committed
856
857
858
      ( (taxonomy_vocabulary.machine_name IN  ('uwaterloo_audience'))
        AND (field_audience_taxonomy_term_data.type IN  ('uw_event'))
        AND (field_audience_taxonomy_term_data.status = '1')
859
        AND taxonomy_term_hierarchy.parent = :tid
860
861
862
863
864
865
        AND ((((DATE_FORMAT(field_audience_taxonomy_term_data__field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
             <= :today)
        AND (DATE_FORMAT(field_audience_taxonomy_term_data__field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T')
             >= :today) ))
        OR (DATE_FORMAT(field_audience_taxonomy_term_data__field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
       >= :today)) )
866
    GROUP BY
867
      tid, name, vid, machine_name
868
    ORDER BY
869
      taxonomy_term_data.weight";
870
  $args = array(
871
872
      ':tid' => $tid,
      ':today' => $today,
873
874
875
  );

  $result = db_query($query, $args);
l26yan's avatar
l26yan committed
876
  $return = FALSE;
877
878
879
880
881
882
883
884
  foreach ($result as $id => $record) {
    $return[$id]['tid'] = $record->tid;
    $return[$id]['name'] = $record->name;
    $return[$id]['count'] = $record->count;
  }

  return $return;
}
l26yan's avatar
l26yan committed
885

l26yan's avatar
l26yan committed
886
887
888
/**
 * Full archive
 */
l26yan's avatar
l26yan committed
889
890
function uw_ct_event_by_date() {

l26yan's avatar
l26yan committed
891
  $year_array =  uw_ct_event_get_year_array();
l26yan's avatar
l26yan committed
892
  $combine_year = array();
l26yan's avatar
l26yan committed
893
  $combine_year = uw_ct_event_get_combine_year_array($year_array[1], $year_array[2]);
894
  $links = array();
l26yan's avatar
l26yan committed
895
  foreach ($combine_year as $key => $record_year) {
896

l26yan's avatar
l26yan committed
897
    $year_link = l($key, 'events/archive/' . $key) . ' (' . $record_year . ')';
l26yan's avatar
l26yan committed
898

l26yan's avatar
l26yan committed
899
900
    $month_date_value = uw_ct_event_get_start_month_array($key);
    $month_date_value2 = uw_ct_event_get_end_month_array($key);
l26yan's avatar
l26yan committed
901

l26yan's avatar
l26yan committed
902
    $combine_month = array();
l26yan's avatar
l26yan committed
903
    $combine_month = uw_ct_event_get_combine_month_array($month_date_value, $month_date_value2);
l26yan's avatar
l26yan committed
904

l26yan's avatar
l26yan committed
905
    $months = array();
Liam Morland's avatar
Liam Morland committed
906
    // ignore individual months if more than 3 years old, unless in the path for this exact year
l26yan's avatar
l26yan committed
907
908
    $selectedyear = explode('/', current_path());
    $selectedyear = substr($selectedyear[count($selectedyear)-1], 0, 4);
909
    if (