Commit a23efa03 authored by ccarigna's avatar ccarigna
Browse files

Changed SQL queries/event counts used in events_by_date block

parent 09afe602
......@@ -161,92 +161,61 @@ function uw_ct_event_block_info() {
function uw_ct_event_block_view($delta = '') {
$block = array();
$futuremonths = 6;
$pastmonths = 12;
$eventsMonthCountSql = "SELECT node.title AS node_title, node.nid AS nid, "."field_data_field_event_date.field_event_date_value AS field_data_field_event_date_field_event_date_value, "."field_data_field_event_date.field_event_date_value2 AS field_data_field_event_date_field_event_date_value2 "."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.status = '1') AND (node.type IN ('uw_event')) ) "."AND( (DATE_FORMAT(STR_TO_DATE(field_data_field_event_date.field_event_date_value2, '%Y-%m-%dT%T'), '%Y-%m') >= :date1 "."AND DATE_FORMAT(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'), '%Y-%m') <= :date2) )) ";
/* Some ugly but working code to generate a multi-demensional array of dates in the needed order and format
* Example: +12 months and -6 months from the present date
* This block will then determine the number of events in each month
* and create a list for organized by year
*/
// First determine the previous $pastmonths into an array keyed on year
$m = strftime('%m');
$y = strftime('%Y');
for ($i = 1; $i <= $pastmonths; $i++) {
$m--;
$m = str_pad($m, 2, '0', STR_PAD_LEFT); // Need to have months like 01, else SQL will return 0 event count
if ($m <= 0) {
$y--;
$m = 12;
}
$eventdates[$y][$m] = "$y-$m";
}
// Now sort the dates, so we have them from lowest months to highest
// for months in current year and previous year
$y = strftime('%Y');
if (array_key_exists($y, $eventdates))
ksort($eventdates[$y]);
if (array_key_exists($y - 1, $eventdates)) {
ksort($eventdates[$y - 1]);
// Need to swap the order so the prev year is first in the array.
// Tried to sort the array but order of months was getting messed up
// More elegant solution could be created but needed to move on.
$temp[$y - 1] = $eventdates[$y - 1];
$temp[$y] = $eventdates[$y];
$eventdates = $temp;
}
// Now get the next $futuremonths after current month - no need to sort as we are
// appending increasing months to the end of the array
$m = strftime('%m');
$y = strftime('%Y');
for ($i = 1; $i <= $futuremonths; $i++) {
$m = str_pad($m, 2, '0', STR_PAD_LEFT);
$eventdates[$y][$m] = "$y-$m";
$m++;
if ($m > 12) {
$y++;
$m = 1;
}
}
$links = array();
foreach ($eventdates as $year => $months) {
// create links to the year and months
$month_links = array();
$yearcount = 0;
// create month links
foreach ($months as $month => $date) {
$result = db_query($eventsMonthCountSql, array(':date1' => $date, ':date2' => $date));
$count = $result->rowCount();
$yearcount += $count;
$month_links[] = l(date('F', mktime(0, 0, 0, $month)), 'events/archive/'.$year.'-'.$month).' ('.$count.')';
}
$year_link = l($year, 'events/archive/'.$year).' ('.$yearcount.')';
$links[] = array(
'data' => $year_link,
'children' => $month_links,
);
switch ($delta) {
case 'events_by_date':
// get years and number of events in each year
$result_year = db_query("
SELECT YEAR(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T')) AS year, COUNT(YEAR(STR_TO_DATE(field_data_field_event_date.field_event_date_value, '%Y-%m-%dT%T'))) as count
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
GROUP BY year ORDER BY year DESC
");
$links = array();
foreach ($result_year as $record_year) {
$year_link = l($record_year->year, 'events/archive/' . $record_year->year) . ' (' . $record_year->count . ')';
// get months for the current year, and number of events in each month
$result_month = db_query("
SELECT DATE_FORMAT(STR_TO_DATE(field_event_date_value, '%Y-%m-%dT%T'), '%m') AS month, COUNT(MONTH(STR_TO_DATE(field_event_date_value, '%Y-%m-%dT%T'))) as count
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 YEAR(STR_TO_DATE(field_event_date_value, '%Y-%m-%dT%T')) = :display_year
GROUP BY month ORDER BY month ASC
", array(':display_year' => $record_year->year));
// create links to the year and months
$months = array();
// create month links
foreach ($result_month as $record_month) {
$months[] = l(date('F', mktime(0, 0, 0, $record_month->month)), 'events/archive/' . $record_year->year . '-' . $record_month->month) . ' (' . $record_month->count . ')';
}
$links[] = array(
'data' => $year_link,
'children' => $months,
);
}
//setup array to be used by theme_item_list
$content = array(
'items' => $links,
'title' => '',
'type' => 'ol',
'attributes' => array(),
);
$block['subject'] = t('Events Archive');
$block['content'] = theme_item_list($content);
break;
}
//setup array to be used by theme_item_list
$content = array(
'items' => $links,
'title' => '',
'type' => 'ol',
'attributes' => array(),
);
$block['subject'] = t('Events by Date');
$block['content'] = theme_item_list($content);
return $block;
return $block;
}
......@@ -265,7 +234,8 @@ function uw_ct_event_page_build() {
$breadcrumb[3] = l('Create/Manage Content', 'admin/workbench/create');
drupal_set_breadcrumb($breadcrumb);
} else {
}
else {
$path = explode("/", $path);
switch ($path[0]) {
......@@ -275,39 +245,21 @@ function uw_ct_event_page_build() {
$breadcrumb = drupal_get_breadcrumb();
$breadcrumb[1] = l('Events', 'events');
/* TODO: update the breadcrumbs to use the event date information
// BUT --
// how to handle repeating dates or dates that carry over multiple months (?)
$year = date('Y', $node->created);
$month_name = date('F', $node->created);
$month_number = date('n', $node->created);
$breadcrumb[2] = l($year, 'events/archive/'.$year);
$breadcrumb[3] = l($month_name, 'events/archive/'.$year.'/'.$month_number);
$breadcrumb[3] = l($month_name, 'events/archive/'.$year.'-'.$month_number);
// */
drupal_set_breadcrumb($breadcrumb);
}
break;
/*
case 'events':
if (isset($path[1])) {
$breadcrumb = drupal_get_breadcrumb();
$breadcrumb[1] = l('Events', 'events');
if ($path[1] == 'archive' AND isset($path[2])) {
$dateparts = explode('-', $path[2]);
if (isset($dateparts[1])) {
$timestamp = mktime(0, 0, 0, $dateparts[1], 1, $dateparts[0]);
$year = date('Y', $timestamp);
$month_name = date('F', $timestamp);
$breadcrumb[3] = l($month_name, 'events/archive/'.$path[2]);
} else {
$timestamp = mktime(0, 0, 0, 1, 1, $dateparts[0]);
$year = date('Y', $timestamp);
}
$breadcrumb[2] = l($year, 'events/archive/'.$year);
}
drupal_set_breadcrumb($breadcrumb);
}
break;
// */
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment