uw_ct_event.module 60.3 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
  // Setting the default images for metatag twitter cards and open-graph.
99
100
  // The node:field_image is an image within the node body and the
  // node:field_event_image is the social media listing page image.
101
    if (module_exists('metatag') && $form_id == 'uw_event_node_form' ) {
102
103
      $form['metatags'][LANGUAGE_NONE]['open-graph']['og:image']['value']['#default_value'] = '[node:field_event_image],[node:field_image]';
      $form['metatags'][LANGUAGE_NONE]['twitter-cards']['twitter:image:src']['value']['#default_value'] = '[node:field_event_image],[node:field_image]';
104
    }
l26yan's avatar
l26yan committed
105
106
}

107
/**
Liam Morland's avatar
Liam Morland committed
108
 * Implements hook_views_pre_view().
109
110
 */
function uw_ct_event_views_pre_view(&$view, &$display_id, &$args) {
Liam Morland's avatar
Liam Morland committed
111
112
  // 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
113
  // e.g.:
l26yan's avatar
l26yan committed
114
  // events/archive/2013-10
115
  // events/calendar/2013-10
116
  // events/day/2013-10-31
Liam Morland's avatar
Liam Morland committed
117
  // ***WE WILL HAVE TO REVIEW AND POSSIBLY CHANGE WHAT URLS AFFECT THE MINI-CALENDAR
l26yan's avatar
l26yan committed
118
  if ($view->name == 'events_with_calendar' && $display_id == 'block_1') {
119
120
    $path = current_path();
    $path = explode("/", $path);
l26yan's avatar
l26yan committed
121
    if (isset($path[2]) && (preg_match('/^(\d{4})$/', $path[2], $match))) {
122
123
124
      if ($path[2] != date("Y")) {
        $view->args[0] = $match[1] . '-1';
      }
l26yan's avatar
l26yan committed
125
    }
126
    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))) {
127
128
      $view->args[0] = $match[1];
    }
129
    if (isset($path[2]) && preg_match('/^(\d{4}-(?:[123456789]|1[012]))-(?:[123456789]|[12]\d|3[01])$/', $path[2], $match)) {
130
131
132
133
134
      $view->args[0] = $match[1];
    }
  }
}

l26yan's avatar
l26yan committed
135
/**
Liam Morland's avatar
Liam Morland committed
136
 * Implements hook_views_query_alter().
l26yan's avatar
l26yan committed
137
138
 */
function uw_ct_event_views_query_alter(&$view, &$query) {
139
140
  $path = current_path();
  $path = explode("/", $path);
Liam Morland's avatar
Liam Morland committed
141

l26yan's avatar
l26yan committed
142
  $today = date("Y-m-d");
143
  $future_date = date("Y-m-d", strtotime('+365 day'));
144

145
  if ($view->name == 'events' && isset($view->exposed_raw_input['date'])) {
146
147
148
149
150
151
152
153

    // 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];
154
          if (isset($archive_year) && $view->exposed_raw_input['date'] == $archive_year) {
Liam Morland's avatar
Liam Morland committed
155
            unset($query->where[1]['conditions'][$key]);
156
157
158
159
160
          }
        }
      }
      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
161
          $archive_year_month = $match[0];
162
          if (isset($archive_year_month) && $view->exposed_raw_input['date'] == $archive_year_month) {
Liam Morland's avatar
Liam Morland committed
163
            unset($query->where[1]['conditions'][$key]);
164
165
166
167
168
          }
        }
      }
    }

169
    // Modify query for 'Current and upcoming' to include 'OR' section
