Skip to content

Instantly share code, notes, and snippets.

@clrockwell
Created April 2, 2014 03:33
Show Gist options
  • Save clrockwell/41e0381958e2ebb9b3cb to your computer and use it in GitHub Desktop.
Save clrockwell/41e0381958e2ebb9b3cb to your computer and use it in GitHub Desktop.
truckingshow.com has a *lot* of dates to keep up with; this makes displaying them site wide very easy, including within nodes that have the PHP filter turned on. Use of the DateAPI was crucial in working out timezone issues (as Drupal saves times in UTC)
<?php
/**
* @file
* Give access to important dates
* based on date nodes
*/
function important_dates_menu() {
$items['test-form'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('storageform'),
'access callback' => TRUE,
);
return $items;
}
/**
* Get important dates
* in array format
*/
function get_important_dates() {
//$return_static = &drupal_static(__FUNCTION__);
//if (!isset($return_static)) {
// can we get from cache first
// if ($cache = cache_get('important_dates_array')) {
// $return_static = $cache->data;
// }
// else {
$current = array();
$next = array();
$previous = array();
$events = _get_prev_curnt_nxt_entity();
$eventDates = _get_dates_for_events($events);
// $eventDates is returnable as is but
// we want to simply the current ones into
// easily used dates
$return['full'] = $eventDates;
$return['c'] = array();
$return['c']['vipStart'] = _setTime($eventDates['Current']['mats_vip_session']['start_date']);
$return['c']['vipEnd'] = _setTime($eventDates['Current']['mats_vip_session']['end_date']);
$return['c']['day1Start'] = _setTime($eventDates['Current']['mats_day_1_general']['start_date']);
$return['c']['day1End'] = _setTime($eventDates['Current']['mats_day_1_general']['end_date']);
$return['c']['day2Start'] = _setTime($eventDates['Current']['mats_day_2']['start_date']);
$return['c']['day2End'] = _setTime($eventDates['Current']['mats_day_2']['end_date']);
$return['c']['day3Start'] = _setTime($eventDates['Current']['mats_day_3']['start_date']);
$return['c']['day3End'] = _setTime($eventDates['Current']['mats_day_3']['end_date']);
$full = _setTime($eventDates['Current']['mats_vip_session']['start_date'], 'F j');
$full .= ' - ';
$full .= _setTime($eventDates['Current']['mats_day_3']['end_date'], 'j');
// March 27 - 29
$return['c']['full'] = $full;
// 2013
$return['c']['year'] = _setTime($eventDates['Current']['mats_day_3']['end_date'], 'Y');
$dayOne = _setTime($eventDates['Current']['mats_vip_session']['start_date'], 'l, F j');
$dayTwo = _setTime($eventDates['Current']['mats_day_2']['start_date'], 'l, F j');
$dayThree = _setTime($eventDates['Current']['mats_day_3']['start_date'], 'l, F j');
// Thursday, March 21
// Friday, March 22
// Saturday, March 24
$return['c']['d1'] = $dayOne;
$return['c']['d2'] = $dayTwo;
$return['c']['d3'] = $dayThree;
$hours_format = 'gA';
$vipHours = _setTime($eventDates['Current']['mats_vip_session']['start_date'], $hours_format);
$vipHours .= ' - ' . _setTime($eventDates['Current']['mats_vip_session']['end_date'], $hours_format);
$day1Hours = _setTime($eventDates['Current']['mats_day_1_general']['start_date'], $hours_format);
$day1Hours .= ' - ' . _setTime($eventDates['Current']['mats_day_1_general']['end_date'], $hours_format);
$day2Hours = _setTime($eventDates['Current']['mats_day_2']['start_date'], $hours_format);
$day2Hours .= ' - ' . _setTime($eventDates['Current']['mats_day_2']['end_date'], $hours_format);
$day3Hours = _setTime($eventDates['Current']['mats_day_3']['start_date'], $hours_format);
$day3Hours .= ' - ' . _setTime($eventDates['Current']['mats_day_3']['end_date'], $hours_format);
// 10AM - 6PM
$return['c']['vipHours'] = $vipHours;
$return['c']['d1h'] = $day1Hours;
$return['c']['d2h'] = $day2Hours;
$return['c']['d3h'] = $day3Hours;
$comp_attendee_open = _setTime($eventDates['Current']['complimentary_attendee_registration']['start_date'], 'l, F j');
$comp_attendee_close = _setTime($eventDates['Current']['complimentary_attendee_registration']['end_date'], 'l, F j');
$comp_vip_open = _setTime($eventDates['Current']['complimentary_vip_registration']['start_date'], 'l, F j');
$comp_vip_close = _setTime($eventDates['Current']['complimentary_vip_registration']['end_date'], 'l, F j');
$return['c']['attOpen'] = $comp_attendee_open;
$return['c']['attClose'] = $comp_attendee_close;
$return['c']['vipOpen'] = $comp_vip_open;
$return['c']['vipClose'] = $comp_vip_close;
$return_static = $return;
//cache_set('important_dates_array', $return_static, 'cache');
// }
//}
return $return_static;
}
function _setTime($time, $format = false) {
$timezone = 'America/Kentucky/Louisville';
$date = new DateObject(strtotime($time.'UTC'), $timezone);
//$date->setTimezone(new DateTimeZone($timezone));
$return = '';
if ($format)
{
//$return = date_format_date($date, 'custom', $format);
$return = $date->format($format);
// $date->format($format);
// $return = format_date(strtotime($time), 'custom', $format, $timezone );
}
else
{
$return = $date->originalTime;
}
// print '<br>Return Value: <br>';
return $return;
}
/**
* Get the Event entities that are
* previous, current, next
* Should never return more than 3?
*/// the previous, current, nex events
function _get_prev_curnt_nxt_entity() {
$en = new EntityFieldQuery();
$en->entityCondition('entity_type', 'node');
$en->entityCondition('bundle', 'event');
$en->fieldCondition('field_archive_status', 'value', array('Previous', 'Current', 'Next'));
$return = reset($en->execute());
$statuses = array();
foreach ($return as $status) {
// this is throwing an exception when I use entity_load('node', $event);
// for now I'm using node_load for lack of time in debugging this
$_event = node_load($status->nid);
// what is the archive status of this event
$archiveStatus = field_get_items('node', $_event, 'field_archive_status', $_event->language);
$archiveStatus = $archiveStatus[0]['value'];
$statuses[$archiveStatus] = $status;
}
return $statuses;
}
/**
* Takes an array of objects that
* represent Events and gets all dates
* for those events that have specific
* taxonomy terms
*
* $events is an array of objects that each have
* 'nid', 'vid', 'type' as properties
*
* Returns an array with node id's as keys
* and all dates that correspond with that
* nid as array values
*/
function _get_dates_for_events($events) {
$eventsDates = array();
// the results need to be filtered against taxonomy
// Attendee, General, Exhibitor, Media
$acceptableTerms = array(1,2,3,4); // those are the tid's in taxonomy_term_data
foreach ($events as $status => $event) {
$select = db_select('og_membership', 'og');
$select->join('taxonomy_index', 'ti', 'og.etid = ti.nid');
$select->condition('ti.tid', $acceptableTerms, 'IN');
$select->condition('og.gid', $event->nid, '=');
$select->fields('og', array('etid'));
$theseDates = $select->execute();
foreach ($theseDates as $sel) {
$results[$status][] = $sel;
}
// an array of arrays keyed by the event id (etid)
return load_dates_to_status($results);
}
}
/**
* Takes an array of status => array(dates)
* and loads with necessary info
*/
function load_dates_to_status($statuses) {
$return = array();
foreach ($statuses as $key => $value) {
$return[$key] = array();
foreach ($value as $k => $nid) {
// load the date node
$thisDate = node_load($nid->etid);
// make the node title machine readable to key the array by
$title = preg_replace('@[^a-z0-9_]+@','_',strtolower($thisDate->title));
// the date field (added to custom content type date)
$thisField = reset(field_get_items('node', $thisDate, 'field_date', $thisDate->language));
// add the value of the date field, and optionally the value 2 (end date)
$addDate[$title]['start_date'] = $thisField['value'];
if (isset($thisField['value2']) && !empty($thisField['value2'])) {
$addDate[$title]['end_date'] = $thisField['value2'];
}
// add back into main array
$return[$key] += $addDate;
}
}
return $return;
}
/**
* Helper function to return date in
* specified format (php date function)
* In current state, will only return dates from "Current" event
* @param string $dateNode machine readable (translated from node title) title
* that also includes :start_date or :end_date (e.g. media_request_open:start_date)
* @default :start_date
* @param string $format @see http://php.net/manual/en/function.date.php
* @return string a formatted date
*/
function gid($dateNode, $format = false) {
$d = get_important_dates();
$dateToGet = explode(':', $dateNode);
if (count($dateToGet) > 1) {
// admin specified start/end time
$getTime = $dateToGet[1];
}
else {
// nothing specified so default to start time
$getTime = 'start_date';
}
$time = $d['full']['Current'][$dateToGet[0]][$getTime];
$formatted = _setTime($time, $format);
return $formatted;
}
function get_current_year() {
$d = get_important_dates();
return $d['c']['year'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment