uw_ct_event.module 57.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
    if ($view->exposed_raw_input['date'] == 'Current') {
164
165
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
Liam Morland's avatar
Liam Morland committed
166
167
168
      $query->where[1]['conditions'][$key]['field'] = "((DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d') <= :field_data_field_event_date_field_event_date_value)
          AND (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d') >= :field_data_field_event_date_field_event_date_value2))
        OR
169
170
171
172
          (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d') >= :field_data_field_event_date_field_event_date_value)";
      $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
173
    if ($view->exposed_raw_input['date'] == 'Today') {
174
175
176
177
178
179
180
181
182
      end($query->where[1]['conditions']);
      $key = key($query->where[1]['conditions']);
      $query->where[1]['conditions'][$key]['field'] = "((DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d') <= :field_data_field_event_date_field_event_date_value)
          AND (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d') >= :field_data_field_event_date_field_event_date_value2))
        OR
          (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d') = :field_data_field_event_date_field_event_date_value)";
      $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
183
    if ($view->exposed_raw_input['date'] == 'Past') {
184
185
186
187
188
      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';
        }
189
190
      }
    }
l26yan's avatar
l26yan committed
191
  }
192
193
194
  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
195
196
    $query->where[1]['conditions'][$key]['field'] =
      "((DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d')
197
            <= :field_data_field_event_date_field_event_date_value)
Liam Morland's avatar
Liam Morland committed
198
199
         AND
           (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d')
200
201
            >= :field_data_field_event_date_field_event_date_value2)
         AND
Liam Morland's avatar
Liam Morland committed
202
           (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d')
203
204
205
           <= :future_date)
       )
       OR
Liam Morland's avatar
Liam Morland committed
206
       ((DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d')
207
208
         >= :field_data_field_event_date_field_event_date_value)
         AND
Liam Morland's avatar
Liam Morland committed
209
           (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), SEC_TO_TIME(-14400)), '%Y-%m-%d')
210
211
212
          <= :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);
  }
213
214
215
}

/**
lkmorlan's avatar
lkmorlan committed
216
 * Implements hook_node_form_ID_alter().
Liam Morland's avatar
Liam Morland committed
217
218
219
220
221
 *
 * 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
222
223
 */
function uw_ct_event_form_uw_event_node_form_alter(&$form, &$form_state, $form_id) {
224
225
226
227
228
229
  // 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 {
230
    $request = variable_get('uw_location_data_json_url', 'https://uwaterloo.ca/web-resources') . '/location/json/list';
231
232
    $response = drupal_http_request($request);
    $locations = json_decode($response->data);
mnshantz's avatar
mnshantz committed
233

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

240
  if (isset($locations)) {
241
    // Add javascript for pre-filling in location data
242
243
    $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
244

245
246
247
248
249
250
251
252
253
254
255
256
257
    // 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
258

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

/**
269
270
271
 * Remove the delete checkbox from location element.
 * Code snippet courtesy of Fabian - ref: http://drupal.org/node/381458#comment-4627442
 */
272
function uw_ct_event_remove_location_delete(&$form_element, &$form_state) {
Liam Morland's avatar
Liam Morland committed
273
274
  // unset($form_element['delete_location']);
  // unset($form_element['#value']);
275
276
277
  $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.');
278
279
  return $form_element;
}
280

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

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

309
  $block = array();
lkmorlan's avatar
lkmorlan committed
310

311
  switch ($delta) {
312
    case 'front_page':
313
      // Use timestampadd to correct for time zone (NOTE: having to do this MAY be a bug)
314
      // 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
315
      $result = db_query("
lkmorlan's avatar
lkmorlan committed
316
        SELECT node.nid, node.title, node.sticky,
317
318
          DATE(TIMESTAMPADD(SECOND, if(field_event_date_offset = 0, -18000, field_event_date_offset), STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'))) AS date_start,
          DATE(TIMESTAMPADD(SECOND, if(field_event_date_offset2 = 0, -18000, field_event_date_offset2), STR_TO_DATE(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'))) AS date_end
319
320
          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
321
          -- Show any event that *ends* in the future.
322
          HAVING date_end >= CURRENT_DATE
lkmorlan's avatar
lkmorlan committed
323
          ORDER BY node.sticky DESC, date_start ASC, date_end ASC
324
325
326
          LIMIT 3
      ");
      $items = array();
327
      $friendly_months = array('Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June', 'July', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.');
328
      foreach ($result as $record) {
lkmorlan's avatar
lkmorlan committed
329
330
331
332
        $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) {
333
          $date_formatted = $friendly_months[date('n', $date_start) - 1] . '&nbsp;' . date('j, Y', $date_start);
lkmorlan's avatar
lkmorlan committed
334
335
336
337
        }
        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)) {
338
            $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
339
340
341
          }
          // Same year. "Nov. 24 to Dec. 2, 2005"
          else {
l26yan's avatar
l26yan committed
342
            $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
343
344
345
346
          }
        }
        // Fully different dates."Nov. 24, 2005 to Jan. 2, 2006"
        else {
l26yan's avatar
l26yan committed
347
          $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
348
        }
349
        $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>';
350
351
352
353
      }

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

365
366
367
    // Create a link to the rss feed for news
    case 'events_public_feed':
      $block['content'] = events_public_feed_link();
368
      break;
mnshantz's avatar
mnshantz committed
369

370
371
372
373
    // 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
374

375
376
377
378
379
    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
380
        $block['subject'] = t('<button>Events by audience</button>');
381
        $block['content'] = $html;
l26yan's avatar
l26yan committed
382
383
      }
      else {
384
385
386
387
388
389
390
391
392
393
394
395
396
        $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
397
398
  $event_exist = db_query("SELECT count(nid) FROM {node} node WHERE type = 'uw_event'")->fetchField();

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

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

l26yan's avatar
l26yan committed
418
419
/**
 * Implements hook_init().
kpaxman's avatar
kpaxman committed
420
 * Redirect events/archive, events/audience, events/type pages to events
421
 * Redirect invalid term ids and dates on above paths to events
l26yan's avatar
l26yan committed
422
423
424
425
426
427
428
429
430
431
432
 */
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
433

434
435
436
437
438
439
440
441
  // 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');
442

443
444
  // 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])) {
445
    if (is_numeric($path[2]) && in_array($path[2], $uw_ct_event_audience_tids)) {
446
      drupal_goto($base_url . '/events/audience/' . $path[2]);
447
448
449
    }
  }

l26yan's avatar
l26yan committed
450
451
  // 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])) {
452
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_audience_tids)) {
l26yan's avatar
l26yan committed
453
      drupal_set_message(t('Invalid event audience URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
454
455
456
457
458
459
      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])) {
460
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_type_tids)) {
l26yan's avatar
l26yan committed
461
      drupal_set_message(t('Invalid event type URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
462
463
464
      drupal_goto($base_url . '/events');
    }
  }
kpaxman's avatar
kpaxman committed
465
466
467

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

  // Redirect event-tags/$PATH[1], when $PATH[1] is taxonomy term name of tag
  if ($path[0] == 'taxonomy' && $path[1] == 'term' && isset($path[2])) {
476
    if (in_array($path[2], $uw_ct_event_tag_tids)) {
l26yan's avatar
l26yan committed
477
478
479
480
481
482
      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])) {
483
    if (!is_numeric($path[2]) || !in_array($path[2], $uw_ct_event_tag_tids)) {
l26yan's avatar
l26yan committed
484
      drupal_set_message(t('Invalid event tag URL. You have been redirected to the main "events" page.'), 'warning');
l26yan's avatar
l26yan committed
485
486
487
      drupal_goto($base_url . '/events');
    }
  }
488
489
490
491
492
493
494
495
496
497
498
499
  
  // 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
500
501
}

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

516
517
  }
  else {
518
519
    $path = explode("/", $path);
    switch ($path[0]) {
520
521
522
523
      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
524
          $breadcrumb[1] = l(t('Events'), 'events');
l26yan's avatar
l26yan committed
525
          drupal_add_js(drupal_get_path('module', 'uw_ct_event') . '/js/uw_ct_event.js');
lkmorlan's avatar
lkmorlan committed
526

527
528
          // 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
529
          // add the timezone offset to the stored datetime to get the actual datetime (NOTE: having to do this MAY be a bug)
530
531
532
533
          $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
534
535
          $breadcrumb[2] = l($year, 'events/archive/' . $year);
          $breadcrumb[3] = l($month_name, 'events/archive/' . $year . '-' . $month_number);
536
537
538
          drupal_set_breadcrumb($breadcrumb);
        }
        break;
lkmorlan's avatar
lkmorlan committed
539

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

mnshantz's avatar
mnshantz committed
561
/**
lkmorlan's avatar
lkmorlan committed
562
 * Implements hook_element_info_alter().
mnshantz's avatar
mnshantz committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
 */
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
578

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

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

mnshantz's avatar
mnshantz committed
591
592
593
594
  }
  return $element;
}

595
596
597
598
/**
 * Implements hook_theme_registry_alter().
 */
function uw_ct_event_theme_registry_alter(&$theme_registry) {
599
600
601
602
603
604
605
606
607
608
  $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',
  );
609
610
611
612
613
  $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
614
615
  $theme_registry['date_views_pager'] = array(
    'template' => 'date-views-pager',
616
    'path' => drupal_get_path('module', 'uw_ct_event') . '/templates',
617
618
    'theme path' => drupal_get_path('module', 'date') . '/date_views',
    'type' => 'module',
l26yan's avatar
l26yan committed
619
    'preprocess functions' => array(
620
      'template_preprocess_event_date_views_pager',
l26yan's avatar
l26yan committed
621
622
    ),
  );
l26yan's avatar
l26yan committed
623
624
625
626
627
628
  $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
629
630
631
632
633
634
635
636
637
638
        '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
639
640
      ),
  );
641
642
643
644
645
  $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',
  );
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
}

/**
 * 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
662
      ),
663
    ),
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
    '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',
      ),
    ),
684
685
686
687
688
689
690
691
692
693
    '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',
      ),
    ),
694
  );
695
}
mnshantz's avatar
mnshantz committed
696

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

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

715
  $event_exist = db_query("SELECT count(node.nid)
l26yan's avatar
l26yan committed
716
717
    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
718
    LIMIT 1")->fetchField();
l26yan's avatar
l26yan committed
719
720
721
722
723
724
725
726

  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
727
}
728
729
730

/**
 * Implements hook_preprocess_HOOK().
l26yan's avatar
l26yan committed
731
 * 1. Add scope="col" for web accessibility.
l26yan's avatar
l26yan committed
732
 * Remove headers= "Sunday"... for web accessibility
l26yan's avatar
l26yan committed
733
 * 2. Disable the options of the select list for type and audience
Liam Morland's avatar
Liam Morland committed
734
 * which are not selected by event node
735
736
737
738
739
740
741
742
 */
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
    $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']);
764
  }
Liam Morland's avatar
Liam Morland committed
765

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

768
    $type_tid = uw_ct_event_get_tid_from_machine_name('uw_event_type');
l26yan's avatar
l26yan committed
769
    $using_type_tid = uw_ct_event_get_using_taxonomy_term('uw_event_type');
770

l26yan's avatar
l26yan committed
771
    foreach ($type_tid as $item) {
772
773
774
775
776
777
778
779
      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
780
781
782
        $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
783
784
      }
    }
l26yan's avatar
l26yan committed
785

786
    $audience_tid = uw_ct_event_get_tid_from_machine_name('uwaterloo_audience');
l26yan's avatar
l26yan committed
787
    $using_audience_tid = uw_ct_event_get_using_taxonomy_term('uwaterloo_audience');
788

l26yan's avatar
l26yan committed
789
    foreach ($audience_tid as $item) {
790
791
792
793
794
795
796
797
      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
798
799
800
801
802
803
        $search = '<option value="' . $item . '">';
        $replace = '<option value="' . $item . '" disabled="true">';
        $variables['page']['#children'] = str_replace($search, $replace, $variables['page']['#children']);
      }
    }
  }
804
}
805
806