170
// 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
171
    if ($view->exposed_raw_input['date'] == 'Current') {
172
173
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
174
175
      $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
176
        OR
177
          (DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T') >= :field_data_field_event_date_field_event_date_value)";
178
179
180
      $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
181
// 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
182
    if ($view->exposed_raw_input['date'] == 'Today') {
183
184
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
185
      $query->where[1]['conditions'][$key]['field'] =
186
187
        "((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))
188
        OR
189
          (DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T') = :field_data_field_event_date_field_event_date_value)";
190
191
192
      $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
193
    if ($view->exposed_raw_input['date'] == 'Past') {
194
195
196
197
198
      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';
        }
199
200
      }
    }
l26yan's avatar
l26yan committed
201
  }
202
203
204
  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
205
    $query->where[1]['conditions'][$key]['field'] =
206
      "((DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
207
            <= :field_data_field_event_date_field_event_date_value)
Liam Morland's avatar
Liam Morland committed
208
         AND
209
           (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T')
210
211
            >= :field_data_field_event_date_field_event_date_value2)
         AND
212
           (DATE_FORMAT(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T')
213
214
215
           <= :future_date)
       )
       OR
216
       ((DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
217
218
         >= :field_data_field_event_date_field_event_date_value)
         AND
219
           (DATE_FORMAT(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')
220
221
222
          <= :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);
  }
223
224
225
}

/**
lkmorlan's avatar
lkmorlan committed
226
 * Implements hook_node_form_ID_alter().
Liam Morland's avatar
Liam Morland committed
227
228
229
230
231
 *
 * 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
232
233
 */
function uw_ct_event_form_uw_event_node_form_alter(&$form, &$form_state, $form_id) {
234
235
236
237
238
239
  // 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 {
240
    $request = variable_get('uw_location_data_json_url', 'https://uwaterloo.ca/web-resources') . '/location/json/list';
241
242
    $response = drupal_http_request($request);
    $locations = json_decode($response->data);
mnshantz's avatar
mnshantz committed
243

244
245
246
247
248
    if (isset($locations) && !isset($locations->error)) {
      // set the cache
      cache_set('uw_ct_event:locations', $locations, 'cache', CACHE_TEMPORARY);
    }
  }
lkmorlan's avatar
lkmorlan committed
249

250
  if (isset($locations)) {
251
    // Add javascript for pre-filling in location data
252
253
    $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
254

255
256
257
258
259
260
261
262
263
264
265
266
267
    // 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
268

269
270
271
272
273
    $form['#attached']['js'][] = array(
      'data' => array('uw_ct_event' => $settings),
      'type' => 'setting',
    );
  }
274
275
276
277
278
  // Remove the 'delete this location' checkbox on the edit form
  $form['field_event_location']['und'][0]['#after_build'][] = 'uw_ct_event_remove_location_delete';
}

/**
279
280
281
 * Remove the delete checkbox from location element.
 * Code snippet courtesy of Fabian - ref: http://drupal.org/node/381458#comment-4627442
 */
282
function uw_ct_event_remove_location_delete(&$form_element, &$form_state) {
Liam Morland's avatar
Liam Morland committed
283
284
  // unset($form_element['delete_location']);
  // unset($form_element['#value']);
285
286
287
  $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.');
288
289
  return $form_element;
}
290

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

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

319
  $block = array();
lkmorlan's avatar
lkmorlan committed
320

321
  switch ($delta) {
322
    case 'front_page':
323
324
325
326
327
328
329
      $events = db_select('node', 'n');
      $events->leftJoin('field_data_field_event_date', 'events', 'events.entity_id = n.nid');
      $events->fields('events', array('field_event_date_value', 'field_event_date_value2'));
      $events->fields('n', array('nid', 'title', 'sticky'));
      $events->condition('n.type', 'uw_event', '=');
      $events->condition('n.status' , 1, '=');
      $events->condition('n.promote', 1, '=');
330
      $events->havingCondition('field_event_date_value2', date('Y-m-d'), '>=');
331
      $events->orderBy('n.sticky');
332
      $events->orderBy('events.field_event_date_value', 'ASC');
333
      $events->orderBy('n.title');
334
      $events->range(0, 3);
335

336
      $result = $events->execute();
337

338
      $items = array();
339
      $friendly_months = array('Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June', 'July', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.');
340
      foreach ($result as $record) {
341
342
        $date_start = strtotime($record->field_event_date_value);
        $date_end = strtotime($record->field_event_date_value2);
lkmorlan's avatar
lkmorlan committed
343
        // Start and end on same date. "Nov. 24, 2005"
344
        if (date('Y-m-d', $date_start) === date('Y-m-d', $date_end)) {
345
          $date_formatted = $friendly_months[date('n', $date_start) - 1] . '&nbsp;' . date('j, Y', $date_start);
lkmorlan's avatar
lkmorlan committed
346
347
348
349
        }
        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)) {
350
            $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
351
352
353
          }
          // Same year. "Nov. 24 to Dec. 2, 2005"
          else {
l26yan's avatar
l26yan committed
354
            $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
355
356
357
358
          }
        }
        // Fully different dates."Nov. 24, 2005 to Jan. 2, 2006"
        else {
l26yan's avatar
l26yan committed
359
          $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
360
        }
361
        $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>';
362
363
364
365
      }

      if (!empty($items)) {
        $block['subject'] = t('Events');
Liam Morland's avatar
Liam Morland committed
366
        // setup array to be used by theme_item_list
367
368
369
370
371
372
        $content = array(
          'items' => $items,
          'title' => '',
          'type' => 'ol',
          'attributes' => array(),
        );
373
        $block['content'] = theme('item_list', $content) . '<span class="home-listing-block-more-link">' . l(t("All upcoming events"), 'events') . '</span>';
374
      }
375
      break;
376

377
378
379
    // Create a link to the rss feed for news
    case 'events_public_feed':
      $block['content'] = events_public_feed_link();
380
      break;
mnshantz's avatar
mnshantz committed
381

382
383
384
385
    // 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
386

387
388
389
390
391
    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
392
        $block['subject'] = t('<button>Events by audience</button>');
393
        $block['content'] = $html;
l26yan's avatar
l26yan committed
394
395
      }
      else {
396
397
398
399
400
401
402
403
404
405
406
407
408
        $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
409
410
  $event_exist = db_query("SELECT count(nid) FROM {node} node WHERE type = 'uw_event'")->fetchField();

411
  if ($block->delta == 'events_with_calendar-block_1') {
l26yan's avatar
l26yan committed
412
413
    if ($event_exist) {
      $by_date = uw_ct_event_by_date();
l26yan's avatar
l26yan committed
414
      $header = '<h2><button>Events by date</button></h2>';
l26yan's avatar
l26yan committed
415
416
417
418
419
420
      $before = $data['content']['#markup'];
      $data['content']['#markup'] = $header . $before . $by_date;
    }
    else {
      $data['content']['#markup'] = '';
    }
421
  }
l26yan's avatar
l26yan committed
422
423
424
425
426
427

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

l26yan's avatar
l26yan committed
430
431
/**
 * Implements hook_init().
kpaxman's avatar
kpaxman committed
432
 * Redirect events/archive, events/audience, events/type pages to events
433
 * Redirect invalid term ids and dates on above paths to events
l26yan's avatar
l26yan committed
434
435
436
437
438
439
440
441
442
443
444
 */
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
445

446
447
448
449
450
451
452
453
  // 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');
454

455
456
  // 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])) {
457
    if (is_numeric($path[2]) && in_array($path[2], $uw_ct_event_audience_tids)) {
458
      drupal_goto($base_url . '/events/audience/' . $path[2]);
459
460
461
    }
  }

l26yan's avatar
l26yan committed
462
463
  // 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])) {