function uw_ct_event_audience_taxonomy_tree($tid) {
807
808
  $today = date("Y-m-d");
  $taxonomy = uw_ct_event_audience_get_taxonomy_parents($tid, $today);
809
810
  if ($taxonomy) {
    $html = '<ul>';
l26yan's avatar
l26yan committed
811
    foreach ($taxonomy as $id => $item) {
812
      $html .= '<li>';
l26yan's avatar
l26yan committed
813
      $html .= l($item['name'], 'events/audience/' . $item['tid']);
814
815
816
817
818
819
820
821
822
      $html .= ' (' . $item['count'] . ')';
      $html .= uw_ct_event_audience_taxonomy_tree($item['tid']);
      $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
  }
}

823
function uw_ct_event_audience_get_taxonomy_parents($tid, $today) {
824
  $query = "SELECT
825
826
827
828
829
      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
830
    FROM
831
      {taxonomy_term_data} taxonomy_term_data
832
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
833
834
835
      {field_data_field_audience} field_data_field_audience ON
        taxonomy_term_data.tid = field_data_field_audience.field_audience_tid
          AND
836
        (field_data_field_audience.entity_type = 'node' AND field_data_field_audience.deleted = '0')
837
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
838
      {node} field_audience_taxonomy_term_data ON
839
        field_data_field_audience.entity_id = field_audience_taxonomy_term_data.nid
840
    LEFT JOIN
841
842
      {taxonomy_vocabulary} taxonomy_vocabulary ON
        taxonomy_term_data.vid = taxonomy_vocabulary.vid
843
844
845
    LEFT JOIN
      {taxonomy_term_hierarchy} taxonomy_term_hierarchy ON
        taxonomy_term_data.tid = taxonomy_term_hierarchy.tid
846
    LEFT JOIN
Liam Morland's avatar
Liam Morland committed
847
848
849
      {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'
850
          AND field_audience_taxonomy_term_data__field_data_field_event_date.deleted = '0')
851
    WHERE
Liam Morland's avatar
Liam Morland committed
852
853
854
      ( (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')
855
856
        AND taxonomy_term_hierarchy.parent = :tid
        AND ((((DATE_FORMAT(ADDTIME(STR_TO_DATE(field_audience_taxonomy_term_data__field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'),
Liam Morland's avatar
Liam Morland committed
857
            SEC_TO_TIME(-18000)), '%Y-%m-%d') <= :today)
858
859
        AND (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_audience_taxonomy_term_data__field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'),
            SEC_TO_TIME(-18000)), '%Y-%m-%d') >= :today) ))
Liam Morland's avatar
Liam Morland committed
860
        OR (DATE_FORMAT(ADDTIME(STR_TO_DATE(field_audience_taxonomy_term_data__field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'),
861
      SEC_TO_TIME(-18000)), '%Y-%m-%d') >= :today)) )
862
    GROUP BY
863
      tid, name, vid, machine_name
864
    ORDER BY
865
      taxonomy_term_data.weight";
866
  $args = array(
867
868
      ':tid' => $tid,
      ':today' => $today,
869
870
871
  );

  $result = db_query($query, $args);
l26yan's avatar
l26yan committed
872
  $return = FALSE;
873
874
875
876
877
878
879
880
  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
881

l26yan's avatar
l26yan committed
882
883
884
/**
 * Full archive
 */
l26yan's avatar
l26yan committed
885
886
function uw_ct_event_by_date() {

l26yan's avatar
l26yan committed
887
  $year_array =  uw_ct_event_get_year_array();
l26yan's avatar
l26yan committed
888
  $combine_year = array();
l26yan's avatar
l26yan committed
889
  $combine_year = uw_ct_event_get_combine_year_array($year_array[1], $year_array[2]);
890
  $links = array();
l26yan's avatar
l26yan committed
891
  foreach ($combine_year as $key => $record_year) {
892

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

l26yan's avatar
l26yan committed
895
896
    $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
897

l26yan's avatar
l26yan committed
898
    $combine_month = array();
l26yan's avatar
l26yan committed
899
    $combine_month = uw_ct_event_get_combine_month_array($month_date_value, $month_date_value2);
l26yan's avatar
l26yan committed
900

l26yan's avatar
l26yan committed
901
    $months = array();
Liam Morland's avatar
Liam Morland committed
902
    // ignore individual months if more than 3 years old, unless in the path for this exact year
l26yan's avatar
l26yan committed
903
904
    $selectedyear = explode('/', current_path());
    $selectedyear = substr($selectedyear[count($selectedyear)-1], 0, 4);
905
    if ($key > date('Y')-2 || $key == $selectedyear) {
l26yan's avatar
l26yan committed
906
907
      foreach ($combine_month as $key_month => $record_month) {
        $months[] = l(date('F', mktime(0, 0, 0, $key_month, 1)), 'events/archive/' . $key . '-' . $key_month) . ' (' . $record_month . ')';
l26yan's avatar
l26yan committed
908
909
910
911
912
913
914
915
      }
    }
    $links[] = array(
        'data' => $year_link,
        'children' => $months,
    );
  }

Liam Morland's avatar
Liam Morland committed
916
  // setup array to be used by theme_item_list
l26yan's avatar
l26yan committed
917
  $content_header = "<h3 class='full-archive'><button>Full archive</button></h3>";
l26yan's avatar
l26yan committed
918
919
920
921
922
923
  $content = array(
      'items' => $links,
      'title' => '',
      'type' => 'ol',
      'attributes' => array(),
  );
l26yan's avatar
l26yan committed
924
  $return = FALSE;