464
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_audience_tids)) {
l26yan's avatar
l26yan committed
465
      drupal_set_message(t('Invalid event audience URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
466
467
468
469
470
471
      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])) {
472
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_type_tids)) {
l26yan's avatar
l26yan committed
473
      drupal_set_message(t('Invalid event type URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
474
475
476
      drupal_goto($base_url . '/events');
    }
  }
kpaxman's avatar
kpaxman committed
477
478
479

  // 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])) {
480
    if (!preg_match('/^\d{4}(?:-(?:[123456789]|1[012]))?$/', $path[2])) {
l26yan's avatar
l26yan committed
481
      drupal_set_message(t('Invalid event date URL. You have been redirected to the main "events" page.'), 'warning');
kpaxman's avatar
kpaxman committed
482
      drupal_goto($base_url . '/events');
Liam Morland's avatar
Liam Morland committed
483
    }
kpaxman's avatar
kpaxman committed
484
  }
l26yan's avatar
l26yan committed
485

486
487
488
  // Redirect taxonomy/term/$PATH[2], when $PATH[2] is taxonomy term id of tag
  // Only do when the path ends in taxonomy/term/xx, because taxonomy/term/xx/edit breaks otherwise
  if ($path[0] == 'taxonomy' && $path[1] == 'term' && isset($path[2]) && !isset($path[3])) {
489
    if (in_array($path[2], $uw_ct_event_tag_tids)) {
l26yan's avatar
l26yan committed
490
491
492
493
494
495
      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])) {
496
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_tag_tids)) {
l26yan's avatar
l26yan committed
497
      drupal_set_message(t('Invalid event tag URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
498
499
500
      drupal_goto($base_url . '/events');
    }
  }
501

502
503
504
505
506
507
508
509
510
511
512
  // 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
513
514
}

515
516
517
518
519
520
/**
 * Implements hook_page_build().
 *
 * Add the breadcrumbs for the management view
 */
function uw_ct_event_page_build() {
521
  $path = current_path();
522
523
  if ($path == 'admin/workbench/manage/events') {
    $breadcrumb = drupal_get_breadcrumb();
Liam Morland's avatar
Liam Morland committed
524
525
526
    $breadcrumb[1] = l(t('Administration'), 'admin');
    $breadcrumb[2] = l(t('My Workbench'), 'admin/workbench');
    $breadcrumb[3] = l(t('Create/Manage Content'), 'admin/workbench/create');
527
    drupal_set_breadcrumb($breadcrumb);
528

529
530
  }
  else {
531
532
    $path = explode("/", $path);
    switch ($path[0]) {
533
534
535
536
      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
537
          $breadcrumb[1] = l(t('Events'), 'events');
l26yan's avatar
l26yan committed
538
          drupal_add_js(drupal_get_path('module', 'uw_ct_event') . '/js/uw_ct_event.js');
lkmorlan's avatar
lkmorlan committed
539

540
541
          // 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
542
          // add the timezone offset to the stored datetime to get the actual datetime (NOTE: having to do this MAY be a bug)
543
544
545
546
          $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
547
548
          $breadcrumb[2] = l($year, 'events/archive/' . $year);
          $breadcrumb[3] = l($month_name, 'events/archive/' . $year . '-' . $month_number);
549
550
551
          drupal_set_breadcrumb($breadcrumb);
        }
        break;
lkmorlan's avatar
lkmorlan committed
552

553
      case 'events':
554
        drupal_add_js(drupal_get_path('module', 'uw_ct_event') . '/js/uw_ct_event.js');
l26yan's avatar
l26yan committed
555
        if (isset($path[1]) && $path[1] == 'archive' && isset($path[2])) {
556
          $breadcrumb = drupal_get_breadcrumb();
l26yan's avatar
l26yan committed
557
          if (isset($breadcrumb[1])) {
l26yan's avatar
l26yan committed
558
            unset($breadcrumb[1]);
559
          }
l26yan's avatar
l26yan committed
560
561
          drupal_set_breadcrumb($breadcrumb);
        }
l26yan's avatar
l26yan committed
562
        if (isset($path[1]) && ($path[1] == 'calendar' || $path[1] == 'day')) {
563
          $breadcrumb = drupal_get_breadcrumb();
l26yan's avatar
l26yan committed
564
          if (isset($breadcrumb[1])) {
l26yan's avatar
l26yan committed
565
            unset($breadcrumb[1]);
566
567
568
          }
          drupal_set_breadcrumb($breadcrumb);
        }
l26yan's avatar
l26yan committed
569
        break;
570
    }
571
572
  }
}
573

mnshantz's avatar
mnshantz committed
574
/**
lkmorlan's avatar
lkmorlan committed
575
 * Implements hook_element_info_alter().
mnshantz's avatar
mnshantz committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
 */
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
591

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

598
    case 'field_event_link':
mnshantz's avatar
mnshantz committed
599
600
      $element['title']['#title'] = t('Link text');
      $element['url']['#title'] = t('URL');
lkmorlan's avatar
lkmorlan committed
601
      $element['title']['#description'] = t('The link text is limited to ' . $element['title']['#maxlength'] . ' characters maximum.');
mnshantz's avatar
mnshantz committed
602
      break;
lkmorlan's avatar
lkmorlan committed
603

mnshantz's avatar
mnshantz committed
604
605
606
607
  }
  return $element;
}

608
609
610
611
/**
 * Implements hook_theme_registry_alter().
 */
function uw_ct_event_theme_registry_alter(&$theme_registry) {
612
613
614
615
616
617
618
619
620
621
  $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',
  );
622
623
624
625
626
  $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
627
628
  $theme_registry['date_views_pager'] = array(
    'template' => 'date-views-pager',
629
    'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
630
631
    'theme path' => drupal_get_path('module', 'date') . '/date_views',
    'type' => 'module',
l26yan's avatar
l26yan committed
632
    'preprocess functions' => array(
633
      'template_preprocess_event_date_views_pager',
l26yan's avatar
l26yan committed
634
635
    ),
  );
l26yan's avatar
l26yan committed
636
637
638
639
640
641
  $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
642
643
644
645
646
647
648
649
650
651
        '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
652
653
      ),
  );
654
655
656
657
658
  $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',
  );
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
}

/**
 * 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
675
      ),
676
    ),
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
    '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',
      ),
    ),
697
698
699
700
701
702
703
704
705
706
    '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',
      ),
    ),
707
  );
708
}
mnshantz's avatar
mnshantz committed
709

l26yan's avatar
l26yan committed
710
711
712
/**
 * custom html block: To add the feed link to a block
 */
lkmorlan's avatar
lkmorlan committed
713
function events_public_feed_link() {
kpaxman's avatar
kpaxman committed
714
  $basepath = base_path() . 'events/events.xml';
lkmorlan's avatar
lkmorlan committed
715
  return '<div class="rss_link"><a href="' . $basepath . '">Subscribe to events feed</a></div>';
mnshantz's avatar
mnshantz committed
716
}
l26yan's avatar
l26yan committed
717
718
719
720
721
722

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

728
  $event_exist = db_query("SELECT count(node.nid)
l26yan's avatar
l26yan committed
729
730
    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
731
    LIMIT 1")->fetchField();
l26yan's avatar
l26yan committed
732
733
734
735
736
737
738
739

  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
740
}
741
742
743

/**
 * Implements hook_preprocess_HOOK().
l26yan's avatar
l26yan committed
744
 * 1. Add scope="col" for web accessibility.
l26yan's avatar
l26yan committed
745
 * Remove headers= "Sunday"... for web accessibility
l26yan's avatar
l26yan committed
746
 * 2. Disable the options of the select list for type and audience
Liam Morland's avatar
Liam Morland committed
747
 * which are not selected by event node
748
749
750
751
752
753
754
755
 */
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    $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']);
777
  }
Liam Morland's avatar
Liam Morland committed
778

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

781
    $type_tid = uw_ct_event_get_tid_from_machine_name('uw_event_type');
l26yan's avatar
l26yan committed
782
    $using_type_tid = uw_ct_event_get_using_taxonomy_term('uw_event_type');
783

l26yan's avatar
l26yan committed
784
    foreach ($type_tid as $item) {
785
786
787
788
789
790
791
792
      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
793
794
795
        $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
796
797
      }
    }
l26yan's avatar
l26yan committed
798

799
    $audience_tid = uw_ct_event_get_tid_from_machine_name('uwaterloo_audience');
l26yan's avatar
l26yan committed
800
    $using_audience_tid = uw_ct_event_get_using_taxonomy_term('uwaterloo_audience');
801

l26yan's avatar
l26yan committed
802
    foreach ($audience_tid as $item) {
803
804
805
806
807
808
809
810
      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
811
812
813
814
815
816
        $search = '<option value="' . $item . '">';
        $replace = '<option value="' . $item . '" disabled="true">';
        $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
      }
    }
  }
817
}
818
819

function uw_ct_event_audience_taxonomy_tree($tid) {
820
821
  $today = date("Y-m-d");
  $taxonomy = uw_ct_event_audience_get_taxonomy_parents($tid, $today);
822
823
  if ($taxonomy) {
    $html = '<ul>';
l26yan's avatar
l26yan committed
824
    foreach ($taxonomy as $id => $item) {
825
      $html .= '<li>';
l26yan's avatar
l26yan committed
826
      $html .= l($item['name'], 'events/audience/' . $item['tid']);
827
828
829
830
831
832
833
834
      $html .= ' (' . $item['count'] . ')';
      $html .= uw_ct_event_audience_taxonomy_tree($item['tid']);
      $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
  }
}
835
// 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
836
function uw_ct_event_audience_get_taxonomy_parents($tid, $today) {
837
  $query = "SELECT
838
839
840
841
842
      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
843
    FROM
844
      {taxonomy_term_data} taxonomy_term_data
845
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
846
847
848
      {field_data_field_audience} field_data_field_audience ON
        taxonomy_term_data.tid = field_data_field_audience.field_audience_tid
          AND
849
        (field_data_field_audience.entity_type = 'node' AND field_data_field_audience.deleted = '0')
850
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
851
      {node} field_audience_taxonomy_term_data ON
852
        field_data_field_audience.entity_id = field_audience_taxonomy_term_data.nid
853
    LEFT JOIN
854
855
      {taxonomy_vocabulary} taxonomy_vocabulary ON
        taxonomy_term_data.vid = taxonomy_vocabulary.vid
856
857
858
    LEFT JOIN
      {taxonomy_term_hierarchy} taxonomy_term_hierarchy ON
        taxonomy_term_data.tid = taxonomy_term_hierarchy.tid
859
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
860
861
862
      {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'
863
          AND field_audience_taxonomy_term_data__field_data_field_event_date.deleted = '0')
864
    WHERE
Liam Morland's avatar
Liam Morland committed
865
866
867
      ( (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')
868
        AND taxonomy_term_hierarchy.parent = :tid
869
870
871
872
873
874
        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)) )
875
    GROUP BY
876
      tid, name, vid, machine_name
877
    ORDER BY
878
      taxonomy_term_data.weight";
879
  $args = array(
880
881
      ':tid' => $tid,
      ':today' => $today,
882
883
884
  );

  $result = db_query($query, $args);
l26yan's avatar
l26yan committed
885
  $return = FALSE;
886
887
888
889
890
891
892
893
  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
894

l26yan's avatar
l26yan committed
895
896
897
/**
 * Full archive
 */
l26yan's avatar
l26yan committed
898
899
function uw_ct_event_by_date() {

l26yan's avatar
l26yan committed
900
  $year_array =  uw_ct_event_get_year_array();
l26yan's avatar
l26yan committed
901
  $combine_year = array();
l26yan's avatar
l26yan committed
902
  $combine_year = uw_ct_event_get_combine_year_array($year_array[1], $year_array[2]);
903
  $links = array();
l26yan's avatar
l26yan committed
904
  foreach ($combine_year as $key => $record_year) {
905

l26yan's avatar
l26yan committed
906